-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Moohan/tests/clean_and_improve_tests
Clean up the tests
- Loading branch information
Showing
12 changed files
with
65 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
test_that("errors for incorrect case", { | ||
expect_error( | ||
phsopendata:::check_dataset_name("hospital codes"), | ||
check_dataset_name("hospital codes"), | ||
regexp = "dataset_name must be in dash-case" | ||
) | ||
}) | ||
|
||
test_that("errors for incorrect type", { | ||
expect_error( | ||
phsopendata:::check_dataset_name(20), | ||
check_dataset_name(20), | ||
regexp = "dataset_name must be of type character" | ||
) | ||
}) | ||
|
||
test_that("returns nothing for valid type and format", { | ||
expect_equal( | ||
phsopendata:::check_dataset_name("hospital-codes"), | ||
check_dataset_name("hospital-codes"), | ||
NULL | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
test_that("errors for length > 1", { | ||
expect_error( | ||
phsopendata:::check_res_id(letters), | ||
check_res_id(letters), | ||
regexp = "You supplied a res_id with a length of 26" | ||
) | ||
}) | ||
|
||
test_that("errors for incorrect type", { | ||
expect_error( | ||
phsopendata:::check_res_id(20), | ||
check_res_id(20), | ||
regexp = "(must be of type character)" | ||
) | ||
}) | ||
|
||
test_that("errors for invalid format", { | ||
expect_error( | ||
phsopendata:::check_res_id("wrong format"), | ||
check_res_id("wrong format"), | ||
regexp = "is in an invalid format." | ||
) | ||
}) | ||
|
||
test_that("returns nothing for correct format/length/type", { | ||
expect_equal( | ||
phsopendata:::check_res_id("a965ee86-0974-4c93-bbea-e839e27d7085"), | ||
check_res_id("a965ee86-0974-4c93-bbea-e839e27d7085"), | ||
NULL | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
skip_if_offline(host = "www.opendata.nhs.scot") | ||
|
||
test_that("throws error for non-existent res_ids", { | ||
expect_error( | ||
data <- phsopendata:::dump_download("not-real"), | ||
dump_download("not-real"), | ||
regexp = "Can't find resource with ID" | ||
) | ||
}) | ||
|
||
test_that("downloads full resource", { | ||
data <- phsopendata:::dump_download("a794d603-95ab-4309-8c92-b48970478c14") | ||
data <- dump_download("a794d603-95ab-4309-8c92-b48970478c14") | ||
|
||
expect_equal(nrow(data), 926) | ||
expect_equal(ncol(data), 15) | ||
expect_length(data, 15) | ||
expect_named(data) | ||
expect_s3_class(data, "tbl_df") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
skip_if_offline(host = "www.opendata.nhs.scot") | ||
|
||
test_that("returns nothing if no error", { | ||
content <- httr::content( | ||
httr::GET( | ||
phsopendata:::request_url("package_list", "") | ||
) | ||
) | ||
expect_equal( | ||
phsopendata:::error_check(content), NULL | ||
expect_null( | ||
error_check(phs_GET("package_list", "")) | ||
) | ||
}) | ||
|
||
test_that("throws error if error in httr content", { | ||
content <- httr::content( | ||
httr::GET( | ||
phsopendata:::request_url("datastore_search", "id=doop") | ||
) | ||
) | ||
expect_error( | ||
phsopendata:::error_check(content), | ||
error_check(phs_GET("datastore_search", "id=doop")), | ||
regexp = 'Resource "doop" was not found.' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
test_that("creates commas separated string from vector", { | ||
expect_equal( | ||
phsopendata:::parse_col_select(letters[1:5]), | ||
parse_col_select(letters[1:5]), | ||
"a,b,c,d,e" | ||
) | ||
}) | ||
|
||
test_that("returns NULL if input is NULL", { | ||
expect_equal( | ||
phsopendata:::parse_col_select(NULL), | ||
NULL | ||
) | ||
expect_null(parse_col_select(NULL)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
skip_if_offline(host = "www.opendata.nhs.scot") | ||
|
||
test_that("correctly extracts error from API response", { | ||
content <- httr::content( | ||
httr::GET( | ||
phsopendata:::request_url("datastore_search", "id=doop") | ||
request_url("datastore_search", "id=doop") | ||
) | ||
) | ||
expect_equal( | ||
phsopendata:::parse_error(content$error), | ||
parse_error(content$error), | ||
"Not Found Error: Not found: Resource \"doop\" was not found." | ||
) | ||
|
||
content <- httr::content( | ||
httr::GET( | ||
phsopendata:::request_url("datastore_search", "") | ||
request_url("datastore_search", "") | ||
) | ||
) | ||
expect_equal( | ||
phsopendata:::parse_error(content$error), | ||
parse_error(content$error), | ||
"resource_id: Missing value" | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
test_that("returns NULL if `row_filters` = NULL", { | ||
expect_null( | ||
phsopendata:::parse_row_filters(NULL) | ||
parse_row_filters(NULL) | ||
) | ||
}) | ||
|
||
test_that("throws error for length > 1", { | ||
expect_error( | ||
phsopendata:::parse_row_filters(list(x = letters)), | ||
parse_row_filters(list(x = letters)), | ||
regexp = "(list must only contain vectors of length 1.)" | ||
) | ||
}) | ||
|
||
test_that("throws error for non-unique names", { | ||
expect_error( | ||
phsopendata:::parse_row_filters(list(x = 1, x = 2)), | ||
parse_row_filters(list(x = 1, x = 2)), | ||
regexp = "Only one filter per field is currently supported by `get_resource`" | ||
) | ||
}) | ||
|
||
test_that("returns JSON string from list", { | ||
expect_true( | ||
jsonlite::validate( | ||
phsopendata:::parse_row_filters(list(x = 5, y = 6)) | ||
parse_row_filters(list(x = 5, y = 6)) | ||
) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
skip_if_offline(host = "www.opendata.nhs.scot") | ||
|
||
test_that("returns httr::content", { | ||
x <- phsopendata:::phs_GET("package_list", "") | ||
expect_true( | ||
!is.null(x$help) && !is.null(x$success) | ||
content <- phs_GET("package_list", "") | ||
|
||
expect_true(content$success) | ||
|
||
expect_equal( | ||
content$help, | ||
"https://www.opendata.nhs.scot/api/3/action/help_show?name=package_list" | ||
) | ||
}) | ||
|
||
test_that("error_check() works as expected", { | ||
# no error for valid endpoint | ||
expect_type( | ||
phsopendata:::phs_GET("package_list", ""), | ||
phs_GET("package_list", ""), | ||
"list" | ||
) | ||
|
||
# not found error | ||
expect_error( | ||
phsopendata:::phs_GET("datastore_search", "id=doop"), | ||
phs_GET("datastore_search", "id=doop"), | ||
regexp = 'Resource "doop" was not found.' | ||
) | ||
}) | ||
|
||
test_that("request_url() works as expected", { | ||
# invalid action argument | ||
expect_error( | ||
phsopendata:::phs_GET("", ""), | ||
phs_GET("", ""), | ||
regexp = "API call failed" | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
test_that("returns correct URL format", { | ||
expect_equal( | ||
phsopendata:::request_url("datastore_search", "id=doop"), | ||
request_url("datastore_search", "id=doop"), | ||
"https://www.opendata.nhs.scot/api/3/action/datastore_search?id=doop" | ||
) | ||
|
||
expect_equal( | ||
phsopendata:::request_url("dump", "id=doop"), | ||
request_url("dump", "id=doop"), | ||
"https://www.opendata.nhs.scot/datastore/dump/id=doop?bom=true" | ||
) | ||
}) | ||
|
||
test_that("rejects invalid actions", { | ||
expect_error( | ||
phsopendata:::request_url("beep", ""), | ||
request_url("beep", ""), | ||
regexp = "API call failed." | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,40 @@ | ||
test_that("returns true as expected", { | ||
# all are null | ||
expect_true( | ||
phsopendata:::use_dump_check(list(), NULL) | ||
use_dump_check(list(), NULL) | ||
) | ||
|
||
# rows > 99999 and all query is NULL | ||
expect_true( | ||
suppressWarnings( | ||
phsopendata:::use_dump_check(list(), 100000) | ||
) | ||
) | ||
use_dump_check(list(), 100000) | ||
) %>% | ||
expect_warning() | ||
|
||
# query entries are not all NULL and rows > 99999 | ||
expect_true( | ||
suppressWarnings( | ||
phsopendata:::use_dump_check(list(q = 4), 100000) | ||
) | ||
) | ||
use_dump_check(list(q = 4), 100000) | ||
) %>% | ||
expect_warning() | ||
|
||
expect_true( | ||
suppressWarnings( | ||
phsopendata:::use_dump_check(list(q = 4), 100000) | ||
) | ||
) | ||
use_dump_check(list(q = 4), 100000) | ||
) %>% | ||
expect_warning() | ||
}) | ||
|
||
test_that("returns false as expected", { | ||
# rows is NULL and query list is not all NULL | ||
expect_false( | ||
phsopendata:::use_dump_check(list(fields = "Age"), NULL) | ||
use_dump_check(list(fields = "Age"), NULL) | ||
) | ||
|
||
# rows is under 99999 and query list is not all NULL | ||
expect_false( | ||
phsopendata:::use_dump_check(list(fields = "Age"), 100) | ||
use_dump_check(list(fields = "Age"), 100) | ||
) | ||
|
||
# rows is under 99999 and query list is all NULL | ||
expect_false( | ||
phsopendata:::use_dump_check(list(), 100) | ||
use_dump_check(list(), 100) | ||
) | ||
}) |