Skip to content

Commit

Permalink
Apply #86 to many test files
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Aug 8, 2022
1 parent 21411b6 commit de195ee
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 318 deletions.
52 changes: 26 additions & 26 deletions tests/testthat/test-gbif_verify_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ context("test_gbif_verify_keys")
testthat::test_that("test several input types", {
# input contains nothing
keys_null1 <- list()
expect_null(gbif_verify_keys(keys_null1))
testthat::expect_null(gbif_verify_keys(keys_null1))

# input contains invalid taxon keys (they contain letters)
keys_err1 <- c("1", "128", "120391203", "AE", "12k")
expect_error(
testthat::expect_error(
gbif_verify_keys(keys_err1),
"Invalid keys: AE,12k ."
)

# input contains empty strings
keys_err2 <- c("", "", "1")
expect_error(
testthat::expect_error(
gbif_verify_keys(keys_err2),
"Invalid keys: \"\"\"\",\"\"\"\" ."
)

# input contains dates
expect_error(
testthat::expect_error(
gbif_verify_keys(as.Date("2018-01-01")),
"keys should be a vector, a named list or a data.frame."
)

# input df doesn't contain column with name keys
expect_error(gbif_verify_keys(data.frame(bad_col_name = 12)),
testthat::expect_error(gbif_verify_keys(data.frame(bad_col_name = 12)),
paste(
"Column with keys not found.",
"Did you forget maybe to pass",
Expand Down Expand Up @@ -85,31 +85,31 @@ testthat::test_that("output type and content", {
)

# output type is "list"
expect_type(output1, "list")
expect_type(output2, "list")
expect_type(output3, "list")
expect_type(output4, "list")
expect_null(gbif_verify_keys(keys = c(NA, NA)))
expect_null(gbif_verify_keys(keys = c(NA_character_, NA_character_)))
testthat::expect_type(output1, "list")
testthat::expect_type(output2, "list")
testthat::expect_type(output3, "list")
testthat::expect_type(output4, "list")
testthat::expect_null(gbif_verify_keys(keys = c(NA, NA)))
testthat::expect_null(gbif_verify_keys(keys = c(NA_character_, NA_character_)))

# output class is tibble data.frame
expect_equal(class(output1), c("tbl_df", "tbl", "data.frame"))
expect_equal(class(output2), c("tbl_df", "tbl", "data.frame"))
expect_equal(class(output3), c("tbl_df", "tbl", "data.frame"))
expect_equal(class(output2), c("tbl_df", "tbl", "data.frame"))
testthat::expect_equal(class(output1), c("tbl_df", "tbl", "data.frame"))
testthat::expect_equal(class(output2), c("tbl_df", "tbl", "data.frame"))
testthat::expect_equal(class(output3), c("tbl_df", "tbl", "data.frame"))
testthat::expect_equal(class(output2), c("tbl_df", "tbl", "data.frame"))


# output content is the same for all kinds of inputs
expect_true(nrow(output1) == nrow(output_keys))
expect_true(nrow(output2) == nrow(output_keys))
expect_true(nrow(output3) == nrow(output_keys))
expect_true(nrow(output4) == nrow(output_keys))
expect_true(nrow(gbif_verify_keys(keys = "1")) == 1)
expect_equal(output1, output_keys)
expect_equal(output2, output_keys)
expect_equal(output3, output_keys)
expect_equal(output3, output_keys)
expect_equal(output4, output_keys)
testthat::expect_true(nrow(output1) == nrow(output_keys))
testthat::expect_true(nrow(output2) == nrow(output_keys))
testthat::expect_true(nrow(output3) == nrow(output_keys))
testthat::expect_true(nrow(output4) == nrow(output_keys))
testthat::expect_true(nrow(gbif_verify_keys(keys = "1")) == 1)
testthat::expect_equal(output1, output_keys)
testthat::expect_equal(output2, output_keys)
testthat::expect_equal(output3, output_keys)
testthat::expect_equal(output3, output_keys)
testthat::expect_equal(output4, output_keys)
})

testthat::test_that("function works even with duplicated taxon keys", {
Expand Down Expand Up @@ -139,5 +139,5 @@ testthat::test_that("function works even with duplicated taxon keys", {
is_synonym = rep(c(NA, NA, TRUE, FALSE), 2)
)

expect_equal(output_with_duplicates, expected_output_with_duplicates)
testthat::expect_equal(output_with_duplicates, expected_output_with_duplicates)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-indicator_introduction_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ testthat::test_that("Param: labels", {
})

testthat::test_that("Test warnings", {
expect_warning(indicator_introduction_year(input_test_df),
testthat::expect_warning(indicator_introduction_year(input_test_df),
paste0(
nrow_no_first_obs,
" records have no information about year of introduction ",
Expand Down
44 changes: 22 additions & 22 deletions tests/testthat/test-input_apply_decision_rules.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
context("test_input_decision_rules")

df_dr <- tibble(
df_dr <- dplyr::tibble(
taxonID = c(rep(1008955, 10), rep(2493598, 3)),
y = c(seq(2009, 2018), seq(2016, 2018)),
obs = c(1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 3, 0)
)

df_duplicate <- tibble(taxonID = c(1, 1), y = c(2001, 2001), obs = c(5, 3))
df_duplicate <- dplyr::tibble(taxonID = c(1, 1), y = c(2001, 2001), obs = c(5, 3))

df_duplicates <- tibble(
df_duplicates <- dplyr::tibble(
taxonID = c(1, 1, 2, 2, 3),
y = c(2000, 2001, 2001, 2001, 2000),
obs = c(0, 0, 1, 0, 1)
)

df_hole <- tibble(
df_hole <- dplyr::tibble(
taxonID = c(rep(1234, 3)),
y = c(2010, 2011, 2018),
obs = c(1, 2, 5)
)

df_holes <- tibble(
df_holes <- dplyr::tibble(
taxonID = c(rep(1234, 3), rep(1256, 4)),
y = c(2010, 2011, 2018, 2014, 2016, 2018, 2020),
obs = c(1, 2, 3, 5, 1, 2, 5)
)

testthat::test_that("Test inputs' types.", {
# df
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = 3,
y_var = "n_observations",
eval_year = 2018
Expand All @@ -40,7 +40,7 @@ testthat::test_that("Test inputs' types.", {
fixed = TRUE
)
# y_var
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = 3,
eval_year = 2018
Expand All @@ -52,7 +52,7 @@ testthat::test_that("Test inputs' types.", {
fixed = TRUE
)
# eval_year
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "n_observations",
eval_year = "yyyy"
Expand All @@ -63,7 +63,7 @@ testthat::test_that("Test inputs' types.", {
),
fixed = TRUE
)
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "n_observations",
eval_year = list(a = 2018)
Expand All @@ -75,7 +75,7 @@ testthat::test_that("Test inputs' types.", {
fixed = TRUE
)
# year
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "n_observations",
year = 3,
Expand All @@ -88,7 +88,7 @@ testthat::test_that("Test inputs' types.", {
fixed = TRUE
)
# taxonKey
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "n_observations",
eval_year = 2018,
Expand All @@ -103,15 +103,15 @@ testthat::test_that("Test inputs' types.", {
})

testthat::test_that("Test input length.", {
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_dr,
y_var = c("n_observations", "why a second col?"),
eval_year = 2018
),
paste("Multiple values for argument y_var provided.")
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_dr,
y_var = c("n_observations"),
Expand All @@ -120,7 +120,7 @@ testthat::test_that("Test input length.", {
),
paste("Multiple values for argument year provided.")
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_dr,
y_var = c("n_observations"),
Expand All @@ -132,7 +132,7 @@ testthat::test_that("Test input length.", {
})

testthat::test_that("Test df contains all needed columns and information.", {
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "y_var_wrong",
eval_year = 2018,
Expand All @@ -145,7 +145,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
),
fixed = TRUE
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_dr,
taxonKey = "taxonID",
Expand All @@ -158,7 +158,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
"Check value of argument year."
)
)
expect_error(apply_decision_rules(
testthat::expect_error(apply_decision_rules(
df = df_dr,
y_var = "obs",
year = "y",
Expand All @@ -171,7 +171,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
),
fixed = TRUE
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_dr,
y_var = "obs",
Expand All @@ -184,7 +184,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
"Check value of argument eval_year."
)
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_duplicate,
y_var = "obs",
Expand All @@ -197,7 +197,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
"of one or more taxa contain duplicates."
)
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_duplicates,
y_var = "obs",
Expand All @@ -210,7 +210,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
"of one or more taxa contain duplicates."
)
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_hole,
y_var = "obs",
Expand All @@ -222,7 +222,7 @@ testthat::test_that("Test df contains all needed columns and information.", {
"as they contain missing time values: 1234."
)
)
expect_error(
testthat::expect_error(
apply_decision_rules(
df = df_holes,
y_var = "obs",
Expand Down
Loading

0 comments on commit de195ee

Please sign in to comment.