From 88ca7c4fe50d1b5bfeabe82e717e7f713a981e48 Mon Sep 17 00:00:00 2001 From: Dave Slager Date: Tue, 16 Jan 2024 07:54:50 -0800 Subject: [PATCH 1/3] setup vcr for tests --- .gitattributes | 2 ++ .gitignore | 1 - DESCRIPTION | 3 ++- tests/testthat/helper-vcr.R | 10 ++++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .gitattributes create mode 100644 tests/testthat/helper-vcr.R diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..165ff7a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +tests/fixtures/**/* -diff diff --git a/.gitignore b/.gitignore index e53e426..9c14b52 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ .Rhistory .RData *.Rproj -.gitattributes krlocal.R /.Rapp.history .DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index 3c6bbb9..6ae79f1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,7 +32,8 @@ Suggests: knitr, rmarkdown, testthat, - covr + covr, + vcr (>= 0.6.0) RoxygenNote: 7.2.3 X-schema.org-applicationCategory: Data Access X-schema.org-keywords: birds, birding, ebird, database, data, biology, observations, sightings, ornithology diff --git a/tests/testthat/helper-vcr.R b/tests/testthat/helper-vcr.R new file mode 100644 index 0000000..b2abf49 --- /dev/null +++ b/tests/testthat/helper-vcr.R @@ -0,0 +1,10 @@ +library("vcr") # *Required* as vcr is set up on loading +invisible(vcr::vcr_configure( + filter_sensitive_data = list("<<>>" = Sys.getenv('EBIRD_KEY')), + dir = vcr::vcr_test_path("fixtures") +)) +vcr::check_cassette_names() +# Create fake EBIRD_KEY if not present (e.g, on CI or CRAN) +if (!nzchar(Sys.getenv("EBIRD_KEY"))) { + Sys.setenv("EBIRD_KEY" = "fake_key") +} From 8836b2512f1f323908d28e763ad0bb40e7f3c095 Mon Sep 17 00:00:00 2001 From: Dave Slager Date: Sun, 21 Jan 2024 18:40:01 -0800 Subject: [PATCH 2/3] use vcr for ebirdchecklistfeed tests --- tests/testthat/test-ebirdchecklistfeed.R | 43 +++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/tests/testthat/test-ebirdchecklistfeed.R b/tests/testthat/test-ebirdchecklistfeed.R index 7932161..9d244ad 100644 --- a/tests/testthat/test-ebirdchecklistfeed.R +++ b/tests/testthat/test-ebirdchecklistfeed.R @@ -1,22 +1,27 @@ -context("ebirdchecklistfeed") - -test_that("ebirdchecklistfeed succeeds reproducibly", { - skip_on_cran() - skip_on_ci() +vcr::use_cassette("ebirdchecklistfeed", { - out1 <- ebirdchecklistfeed(loc = "L207391", date = "2020-03-24", max = 3) - expect_is(out1, "data.frame") - expect_is(out1$userDisplayName, "character") - expect_equal(nrow(out1), 3) - expect_equal(ncol(out1), 9) -}) - -test_that("ebirdchecklistfeed errors for bad input", { - skip_on_cran() - skip_on_ci() + test_that("ebirdchecklistfeed succeeds reproducibly", { + # Check that max query was readjusted to 200 with warning + expect_warning( + out1 <- ebirdchecklistfeed(loc = "L207391", date = "2020-03-24", max = 250), + "Maximum number of results supplied was > 200, using 200." + ) + # But the query is still successful + expect_is(out1, "data.frame") + expect_is(out1$userDisplayName, "character") + expect_equal(nrow(out1), 7) + expect_equal(ncol(out1), 9) + }) + + test_that("ebirdchecklistfeed errors for bad input", { + # Bad location ID + expect_error(ebirdchecklistfeed(loc = "LL207391", date = "2020-03-25", max = 10)) + }) + + test_that("ebirdchecklistfeed errors locally", { + # Bad dates + expect_error(ebirdchecklistfeed(loc = "L207391", date = "2121-03-25")) + expect_error(ebirdchecklistfeed(loc = "L207391", date = "03-25-2020")) + }) - expect_warning(ebirdchecklistfeed(loc = "L207391", date = "2020-03-24", max = 250)) - expect_error(ebirdchecklistfeed(loc = "L207391", date = "2121-03-25")) - expect_error(ebirdchecklistfeed(loc = "L207391", date = "03-25-2020")) - expect_error(ebirdchecklistfeed(loc = "LL207391", date = "2020-03-25", max = 10)) }) From 9d0eb99772eab95cb73251f9755938abf9cc5efa Mon Sep 17 00:00:00 2001 From: Dave Slager Date: Sun, 21 Jan 2024 18:53:44 -0800 Subject: [PATCH 3/3] add yml file for ebirdchecklistfeed --- tests/fixtures/ebirdchecklistfeed.yml | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tests/fixtures/ebirdchecklistfeed.yml diff --git a/tests/fixtures/ebirdchecklistfeed.yml b/tests/fixtures/ebirdchecklistfeed.yml new file mode 100644 index 0000000..3dac571 --- /dev/null +++ b/tests/fixtures/ebirdchecklistfeed.yml @@ -0,0 +1,110 @@ +http_interactions: +- request: + method: get + uri: https://ebird.org/ws2.0/product/lists/L207391/2020/03/24?maxResults=200 + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + X-eBirdApiToken: <<>> + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + date: Wed, 17 Jan 2024 06:27:48 GMT + server: Apache + cache-control: no-cache, no-store, max-age=0, must-revalidate + content-encoding: gzip + content-type: application/json;charset=utf-8 + expires: '0' + pragma: no-cache + strict-transport-security: max-age=31536000 ; includeSubDomains + vary: Accept-Encoding,Origin,Access-Control-Request-Method,Access-Control-Request-Headers + x-content-type-options: nosniff + x-frame-options: DENY + x-xss-protection: 1; mode=block + transfer-encoding: chunked + body: + encoding: '' + file: no + string: '[{"locId":"L207391","subId":"S66174580","userDisplayName":"David Wood","numSpecies":10,"obsDt":"24 + Mar 2020","obsTime":"14:47","isoObsDate":"2020-03-24 14:47","subID":"S66174580","loc":{"locId":"L207391","name":"Mount + Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S66173590","userDisplayName":"Sofia + Prado-Irwin","numSpecies":15,"obsDt":"24 Mar 2020","obsTime":"14:31","isoObsDate":"2020-03-24 + 14:31","subID":"S66173590","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S66194110","userDisplayName":"Jeffrey + Gantz","numSpecies":19,"obsDt":"24 Mar 2020","obsTime":"13:30","isoObsDate":"2020-03-24 + 13:30","subID":"S66194110","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S66171158","userDisplayName":"Ann + Gurka","numSpecies":21,"obsDt":"24 Mar 2020","obsTime":"13:00","isoObsDate":"2020-03-24 + 13:00","subID":"S66171158","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S71419937","userDisplayName":"Jacqui + Kluft","numSpecies":20,"obsDt":"24 Mar 2020","obsTime":"10:30","isoObsDate":"2020-03-24 + 10:30","subID":"S71419937","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S70981564","userDisplayName":"Barbara + Olson","numSpecies":20,"obsDt":"24 Mar 2020","obsTime":"10:30","isoObsDate":"2020-03-24 + 10:30","subID":"S70981564","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}},{"locId":"L207391","subId":"S66184472","userDisplayName":"Jason + Barcus","numSpecies":24,"obsDt":"24 Mar 2020","obsTime":"07:30","isoObsDate":"2020-03-24 + 07:30","subID":"S66184472","loc":{"locId":"L207391","name":"Mount Auburn Cemetery","latitude":42.371,"longitude":-71.146,"countryCode":"US","countryName":"United + States","subnational1Name":"Massachusetts","subnational1Code":"US-MA","subnational2Code":"US-MA-017","subnational2Name":"Middlesex","isHotspot":true,"locName":"Mount + Auburn Cemetery","lat":42.371,"lng":-71.146,"hierarchicalName":"Mount Auburn + Cemetery, Middlesex, Massachusetts, US","locID":"L207391"}}]' + recorded_at: 2024-01-17 06:27:49 GMT + recorded_with: vcr/1.2.2, webmockr/0.9.0 +- request: + method: get + uri: https://ebird.org/ws2.0/product/lists/LL207391/2020/03/25?maxResults=10 + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + X-eBirdApiToken: <<>> + response: + status: + status_code: 400 + category: Client error + reason: Bad Request + message: 'Client error: (400) Bad Request' + headers: + date: Wed, 17 Jan 2024 06:27:49 GMT + server: Apache + cache-control: no-cache, no-store, max-age=0, must-revalidate + content-type: application/json + expires: '0' + pragma: no-cache + strict-transport-security: max-age=31536000 ; includeSubDomains + vary: Accept-Encoding,Origin,Access-Control-Request-Method,Access-Control-Request-Headers + x-content-type-options: nosniff + x-frame-options: DENY + x-xss-protection: 1; mode=block + content-encoding: gzip + content-length: '211' + connection: close + body: + encoding: '' + file: no + string: '{"errors":[{"status":"400 BAD_REQUEST","code":"methodInvocation","title":"Field + regionCode of recListsCmd: Property regionCode threw exception; nested exception + is java.lang.IllegalArgumentException: Unable to find matching region type + for LL207391"}]}' + recorded_at: 2024-01-17 06:27:49 GMT + recorded_with: vcr/1.2.2, webmockr/0.9.0