From 79857e9f30b65453d28a0bb2463147f460b643f2 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Tue, 30 Nov 2021 19:03:17 +0100 Subject: [PATCH] Use dplyr::tibble in tests as well --- tests/testthat/custom_tests/mock_data.R | 24 ++++++++++++------------ tests/testthat/test-csv_reader.R | 2 +- tests/testthat/test-processing.R | 16 ++++++++-------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/testthat/custom_tests/mock_data.R b/tests/testthat/custom_tests/mock_data.R index 7262ead6..4b09b694 100644 --- a/tests/testthat/custom_tests/mock_data.R +++ b/tests/testthat/custom_tests/mock_data.R @@ -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)) { @@ -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() { @@ -109,7 +109,7 @@ 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)) } @@ -117,7 +117,7 @@ get_expected_totals_data_for_get_regional_data_tests_only_level_1_regions <- fun 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") ) @@ -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", @@ -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", @@ -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)) } @@ -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") ) @@ -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() { @@ -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)) } diff --git a/tests/testthat/test-csv_reader.R b/tests/testthat/test-csv_reader.R index c611980a..bc166e27 100644 --- a/tests/testthat/test-csv_reader.R +++ b/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) diff --git a/tests/testthat/test-processing.R b/tests/testthat/test-processing.R index e40cbc08..10fdcc3c 100644 --- a/tests/testthat/test-processing.R +++ b/tests/testthat/test-processing.R @@ -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), @@ -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) })