diff --git a/tests/testthat/test_box.R b/tests/testthat/test_box.R new file mode 100644 index 0000000..bf3ef2f --- /dev/null +++ b/tests/testthat/test_box.R @@ -0,0 +1,30 @@ +context("box") + +check_api <- function() { + code <- NA + try(code <- httr::status_code(httr::GET(osem_endpoint()))) + if (is.na(code)) skip("API not available") +} + +try({ + boxes <- osem_boxes() +}) + +test_that("a box can be converted to sf object", { + check_api() + + box <- osem_box(boxes$X_id[[1]]) + box_sf <- sf::st_as_sf(box) + + expect_true(sf::st_is_simple(box_sf)) + expect_true("sf" %in% class(box_sf)) +}) + +test_that("a box converted to sf object keeps all attributes", { + check_api() + + box <- osem_box(boxes$X_id[[1]]) + box_sf <- sf::st_as_sf(box) + + expect_true(all(names(box) %in% names(box_sf))) +}) diff --git a/tests/testthat/test_phenomena.R b/tests/testthat/test_phenomena.R new file mode 100644 index 0000000..5e1c5ac --- /dev/null +++ b/tests/testthat/test_phenomena.R @@ -0,0 +1,38 @@ +context("phenomena") + +check_api <- function() { + code <- NA + try(code <- httr::status_code(httr::GET(osem_endpoint()))) + if (is.na(code)) skip("API not available") +} + +try({ + boxes <- osem_boxes() + all_phen <- unique(unlist(boxes$phenomena)) +}) + +test_that("phenomena from boxes is a list of counts", { + check_api() + + phenomena <- osem_phenomena(boxes) + + expect_true(is.numeric(unlist(phenomena))) + expect_true(is.list(phenomena)) +}) + +test_that("phenomena from boxes has all phenomena", { + check_api() + + phenomena <- osem_phenomena(boxes) + + expect_true(all(all_phen %in% names(phenomena))) + expect_true(all(names(phenomena) %in% all_phen)) +}) + +test_that("phenomena from a not sensebox data.frame returns error", { + expect_error(osem_phenomena(list()), "no applicable method") + expect_error(osem_phenomena(data.frame()), "no applicable method") + boxes_df <- boxes + class(boxes_df) <- c("data.frame") + expect_error(osem_phenomena(boxes_df), "no applicable method") +})