tests/testthat/test-split_by_key.R

test_that("split_by_key correctly splits JSON records into folders by unique keys", {
  
  # Create temporary input and output directories
  input_dir <- file.path(tempdir(), "test_split_key_in")
  output_dir <- file.path(tempdir(), "test_split_key_out")
  dir.create(input_dir, showWarnings = FALSE)
  
  # Create a mock JSON file with nested data
  mock_records <- list(
    Data = list(
      list(id = 1, Country = "Poland"),
      list(id = 2, Country = "Germany"),
      list(id = 3, Country = "Poland")
    )
  )
  
  jsonlite::write_json(mock_records, file.path(input_dir, "sample.json"), auto_unbox = TRUE, pretty = TRUE)
  
  # Execute splitting
  result <- suppressMessages(
    split_by_key(
      input_dir = input_dir,
      output_dir = output_dir,
      split_key = "Country",
      data_path = "Data"
    )
  )
  
  expect_true(result)
  
  # Check if subfolders were created based on unique 'Country' values
  expect_true(dir.exists(file.path(output_dir, "Poland")))
  expect_true(dir.exists(file.path(output_dir, "Germany")))
  
  # Check if split files exist
  expect_true(file.exists(file.path(output_dir, "Poland", "sample.json")))
  expect_true(file.exists(file.path(output_dir, "Germany", "sample.json")))
  
  # Clean up directories
  unlink(input_dir, recursive = TRUE)
  unlink(output_dir, recursive = TRUE)
})

Try the normanR package in your browser

Any scripts or data that you put into this service are public.

normanR documentation built on Sept. 12, 2026, 5:10 p.m.