Browse Source

add lintr config, make code lint compliant, fixes #20

tags/v0.4.0
noerw 1 year ago
parent
commit
364f612216

+ 1
- 0
.Rbuildignore View File

@@ -6,3 +6,4 @@
6 6
 ^appveyor\.yml$
7 7
 ^CONDUCT\.md$
8 8
 ^codecov\.yml$
9
+^\.lintr$

+ 14
- 0
.lintr View File

@@ -0,0 +1,14 @@
1
+exclusions: list('inst/doc/osem-intro.R')
2
+linters: with_defaults(
3
+    # we use snake case
4
+    camel_case_linter = NULL,
5
+    # '=' for assignment is fine :^)
6
+    assignment_linter = NULL,
7
+    # single quotes are fine
8
+    single_quotes_linter = NULL,
9
+    # nobody reads code on a vt100 anymore
10
+    line_length_linter(120), 
11
+    # this one throws lots of false positives, dial down the noise
12
+    object_usage_linter = NULL,
13
+    NULL
14
+  )

+ 2
- 1
.travis.yml View File

@@ -10,6 +10,7 @@ r:
10 10
 
11 11
 r_github_packages:
12 12
   - r-lib/covr
13
+  - jimhester/lintr
13 14
   
14 15
 before_install:
15 16
   - sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable --yes
@@ -21,4 +22,4 @@ before_install:
21 22
  
22 23
 after_success:
23 24
   - Rscript -e 'covr::codecov()'
24
-  #- Rscript -e 'lintr::lint_package()'
25
+  - Rscript -e 'lintr::lint_package()'

+ 1
- 0
DESCRIPTION View File

@@ -18,6 +18,7 @@ Suggests:
18 18
     rmarkdown,
19 19
     lubridate,
20 20
     units,
21
+    lintr,
21 22
     testthat,
22 23
     covr
