Skip to content

Commit

Permalink
add test for multiple filters in mixed format
Browse files Browse the repository at this point in the history
add test for invalid row filter & col select
add test for multiple filters in get dataset
  • Loading branch information
csillasch committed Jan 15, 2025
1 parent aa6073b commit f0351af
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/testthat/test-get_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ test_that("get_dataset filters error properly", {
regexp = "API error"
)
})

test_that("get_dataset works with multiple filters", {
n_resources <- 3
columns <- c("Date", "PracticeCode", "HSCP", "AllAges")

expect_message(
data <- get_dataset("gp-practice-populations",
max_resources = n_resources,
row_filters = list(PracticeCode = c("10002", "10017")),
col_select = columns
)
)

expect_s3_class(data, "tbl_df")
expect_equal(nrow(data), n_resources * 6)
expect_named(data, columns)
expect_true(all(data[["PracticeCode"]] %in% c("10002", "10017")))
})

38 changes: 38 additions & 0 deletions tests/testthat/test-get_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,41 @@ test_that("returns data for multiple filters", {
expect_length(unique(data_full$GPPractice), 2)
expect_length(unique(data_full$PrescribedType), 1)
})

test_that("returns data for multiple filters in mixed format", {
expect_message(
delays <- get_resource(
res_id = "fd354e4b-6211-48ba-8e4f-8356a5ed4215",
col_select = c("MonthOfDelay", "ReasonForDelay", "NumberOfDelayedBedDays"),
row_filters = list("HBT" = "S08000031", MonthOfDelay = c(201607:201707))
)
)

expect_s3_class(delays, "tbl_df")
expect_equal(nrow(delays), 195)
expect_named(delays, c("MonthOfDelay", "ReasonForDelay", "NumberOfDelayedBedDays"))
expect_length(unique(delays$MonthOfDelay), 13)
})

test_that("errors on invalid filters", {
# non-existent column in row_filters
expect_error(
delays <- get_resource(
res_id = "fd354e4b-6211-48ba-8e4f-8356a5ed4215",
col_select = c("MonthOfDelay", "ReasonForDelay", "NumberOfDelayedBedDays"),
row_filters = c("HBT" = "S08000031", "Month" = 201607)
),
regexp = "row_filters: invalid value"
)

# non-existent column in col_select
expect_error(
delays <- get_resource(
res_id = "fd354e4b-6211-48ba-8e4f-8356a5ed4215",
col_select = c("Month", "ReasonForDelay", "NumberOfDelayedBedDays"),
row_filters = c("HBT" = "S08000031", "MonthOfDelay" = 201607)
),
regexp = "col_select: invalid value"
)

})

0 comments on commit f0351af

Please sign in to comment.