From 146f32f944074ed11f9862a4af0ac0097ea90034 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 4 May 2022 14:36:13 +0200 Subject: [PATCH 1/3] Fix #107 Add assertions and tests --- R/get_cam_op.R | 10 ++++++++++ tests/testthat/test-get_cam_op.R | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/R/get_cam_op.R b/R/get_cam_op.R index f8df1643..16fee44c 100644 --- a/R/get_cam_op.R +++ b/R/get_cam_op.R @@ -39,6 +39,16 @@ get_cam_op <- function(datapkg, # check data package check_datapkg(datapkg) + # Check that station_col is one of the columns in deployments + assertthat::assert_that( + station_col %in% names(datapkg$deployments), + msg = glue("station column name (station_col) not valid: ", + "it must be one of the deployments column names.") + ) + + assertthat::assert_that(use_prefix %in% c(TRUE, FALSE), + msg = "use_prefix must be TRUE or FALSE.") + # extract and apply filtering on deployments deploys <- apply_filter_predicate( df = datapkg$deployments, diff --git a/tests/testthat/test-get_cam_op.R b/tests/testthat/test-get_cam_op.R index 8126a73b..70c20435 100644 --- a/tests/testthat/test-get_cam_op.R +++ b/tests/testthat/test-get_cam_op.R @@ -1,6 +1,13 @@ test_that("input camtrap dp is checked properly", { + # character instead of datapackage expect_error(get_cam_op("aaa")) + # numeric instead of datapackage expect_error(get_cam_op(1)) + # station_col value is not a column of deployments + expect_error(get_cam_op(mica, station_col = "bla")) + # use_prefix must be TRUE or FALSE + expect_error(get_cam_op(mica, use_prefix = "bla")) + expect_error(get_cam_op(mica, use_prefix = NA)) }) test_that("output is a matrix", { From 6a8cdea958ad88f66f2f8414a6581179a3ac0ac0 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 4 May 2022 14:45:09 +0200 Subject: [PATCH 2/3] Add dplyr:: before function names in some tests --- tests/testthat/test-get_n_obs.R | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/testthat/test-get_n_obs.R b/tests/testthat/test-get_n_obs.R index 54fcf212..95dc7f34 100644 --- a/tests/testthat/test-get_n_obs.R +++ b/tests/testthat/test-get_n_obs.R @@ -165,10 +165,10 @@ test_that(paste( species <- "Anas platyrhynchos" n_obs_via_sequence_id <- mica$observations %>% - filter(deploymentID == deploy_id) %>% - filter(scientificName == species) %>% - pull(sequenceID ) %>% - n_distinct() + dplyr::filter(.data$deploymentID == deploy_id) %>% + dplyr::filter(.data$scientificName == species) %>% + dplyr::pull(.data$sequenceID) %>% + dplyr::n_distinct() # one sequenceID linked to two observations (different age, sex and count) n_obs <- get_n_obs(mica, species = "Mallard", @@ -195,8 +195,8 @@ test_that("multiple sex values allowed", { expect_equal( tot_n_obs_females_unknown, mica$observations %>% - filter(sex %in% sex_value) %>% - distinct(sequenceID ) %>% + dplyr::filter(.data$sex %in% sex_value) %>% + dplyr::distinct(.data$sequenceID) %>% nrow() ) expect_equal(nrow(n_obs_females_unknown), nrow(mica$deployments)) @@ -206,8 +206,8 @@ test_that("life_stage filters data correctly", { life_stage_value <- "subadult" n_obs_subadult_via_distinct <- mica$observations %>% - filter(lifeStage %in% life_stage_value) %>% - distinct(sequenceID ) %>% + dplyr::filter(.data$lifeStage %in% life_stage_value) %>% + dplyr::distinct(.data$sequenceID) %>% nrow() n_obs_subadult <- get_n_obs(mica, species = NULL, life_stage = life_stage_value) tot_n_obs_subadult <- sum(n_obs_subadult$n) @@ -219,9 +219,9 @@ test_that("multiple age values allowed", { life_stage_value <- c("subadult", "adult") n_obs_subadult_adult <- get_n_obs(mica, species = NULL, life_stage = life_stage_value) tot_n_obs_subadult_adult <- sum(n_obs_subadult_adult$n) - n_obs_subadult_adult_calculate <- + n_obs_subadult_adult_calculate <- mica$observations %>% - filter(lifeStage %in% life_stage_value) %>% + dplyr::filter(.data$lifeStage %in% life_stage_value) %>% nrow() expect_equal(tot_n_obs_subadult_adult,n_obs_subadult_adult_calculate ) expect_equal(nrow(n_obs_subadult_adult), nrow(mica$deployments)) @@ -239,4 +239,4 @@ test_that(paste("scientific_name column contains all specified species,", n_obs <- get_n_obs(mica, species = species_value, sex = sex_value) expect_true(all(n_obs$scientificName %in% species_value)) expect_true(all(species_value %in% n_obs$scientificName)) -}) \ No newline at end of file +}) From d3eb49972f70a725a3dd45808bd6601c59fbcdc5 Mon Sep 17 00:00:00 2001 From: Damiano Oldoni Date: Wed, 4 May 2022 14:45:53 +0200 Subject: [PATCH 3/3] Upgrade version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 00251dbf..18bee4ec 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: camtraptor Title: Read, Explore and Visualize Camera Trap Data Packages -Version: 0.12.1 +Version: 0.13.0 Authors@R: c( person(given = "Damiano", family = "Oldoni",