Skip to content

Commit

Permalink
test(app): #1 Add tests for single file metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAxthelm committed Mar 14, 2024
1 parent c37466b commit 3fe4b56
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ Imports:
digest,
jsonlite,
logger
Suggests:
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -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")
101 changes: 101 additions & 0 deletions tests/testthat/test-get_single_file_metadata.R
Original file line number Diff line number Diff line change
@@ -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
)
)
})

0 comments on commit 3fe4b56

Please sign in to comment.