Skip to content

Commit

Permalink
fix error tests for cran/ci
Browse files Browse the repository at this point in the history
  • Loading branch information
yannikbuhl committed Jul 20, 2024
1 parent a309352 commit 326ce2a
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 29 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Package: restatis
Title: R Wrapper to access a wide range of Germany's Federal Statistical System
databases based on the GENESIS Web Service RESTful API of the German
Federal Statistical Office (Statistisches Bundesamt/Destatis)
Title: R Wrapper to Access a Wide Range of Germany's Federal Statistical System Databases Based on the GENESIS Web Service RESTful API of the German Federal Statistical Office (Statistisches Bundesamt/Destatis)
Version: 0.2.0
Authors@R: c(
person("Yannik", "Buhl", , "[email protected]", role = c("aut", "cre")),
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/modified2/api/catalogue/modifieddata-40eb64.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
structure(list(method = "GET", url = "/api/catalogue/modifieddata?username=ABCDEF&password=1234abcd&username=ABCDEF&password=1234abcdselection=61111&type=all&date=20.07.2024",
status_code = 404L, headers = structure(list(Date = "Sat, 20 Jul 2024 16:13:04 GMT",
Server = "Apache", `Content-Length` = "106", `Content-Type` = "application/json"), class = "httr2_headers"),
body = charToRaw("{\"Code\":2,\"Content\":\"Ein Fehler ist aufgetreten. (Bitte geben Sie Ihren Nutzernamen ein.)\",\"Type\":\"ERROR\"}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
21 changes: 21 additions & 0 deletions tests/testthat/test_gen_alternative_terms.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,54 @@ with_mock_dir("terms1", {
expect_true("Copyright" %in% names(attrs))

})

})

#-------------------------------------------------------------------------------
# Test for errors, warnings, messages of parameters ----
#-------------------------------------------------------------------------------

test_that("search term errors on multiple codes", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_alternative_terms(term = c("611*", "711*"), database = "genesis"),
regexp = "Parameter 'term' must be a single string.")

})

test_that("search term errors on too long search term", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_alternative_terms(term = "dies das ananas!", database = "genesis"),
regexp = "Parameter 'term' cannot consist of more than 15 characters.")

})

test_that("search term errors on wrong parameter type", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_alternative_terms(term = "Krankenhaus", similarity = 1, database = "genesis"),
regexp = "Parameter 'similarity' has to be of type 'logical'.")

})

test_that("search term errors on wrong parameter type", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_alternative_terms(term = 1992, similarity = TRUE, database = "genesis"),
regexp = "Parameter 'term' has to be of type 'character'.")

})

35 changes: 35 additions & 0 deletions tests/testthat/test_gen_catalogue.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ with_mock_dir("catalogue3", {
error.ignore = FALSE,
database = "genesis"),
regexp = "No object found for your request")

})

})

#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -91,23 +93,34 @@ with_mock_dir("catalogue5", {
error.ignore = TRUE,
database = "genesis"),
regexp = "Use 'error.ignore = FALSE' to stop the function at the point where no object could be found.")

})

})

#-------------------------------------------------------------------------------
# Test for errors, warnings, messages of parameters ----
#-------------------------------------------------------------------------------

test_that("gen_catalogue function errors on multiple codes", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = c("611*", "711*"),
detailed = TRUE,
category = "tables",
database = "genesis"),
regexp = "Parameter 'code' must be a single string.")

})

test_that("gen_catalogue function errors on numeric code param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = 12345,
detailed = TRUE,
Expand All @@ -119,32 +132,47 @@ test_that("gen_catalogue function errors on numeric code param", {
#-------------------------------------------------------------------------------

test_that("gen_catalogue function errors on wrong categories", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = "611*",
detailed = TRUE,
category = "variables",
database = "genesis"),
regexp = "Available categories are 'tables', 'statistics', and 'cubes'.")

})

test_that("gen_catalogue function errors on too many categories", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = "611*",
detailed = TRUE,
category = c("variables", "statistics", "tables", "cubes"),
database = "genesis"),
regexp = "Parameter 'category' has to have a length of 1 to 3.")

})

#-------------------------------------------------------------------------------

test_that("gen_catalogue function errors on numeric detailed param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = "711*",
detailed = 1,
category = "tables",
database = "genesis"),
regexp = "Parameter 'detailed' has to be of type 'logical' and of length 1.")

})

with_mock_dir("catalogue6", {
Expand All @@ -159,18 +187,25 @@ with_mock_dir("catalogue6", {
category = "tables",
database = "genesis"),
regexp = "Use 'detailed = TRUE' to obtain the complete output.")

})

})

#-------------------------------------------------------------------------------

