Skip to content

Commit

Permalink
fixes for lcd and isd tests that also cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Dec 30, 2019
1 parent f8ad924 commit e1acf73
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 97 deletions.
37 changes: 2 additions & 35 deletions tests/fixtures/isd_query.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
http_interactions:
- request:
method: get
uri: https://www1.ncdc.noaa.gov/pub/data/noaa/1986/011490-99999-1986.gz
body:
encoding: ''
string: ''
headers:
User-Agent: libcurl/7.54.0 r-curl/4.3 crul/0.9.0
Accept-Encoding: gzip, deflate
Accept: application/json, text/xml, application/xml, */*
response:
status:
status_code: '200'
message: OK
explanation: Request fulfilled, document follows
headers:
status: HTTP/1.1 200 OK
date: Mon, 30 Dec 2019 19:45:35 GMT
server: Apache
strict-transport-security: max-age=31536000
last-modified: Wed, 27 Oct 2004 13:05:49 GMT
etag: '"2abc5d3-891e-3e76d7c0fe540"'
accept-ranges: bytes
content-length: '35102'
content-type: application/x-gzip
access-control-allow-origin: '*'
connection: close
body:
encoding: UTF-8
file: yes
string: /Users/sckott/github/ropensci/rnoaa/tests/files/011490-99999-1986.gz
recorded_at: 2019-12-30 19:45:43 GMT
recorded_with: vcr/0.4.0, webmockr/0.5.0
- request:
method: get
uri: https://www1.ncdc.noaa.gov/pub/data/noaa/1985/011490-99999-1985.gz
Expand All @@ -49,7 +16,7 @@ http_interactions:
explanation: Request fulfilled, document follows
headers:
status: HTTP/1.1 200 OK
date: Mon, 30 Dec 2019 19:45:40 GMT
date: Mon, 30 Dec 2019 20:28:49 GMT
server: Apache
strict-transport-security: max-age=31536000
last-modified: Mon, 22 Nov 2004 19:35:35 GMT
Expand All @@ -63,5 +30,5 @@ http_interactions:
encoding: UTF-8
file: yes
string: /Users/sckott/github/ropensci/rnoaa/tests/files/011490-99999-1985.gz
recorded_at: 2019-12-30 19:45:43 GMT
recorded_at: 2019-12-30 20:28:53 GMT
recorded_with: vcr/0.4.0, webmockr/0.5.0
4 changes: 2 additions & 2 deletions tests/fixtures/lcd_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ http_interactions:
explanation: Request fulfilled, document follows
headers:
status: HTTP/1.1 200 OK
date: Mon, 30 Dec 2019 19:54:03 GMT
date: Mon, 30 Dec 2019 20:27:50 GMT
server: Apache
strict-transport-security: max-age=31536000
last-modified: Sat, 15 Dec 2018 17:53:37 GMT
Expand All @@ -31,5 +31,5 @@ http_interactions:
encoding: UTF-8
file: yes
string: /Users/sckott/github/ropensci/rnoaa/tests/files/2017_01338099999.csv
recorded_at: 2019-12-30 19:54:04 GMT
recorded_at: 2019-12-30 20:27:52 GMT
recorded_with: vcr/0.4.0, webmockr/0.5.0
4 changes: 2 additions & 2 deletions tests/fixtures/lcd_not_found.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ http_interactions:
explanation: Nothing matches the given URI
headers:
status: HTTP/1.1 404 Not Found
date: Mon, 30 Dec 2019 19:54:26 GMT
date: Mon, 30 Dec 2019 20:27:54 GMT
server: Apache
strict-transport-security: max-age=31536000
content-length: '256'
Expand All @@ -28,5 +28,5 @@ http_interactions:
encoding: UTF-8
file: yes
string: /Users/sckott/github/ropensci/rnoaa/tests/files/1945_02413099999.csv
recorded_at: 2019-12-30 19:54:26 GMT
recorded_at: 2019-12-30 20:27:54 GMT
recorded_with: vcr/0.4.0, webmockr/0.5.0
29 changes: 13 additions & 16 deletions tests/testthat/test-isd.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
context("isd")

# FIXME: uncomment this test block when figure out writing to disk
# tests alongside caching those files on disk

# delete any cached files
# isd_dir <- rappdirs::user_cache_dir("rnoaa/isd")
# unlink(list.files(isd_dir, full.names = TRUE))

# test_that("isd gets data", {
# skip_on_cran()
# skip_if_government_down()
test_that("isd gets data", {
skip_on_cran()
skip_if_government_down()

# vcr::use_cassette("isd_query", {
# data_a <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1986))
# data_b <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1985))
# })
vcr::use_cassette("isd_query", {
data_a <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1986))
data_b <- suppressMessages(isd(usaf = "011490", wban = "99999", year = 1985))
})

# expect_is(data_a, "tbl_df")
# # no longer a df in a list
# expect_null(suppressWarnings(data_a$data))
# expect_is(data_a$quality, "character")
expect_is(data_a, "tbl_df")
# no longer a df in a list
expect_null(suppressWarnings(data_a$data))
expect_is(data_a$quality, "character")

# expect_lt(NROW(data_a), NROW(data_b))
# })
expect_lt(NROW(data_a), NROW(data_b))
})

test_that("isd fails well", {
skip_on_cran()
Expand Down
81 changes: 39 additions & 42 deletions tests/testthat/test-lcd.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
context("lcd")

# FIXME: uncomment this test block when figure out writing to disk
# tests alongside caching those files on disk

# # clean up first
# clean up first
# lcd_cache$delete_all()

# test_that("lcd", {
# skip_on_cran()
# skip_if_government_down()

# vcr::use_cassette("lcd_1", {
# aa <- lcd(station = "01338099999", year = 2017)
# })

# expect_is(aa, "tbl_df")

# expect_type(aa$station, 'integer')
# expect_type(aa$date, 'character')
# expect_type(aa$latitude, 'double')
# expect_type(aa$longitude, 'double')
# expect_type(aa$elevation, 'double')
# expect_type(aa$hourlysealevelpressure, 'double')
# })

# test_that("lcd fails well", {
# skip_on_cran()
# skip_if_government_down()

# # a station/year combination that doesn't exist
# vcr::use_cassette("lcd_not_found", {
# expect_error(lcd(station = "02413099999", year = "1945"),
# "Not Found", class = "error")
# })

# # class
# expect_error(lcd(5),
# "\"year\" is missing, with no default")
# expect_error(lcd(5, 5),
# "year must be between 1901")
# expect_error(lcd(list(1), 5),
# "station must be of class")
# expect_error(lcd(5, list(1)),
# "year must be of class")
# })
test_that("lcd", {
skip_on_cran()
skip_if_government_down()

vcr::use_cassette("lcd_1", {
aa <- lcd(station = "01338099999", year = 2017)
})

expect_is(aa, "tbl_df")

expect_type(aa$station, 'integer')
expect_type(aa$date, 'character')
expect_type(aa$latitude, 'double')
expect_type(aa$longitude, 'double')
expect_type(aa$elevation, 'double')
expect_type(aa$hourlysealevelpressure, 'double')
})

test_that("lcd fails well", {
skip_on_cran()
skip_if_government_down()

# a station/year combination that doesn't exist
vcr::use_cassette("lcd_not_found", {
expect_error(lcd(station = "02413099999", year = "1945"),
"Not Found", class = "error")
})

# class
expect_error(lcd(5),
"\"year\" is missing, with no default")
expect_error(lcd(5, 5),
"year must be between 1901")
expect_error(lcd(list(1), 5),
"station must be of class")
expect_error(lcd(5, list(1)),
"year must be of class")
})

0 comments on commit e1acf73

Please sign in to comment.