Skip to content

Commit

Permalink
Use dplyr::tibble in tests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo committed Nov 30, 2021
1 parent d3c150b commit 79857e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
24 changes: 12 additions & 12 deletions tests/testthat/custom_tests/mock_data.R
Expand Up @@ -15,7 +15,7 @@ get_expected_data_for_get_regional_data_tests_only_level_1_regions <- function()
dates <- c("2020-01-31", "2020-02-01", "2020-02-02", "2020-02-03", "2020-02-04", "2020-02-05")
provinces <- c("Northland", "Eastland", "Southland", "Westland", "Virginia")
## Fake region codes
region_codes <- tibble::tibble(iso_3166_2 = c("NO", "EA", "SO", "WE", "VA"), region = provinces)
region_codes <- dplyr::tibble(iso_3166_2 = c("NO", "EA", "SO", "WE", "VA"), region = provinces)

expected_data_for_provinces <- list()
for (i in 1:length(provinces)) {
Expand Down Expand Up @@ -87,7 +87,7 @@ get_expected_data_for_get_regional_data_tests_only_level_1_regions <- function()
) %>%
dplyr::arrange(date, province)

return(tibble::tibble(expected_data))
return(dplyr::tibble(expected_data))
}

get_input_data_for_get_regional_data_tests_only_level_1_regions <- function() {
Expand All @@ -109,15 +109,15 @@ get_expected_totals_data_for_get_regional_data_tests_only_level_1_regions <- fun
colnames(totals_data) <- c("province", "iso_3166_2", "cases_total", "deaths_total", "recovered_total", "hosp_total", "tested_total")
totals_data <- totals_data %>% dplyr::arrange(-cases_total)

return(tibble::tibble(totals_data))
return(dplyr::tibble(totals_data))
}


