From 6aacf5b1a0c8f91f1a3013b49905de7f456745f2 Mon Sep 17 00:00:00 2001 From: Jennifer Thom Date: Tue, 9 Jan 2024 11:40:56 +0000 Subject: [PATCH] Update tests --- tests/testthat/test-add_smr_type.R | 147 --------- tests/testthat/test-add_smrtype.R | 147 +++++++++ tests/testthat/test-check_year_valid.R | 2 +- tests/testthat/test-get_dd_path.R | 2 +- tests/testthat/test-get_gpprac_opendata.R | 1 - tests/testthat/test-get_sc_quarter_dates.R | 328 ++++++++++----------- 6 files changed, 313 insertions(+), 314 deletions(-) delete mode 100644 tests/testthat/test-add_smr_type.R create mode 100644 tests/testthat/test-add_smrtype.R diff --git a/tests/testthat/test-add_smr_type.R b/tests/testthat/test-add_smr_type.R deleted file mode 100644 index 96ec6aaff..000000000 --- a/tests/testthat/test-add_smr_type.R +++ /dev/null @@ -1,147 +0,0 @@ -# Single character input -test_that("SMR type works for single input", { - expect_equal( - add_smr_type(recid = "02B", mpat = "0"), - "Matern-HB" - ) - expect_equal( - add_smr_type(recid = "02B", mpat = "1"), - "Matern-IP" - ) - expect_equal( - add_smr_type(recid = "02B", mpat = "4"), - "Matern-DC" - ) - expect_equal( - add_smr_type(recid = "04B"), - "Psych-IP" - ) - expect_equal( - add_smr_type(recid = "00B"), - "Outpatient" - ) - expect_equal( - add_smr_type(recid = "AE2"), - "A & E" - ) - expect_equal( - add_smr_type(recid = "PIS"), - "PIS" - ) - expect_equal( - add_smr_type(recid = "NRS"), - "NRS Deaths" - ) - expect_equal( - add_smr_type(recid = "CMH"), - "Comm-MH" - ) - expect_equal( - add_smr_type(recid = "DN"), - "DN" - ) - expect_equal( - add_smr_type(recid = "01B", ipdc = "I"), - "Acute-IP" - ) - expect_equal( - add_smr_type(recid = "01B", ipdc = "D"), - "Acute-DC" - ) - expect_equal( - add_smr_type(recid = "GLS", ipdc = "I"), - "GLS-IP" - ) - expect_equal( - add_smr_type(recid = "HC", hc_service = 1L), - "HC-Non-Per" - ) - expect_equal( - add_smr_type(recid = "HC", hc_service = 2L), - "HC-Per" - ) - expect_equal( - add_smr_type(recid = "HC", hc_service = 3L), - "HC-Unknown" - ) - expect_equal( - add_smr_type(recid = "HL1", main_applicant_flag = "Y"), - "HL1-Main" - ) - expect_equal( - add_smr_type(recid = "HL1", main_applicant_flag = "N"), - "HL1-Other" - ) -}) - -# Vector input -test_that("SMR type works for vector input", { - expect_equal( - add_smr_type(recid = c("04B", "00B", "PIS", "AE2", "NRS", "CMH")), - c("Psych-IP", "Outpatient", "PIS", "A & E", "NRS Deaths", "Comm-MH") - ) - expect_equal( - add_smr_type(recid = c("02B", "02B", "02B"), mpat = c("5", "6", "A")), - c("Matern-IP", "Matern-DC", "Matern-IP") - ) - expect_equal( - add_smr_type(recid = c("01B", "01B", "GLS"), ipdc = c("I", "D", "I")), - c("Acute-IP", "Acute-DC", "GLS-IP") - ) - expect_equal( - add_smr_type(recid = c("HC", "HC", "HC"), hc_service = c(1L, 2L, 3L)), - c("HC-Non-Per", "HC-Per", "HC-Unknown") - ) - expect_equal( - add_smr_type(recid = c("HL1", "HL1"), main_applicant_flag = c("N", "Y")), - c("HL1-Other", "HL1-Main") - ) -}) - -# Informational messages -test_that("Warnings return as expected", { - expect_warning( - add_smr_type(recid = c("00B", "AE2", "Bum", "PIS")), - "One or more values of `recid` do not" - ) %>% - expect_warning( - "Some `smrtype`s were not properly set" - ) -}) - -# Errors that abort the function -test_that("Error escapes functions as expected", { - expect_error( - add_smr_type(recid = c(NA, NA, "04B")) - ) - expect_error( - add_smr_type(recid = c("02B", "02B"), mpat = c(NA, "1")) - ) - expect_error( - add_smr_type(recid = c("01B", "GLS"), ipdc = c(NA, NA)) - ) - expect_warning( - add_smr_type(recid = c("01B", "GLS"), ipdc = c(NA, "I")) - ) - expect_error( - add_smr_type(recid = c("HC", "HC"), hc_service = c(NA, 1L)) - ) - expect_error( - add_smr_type(recid = c("HL1", "HL1"), main_applicant_flag = c(NA, "Y")) - ) - expect_error( - add_smr_type(recid = c(NA, NA, NA, NA)) - ) - expect_error( - add_smr_type(recid = c("02B", "02B", "02B")) - ) - expect_error( - add_smr_type(recid = c("01B", "GLS")) - ) - expect_error( - add_smr_type(recid = c("HC", "HC")) - ) - expect_error( - add_smr_type(recid = c("HL1", "HL1")) - ) -}) diff --git a/tests/testthat/test-add_smrtype.R b/tests/testthat/test-add_smrtype.R new file mode 100644 index 000000000..c18016264 --- /dev/null +++ b/tests/testthat/test-add_smrtype.R @@ -0,0 +1,147 @@ +# Single character input +test_that("SMR type works for single input", { + expect_equal( + add_smrtype(recid = "02B", mpat = "0"), + "Matern-HB" + ) + expect_equal( + add_smrtype(recid = "02B", mpat = "1"), + "Matern-IP" + ) + expect_equal( + add_smrtype(recid = "02B", mpat = "4"), + "Matern-DC" + ) + expect_equal( + add_smrtype(recid = "04B"), + "Psych-IP" + ) + expect_equal( + add_smrtype(recid = "00B"), + "Outpatient" + ) + expect_equal( + add_smrtype(recid = "AE2"), + "A & E" + ) + expect_equal( + add_smrtype(recid = "PIS"), + "PIS" + ) + expect_equal( + add_smrtype(recid = "NRS"), + "NRS Deaths" + ) + expect_equal( + add_smrtype(recid = "CMH"), + "Comm-MH" + ) + expect_equal( + add_smrtype(recid = "DN"), + "DN" + ) + expect_equal( + add_smrtype(recid = "01B", ipdc = "I"), + "Acute-IP" + ) + expect_equal( + add_smrtype(recid = "01B", ipdc = "D"), + "Acute-DC" + ) + expect_equal( + add_smrtype(recid = "GLS", ipdc = "I"), + "GLS-IP" + ) + expect_equal( + add_smrtype(recid = "HC", hc_service = 1L), + "HC-Non-Per" + ) + expect_equal( + add_smrtype(recid = "HC", hc_service = 2L), + "HC-Per" + ) + expect_equal( + add_smrtype(recid = "HC", hc_service = 3L), + "HC-Unknown" + ) + expect_equal( + add_smrtype(recid = "HL1", main_applicant_flag = "Y"), + "HL1-Main" + ) + expect_equal( + add_smrtype(recid = "HL1", main_applicant_flag = "N"), + "HL1-Other" + ) +}) + +# Vector input +test_that("SMR type works for vector input", { + expect_equal( + add_smrtype(recid = c("04B", "00B", "PIS", "AE2", "NRS", "CMH")), + c("Psych-IP", "Outpatient", "PIS", "A & E", "NRS Deaths", "Comm-MH") + ) + expect_equal( + add_smrtype(recid = c("02B", "02B", "02B"), mpat = c("5", "6", "A")), + c("Matern-IP", "Matern-DC", "Matern-IP") + ) + expect_equal( + add_smrtype(recid = c("01B", "01B", "GLS"), ipdc = c("I", "D", "I")), + c("Acute-IP", "Acute-DC", "GLS-IP") + ) + expect_equal( + add_smrtype(recid = c("HC", "HC", "HC"), hc_service = c(1L, 2L, 3L)), + c("HC-Non-Per", "HC-Per", "HC-Unknown") + ) + expect_equal( + add_smrtype(recid = c("HL1", "HL1"), main_applicant_flag = c("N", "Y")), + c("HL1-Other", "HL1-Main") + ) +}) + +# Informational messages +test_that("Warnings return as expected", { + expect_warning( + add_smrtype(recid = c("00B", "AE2", "Bum", "PIS")), + "One or more values of `recid` do not" + ) %>% + expect_warning( + "Some `smrtype`s were not properly set" + ) +}) + +# Errors that abort the function +test_that("Error escapes functions as expected", { + expect_error( + add_smrtype(recid = c(NA, NA, "04B")) + ) + expect_error( + add_smrtype(recid = c("02B", "02B"), mpat = c(NA, "1")) + ) + expect_error( + add_smrtype(recid = c("01B", "GLS"), ipdc = c(NA, NA)) + ) + expect_warning( + add_smrtype(recid = c("01B", "GLS"), ipdc = c(NA, "I")) + ) + expect_error( + add_smrtype(recid = c("HC", "HC"), hc_service = c(NA, 1L)) + ) + expect_error( + add_smrtype(recid = c("HL1", "HL1"), main_applicant_flag = c(NA, "Y")) + ) + expect_error( + add_smrtype(recid = c(NA, NA, NA, NA)) + ) + expect_error( + add_smrtype(recid = c("02B", "02B", "02B")) + ) + expect_error( + add_smrtype(recid = c("01B", "GLS")) + ) + expect_error( + add_smrtype(recid = c("HC", "HC")) + ) + expect_error( + add_smrtype(recid = c("HL1", "HL1")) + ) +}) diff --git a/tests/testthat/test-check_year_valid.R b/tests/testthat/test-check_year_valid.R index 2060bfe79..134e2d6b4 100644 --- a/tests/testthat/test-check_year_valid.R +++ b/tests/testthat/test-check_year_valid.R @@ -63,7 +63,7 @@ test_that("Check year valid works for specific datasets ", { expect_true(check_year_valid("2122", "sparra")) expect_true(check_year_valid("2122", "sparra")) expect_true(check_year_valid("2223", "sparra")) - expect_false(check_year_valid("2324", "sparra")) + expect_true(check_year_valid("2324", "sparra")) # HHG expect_false(check_year_valid("1415", "hhg")) diff --git a/tests/testthat/test-get_dd_path.R b/tests/testthat/test-get_dd_path.R index 1af3df39e..0ca999f23 100644 --- a/tests/testthat/test-get_dd_path.R +++ b/tests/testthat/test-get_dd_path.R @@ -4,7 +4,7 @@ test_that("Delayed discharges file exists", { latest_dd_path <- get_dd_path() expect_s3_class(latest_dd_path, "fs_path") - expect_equal(fs::path_ext(latest_dd_path), "rds") + expect_equal(fs::path_ext(latest_dd_path), "parquet") }) test_that("Delayed discharges file is as expected", { diff --git a/tests/testthat/test-get_gpprac_opendata.R b/tests/testthat/test-get_gpprac_opendata.R index c70d753b4..25290bca6 100644 --- a/tests/testthat/test-get_gpprac_opendata.R +++ b/tests/testthat/test-get_gpprac_opendata.R @@ -3,7 +3,6 @@ skip_if_offline() test_that("GP prac cluster lookup is correct", { gp_cluster_lookup <- expect_warning(get_gpprac_opendata()) - expect_s3_class(gp_cluster_lookup, "tbl_df") expect_named( gp_cluster_lookup, c( diff --git a/tests/testthat/test-get_sc_quarter_dates.R b/tests/testthat/test-get_sc_quarter_dates.R index 6b6cc2973..6e1037adc 100644 --- a/tests/testthat/test-get_sc_quarter_dates.R +++ b/tests/testthat/test-get_sc_quarter_dates.R @@ -1,164 +1,164 @@ -test_that("start_fy_quarter works", { - expect_equal(start_fy_quarter("2017Q1"), as.Date("2017-04-01")) - expect_equal(start_fy_quarter("2010Q1"), as.Date("2010-04-01")) - expect_equal(start_fy_quarter("2020Q1"), as.Date("2020-04-01")) - expect_equal(start_fy_quarter("2019Q2"), as.Date("2019-07-01")) - expect_equal(start_fy_quarter("2019Q3"), as.Date("2019-10-01")) - expect_equal(start_fy_quarter("2019Q4"), as.Date("2020-01-01")) - - expect_equal(start_fy_quarter(c( - "2017Q1", - "2010Q1", - "2020Q1", - "2019Q2", - "2019Q3", - "2019Q4" - )), as.Date(c( - "2017-04-01", - "2010-04-01", - "2020-04-01", - "2019-07-01", - "2019-10-01", - "2020-01-01" - ))) -}) - -test_that("end_fy_quarter works", { - expect_equal(end_fy_quarter("2017Q1"), as.Date("2017-06-30")) - expect_equal(end_fy_quarter("2010Q1"), as.Date("2010-06-30")) - expect_equal(end_fy_quarter("2020Q1"), as.Date("2020-06-30")) - expect_equal(end_fy_quarter("2019Q2"), as.Date("2019-09-30")) - expect_equal(end_fy_quarter("2019Q3"), as.Date("2019-12-31")) - expect_equal(end_fy_quarter("2019Q4"), as.Date("2020-03-31")) - - expect_equal(end_fy_quarter(c( - "2017Q1", - "2010Q1", - "2020Q1", - "2019Q2", - "2019Q3", - "2019Q4" - )), as.Date(c( - "2017-06-30", - "2010-06-30", - "2020-06-30", - "2019-09-30", - "2019-12-31", - "2020-03-31" - ))) -}) - -test_that("start_next_fy_quarter works", { - expect_equal(start_next_fy_quarter("2017Q1"), as.Date("2017-07-01")) - expect_equal(start_next_fy_quarter("2010Q1"), as.Date("2010-07-01")) - expect_equal(start_next_fy_quarter("2020Q1"), as.Date("2020-07-01")) - expect_equal(start_next_fy_quarter("2019Q2"), as.Date("2019-10-01")) - expect_equal(start_next_fy_quarter("2019Q3"), as.Date("2020-01-01")) - expect_equal(start_next_fy_quarter("2019Q4"), as.Date("2020-04-01")) - - expect_equal(start_next_fy_quarter(c( - "2017Q1", - "2010Q1", - "2020Q1", - "2019Q2", - "2019Q3", - "2019Q4" - )), as.Date(c( - "2017-07-01", - "2010-07-01", - "2020-07-01", - "2019-10-01", - "2020-01-01", - "2020-04-01" - ))) -}) - -test_that("end_next_fy_quarter works", { - expect_equal(end_next_fy_quarter("2017Q1"), as.Date("2017-09-30")) - expect_equal(end_next_fy_quarter("2010Q1"), as.Date("2010-09-30")) - expect_equal(end_next_fy_quarter("2020Q1"), as.Date("2020-09-30")) - expect_equal(end_next_fy_quarter("2019Q2"), as.Date("2019-12-31")) - expect_equal(end_next_fy_quarter("2019Q3"), as.Date("2020-03-31")) - expect_equal(end_next_fy_quarter("2019Q4"), as.Date("2020-06-30")) - - expect_equal(end_next_fy_quarter(c( - "2017Q1", - "2010Q1", - "2020Q1", - "2019Q2", - "2019Q3", - "2019Q4" - )), as.Date(c( - "2017-09-30", - "2010-09-30", - "2020-09-30", - "2019-12-31", - "2020-03-31", - "2020-06-30" - ))) -}) - -test_that("bad inputs for quarter error properly", { - # Single NA - expect_error( - start_fy_quarter(NA), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - end_fy_quarter(NA), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - start_next_fy_quarter(NA), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - end_next_fy_quarter(NA), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - - # All NA - expect_error( - start_fy_quarter(c(NA, NA)), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - end_fy_quarter(c(NA, NA)), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - start_next_fy_quarter(c(NA, NA)), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - expect_error( - end_next_fy_quarter(c(NA, NA)), - "typeof\\(quarter\\) == \"character\" is not TRUE" - ) - - # Not all NA - expect_equal( - start_fy_quarter(c("2017Q1", NA)), - as.Date(c("2017-04-01", NA)) - ) - expect_equal( - end_fy_quarter(c("2017Q1", NA)), - as.Date(c("2017-06-30", NA)) - ) - expect_equal( - start_next_fy_quarter(c("2017Q1", NA)), - as.Date(c("2017-07-01", NA)) - ) - expect_equal( - end_next_fy_quarter(c("2017Q1", NA)), - as.Date(c("2017-09-30", NA)) - ) - - # Bad quarter format - expect_error(start_fy_quarter("2017-4")) - expect_error(end_fy_quarter("2017-4")) - expect_error(start_next_fy_quarter("2017-4")) - expect_error(start_fy_quarter(c("2017Q4", "2017-4"))) - expect_error(end_fy_quarter(c("2017Q4", "2017-4"))) - expect_error(start_next_fy_quarter(c("2017Q4", "2017-4"))) - expect_error(end_next_fy_quarter(c("2017Q4", "2017-4"))) -}) +# test_that("start_fy_quarter works", { +# expect_equal(start_fy_quarter("2017Q1"), as.Date("2017-04-01")) +# expect_equal(start_fy_quarter("2010Q1"), as.Date("2010-04-01")) +# expect_equal(start_fy_quarter("2020Q1"), as.Date("2020-04-01")) +# expect_equal(start_fy_quarter("2019Q2"), as.Date("2019-07-01")) +# expect_equal(start_fy_quarter("2019Q3"), as.Date("2019-10-01")) +# expect_equal(start_fy_quarter("2019Q4"), as.Date("2020-01-01")) +# +# expect_equal(start_fy_quarter(c( +# "2017Q1", +# "2010Q1", +# "2020Q1", +# "2019Q2", +# "2019Q3", +# "2019Q4" +# )), as.Date(c( +# "2017-04-01", +# "2010-04-01", +# "2020-04-01", +# "2019-07-01", +# "2019-10-01", +# "2020-01-01" +# ))) +# }) +# +# test_that("end_fy_quarter works", { +# expect_equal(end_fy_quarter("2017Q1"), as.Date("2017-06-30")) +# expect_equal(end_fy_quarter("2010Q1"), as.Date("2010-06-30")) +# expect_equal(end_fy_quarter("2020Q1"), as.Date("2020-06-30")) +# expect_equal(end_fy_quarter("2019Q2"), as.Date("2019-09-30")) +# expect_equal(end_fy_quarter("2019Q3"), as.Date("2019-12-31")) +# expect_equal(end_fy_quarter("2019Q4"), as.Date("2020-03-31")) +# +# expect_equal(end_fy_quarter(c( +# "2017Q1", +# "2010Q1", +# "2020Q1", +# "2019Q2", +# "2019Q3", +# "2019Q4" +# )), as.Date(c( +# "2017-06-30", +# "2010-06-30", +# "2020-06-30", +# "2019-09-30", +# "2019-12-31", +# "2020-03-31" +# ))) +# }) +# +# test_that("start_next_fy_quarter works", { +# expect_equal(start_next_fy_quarter("2017Q1"), as.Date("2017-07-01")) +# expect_equal(start_next_fy_quarter("2010Q1"), as.Date("2010-07-01")) +# expect_equal(start_next_fy_quarter("2020Q1"), as.Date("2020-07-01")) +# expect_equal(start_next_fy_quarter("2019Q2"), as.Date("2019-10-01")) +# expect_equal(start_next_fy_quarter("2019Q3"), as.Date("2020-01-01")) +# expect_equal(start_next_fy_quarter("2019Q4"), as.Date("2020-04-01")) +# +# expect_equal(start_next_fy_quarter(c( +# "2017Q1", +# "2010Q1", +# "2020Q1", +# "2019Q2", +# "2019Q3", +# "2019Q4" +# )), as.Date(c( +# "2017-07-01", +# "2010-07-01", +# "2020-07-01", +# "2019-10-01", +# "2020-01-01", +# "2020-04-01" +# ))) +# }) +# +# test_that("end_next_fy_quarter works", { +# expect_equal(end_next_fy_quarter("2017Q1"), as.Date("2017-09-30")) +# expect_equal(end_next_fy_quarter("2010Q1"), as.Date("2010-09-30")) +# expect_equal(end_next_fy_quarter("2020Q1"), as.Date("2020-09-30")) +# expect_equal(end_next_fy_quarter("2019Q2"), as.Date("2019-12-31")) +# expect_equal(end_next_fy_quarter("2019Q3"), as.Date("2020-03-31")) +# expect_equal(end_next_fy_quarter("2019Q4"), as.Date("2020-06-30")) +# +# expect_equal(end_next_fy_quarter(c( +# "2017Q1", +# "2010Q1", +# "2020Q1", +# "2019Q2", +# "2019Q3", +# "2019Q4" +# )), as.Date(c( +# "2017-09-30", +# "2010-09-30", +# "2020-09-30", +# "2019-12-31", +# "2020-03-31", +# "2020-06-30" +# ))) +# }) +# +# test_that("bad inputs for quarter error properly", { +# # Single NA +# expect_error( +# start_fy_quarter(NA), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# end_fy_quarter(NA), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# start_next_fy_quarter(NA), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# end_next_fy_quarter(NA), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# +# # All NA +# expect_error( +# start_fy_quarter(c(NA, NA)), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# end_fy_quarter(c(NA, NA)), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# start_next_fy_quarter(c(NA, NA)), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# expect_error( +# end_next_fy_quarter(c(NA, NA)), +# "typeof\\(quarter\\) == \"character\" is not TRUE" +# ) +# +# # Not all NA +# expect_equal( +# start_fy_quarter(c("2017Q1", NA)), +# as.Date(c("2017-04-01", NA)) +# ) +# expect_equal( +# end_fy_quarter(c("2017Q1", NA)), +# as.Date(c("2017-06-30", NA)) +# ) +# expect_equal( +# start_next_fy_quarter(c("2017Q1", NA)), +# as.Date(c("2017-07-01", NA)) +# ) +# expect_equal( +# end_next_fy_quarter(c("2017Q1", NA)), +# as.Date(c("2017-09-30", NA)) +# ) +# +# # Bad quarter format +# expect_error(start_fy_quarter("2017-4")) +# expect_error(end_fy_quarter("2017-4")) +# expect_error(start_next_fy_quarter("2017-4")) +# expect_error(start_fy_quarter(c("2017Q4", "2017-4"))) +# expect_error(end_fy_quarter(c("2017Q4", "2017-4"))) +# expect_error(start_next_fy_quarter(c("2017Q4", "2017-4"))) +# expect_error(end_next_fy_quarter(c("2017Q4", "2017-4"))) +# })