23 24
 Authors@R: c(person("Norwin", "Roosen", role = c("aut", "cre"), email = "bugs@nroo.de"),

+ 2
- 2
R/00utils.R View File

@@ -30,6 +30,6 @@ dplyr_class_wrapper = function(callback) {
30 30
 #'
31 31
 #' @noRd
32 32
 is_non_interactive = function () {
33
-  ff <- sapply(sys.calls(), function(f) as.character(f[1]))
34
-  any(ff %in% c("knit2html", "render")) || !interactive()
33
+  ff = sapply(sys.calls(), function(f) as.character(f[1]))
34
+  any(ff %in% c('knit2html', 'render')) || !interactive()
35 35
 }

+ 1
- 1
R/api.R View File

@@ -24,7 +24,7 @@ get_boxes_ = function (..., endpoint) {
24 24
   df = dplyr::bind_rows(boxesList)
25 25
   df$exposure = df$exposure %>% as.factor()
26 26
   df$model    = df$model %>% as.factor()
27
-  if(!is.null(df$grouptag))
27
+  if (!is.null(df$grouptag))
28 28
     df$grouptag = df$grouptag %>% as.factor()
29 29
   df
30 30
 }

+ 8
- 6
R/box.R View File

@@ -105,23 +105,25 @@ parse_senseboxdata = function (boxdata) {
105 105
   # to allow a simple data.frame structure
106 106
   sensors = boxdata$sensors
107 107
   location = boxdata$currentLocation
108
-  boxdata[c('loc', 'locations', 'currentLocation', 'sensors', 'image', 'boxType')] <- NULL
108
+  boxdata[c('loc', 'locations', 'currentLocation', 'sensors', 'image', 'boxType')] = NULL
109 109
   thebox = as.data.frame(boxdata, stringsAsFactors = F)
110 110
 
111 111
   # parse timestamps (updatedAt might be not defined)
112
-  thebox$createdAt = as.POSIXct(strptime(thebox$createdAt, format='%FT%T', tz = 'GMT'))
112
+  thebox$createdAt = as.POSIXct(strptime(thebox$createdAt, format = '%FT%T', tz = 'GMT'))
113 113
   if (!is.null(thebox$updatedAt))
114
-    thebox$updatedAt = as.POSIXct(strptime(thebox$updatedAt, format='%FT%T', tz = 'GMT'))
114
+    thebox$updatedAt = as.POSIXct(strptime(thebox$updatedAt, format = '%FT%T', tz = 'GMT'))
115 115
 
116 116
   # extract metadata from sensors
117
-  thebox$phenomena = list(unlist(lapply(sensors, function(s) { s$title })))
117
+  thebox$phenomena = lapply(sensors, function(s) s$title) %>% unlist %>% list
118
+
118 119
   # FIXME: if one sensor has NA, max() returns bullshit
119
-  thebox$lastMeasurement = max(lapply(sensors, function(s) {
120
+  get_last_measurement = function(s) {
120 121
     if (!is.null(s$lastMeasurement))
121 122
       as.POSIXct(strptime(s$lastMeasurement$createdAt, format = '%FT%T', tz = 'GMT'))
122 123
     else
123 124
       NA
124
-  })[[1]])
125
+  }
126
+  thebox$lastMeasurement = max(lapply(sensors, get_last_measurement)[[1]])
125 127
 
126 128
   # extract coordinates & transform to simple feature object
127 129
   thebox$lon = location$coordinates[[1]]

+ 9
- 9
R/box_utils.R View File

@@ -1,12 +1,13 @@
1 1
 #' @export
2
-plot.sensebox = function (x, ..., mar = c(2,2,1,1)) {
2
+plot.sensebox = function (x, ..., mar = c(2, 2, 1, 1)) {
3 3
   if (
4
-    !requireNamespace("sf", quietly = TRUE) ||
5
-    !requireNamespace("maps", quietly = TRUE) ||
6
-    !requireNamespace("maptools", quietly = TRUE) ||
7
-    !requireNamespace("rgeos", quietly = TRUE)
4
+    !requireNamespace('sf', quietly = TRUE) ||
5
+    !requireNamespace('maps', quietly = TRUE) ||
6
+    !requireNamespace('maptools', quietly = TRUE) ||
7
+    !requireNamespace('rgeos', quietly = TRUE)
8 8
   ) {
9
-    stop('this functions requires additional packages. install them with\n    install.packages(c("sf", "maps", "maptools", "rgeos"))')
9
+    stop('this functions requires additional packages. install them with
10
+    install.packages(c("sf", "maps", "maptools", "rgeos"))')
10 11
   }
11 12
 
12 13
   geom = x %>%
@@ -21,7 +22,7 @@ plot.sensebox = function (x, ..., mar = c(2,2,1,1)) {
21 22
 
22 23
   oldpar = par()
23 24
   par(mar = mar)
24
-  plot(world, col = 'gray', xlim = bbox[c(1,3)], ylim = bbox[c(2,4)], axes = T)
25
+  plot(world, col = 'gray', xlim = bbox[c(1, 3)], ylim = bbox[c(2, 4)], axes = T)
25 26
   plot(geom, add = T, col = x$exposure)
26 27
   legend('left', legend = levels(x$exposure), col = 1:length(x$exposure), pch = 1)
27 28
   par(mar = oldpar$mar)
@@ -48,7 +49,7 @@ summary.sensebox = function(object, ...) {
48 49
   table(object$model) %>% print()
49 50
   cat('\n')
50 51
 
51
-  diffNow = (utc_date(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit='hours')
52
+  diffNow = (utc_date(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit = 'hours')
52 53
   list(
53 54
     'last_measurement_within' = c(
54 55
       '1h'    = nrow(dplyr::filter(object, diffNow <= 1)),
@@ -122,4 +123,3 @@ mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox)
122 123
 st_as_sf.sensebox = function (x, ...) {
123 124
   NextMethod(x, ..., coords = c('lon', 'lat'), crs = 4326)
124 125
 }
125
-

+ 1
- 1
R/measurement.R View File

@@ -170,6 +170,6 @@ paged_measurements_req = function (query) {
170 170
     dplyr::bind_rows()
171 171
 
172 172
   # coerce all character columns (sensorId, unit, ...) to factors AFTER binding
173
-  df[sapply(df, is.character)] <- lapply(df[sapply(df, is.character)], as.factor)
173
+  df[sapply(df, is.character)] = lapply(df[sapply(df, is.character)], as.factor)
174 174
   df
175 175
 }

+ 1
- 1
R/measurement_utils.R View File

@@ -1,5 +1,5 @@
1 1
 #' @export
2
-plot.osem_measurements = function (x, ..., mar = c(2,4,1,1)) {
2
+plot.osem_measurements = function (x, ..., mar = c(2, 4, 1, 1)) {
3 3
   oldpar = par()
4 4
   par(mar = mar)
5 5
   plot(value~createdAt, x, col = factor(x$sensorId), xlab = NA, ylab = x$unit[1], ...)

+ 3
- 1
R/opensensmapr.R View File

@@ -77,6 +77,8 @@
77 77
 '_PACKAGE'
78 78
 
79 79
 #' @importFrom graphics plot legend par
80
-
81 80
 #' @importFrom magrittr %>%
82 81
 `%>%` = magrittr::`%>%`
82
+
83
+# just to make R CMD check happy, due to NSE (dplyr) functions
84
+globalVariables(c('lastMeasurement'))

+ 0
- 1
inst/doc/osem-intro.R View File

@@ -70,4 +70,3 @@ st_as_sf(pm25) %>% st_geometry() %>% plot(col = factor(pm25$invalid), axes = T)
70 70
 
71 71
 ## ------------------------------------------------------------------------
72 72
 pm25 %>% filter(invalid == FALSE) %>% plot()
73
-

+ 4
- 4
tests/testthat.R View File

@@ -1,5 +1,5 @@
1
-library("testthat")
2
-library("opensensmapr")
3
-library("sf")
1
+library('testthat')
2
+library('opensensmapr')
3
+library('sf')
4 4
 
5
-test_check("opensensmapr")
5
+test_check('opensensmapr')

+ 8
- 0
tests/testthat/lint.R View File

@@ -0,0 +1,8 @@
1
+if (requireNamespace('lintr', quietly = TRUE)) {
2
+  context('lints')
3
+
4
+  test_that('Package Style', {
5
+    skip_on_cran()
6
+    lintr::expect_lint_free()
7
+  })
8
+}

+ 8
- 15
tests/testthat/test_box.R View File

@@ -1,30 +1,23 @@
1
-context("box")
2
-
3
-check_api <- function() {
4
-  skip_on_cran()
5
-
6
-  code <- NA
7
-  try(code <- httr::status_code(httr::GET(osem_endpoint())))
8
-  if (is.na(code)) skip("API not available")
9
-}
1
+source('testhelpers.R')
2
+context('box')
10 3
 
11 4
 try({
12
-  boxes <- osem_boxes()
5
+  boxes = osem_boxes()
13 6
 })
14 7
 
15
-test_that("a single box can be retrieved by ID", {
8
+test_that('a single box can be retrieved by ID', {
16 9
   check_api()
17 10
 
18
-  box <- osem_box(boxes$X_id[[1]])
11
+  box = osem_box(boxes$X_id[[1]])
19 12
 
20
-  expect_true("sensebox" %in% class(box))
21
-  expect_true("data.frame" %in% class(box))
13
+  expect_true('sensebox' %in% class(box))
14
+  expect_true('data.frame' %in% class(box))
22 15
   expect_true(nrow(box) == 1)
23 16
   expect_true(box$X_id == boxes$X_id[[1]])
24 17
 })
25 18
 
26 19
 
27
-test_that("[.sensebox maintains attributes", {
20
+test_that('[.sensebox maintains attributes', {
28 21
   check_api()
29 22
 
30 23
   expect_true(all(attributes(boxes[1:nrow(boxes), ]) %in% attributes(boxes)))

+ 47
- 51
tests/testthat/test_boxes.R View File

@@ -1,112 +1,108 @@
1
-context("boxes")
1
+source('testhelpers.R')
2
+context('boxes')
2 3
 
3
-check_api <- function() {
4
-  skip_on_cran()
5
-
6
-  code <- NA
7
-  try(code <- httr::status_code(httr::GET(osem_endpoint())))
8
-  if (is.na(code)) skip("API not available")
9
-}
10
-
11
-test_that("a list of all boxes can be retrieved and returns a sensebox data.frame", {
4
+test_that('a list of all boxes can be retrieved and returns a sensebox data.frame', {
12 5
   check_api()
13 6
 
14
-  boxes <- osem_boxes()
7
+  boxes = osem_boxes()
15 8
   expect_true(is.data.frame(boxes))
16 9
   expect_true(is.factor(boxes$model))
17 10
   expect_true(is.character(boxes$name))
18 11
   expect_length(names(boxes), 14)
19
-  expect_true(any("sensebox" %in% class(boxes)))
12
+  expect_true(any('sensebox' %in% class(boxes)))
20 13
 })
21 14
 
22
-test_that("both from and to are required when requesting boxes, error otherwise", {
23
-  expect_error(osem_boxes(from = as.POSIXct("2017-01-01")), "must be used together")
24
-  expect_error(osem_boxes(to   = as.POSIXct("2017-01-01")), "must be used together")
15
+test_that('both from and to are required when requesting boxes, error otherwise', {
16
+  expect_error(osem_boxes(from = as.POSIXct('2017-01-01')), 'must be used together')
17
+  expect_error(osem_boxes(to   = as.POSIXct('2017-01-01')), 'must be used together')
25 18
 })
26 19
 
27
-test_that("a list of boxes with exposure filter returns only the requested exposure", {
20
+test_that('a list of boxes with exposure filter returns only the requested exposure', {
28 21
   check_api()
29 22
 
30
-  boxes <- osem_boxes(exposure = "mobile")
31
-  expect_true(all(boxes$exposure == "mobile"))
23
+  boxes = osem_boxes(exposure = 'mobile')
24
+  expect_true(all(boxes$exposure == 'mobile'))
32 25
 })
33 26
 
34
-test_that("a list of boxes with model filter returns only the requested model", {
27
+test_that('a list of boxes with model filter returns only the requested model', {
35 28
   check_api()
36 29
 
37
-  boxes <- osem_boxes(model = "homeWifi")
38
-  expect_true(all(boxes$model == "homeWifi"))
30
+  boxes = osem_boxes(model = 'homeWifi')
31
+  expect_true(all(boxes$model == 'homeWifi'))
39 32
 })
40 33
 
41
-test_that("box query can combine exposure and model filter", {
34
+test_that('box query can combine exposure and model filter', {
42 35
   check_api()
43 36
 
44
-  boxes <- osem_boxes(exposure = "mobile", model = "homeWifi")
45
-  expect_true(all(boxes$model == "homeWifi"))
46
-  expect_true(all(boxes$exposure == "mobile"))
37
+  boxes = osem_boxes(exposure = 'mobile', model = 'homeWifi')
38
+  expect_true(all(boxes$model == 'homeWifi'))
39
+  expect_true(all(boxes$exposure == 'mobile'))
47 40
 })
48 41
 
49
-test_that("a list of boxes with grouptype returns only boxes of that group", {
42
+test_that('a list of boxes with grouptype returns only boxes of that group', {
50 43
   check_api()
51 44
 
52
-  boxes <- osem_boxes(grouptag = "codeformuenster")
53
-  expect_true(all(boxes$grouptag == "codeformuenster"))
45
+  boxes = osem_boxes(grouptag = 'codeformuenster')
46
+  expect_true(all(boxes$grouptag == 'codeformuenster'))
54 47
 })
55 48
 
56
-test_that("endpoint can be (mis)configured", {
49
+test_that('endpoint can be (mis)configured', {
57 50
   check_api()
58 51
 
59
-  expect_error(osem_boxes(endpoint = "http://not.the.opensensemap.org"), "resolve host")
52
+  expect_error(osem_boxes(endpoint = 'http://not.the.opensensemap.org'), 'resolve host')
60 53
 })
61 54
 
62
-test_that("a response with no matches returns empty sensebox data.frame and a warning", {
55
+test_that('a response with no matches returns empty sensebox data.frame', {
63 56
   check_api()
64 57
 
65
-  suppressWarnings(boxes <- osem_boxes(grouptag = "does_not_exist"))
58
+  suppressWarnings({
59
+    boxes = osem_boxes(grouptag = 'does_not_exist')
60
+  })
66 61
   expect_true(is.data.frame(boxes))
67
-  expect_true(any("sensebox" %in% class(boxes)))
62
+  expect_true(any('sensebox' %in% class(boxes)))
68 63
 })
69 64
 
70
-test_that("a response with no matches gives a warning", {
65
+test_that('a response with no matches gives a warning', {
71 66
   check_api()
72 67
 
73
-  expect_warning(osem_boxes(grouptag = "does_not_exist"), "no senseBoxes found")
68
+  expect_warning(osem_boxes(grouptag = 'does_not_exist'), 'no senseBoxes found')
74 69
 })
75 70
 
76
-test_that("data.frame can be converted to sensebox data.frame", {
77
-  df <- osem_as_sensebox(data.frame(c(1,2), c("a", "b")))
78
-  expect_equal(class(df), c("sensebox", "data.frame"))
71
+test_that('data.frame can be converted to sensebox data.frame', {
72
+  df = osem_as_sensebox(data.frame(c(1, 2), c('a', 'b')))
73
+  expect_equal(class(df), c('sensebox', 'data.frame'))
79 74
 })
80 75
 
81
-test_that("boxes can be converted to sf object", {
76
+test_that('boxes can be converted to sf object', {
82 77
   check_api()
83 78
 
84
-  boxes <- osem_boxes()
85
-  boxes_sf <- sf::st_as_sf(boxes)
79
+  boxes = osem_boxes()
80
+  boxes_sf = sf::st_as_sf(boxes)
86 81
 
87 82
   expect_true(all(sf::st_is_simple(boxes_sf)))
88
-  expect_true("sf" %in% class(boxes_sf))
83
+  expect_true('sf' %in% class(boxes_sf))
89 84
 })
90 85
 
91
-test_that("boxes converted to sf object keep all attributes", {
86
+test_that('boxes converted to sf object keep all attributes', {
92 87
   check_api()
93 88
 
94
-  boxes <- osem_boxes()
95
-  boxes_sf <- sf::st_as_sf(boxes)
89
+  boxes = osem_boxes()
90
+  boxes_sf = sf::st_as_sf(boxes)
96 91
 
97 92
   # coord columns get removed!
98
-  cols <- names(boxes)[!names(boxes) %in% c('lon', 'lat')]
93
+  cols = names(boxes)[!names(boxes) %in% c('lon', 'lat')]
99 94
   expect_true(all(cols %in% names(boxes_sf)))
100 95
 
101
-  expect_true("sensebox" %in% class(boxes_sf))
96
+  expect_true('sensebox' %in% class(boxes_sf))
102 97
 })
103 98
 
104
-test_that("box retrieval does not give progress information in non-interactive mode", {
99
+test_that('box retrieval does not give progress information in non-interactive mode', {
105 100
   check_api()
106 101
 
107
-  if (!opensensmapr:::is_non_interactive()) skip("interactive session")
102
+  if (!opensensmapr:::is_non_interactive()) skip('interactive session')
108 103
 
109
-  out <- capture.output(b <- osem_boxes())
104
+  out = capture.output({
105
+    b = osem_boxes()
106
+  })
110 107
   expect_length(out, 0)
111 108
 })
112
-

+ 4
- 11
tests/testthat/test_counts.R View File

@@ -1,17 +1,10 @@
1
-context("counts")
1
+source('testhelpers.R')
2
+context('counts')
2 3
 
3
-check_api <- function() {
4
-  skip_on_cran()
5
-
6
-  code <- NA
7
-  try(code <- httr::status_code(httr::GET(osem_endpoint())))
8
-  if (is.na(code)) skip("API not available")
9
-}
10
-
11
-test_that("counts can be retrieved as a list of numbers", {
4
+test_that('counts can be retrieved as a list of numbers', {
12 5
   check_api()
13 6
 
14
-  counts <- osem_counts()
7
+  counts = osem_counts()
15 8
 
16 9
   expect_true(is.list(counts))
17 10
   expect_true(is.numeric(unlist(counts)))

+ 50
- 55
tests/testthat/test_measurements.R View File

@@ -1,121 +1,116 @@
1
-context("measurements")
2
-
3
-check_api <- function() {
4
-  skip_on_cran()
5
-
6
-  code <- NA
7
-  try(code <- httr::status_code(httr::GET(osem_endpoint())))
8
-  if (is.na(code)) skip("API not available")
9
-}
1
+source('testhelpers.R')
2
+context('measurements')
10 3
 
11 4
 try({
12
-  boxes <- osem_boxes()
13
- })
5
+  boxes = osem_boxes()
6
+})
14 7
 
15
-test_that("measurements can be retrieved for a phenomenon", {
8
+test_that('measurements can be retrieved for a phenomenon', {
16 9
   check_api()
17 10
 
18
-  measurements <- osem_measurements(x = "Windgeschwindigkeit")
11
+  measurements = osem_measurements(x = 'Windgeschwindigkeit')
19 12
   expect_true(is.data.frame(measurements))
20
-  expect_true("osem_measurements" %in% class(measurements))
13
+  expect_true('osem_measurements' %in% class(measurements))
21 14
 })
22 15
 
23
-test_that("measurement retrieval does not give progress information in non-interactive mode", {
16
+test_that('measurement retrieval does not give progress information in non-interactive mode', {
24 17
   check_api()
25 18
 
26
-  if (!opensensmapr:::is_non_interactive()) skip("interactive session")
19
+  if (!opensensmapr:::is_non_interactive()) skip('interactive session')
27 20
 
28
-  out <- capture.output(measurements <- osem_measurements(x = "Licht"))
21
+  out = capture.output({
22
+    measurements = osem_measurements(x = 'Licht')
23
+  })
29 24
   expect_length(out, 0)
30 25
 })
31 26
 
32
-test_that("a response with no matching senseBoxes gives an error", {
27
+test_that('a response with no matching senseBoxes gives an error', {
33 28
   check_api()
34 29
 
35
-  expect_error(osem_measurements(x = "Windgeschwindigkeit", exposure = "indoor"), "No senseBoxes found")
30
+  expect_error(osem_measurements(x = 'Windgeschwindigkeit', exposure = 'indoor'), 'No senseBoxes found')
36 31
 })
37 32
 
38
-test_that("data.frame can be converted to measurements data.frame", {
39
-  df <- osem_as_measurements(data.frame(c(1,2), c("a", "b")))
40
-  expect_equal(class(df), c("osem_measurements", "data.frame"))
33
+test_that('data.frame can be converted to measurements data.frame', {
34
+  df = osem_as_measurements(data.frame(c(1, 2), c('a', 'b')))
35
+  expect_equal(class(df), c('osem_measurements', 'data.frame'))
41 36
 })
42 37
 
43
-test_that("columns can be specified for phenomena", {
38
+test_that('columns can be specified for phenomena', {
44 39
   check_api()
45 40
 
46
-  cols <- c("value", "boxId", "boxName")
47
-  measurements <- osem_measurements(x = "Windgeschwindigkeit", columns = cols)
41
+  cols = c('value', 'boxId', 'boxName')
42
+  measurements = osem_measurements(x = 'Windgeschwindigkeit', columns = cols)
48 43
   expect_equal(names(measurements), cols)
49 44
 })
50 45
 
51
-test_that("measurements can be retrieved for a phenomenon and exposure", {
46
+test_that('measurements can be retrieved for a phenomenon and exposure', {
52 47
   check_api()
53 48
 
54
-  measurements <- osem_measurements(x = "Temperatur", exposure = "unknown",
55
-                                    columns = c("value", "boxId", "boxName"))
49
+  measurements = osem_measurements(x = 'Temperatur', exposure = 'unknown',
50
+                                    columns = c('value', 'boxId', 'boxName'))
56 51
   expect_equal(nrow(measurements), 0)
57 52
 })
58 53
 
59
-test_that("measurements of specific boxes can be retrieved for one phenomenon and returns a measurements data.frame", {
54
+test_that('measurements of specific boxes can be retrieved for one phenomenon and returns a measurements data.frame', {
60 55
   check_api()
61 56
 
62 57
   # fix for subsetting
63
-  class(boxes) <- c("data.frame")
64
-  three_boxes <- boxes[1:3,]
65
-  class(boxes) <- c("sensebox", "data.frame")
66
-  three_boxes <- osem_as_sensebox(three_boxes)
67
-  phens <- names(osem_phenomena(three_boxes))
58
+  class(boxes) = c('data.frame')
59
+  three_boxes = boxes[1:3, ]
60
+  class(boxes) = c('sensebox', 'data.frame')
61
+  three_boxes = osem_as_sensebox(three_boxes)
62
+  phens = names(osem_phenomena(three_boxes))
68 63
 
69
-  measurements <- osem_measurements(x = three_boxes, phenomenon = phens[[1]])
64
+  measurements = osem_measurements(x = three_boxes, phenomenon = phens[[1]])
70 65
   expect_true(is.data.frame(measurements))
71
-  expect_true("osem_measurements" %in% class(measurements))
66
+  expect_true('osem_measurements' %in% class(measurements))
72 67
 })
73 68
 
74
-test_that("measurements can be retrieved for a bounding box", {
69
+test_that('measurements can be retrieved for a bounding box', {
75 70
   check_api()
76 71
 
77
-  sfc <- sf::st_sfc(sf::st_linestring(x = matrix(data = c(7, 8, 50, 51), ncol = 2)), crs = 4326)
78
-  bbox <- sf::st_bbox(sfc)
79
-  measurements <- osem_measurements(x = bbox, phenomenon = "Windrichtung")
72
+  sfc = sf::st_sfc(sf::st_linestring(x = matrix(data = c(7, 8, 50, 51), ncol = 2)), crs = 4326)
73
+  bbox = sf::st_bbox(sfc)
74
+  measurements = osem_measurements(x = bbox, phenomenon = 'Windrichtung')
80 75
   expect_true(all(unique(measurements$lat) > 50))
81 76
   expect_true(all(unique(measurements$lat) < 51))
82 77
   expect_true(all(unique(measurements$lon) < 8))
83 78
   expect_true(all(unique(measurements$lon) > 7))
84 79
 })
85 80
 
86
-test_that("measurements can be retrieved for a time period", {
81
+test_that('measurements can be retrieved for a time period', {
87 82
   check_api()
88 83
 
89
-  from_date <- as.POSIXct("2018-01-01 12:00:00")
90
-  to_date <- as.POSIXct("2018-01-01 13:00:00")
91
-  measurements <- osem_measurements(x = "Temperature", from = from_date, to = to_date)
84
+  from_date = as.POSIXct('2018-01-01 12:00:00')
85
+  to_date = as.POSIXct('2018-01-01 13:00:00')
86
+  measurements = osem_measurements(x = 'Temperature', from = from_date, to = to_date)
92 87
 
93 88
   expect_true(all(measurements$createdAt < to_date))
94 89
   expect_true(all(measurements$createdAt > from_date))
95 90
 })
96 91
 
97
-test_that("measurements can be retrieved for a time period > 31 days", {
92
+test_that('measurements can be retrieved for a time period > 31 days', {
98 93
   check_api()
99 94
 
100
-  from_date <- as.POSIXct("2017-11-01 12:00:00")
101
-  to_date <- as.POSIXct("2018-01-01 13:00:00")
102
-  measurements <- osem_measurements(x = "Windrichtung", from = from_date, to = to_date)
95
+  from_date = as.POSIXct('2017-11-01 12:00:00')
96
+  to_date = as.POSIXct('2018-01-01 13:00:00')
97
+  measurements = osem_measurements(x = 'Windrichtung', from = from_date, to = to_date)
103 98
 
104 99
   expect_true(all(measurements$createdAt < to_date))
105 100
   expect_true(all(measurements$createdAt > from_date))
106 101
 })
107 102
 
108
-test_that("both from and to are required when requesting measurements, error otherwise", {
109
-  expect_error(osem_measurements(x = "Temperature", from = as.POSIXct("2017-01-01")), "only together with")
110
-  expect_error(osem_measurements(x = "Temperature", to   = as.POSIXct("2017-01-01")), "only together with")
103
+test_that('both from and to are required when requesting measurements, error otherwise', {
104
+  expect_error(osem_measurements(x = 'Temperature', from = as.POSIXct('2017-01-01')), 'only together with')
105
+  expect_error(osem_measurements(x = 'Temperature', to   = as.POSIXct('2017-01-01')), 'only together with')
111 106
 })
112 107
 
113
-test_that("[.osem_measurements maintains attributes", {
108
+test_that('[.osem_measurements maintains attributes', {
114 109
   check_api()
115 110
 
116
-  sfc <- sf::st_sfc(sf::st_linestring(x = matrix(data = c(7, 8, 50, 51), ncol = 2)), crs = 4326)
117
-  bbox <- sf::st_bbox(sfc)
118
-  m <- osem_measurements(x = bbox, phenomenon = "Windrichtung")
111
+  sfc = sf::st_sfc(sf::st_linestring(x = matrix(data = c(7, 8, 50, 51), ncol = 2)), crs = 4326)
112
+  bbox = sf::st_bbox(sfc)
113
+  m = osem_measurements(x = bbox, phenomenon = 'Windrichtung')
119 114
 
120 115
   expect_true(all(attributes(m[1:nrow(m), ]) %in% attributes(m)))
121 116
 })

+ 14
- 21
tests/testthat/test_phenomena.R View File

@@ -1,40 +1,33 @@
1
-context("phenomena")
2
-
3
-check_api <- function() {
4
-  skip_on_cran()
5
-
6
-  code <- NA
7
-  try(code <- httr::status_code(httr::GET(osem_endpoint())))
8
-  if (is.na(code)) skip("API not available")
9
-}
1
+source('testhelpers.R')
2
+context('phenomena')
10 3
 
11 4
 try({
12
-  boxes <- osem_boxes()
13
-  all_phen <- unique(unlist(boxes$phenomena))
5
+  boxes = osem_boxes()
6
+  all_phen = unique(unlist(boxes$phenomena))
14 7
 })
15 8
 
16
-test_that("phenomena from boxes is a list of counts", {
9
+test_that('phenomena from boxes is a list of counts', {
17 10
   check_api()
18 11
 
19
-  phenomena <- osem_phenomena(boxes)
12
+  phenomena = osem_phenomena(boxes)
20 13
 
21 14
   expect_true(is.numeric(unlist(phenomena)))
22 15
   expect_true(is.list(phenomena))
23 16
 })
24 17
 
25
-test_that("phenomena from boxes has all phenomena", {
18
+test_that('phenomena from boxes has all phenomena', {
26 19
   check_api()
27 20
 
28
-  phenomena <- osem_phenomena(boxes)
21
+  phenomena = osem_phenomena(boxes)
29 22
 
30 23
   expect_true(all(all_phen %in% names(phenomena)))
31 24
   expect_true(all(names(phenomena) %in% all_phen))
32 25
 })
33 26
 
34
-test_that("phenomena from a not sensebox data.frame returns error", {
35
-  expect_error(osem_phenomena(list()), "no applicable method")
36
-  expect_error(osem_phenomena(data.frame()), "no applicable method")
37
-  boxes_df <- boxes
38
-  class(boxes_df) <- c("data.frame")
39
-  expect_error(osem_phenomena(boxes_df), "no applicable method")
27
+test_that('phenomena from a not sensebox data.frame returns error', {
28
+  expect_error(osem_phenomena(list()), 'no applicable method')
29
+  expect_error(osem_phenomena(data.frame()), 'no applicable method')
30
+  boxes_df = boxes
31
+  class(boxes_df) = c('data.frame')
32
+  expect_error(osem_phenomena(boxes_df), 'no applicable method')
40 33
 })

+ 9
- 0
tests/testthat/testhelpers.R View File

@@ -0,0 +1,9 @@
1
+check_api = function() {
2
+  skip_on_cran()
3
+
4
+  code = NA
5
+  try({
6
+    code = httr::status_code(httr::GET(osem_endpoint()))
7
+  })
8
+  if (is.na(code)) skip('API not available')
9
+}

Loading…
Cancel
Save