diff --git a/DESCRIPTION b/DESCRIPTION index b00f76fa..497f740d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,3 +19,7 @@ Imports: digest, jsonlite, logger +Suggests: + testthat (>= 3.0.0), + withr +Config/testthat/edition: 3 diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 00000000..58fa1f4e --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(pacta.workflow.utils) + +test_check("pacta.workflow.utils") diff --git a/tests/testthat/test-get_single_file_metadata.R b/tests/testthat/test-get_single_file_metadata.R new file mode 100644 index 00000000..1bb58b47 --- /dev/null +++ b/tests/testthat/test-get_single_file_metadata.R @@ -0,0 +1,101 @@ +test_that("get_single_file_metadata processes CSV tables correctly", { + csv_file <- withr::local_tempfile(fileext = ".csv") + write.csv(mtcars, csv_file, row.names = FALSE) + test_time <- as.POSIXct("2020-01-01T12:34:56+00:00") + Sys.setFileTime(csv_file, test_time) + metadata <- get_single_file_metadata(csv_file) + expect_identical( + metadata, + list( + file_name = basename(csv_file), + file_extension = "csv", + file_path = csv_file, + file_size = 1303L, + file_last_modified = format( + as.POSIXlt(test_time, tz = "UTC"), + "%Y-%m-%dT%H:%M:%S+00:00" + ), + file_md5 = "5143f7b8ed70e91698d432d721c11a63", + summary_info = list( + nrow = 32L, + colnames = colnames(mtcars), + class = "data.frame" + ) + ) + ) +}) + +test_that("get_single_file_metadata processes RDS tables correctly", { + rds_file <- withr::local_tempfile(fileext = ".rds") + saveRDS(mtcars, rds_file) + test_time <- as.POSIXct("2020-01-01T12:34:56+00:00") + Sys.setFileTime(rds_file, test_time) + metadata <- get_single_file_metadata(rds_file) + expect_identical( + metadata, + list( + file_name = basename(rds_file), + file_extension = "rds", + file_path = rds_file, + file_size = 1225L, + file_last_modified = format( + as.POSIXlt(test_time, tz = "UTC"), + "%Y-%m-%dT%H:%M:%S+00:00" + ), + file_md5 = "23d45331b5667757134959aa333240ae", + summary_info = list( + nrow = 32L, + colnames = colnames(mtcars), + class = "data.frame" + ) + ) + ) +}) + +test_that("get_single_file_metadata processes RDS non-tables correctly", { + rds_file <- withr::local_tempfile(fileext = ".rds") + saveRDS("This is a string", rds_file) + test_time <- as.POSIXct("2020-01-01T12:34:56+00:00") + Sys.setFileTime(rds_file, test_time) + metadata <- get_single_file_metadata(rds_file) + expect_identical( + metadata, + list( + file_name = basename(rds_file), + file_extension = "rds", + file_path = rds_file, + file_size = 67L, + file_last_modified = format( + as.POSIXlt(test_time, tz = "UTC"), + "%Y-%m-%dT%H:%M:%S+00:00" + ), + file_md5 = "9dbb0d8e2235c70d2d8a77b1409f1597", + summary_info = list( + class = "character" + ) + ) + ) +}) + +test_that("get_single_file_metadata processes txt files correctly", { + txt_file <- withr::local_tempfile(fileext = ".txt") + writeLines("This is a string", txt_file) + test_time <- as.POSIXct("2020-01-01T12:34:56+00:00") + Sys.setFileTime(txt_file, test_time) + metadata <- get_single_file_metadata(txt_file) + expect_identical( + metadata, + list( + file_name = basename(txt_file), + file_extension = "txt", + file_path = txt_file, + file_size = 17L, + file_last_modified = format( + as.POSIXlt(test_time, tz = "UTC"), + "%Y-%m-%dT%H:%M:%S+00:00" + ), + file_md5 = "9ac4dbbc3c0ad2429e61d0df5dc28add" + # No summary info + ) + ) +})