Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit parsing issues #282

Merged
merged 19 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: camtraptor
Title: Read, Explore and Visualize Camera Trap Data Packages
Version: 0.20.1
Version: 0.20.2
Authors@R: c(
person("Damiano", "Oldoni", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3445-7562")),
Expand Down
4 changes: 4 additions & 0 deletions R/read_camtrap_dp.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ read_camtrap_dp <- function(file = NULL,
check_package(package, media = media)

# Inherit parsing issues from reading
attr(package$data$deployments, which = "problems") <- issues_deployments
attr(package$data$observations, which = "problems") <- issues_observations
if (media) {
attr(package$data$media, which = "problems") <- issues_media
}

return(package)
}
7 changes: 3 additions & 4 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,11 @@ convert_to_0.1.6 <- function(package, from = "1.0-rc.1", media = TRUE){

# notify about conversion
message(
writeLines(
c(
glue::glue(
"The dataset uses Camtrap DP version 1.0-rc.1, it has been converted to 0.1.6.",
"See https://inbo.github.io/camtraptor/#camtrap-dp for details."
"See https://inbo.github.io/camtraptor/#camtrap-dp for details.",
.seq = "\n"
)
)
)
# convert metadata
package <- convert_metadata_to_0.1.6(package, from)
Expand Down
64 changes: 42 additions & 22 deletions tests/testthat/test-read_camtrap_dp.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## read camera trap data package from v1.0-rc1
path_to_json_v1rc1 <- "https://raw.githubusercontent.com/tdwg/camtrap-dp/1.0-rc.1/example/datapackage.json"
dp_v1_rc1_with_media <- suppressMessages(
read_camtrap_dp(path_to_json_v1rc1)
)
dp_v1_rc1_without_media <- suppressMessages(
read_camtrap_dp(path_to_json_v1rc1, media = FALSE)
)

test_that("file argument is checked properly", {
expect_error(read_camtrap_dp("aaa"))
expect_error(read_camtrap_dp(1))
Expand Down Expand Up @@ -31,15 +40,6 @@ test_that("only DP versions 1.0-rc.1 and dp 0.1.6 are supported", {
)
})

## read camera trap data package from v1.0-rc1
path_to_json_v1rc1 <- "https://raw.githubusercontent.com/tdwg/camtrap-dp/1.0-rc.1/example/datapackage.json"
dp_v1_rc1_with_media <- suppressMessages(
read_camtrap_dp(path_to_json_v1rc1)
)
dp_v1_rc1_without_media <- suppressMessages(
read_camtrap_dp(path_to_json_v1rc1, media = FALSE)
)

test_that("test warnings while reading files with parsing issues", {
local_edition(2)
camtrap_dp_file_with_issues <- system.file(
Expand All @@ -48,35 +48,52 @@ test_that("test warnings while reading files with parsing issues", {
package = "camtraptor"
)
w <- capture_warnings(
camtraptor::read_camtrap_dp(file = camtrap_dp_file_with_issues)
dp_issues <- camtraptor::read_camtrap_dp(file = camtrap_dp_file_with_issues)
damianooldoni marked this conversation as resolved.
Show resolved Hide resolved
)
# warning on deployments
expect_equal(
expect_identical(
w[2], # w[1] is returned by readr via frictionless
paste0(
"One or more parsing issues occurred while reading `deployments`. ",
"Check `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
)
)
problems_deploys <- readr::problems(dp_issues$data$deployments)
expect_identical(nrow(problems_deploys), 2L)
expect_identical(problems_deploys$row, c(1L,2L))
expect_identical(problems_deploys$col, c(7L,7L))
expect_identical(problems_deploys$expected, rep("date like %Y-%m-%dT%H:%M:%S%z", 2))

# warning on observations
expect_equal(
expect_identical(
w[4], # w[3] is returned by readr via frictionless
paste0(
"One or more parsing issues occurred while reading `observations`. ",
"Check `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
)
)
problems_obs <- readr::problems(dp_issues$data$observations)
expect_identical(nrow(problems_obs), 2L)
expect_identical(problems_obs$row, c(1L,2L))
expect_identical(problems_obs$col, c(5L,5L))
expect_identical(problems_obs$expected, rep("date like %Y-%m-%dT%H:%M:%S%z", 2))

# warning on media
expect_equal(
expect_identical(
w[6], # w[5] is returned by readr via frictionless
paste0(
"One or more parsing issues occurred while reading `media`. ",
"Check `?read_camtrap_dp()` for examples on how to use ",
"`readr::problems()`."
)
)
problems_media <- readr::problems(dp_issues$data$media)
expect_identical(nrow(problems_media), 1L)
expect_identical(problems_media$row, 2L)
expect_identical(problems_media$col, 5L)
expect_identical(problems_media$expected, "date like %Y-%m-%dT%H:%M:%S%z")
})

test_that("media is checked properly", {
Expand All @@ -97,12 +114,13 @@ test_that("output is a list", {
file = dp_path,
media = FALSE
))

expect_true(is.list(dp_without_media))
expect_equal(class(dp_without_media), "list")
expect_type(dp_without_media, "list")
expect_true(is.list(dp_v1_rc1_with_media))
expect_equal(class(dp_v1_rc1_with_media), "list")
expect_type(dp_v1_rc1_with_media, "list")
expect_true(is.list(dp_v1_rc1_without_media))
expect_equal(class(dp_v1_rc1_without_media), "list")
expect_type(dp_v1_rc1_without_media, "list")
})

test_that("output data slot is a list of length 3", {
Expand All @@ -113,12 +131,13 @@ test_that("output data slot is a list of length 3", {
file = dp_path,
media = FALSE
))

expect_true("data" %in% names(dp_without_media))
damianooldoni marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(length(dp_without_media$data), 3)
expect_length(dp_without_media$data, 3)
expect_true("data" %in% names(dp_v1_rc1_with_media))
expect_equal(length(dp_v1_rc1_with_media$data), 3)
expect_length(dp_v1_rc1_with_media$data, 3)
expect_true("data" %in% names(dp_v1_rc1_without_media))
expect_equal(length(dp_v1_rc1_without_media$data), 3)
expect_length(dp_v1_rc1_without_media$data, 3)
})

test_that("media arg influences only slot media", {
Expand Down Expand Up @@ -334,7 +353,7 @@ test_that(
"read deployments v1.0-rc1: baitUse is a factor, not a boolean", {
expect_s3_class(dp_v1_rc1_with_media$data$deployments$baitUse, "factor")
baitUse_levels <- c("none", "scent", "food", "visual", "acoustic", "other")
expect_equal(
expect_identical(
levels(dp_v1_rc1_with_media$data$deployments$baitUse), baitUse_levels
)
# boolean NA becomes a factor NA
Expand Down Expand Up @@ -379,11 +398,12 @@ test_that(
file = dp_path,
media = FALSE
))

cols_deployments_dp_v1_rc1 <- dp_v1_rc1_without_media$data$deployments %>%
names()
cols_deployments_dp_v0_1_6 <- dp_without_media$data$deployments %>%
names()
expect_equal(cols_deployments_dp_v0_1_6, cols_deployments_dp_v1_rc1)
expect_identical(cols_deployments_dp_v0_1_6, cols_deployments_dp_v1_rc1)
}
)

Expand Down Expand Up @@ -573,6 +593,6 @@ test_that(
names()
cols_media_dp_v0_1_6 <- dp_with_media$data$media %>%
names()
expect_equal(cols_media_dp_v1_rc1, cols_media_dp_v0_1_6)
expect_identical(cols_media_dp_v1_rc1, cols_media_dp_v0_1_6)
}
)