Skip to content

Commit

Permalink
add offline tests for read_dataset_id[s]()
Browse files Browse the repository at this point in the history
  • Loading branch information
RWParsons committed Dec 2, 2024
1 parent d71969f commit a238440
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Description: Uses the MyHospitals API so that hospital data from the AIHW can be
License: MIT + file LICENSE
Suggests:
curl,
httptest2,
knitr,
rmarkdown,
testthat (>= 3.0.0)
Expand Down
92 changes: 92 additions & 0 deletions tests/testthat/_snaps/find-datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,95 @@
# reported_measure_summary_reported_measure_name <chr>,
# reporting_end_date <chr>, reporting_start_date <chr>, ...

# read_dataset_ids() offline

Code
d_mental_health_adms
Output
# A tibble: 7,624 x 15
data_set_id data_set_name reported_measure_sum~1 reported_measure_sum~2
<chr> <chr> <chr> <chr>
1 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
2 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
3 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
4 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
5 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
6 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
7 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
8 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
9 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
10 1329 ADM 2012-13 - Numb~ MYH0024 Number of admissions ~
# i 7,614 more rows
# i abbreviated names:
# 1: reported_measure_summary_measure_summary_measure_code,
# 2: reported_measure_summary_measure_summary_measure_name
# i 11 more variables: reported_measure_summary_reported_measure_code <chr>,
# reported_measure_summary_reported_measure_name <chr>,
# reporting_end_date <chr>, reporting_start_date <chr>, ...

---

Code
mental_health_adms_with_caveats
Output
$caveats
# A tibble: 2 x 8
caveats_caveat_code caveats_caveat_display_value caveats_caveat_footnote
<chr> <chr> <chr>
1 X52 <5 If an admissions count is le~
2 X01 <5 <NA>
# i 5 more variables: caveats_caveat_name <chr>,
# suppressions_suppression_code <chr>,
# suppressions_suppression_display_value <chr>,
# suppressions_suppression_footnote <chr>,
# suppressions_suppression_name <chr>
$data
# A tibble: 8,367 x 15
data_set_id data_set_name reported_measure_sum~1 reported_measure_sum~2
<chr> <chr> <chr> <chr>
1 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
2 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
3 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
4 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
5 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
6 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
7 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
8 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
9 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
10 904 ADM 2011-12 - Numb~ MYH0024 Number of admissions ~
# i 8,357 more rows
# i abbreviated names:
# 1: reported_measure_summary_measure_summary_measure_code,
# 2: reported_measure_summary_measure_summary_measure_name
# i 11 more variables: reported_measure_summary_reported_measure_code <chr>,
# reported_measure_summary_reported_measure_name <chr>,
# reporting_end_date <chr>, reporting_start_date <chr>, ...

# read_dataset_by_id() offline

Code
data_904
Output
# A tibble: 660 x 16
data_set_id measure_code reported_measure_code reporting_unit_summary_repor~1
<chr> <chr> <chr> <chr>
1 904 MYH0024 MYH-RM0216 H0012
2 904 MYH0024 MYH-RM0216 H0013
3 904 MYH0024 MYH-RM0216 H0014
4 904 MYH0024 MYH-RM0216 H0015
5 904 MYH0024 MYH-RM0216 H0016
6 904 MYH0024 MYH-RM0216 H0017
7 904 MYH0024 MYH-RM0216 H0018
8 904 MYH0024 MYH-RM0216 H0019
9 904 MYH0024 MYH-RM0216 H0020
10 904 MYH0024 MYH-RM0216 H0021
# i 650 more rows
# i abbreviated name: 1: reporting_unit_summary_reporting_unit_code
# i 12 more variables: reporting_unit_summary_reporting_unit_name <chr>,
# reporting_unit_summary_reporting_unit_type_reporting_unit_type_code <chr>,
# reporting_unit_summary_reporting_unit_type_reporting_unit_type_name <chr>,
# value <chr>, caveats_caveat_code <chr>, caveats_caveat_display_value <chr>,
# caveats_caveat_footnote <chr>, caveats_caveat_name <chr>, ...

51 changes: 50 additions & 1 deletion tests/testthat/test-find-datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,56 @@ test_that("reading mental health admmissions datasets", {
expect_s3_class(d_mental_health_adms, "data.frame")
})

test_that("read_dataset_ids() offline", {
source(test_path("helper-get-fixture.R"))

local_mocked_bindings(
get_datasets = function(...) {
get_fixture("get_datasets_untidy")
},
read_dataset_by_id = function(id) {
get_fixture(glue::glue("dataset-{id}"))
}
)

dataset_ids <- c(
"904", "1329", "1336", "2735",
"2742", "3515", "8115", "8202",
"8289", "8983", "9658", "10349"
)

d_mental_health_adms <- httptest2::without_internet(read_dataset_ids(dataset_ids, tidy_data = FALSE)) |>
dplyr::filter(reporting_end_date <= "2022-06-30") |>
dplyr::arrange(data_set_id)

expect_snapshot(d_mental_health_adms)
expect_s3_class(d_mental_health_adms, "data.frame")

mental_health_adms_with_caveats <- httptest2::without_internet(
read_dataset_ids(dataset_ids, tidy_data = FALSE, return_caveats = TRUE)
)
expect_snapshot(mental_health_adms_with_caveats)
expect_true(inherits(mental_health_adms_with_caveats, "list"))
})


test_that("read_dataset_by_id() offline", {
source(test_path("helper-get-fixture.R"))

local_mocked_bindings(
call_myhosp_api = function(...) {
get_fixture("dataset-904-api-resp")
}
)

data_904 <- httptest2::without_internet(read_dataset_by_id("904"))

# the response from the api is taken from fixture so this is testing
# tidy_resp_to_df()
expect_snapshot(data_904)
expect_s3_class(data_904, "data.frame")
})

test_that("tidying datasets works", {
skip_if_offline()

Expand All @@ -23,7 +73,6 @@ test_that("tidying datasets works", {
# tidy data should have fewer cols than untidy data
expect_gt(ncol(d_untidy), ncol(d_tidy))


d_tidy_with_different_measures <- read_dataset_ids(c(1, 8729), tidy_data = TRUE)
# datasets with different outcome measures should keep the measure names/codes
expect_gt(ncol(d_tidy_with_different_measures), ncol(d_tidy))
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-get-lists.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("list getters", {
skip_if_offline()

expect_s3_class(get_measure_categories(), "data.frame")
expect_s3_class(get_datasets(), "data.frame")
expect_s3_class(get_measure_download_codes(), "data.frame")
Expand All @@ -14,7 +15,7 @@ test_that("get_measure_data() works with get_measure_download_codes() codes", {
# test that it works for 3 randomly selected codes
purrr::map(
sample(codes, size = 3),
~ expect_s3_class(get_measure_data(.x), "data.frame")
~ expect_s3_class(suppressWarnings(get_measure_data(.x)), "data.frame")
)
})

Expand Down

0 comments on commit a238440

Please sign in to comment.