# get_regional_data() tests - including Level 2 regions
get_input_data_for_get_regional_data_tests_with_level_2_regions <- function() {
data <- get_input_data_for_get_regional_data_tests_only_level_1_regions()
colnames(data)[2] <- "level_2_region"
regions_table <- tibble::tibble(
regions_table <- dplyr::tibble(
level_2_region = c("Northland", "Eastland", "Southland", "Westland", "Virginia"),
level_1_region = c("Oneland", "Oneland", "Twoland", "Twoland", "USA")
)
Expand All @@ -137,11 +137,11 @@ get_expected_data_for_get_regional_data_tests_with_level_2_regions <- function()
data <- get_expected_data_for_get_regional_data_tests_only_level_1_regions()
data <- data[, -3]
data$region <- rep(c("Oneland", "Oneland", "Twoland", "USA", "Twoland"), 6)
region_codes <- tibble::tibble(
region_codes <- dplyr::tibble(
iso_3166_2 = c("ON", "TW", "US"),
region = c("Oneland", "Twoland", "USA")
)
level_2_region_codes <- tibble::tibble(
level_2_region_codes <- dplyr::tibble(
iso_3166_2_province = c("NO", "EA", "SO", "WE", "VA"),
region = c(
"Northland", "Eastland", "Southland",
Expand All @@ -168,11 +168,11 @@ get_expected_totals_data_for_get_regional_data_tests_with_level_2_regions <- fun

data <- data[, -2]
data$region <- c("Oneland", "USA", "Twoland", "Twoland", "Oneland")
region_codes <- tibble::tibble(
region_codes <- dplyr::tibble(
iso_3166_2 = c("ON", "TW", "US"),
region = c("Oneland", "Twoland", "USA")
)
level_2_region_codes <- tibble::tibble(
level_2_region_codes <- dplyr::tibble(
iso_3166_2_province = c("NO", "EA", "SO", "WE", "VA"),
region = c(
"Northland", "Eastland", "Southland",
Expand All @@ -188,7 +188,7 @@ get_expected_totals_data_for_get_regional_data_tests_with_level_2_regions <- fun
recovered_total, hosp_total, tested_total
)

return(tibble::tibble(data))
return(dplyr::tibble(data))
}


Expand All @@ -199,7 +199,7 @@ get_expected_data_for_fill_empty_dates_with_na_test <- function() {
dates <- c("2020-01-31", "2020-02-01", "2020-02-02", "2020-02-03")
regions <- c("Northland", "Eastland", "Wisconsin")

region_codes <- tibble::tibble(
region_codes <- dplyr::tibble(
region = regions,
level_1_region_code = c("NO", "EA", "WI")
)
Expand All @@ -213,7 +213,7 @@ get_expected_data_for_fill_empty_dates_with_na_test <- function() {
dplyr::arrange(date, level_1_region) %>%
dplyr::left_join(region_codes, by = c("level_1_region" = "region"))
expected_data$cases <- c(1:5, rep(NA, 4), 10:12)
return(tibble::tibble(expected_data))
return(dplyr::tibble(expected_data))
}

get_input_data_for_complete_cumulative_columns_test <- function() {
Expand All @@ -239,5 +239,5 @@ get_expected_data_for_complete_cumulative_columns_test <- function() {
full_data_with_cum_cases_filled <- cbind(full_data_with_cum_cases_filled, as.integer(c(1, 5, 5, 15, 2, 7, 7, 18, 3, 3, 3, 15)))
colnames(full_data_with_cum_cases_filled)[5] <- "cases_total"

return(tibble::tibble(full_data_with_cum_cases_filled))
return(dplyr::tibble(full_data_with_cum_cases_filled))
}
2 changes: 1 addition & 1 deletion tests/testthat/test-csv_reader.R
@@ -1,5 +1,5 @@
test_path <- "custom_data/mtcars.csv"
target <- tibble::as_tibble(head(mtcars))
target <- dplyr::as_tibble(head(mtcars))

test_that("csv_reader can read in a simple dataset", {
test <- csv_reader(test_path)
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-processing.R
Expand Up @@ -12,31 +12,31 @@ test_that("default functions are called", {
"add_extra_na_cols",
function(x) dplyr::mutate(x, A = A + 2),
)
x <- tibble::tibble(A = c(1, 2, 3))
expected <- tibble::tibble("A" = c(4, 5, 6))
x <- dplyr::tibble(A = c(1, 2, 3))
expected <- dplyr::tibble("A" = c(4, 5, 6))
expect_identical(expected, run_default_processing_fns(x))
})

test_that("optional functions can be empty", {
x <- tibble::tibble(A = c(1, 2, 3))
x <- dplyr::tibble(A = c(1, 2, 3))
expect_identical(x, run_optional_processing_fns(x, c()))
expect_identical(x, run_optional_processing_fns(x))
expect_identical(x, run_optional_processing_fns(x, NULL))
})

test_that("optional functions run", {
x <- tibble::tibble(A = c(1, 2, 3))
x <- dplyr::tibble(A = c(1, 2, 3))
process_fns <- c(function(x) {
return(dplyr::mutate(x, A = A^2))
})
expect_identical(
tibble::tibble(A = c(1, 4, 9)),
dplyr::tibble(A = c(1, 4, 9)),
run_optional_processing_fns(x, process_fns)
)
})

test_that("calculate_columns_from_existing_data returns correct results", {
input_data <- tibble::tibble(
input_data <- dplyr::tibble(
"date" = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-07"), by = 1),
"level_1_region" = c(rep("A", 4), rep("B", 3)),
"cases_new" = c(0, 1, NA_integer_, 1, 1, 1, 1),
Expand Down Expand Up @@ -68,10 +68,10 @@ test_that("add_extra_na_cols is working", {
test_that("set_negative_values_to_zero works", {
dates <- c(rep(Sys.Date(), 100))
values <- 49:-50
df <- tibble::tibble(date = dates, cases_total = values)
df <- dplyr::tibble(date = dates, cases_total = values)
colnames(df) <- c("date", "cases_total")

df_expected <- tibble::tibble(date = dates, cases_total = c(49:0, rep(0, 50)))
df_expected <- dplyr::tibble(date = dates, cases_total = c(49:0, rep(0, 50)))
df_actual <- set_negative_values_to_zero(df)
expect_equal(df_actual, df_expected)
})
Expand Down

0 comments on commit 79857e9

Please sign in to comment.