From d3d758d5546533bed4fead7489f0f8b1602a7fd2 Mon Sep 17 00:00:00 2001 From: nuest Date: Sun, 14 Jan 2018 18:40:22 +0100 Subject: [PATCH] add tests and Travis CI configuration --- .Rbuildignore | 4 +++ .travis.yml | 5 +++ DESCRIPTION | 6 ++-- tests/testthat.R | 4 +++ tests/testthat/test_boxes.R | 72 +++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test_boxes.R diff --git a/.Rbuildignore b/.Rbuildignore index d1a82a8..e42aee3 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,3 +2,7 @@ ^\.Rproj\.user$ ^CHANGES\.md$ ^tools*$ +^\.travis\.yml$ +^appveyor\.yml$ +^CONDUCT\.md$ +^codecov\.yml$ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8d139ac --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r + +language: R +sudo: false +cache: packages diff --git a/DESCRIPTION b/DESCRIPTION index 7142aae..7bb5143 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,7 +8,7 @@ Imports: dplyr, httr, magrittr -Suggests: +Suggests: maps, maptools, readr, @@ -17,7 +17,9 @@ Suggests: knitr, rmarkdown, lubridate, - units + units, + testthat, + covr Author: Norwin Roosen Maintainer: Norwin Roosen Description: Download data (environmental measurements, sensorstations) from the diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..f580690 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library("testthat") +library("opensensmapr") + +test_check("opensensmapr") diff --git a/tests/testthat/test_boxes.R b/tests/testthat/test_boxes.R new file mode 100644 index 0000000..c1d8892 --- /dev/null +++ b/tests/testthat/test_boxes.R @@ -0,0 +1,72 @@ +context("boxes") + +check_api <- function() { + code <- NA + try(code <- httr::status_code(httr::GET(osem_endpoint()))) + if (is.na(code)) skip("API not available") +} + +test_that("a list of all boxes can be retrieved and returns a sensebox data.frame", { + check_api() + + boxes <- osem_boxes() + expect_true(is.data.frame(boxes)) + expect_true(is.factor(boxes$model)) + expect_true(is.character(boxes$name)) + expect_length(names(boxes), 14) + expect_true(any("sensebox" %in% class(boxes))) +}) + +test_that("a list of boxes with exposure filter returns only the requested exposure", { + check_api() + + boxes <- osem_boxes(exposure = "mobile") + expect_true(all(boxes$exposure == "mobile")) +}) + +test_that("a list of boxes with model filter returns only the requested model", { + check_api() + + boxes <- osem_boxes(model = "homeWifi") + expect_true(all(boxes$model == "homeWifi")) +}) + +test_that("box query can combine exposure and model filter", { + check_api() + + boxes <- osem_boxes(exposure = "mobile", model = "homeWifi") + expect_true(all(boxes$model == "homeWifi")) + expect_true(all(boxes$exposure == "mobile")) +}) + +test_that("a list of boxes with grouptype returns only boxes of that group", { + check_api() + + boxes <- osem_boxes(grouptag = "codeformuenster") + expect_true(all(boxes$grouptag == "codeformuenster")) +}) + +test_that("endpoint can be (mis)configured", { + check_api() + + expect_error(osem_boxes(endpoint = "http://not.the.opensensemap.org"), "resolve host") +}) + +test_that("a response with no matches returns empty sensebox data.frame", { + check_api() + + boxes <- osem_boxes(grouptag = "does_not_exist") + expect_true(is.data.frame(boxes)) + expect_true(any("sensebox" %in% class(boxes))) +}) + +test_that("a response with no matches gives a warning", { + check_api() + + expect_warning(osem_boxes(grouptag = "does_not_exist"), "no boxes found") +}) + +test_that("data.frame can be converted to sensebox data.frame", { + df <- osem_as_sensebox(data.frame(c(1,2), c("a", "b"))) + expect_equal(class(df), c("sensebox", "data.frame")) +})