test_that("gen_catalogue function errors on wrong error.ignore param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_catalogue(code = "711*",
detailed = TRUE,
category = "tables",
error.ignore = 1,
database = "genesis"),
regexp = "Parameter 'error.ignore' has to be of type 'logical' and of length 1.")

})

29 changes: 29 additions & 0 deletions tests/testthat/test_gen_find.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ with_mock_dir("find1", {
expect_true("Language" %in% names(attrs))
expect_true("Pagelength" %in% names(attrs))
expect_true("Copyright" %in% names(attrs))

})

})

#-------------------------------------------------------------------------------
Expand All @@ -40,7 +42,9 @@ with_mock_dir("find2_fake", {
expect_error(
gen_find(term = "bus", error.ignore = TRUE, database = "genesis"),
regexp = "test error message")

})

})

#-------------------------------------------------------------------------------
Expand All @@ -64,31 +68,56 @@ with_mock_dir("find3", {
#-------------------------------------------------------------------------------

test_that("gen_find function errors on numeric term param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_find(term = 12345, detailed = TRUE, category = "tables", database = "genesis"),
regexp = "Parameter 'term' has to be of type 'character'.")

})

test_that("gen_find function errors on wrong category", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_find(term = "bus", detailed = TRUE, category = "table", database = "genesis"),
regexp = "Available categories for parameter 'category' for 'genesis' database are 'all', 'tables', 'statistics', 'variables', and 'cubes'.")

})

test_that("gen_find function errors on wrong detailed param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_find(term = "bus", detailed = 1, category = "tables", database = "genesis"),
regexp = "Parameter 'detailed' has to be of type 'logical' and of length 1.")

})

test_that("gen_find function errors on wrong ordering param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_find(term = "bus", ordering = 1, category = "tables", database = "genesis"),
regexp = "Parameter 'ordering' has to be of type 'logical' and of length 1.")

})

test_that("gen_find function errors on wrong error.ignore param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_find(term = "bus", error.ignore = 1, category = "tables", database = "genesis"),
regexp = "Parameter 'error.ignore' has to be of type 'logical' and of length 1.")

})
23 changes: 23 additions & 0 deletions tests/testthat/test_gen_meta_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ with_mock_dir("meta2_fake", {
category = "cube",
database = "genesis"),
regexp = "test error message")

})

})

#-------------------------------------------------------------------------------
Expand All @@ -60,34 +62,55 @@ with_mock_dir("meta3", {
error.ignore = TRUE,
database = "genesis"),
regexp = "Use 'error.ignore = FALSE' to stop the function at the point where no object could be found.")

})

})

#-------------------------------------------------------------------------------
# Test for errors, warnings, messages of parameters ----
#-------------------------------------------------------------------------------

test_that("gen_metadata function errors on numeric code param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_metadata(code = 12345, category = "Table", database = "genesis"),
regexp = "Parameter 'code' has to be of type 'character'.")

})

test_that("gen_metadata function errors on multiple categories", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_metadata(code = "12345", category = c("Table", "Cube"), database = "genesis"),
regexp = "Parameter 'category' must have a length of 1. Please specify the category.")

})

test_that("gen_metadata function errors on wrong category", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_metadata(code = "11111", category = "Table", database = "genesis"),
regexp = "Available categories for parameter 'category' for 'genesis' database are 'cube', 'table', 'statistic', 'variable', and 'value'")

})

test_that("gen_metadata function errors on wrong error.ignore param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_metadata(code = "11111", error.ignore = 1, category = "table", database = "genesis"),
regexp = "Parameter 'error.ignore' has to be of type 'logical' and of length 1.")

})
25 changes: 25 additions & 0 deletions tests/testthat/test_gen_modified_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,60 @@ with_mock_dir("modified4_fake", {
#-------------------------------------------------------------------------------

test_that("gen_modified_data errors on misspecified dates", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_modified_data(code = "61111", date = "1.1.2022"),
regexp = "it has to be of length 1 and format DD.MM.YYYY")

})

test_that("gen_modified_data errors on misspecified dates", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_modified_data(code = "61111", date = 23456),
regexp = "'date', it has to be of type 'character'.")

})

#-------------------------------------------------------------------------------

test_that("gen_modified_data errors on multiple codes", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_modified_data(code = c("611*", "711*")),
regexp = "Parameter 'code' must be a single string.")

})

test_that("gen_modified_data function errors on numeric code param", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_modified_data(code = 12345),
regexp = "Parameter 'code' has to be of type 'character'.")

})

#-------------------------------------------------------------------------------

test_that("gen_modified_data function errors on wrong type value", {

skip_on_cran()
skip_on_ci()

expect_error(
gen_modified_data(code = "12345", type = "diesdasananas"),
regexp = "'arg' should be one of")

})
Loading

0 comments on commit 326ce2a

Please sign in to comment.