From 8975cbc66498cb7cec08f00a1a61f3e9aa53fdeb Mon Sep 17 00:00:00 2001 From: noerw Date: Tue, 8 May 2018 00:52:20 +0200 Subject: [PATCH] fix print.sensebox, add more tests --- R/box_utils.R | 7 ++----- tests/testthat.R | 1 - tests/testthat/test_box.R | 12 +++++++++--- tests/testthat/test_boxes.R | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/R/box_utils.R b/R/box_utils.R index a9f1b15..494a147 100644 --- a/R/box_utils.R +++ b/R/box_utils.R @@ -31,12 +31,9 @@ plot.sensebox = function (x, ..., mar = c(2, 2, 1, 1)) { } #' @export -print.sensebox = function(x, ...) { - important_columns = c('name', 'exposure', 'lastMeasurement', 'phenomena') +print.sensebox = function(x, columns = c('name', 'exposure', 'lastMeasurement', 'phenomena'), ...) { data = as.data.frame(x) - available_columns = important_columns %in% names(data) - print(data[available_columns], ...) - + print(dplyr::select(data, dplyr::one_of(columns)), ...) invisible(x) } diff --git a/tests/testthat.R b/tests/testthat.R index d5e0c50..d22603d 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,5 +1,4 @@ library('testthat') library('opensensmapr') -library('sf') test_check('opensensmapr') diff --git a/tests/testthat/test_box.R b/tests/testthat/test_box.R index 745cd68..9a05b63 100644 --- a/tests/testthat/test_box.R +++ b/tests/testthat/test_box.R @@ -3,7 +3,7 @@ context('box') try({ boxes = osem_boxes() - box = osem_box(boxes$X_id[[1]]) + box = osem_box('57000b8745fd40c8196ad04c') }) test_that('a single box can be retrieved by ID', { @@ -18,6 +18,12 @@ test_that('a single box can be retrieved by ID', { expect_silent(osem_box(boxes$X_id[[1]])) }) +test_that('unknown box throws', { + check_api() + + expect_error(osem_box('asdf')) + expect_error(osem_box('57000b8745fd40c800000000'), 'not found') +}) test_that('[.sensebox maintains attributes', { check_api() @@ -25,14 +31,14 @@ test_that('[.sensebox maintains attributes', { expect_true(all(attributes(boxes[1:nrow(boxes), ]) %in% attributes(boxes))) }) -test_that("print.sensebox filters important attributes", { +test_that("print.sensebox filters important attributes for a single box", { msg = capture.output({ print(box) }) expect_false(any(grepl('description', msg)), 'should filter attribute "description"') }) -test_that("summary.sensebox outputs all metrics", { +test_that("summary.sensebox outputs all metrics for a single box", { msg = capture.output({ summary(box) }) diff --git a/tests/testthat/test_boxes.R b/tests/testthat/test_boxes.R index 96e0262..271a466 100644 --- a/tests/testthat/test_boxes.R +++ b/tests/testthat/test_boxes.R @@ -17,6 +17,13 @@ test_that('both from and to are required when requesting boxes, error otherwise' expect_error(osem_boxes(to = as.POSIXct('2017-01-01')), 'must be used together') }) +test_that('a list of boxes with phenomenon filter returns only the requested phenomenon', { + check_api() + + boxes = osem_boxes(phenomenon='Temperatur', date=Sys.time()) + expect_true(all(grep('Temperatur', boxes$phenomena))) +}) + test_that('a list of boxes with exposure filter returns only the requested exposure', { check_api() @@ -106,3 +113,29 @@ test_that('box retrieval does not give progress information in non-interactive m }) expect_length(out, 0) }) + +test_that('print.sensebox filters important attributes for a set of boxes', { + check_api() + + boxes = osem_boxes() + msg = capture.output({ + print(boxes) + }) + expect_false(any(grepl('description', msg)), 'should filter attribute "description"') +}) + +test_that('summary.sensebox outputs all metrics for a set of boxes', { + check_api() + + boxes = osem_boxes() + msg = capture.output({ + summary(boxes) + }) + expect_true(any(grepl('sensors per box:', msg))) + expect_true(any(grepl('oldest box:', msg))) + expect_true(any(grepl('newest box:', msg))) + expect_true(any(grepl('\\$last_measurement_within', msg))) + expect_true(any(grepl('boxes by model:', msg))) + expect_true(any(grepl('boxes by exposure:', msg))) + expect_true(any(grepl('boxes total:', msg))) +})