Skip to content

Commit

Permalink
Properly check for CSV header, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhpob committed Jun 14, 2024
1 parent a04434d commit dc420e6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
3 changes: 2 additions & 1 deletion R/utilities_make.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ clean_otn_deployment <- function(deployment) {
}
} else if (grepl("^csv$", file_ext)) {
# Check for OTN header
if (ncol(read.csv(deployment, nrows = 1)) == 0) {
check_head <- read.csv(deployment, nrows = 1, check.names = FALSE)
if (ncol(check_head) > length(unique(names(check_head)))) {
deployment <- read.csv(deployment,
skip = 3,
na.strings = c("NA", "")
Expand Down
78 changes: 74 additions & 4 deletions tests/testthat/test-clean_otn_deployment.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ test_that("expected classes", {
)
})




test_that("date times are parsed", {
deployment_xl <- clean_otn_deployment(pbsm$deployment)

Expand All @@ -36,13 +39,35 @@ test_that("date times are parsed", {
)
})




test_that("Works when no header present", {
deployment_sheet1 <- file.path(
td,
"no_header.xlsx"
)

dep <- readxl::read_excel(pbsm$deployment, sheet = 2, skip = 3)

writexl::write_xlsx(list(readme = data.frame(),
deployment = dep),
deployment_sheet1)

expect_equal(
clean_otn_deployment(deployment_sheet1),
clean_otn_deployment(pbsm$deployment)
)
})


test_that("guesses sheet", {
deployment_sheet1 <- file.path(
td,
"one_sheet.xlsx"
)

readxl::read_excel(pbsm$deployment, sheet = 2) |>
readxl::read_excel(pbsm$deployment, sheet = 2, skip = 3) |>
writexl::write_xlsx(deployment_sheet1)

expect_equal(
Expand All @@ -53,13 +78,15 @@ test_that("guesses sheet", {



test_that("accepts csv", {
test_that("accepts csv with header", {
deployment_csv <- file.path(
td,
"pbsm-instrument-deployment-short-form-2018.csv"
)
readxl::read_excel(pbsm$deployment, sheet = 2) |>
write.csv(deployment_csv, row.names = FALSE)
readxl::read_excel(pbsm$deployment, sheet = 2, col_names = FALSE,
.name_repair = "minimal") |>
write.table(deployment_csv, row.names = FALSE, col.names = FALSE,
sep = ",", na = "")

expect_s3_class(
deployment_csv <- clean_otn_deployment(deployment_csv),
Expand Down Expand Up @@ -94,6 +121,49 @@ test_that("accepts csv", {
})




test_that("accepts csv without header", {
deployment_csv <- file.path(
td,
"pbsm-instrument-deployment-short-form-2018.csv"
)
readxl::read_excel(pbsm$deployment, sheet = 2, skip = 3) |>
write.csv(deployment_csv, row.names = FALSE)

expect_s3_class(
deployment_csv <- clean_otn_deployment(deployment_csv),
"data.frame"
)
expect_named(
deployment_csv,
c(
"stationname", "receiver", "internal_transmitter", "deploy_date_time",
"deploy_lat", "deploy_long", "recover_date_time"
)
)
expect_type(deployment_csv$stationname, "character")
expect_type(deployment_csv$receiver, "character")
expect_s3_class(deployment_csv$deploy_date_time, c("POSIXct", "POSIXt"),
exact = TRUE
)
expect_type(deployment_csv$deploy_lat, "double")
expect_type(deployment_csv$deploy_long, "double")
expect_s3_class(deployment_csv$recover_date_time, c("POSIXct", "POSIXt"),
exact = TRUE
)

expect_equal(
attributes(deployment_csv$deploy_date_time)$tzone,
"UTC"
)
expect_equal(
attributes(deployment_csv$recover_date_time)$tzone,
"UTC"
)
})


test_that("errors if not a CSV or XLS(X)", {
expect_error(
clean_otn_deployment("abcs.doc"),
Expand Down

0 comments on commit dc420e6

Please sign in to comment.