From 8bf2329e926a14136e413e5e3af3075a164a5c77 Mon Sep 17 00:00:00 2001 From: Maria Gargiulo Date: Wed, 16 Aug 2023 18:02:01 +0100 Subject: [PATCH] all tests passing; closes #11, closes #13, closes #14 --- R/utils_authentication.R | 16 +++++------ tests/testthat/test-combine_replicates.R | 2 +- tests/testthat/test-confirm_files.R | 34 ++++++++++++++---------- tests/testthat/test-estimate_mse.R | 2 +- tests/testthat/test-lookup_estimates.R | 12 ++++----- tests/testthat/test-read_replicates.R | 31 ++++++++++++--------- tests/testthat/test-summary-observed.R | 2 +- 7 files changed, 53 insertions(+), 46 deletions(-) diff --git a/R/utils_authentication.R b/R/utils_authentication.R index c717feb..d4c0f89 100644 --- a/R/utils_authentication.R +++ b/R/utils_authentication.R @@ -225,8 +225,7 @@ build_path <- function(replicates_dir, violation, replicate_nums) { rep_lista <- tibble::tibble(full_path = path, matches) %>% dplyr::mutate(rep_number = as.integer(rep_number)) %>% - dplyr::filter(violation == violation & rep_number %in% replicate_nums) %>% - purrr::pluck("full_path") + dplyr::filter(violation == violation & rep_number %in% replicate_nums) } else { @@ -237,20 +236,17 @@ build_path <- function(replicates_dir, violation, replicate_nums) { rep_lista <- tibble::tibble(full_path = path, matches) %>% dplyr::mutate(rep_number = as.integer(rep_number)) %>% - dplyr::filter(violation == violation & rep_number %in% replicate_nums) %>% - purrr::pluck("full_path") + dplyr::filter(violation == violation & rep_number %in% replicate_nums) } - check_paths <- file.exists(rep_lista) + if (nrow(rep_lista) < length(replicate_nums)) { - if (any(!check_paths)) { - - paste("replicates_dir missing replicates:", rep_lista[!check_paths], collapse = ",", recycle0 = TRUE) - stop(glue::glue("replicates_dir missing replicates:\n{paste0(rep_lista[!check_paths], collapse = '\n')}")) + missing_reps <- setdiff(replicate_nums, rep_lista$rep_number) + stop(glue::glue("replicates_dir missing replicates:\n{paste0(missing_reps, collapse = '\n')}")) } - return(rep_lista) + return(rep_lista %>% purrr::pluck("full_path")) } else { diff --git a/tests/testthat/test-combine_replicates.R b/tests/testthat/test-combine_replicates.R index 52dafda..ebb097b 100644 --- a/tests/testthat/test-combine_replicates.R +++ b/tests/testthat/test-combine_replicates.R @@ -202,7 +202,7 @@ testthat::test_that("The function should run if more than two decimal places are proportions_table <- proportions_imputed(tab_combine, strata_vars = "sexo", digits = 3)) - + testthat::expect_error( proportions_table <- proportions_imputed(tab_combine, strata_vars = "sexo", diff --git a/tests/testthat/test-confirm_files.R b/tests/testthat/test-confirm_files.R index f660e40..7fcf315 100644 --- a/tests/testthat/test-confirm_files.R +++ b/tests/testthat/test-confirm_files.R @@ -42,39 +42,45 @@ testthat::test_that("Hashing wrong files", { }) testthat::test_that("Function should return an error if the violation type is incorrect", { - + testthat::expect_error(confirm_files(right_dir, "RECLUTAMIENTO", c(1, 2))) - + }) testthat::test_that("Expect message when number of replicates exceed available replicates", { - + testthat::expect_error(confirm_files(right_dir, "reclutamiento", 90:110)) - + }) testthat::test_that("Expect message when first replicate is less than 1", { - + testthat::expect_error(confirm_files(right_dir, "reclutamiento", 0:2)) - + }) testthat::test_that("Expect message when first replicate is less than 1 and last is more than 100", { - + testthat::expect_error(confirm_files(right_dir, "reclutamiento", 0:101)) - + }) -testthat::test_that("Function should return an error if the first_rep argument dos not have the correct format", { - +testthat::test_that("Function should return an error if replicate numbers are misspecified", { + testthat::expect_error(confirm_files(right_dir, "reclutamiento", c("1", 2))) - + }) -testthat::test_that("Function should return an error if the last_rep argument dos not have the correct format", { - +testthat::test_that("Function should return an error if replicate numbers are misspecified", { + testthat::expect_error(confirm_files(right_dir, "RECLUTAMIENTO", c(1, "dos"))) - + +}) + +testthat::test_that("Function should return an error if one or more of the replicates is not in the directory, but within the plausible bounds", { + + testthat::expect_error(confirm_files(right_dir, "reclutamiento", 1:10)) + }) # --- Done diff --git a/tests/testthat/test-estimate_mse.R b/tests/testthat/test-estimate_mse.R index 8a25e1b..da06988 100644 --- a/tests/testthat/test-estimate_mse.R +++ b/tests/testthat/test-estimate_mse.R @@ -95,7 +95,7 @@ testthat::test_that("mse function returns correct results for estimable and non- testthat::test_that("mse function returns correct results when using lookup functionality", { - + local_dir <- system.file("extdata", "right", package = "verdata") replicates <- read_replicates(local_dir, "reclutamiento", 1, 1) diff --git a/tests/testthat/test-lookup_estimates.R b/tests/testthat/test-lookup_estimates.R index 8507756..31739f5 100644 --- a/tests/testthat/test-lookup_estimates.R +++ b/tests/testthat/test-lookup_estimates.R @@ -69,21 +69,21 @@ testthat::test_that("Lookup non existing estimates", { testthat::test_that("Return error if the stratum cantains values different ti 0 and 1", { - + testthat::expect_error(lookup_estimates(wrong_stratum, estimates_dir)) - + }) testthat::test_that("Return error if the data frame includes columns different to `in_`", { - + testthat::expect_error(lookup_estimates(bad_columns, estimates_dir)) - + }) testthat::test_that("Return warning if the estimates directory does not have all files", { - + testthat::expect_warning(lookup_estimates(my_stratum, estimates_dir)) - + }) # --- Done diff --git a/tests/testthat/test-read_replicates.R b/tests/testthat/test-read_replicates.R index da8f2ea..cb8d268 100644 --- a/tests/testthat/test-read_replicates.R +++ b/tests/testthat/test-read_replicates.R @@ -51,39 +51,44 @@ testthat::test_that("Hashing content of wrong with crash set to F", { }) testthat::test_that("Function should return an error if the violation type is incorrect", { - + testthat::expect_error(read_replicates(right_dir, "desplazamiento", c(1, 2))) - + }) testthat::test_that("Expect message when number of replicates exceed available replicates", { - + testthat::expect_error(read_replicates(right_dir, "reclutamiento", 90:110)) - + }) testthat::test_that("Expect message when first replicate is less than 1", { - + testthat::expect_error(read_replicates(right_dir, "reclutamiento", 0:2)) - + }) testthat::test_that("Expect message when first replicate is less than 1 and last is more than 100", { - + testthat::expect_error(read_replicates(right_dir, "reclutamiento", 0:101)) }) -testthat::test_that("Function should return an error if the first_rep argument dos not have the correct format", { - +testthat::test_that("Function should return an error if replicate numbers are misspecified", { + testthat::expect_error(read_replicates(right_dir, "reclutamiento", c("1", 2))) - + }) -testthat::test_that("Function should return an error if the last_rep argument dos not have the correct format", { - +testthat::test_that("Function should return an error if replicate numbers are misspecified", { + testthat::expect_error(read_replicates(right_dir, "RECLUTAMIENTO", c(1, "dos"))) - + }) +testthat::test_that("Function should return an error if one or more of the replicates is not in the directory, but within the plausible bounds", { + + testthat::expect_error(read_replicates(right_dir, "reclutamiento", 1:10)) + +}) # --- Done diff --git a/tests/testthat/test-summary-observed.R b/tests/testthat/test-summary-observed.R index b62308f..662006c 100644 --- a/tests/testthat/test-summary-observed.R +++ b/tests/testthat/test-summary-observed.R @@ -94,7 +94,7 @@ testthat::test_that("Confirm sum = 1 in observed and output has desired structur forced_dis_filter = FALSE, edad_minors_filter = TRUE, include_props = TRUE) - + testthat::has_names(c("etnia", "observed", "obs_prop_no", "obs_prop")) testthat::expect_equal(sum(tab_observed$obs_prop_na), 1)