tests_general.R 12.7 KB
Newer Older
Laura A DeCicco's avatar
Laura A DeCicco committed
1
2
3
4
5
6
7
context("General functions")

test_that("General NWIS retrievals working", {
  testthat::skip_on_cran()
  
  multiSite <- readNWISdata(sites=c("04025500","040263491"), service="iv", 
                            parameterCd="00060")
Laura A DeCicco's avatar
Laura A DeCicco committed
8
  expect_is(multiSite$dateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
9
  # saveRDS(multiSite, "rds/multiSite.rds")
Laura A DeCicco's avatar
Laura A DeCicco committed
10
  
Laura A DeCicco's avatar
Laura A DeCicco committed
11
  expect_error(readNWISdata(), "No arguments supplied")
Laura A DeCicco's avatar
Laura A DeCicco committed
12
  
Laura A DeCicco's avatar
Laura A DeCicco committed
13
  bBoxEx <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010")
Laura A DeCicco's avatar
Laura A DeCicco committed
14
  expect_that(length(unique(bBoxEx$site_no)) > 1, is_true())
Laura A DeCicco's avatar
Laura A DeCicco committed
15
  # saveRDS(bBoxEx, "rds/bBoxEx.rds")
Laura A DeCicco's avatar
Laura A DeCicco committed
16
17
18
19
20
  
  startDate <- as.Date("2013-10-01")
  endDate <- as.Date("2014-09-30")
  waterYear <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010", 
                   service="dv", startDate=startDate, endDate=endDate)
Laura A DeCicco's avatar
Laura A DeCicco committed
21
  # saveRDS(waterYear, "rds/waterYear.rds")
22
  expect_is(waterYear$dateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
23
24
25
  
  siteInfo <- readNWISdata(stateCd="WI", parameterCd="00010",hasDataTypeCd="iv", 
                           service="site")
Laura A DeCicco's avatar
Laura A DeCicco committed
26
  # saveRDS(siteInfo,"rds/siteInfo.rds")
27
28
29
30
31
32
  expect_is(siteInfo$station_nm, "character")
  
  qwData <- readNWISdata(bBox=c(-82.5,41.52,-81,41),startDate=as.Date("2000-01-01"),
                   drain_area_va_min=50, qw_count_nu=50,qw_attributes="expanded",
                    qw_sample_wide="wide",list_of_search_criteria=c("lat_long_bounding_box",
                    "drain_area_va","obs_count_nu"),service="qw")
Laura A DeCicco's avatar
Laura A DeCicco committed
33
  # saveRDS(qwData, "rds/qwData.rds")
34
  expect_is(qwData$startDateTime, "POSIXct")
Laura A DeCicco's avatar
Laura A DeCicco committed
35
  
Laura A DeCicco's avatar
Laura A DeCicco committed
36
  url <- "https://waterservices.usgs.gov/nwis/dv/?site=09037500&format=rdb&ParameterCd=00060&StatCd=00003&startDT=1985-10-02&endDT=2012-09-06"
37
38
  dv <- importRDB1(url, asDateTime = FALSE)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
39
  urlEmpty <- "https://nwis.waterdata.usgs.gov/nwis/qwdata?multiple_site_no=413437087150601&sort_key=site_no&group_key=NONE&inventory_output=0&begin_date=&end_date=&TZoutput=0&param_group=NUT,INN&qw_attributes=0&format=rdb&qw_sample_wide=0&rdb_qw_attributes=expanded&date_format=YYYY-MM-DD&rdb_compression=value&list_of_search_criteria=multiple_site_no"
40
  dv <- importRDB1(urlEmpty, asDateTime = FALSE)
Laura A DeCicco's avatar
Laura A DeCicco committed
41
  # saveRDS(dv, "rds/emptyDV.rds")
42
  expect_that(nrow(dv) == 0, is_true())
43
44
45
  
  dailyStat <- readNWISdata(site=c("03112500","03111520"),service="stat",statReportType="daily",
                           statType=c("p25","p50","p75","min","max"),parameterCd="00065",convertType=FALSE)
Laura A DeCicco's avatar
Laura A DeCicco committed
46
  # saveRDS(dailyStat,"rds/dailyStat.rds")
David Watkins's avatar
David Watkins committed
47
  expect_that(length(dailyStat$min_va) > 1, is_true())
48
49
50
51
  expect_is(dailyStat$p25_va,"character")
  
  waterYearStat <- readNWISdata(site=c("03112500"),service="stat",statReportType="annual",
                                statYearType="water", missingData="on")
Laura A DeCicco's avatar
Laura A DeCicco committed
52
  # saveRDS(waterYearStat, "rds/waterYearStat.rds")
53
54
  expect_is(waterYearStat$mean_va,"numeric")
  expect_is(waterYearStat$parameter_cd,"character")
Laura A DeCicco's avatar
Laura A DeCicco committed
55
56
57
58
59
60
  
  #2 data descriptors, but some random empty "values" tag:
  urlTest <- "https://nwis.waterservices.usgs.gov/nwis/iv/?site=11447650&format=waterml,1.1&ParameterCd=63680&startDT=2016-12-13&endDT=2016-12-13"
  x <- importWaterML1(urlTest)
  expect_equal(ncol(x), 8)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
61
62
63
64
65
66
67
68
  #Test list:
  args <- list(sites="05114000", service="iv", 
               parameterCd="00060", 
               startDate="2014-05-01T00:00Z",
               endDate="2014-05-01T12:00Z")
  
  instData <- readNWISdata(args)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
69
  args <- list(sites="05114000", service="dv", 
Laura A DeCicco's avatar
Laura A DeCicco committed
70
71
72
73
               parameterCd="00060", 
               startDate="2014-05-01",
               endDate="2014-05-01")
  
Laura A DeCicco's avatar
Laura A DeCicco committed
74
  dailyData <- readNWISdata(args)
Laura A DeCicco's avatar
Laura A DeCicco committed
75
  expect_lt(nrow(dailyData), nrow(instData))
Laura A DeCicco's avatar
Laura A DeCicco committed
76
77
78
  args <- list(stateCd="OH",parameterCd="00665")
  sites <- whatNWISsites(args)
  expect_type(sites, "list")
Laura A DeCicco's avatar
Laura A DeCicco committed
79
80
81
82
83
84
85
86
87
  
  #Test counties:
  dailyStaffordVA <- readNWISdata(stateCd = "Virginia",
                                  countyCd="Stafford",
                                  parameterCd = "00060",
                                  startDate = "2015-01-01",
                                  endDate = "2015-01-30")
  expect_gt(nrow(dailyStaffordVA),1)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
88
89
90
  AS <- readNWISdata(stateCd = "AS", service="site")
  expect_gt(nrow(AS),0)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
91
92
93
94
95
})


test_that("General WQP retrievals working", {
  testthat::skip_on_cran()
Laura A DeCicco's avatar
Laura A DeCicco committed
96
97
98
99
  nameToUse <- "pH"
  pHData <- readWQPdata(siteid="USGS-04024315",characteristicName=nameToUse)
  expect_is(pHData$ActivityStartDateTime, 'POSIXct')
  
Laura A DeCicco's avatar
Laura A DeCicco committed
100
101
102
103
104
105
106
107
108
109
  #testing lists:
  startDate <- as.Date("2013-01-01")
  secchi.names = c("Depth, Secchi disk depth",
                   "Depth, Secchi disk depth (choice list)",
                   "Secchi Reading Condition (choice list)",
                   "Secchi depth",
                   "Water transparency, Secchi disc")
  args_2 <- list('startDateLo' = startDate,
               'startDateHi' = "2013-12-31",
                statecode="WI",
110
                characteristicName=secchi.names)
Laura A DeCicco's avatar
Laura A DeCicco committed
111

112
  wqp.summary <- readWQPdata(args_2, querySummary = TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
113
114
115
116
117
118
119
120
121
122
  expect_true("list" %in% class(wqp.summary))
  
  #pretty sloooow:
  wqp.data <- readWQPdata(args_2, querySummary = FALSE)
  expect_false("list" %in% class(wqp.data))
  
  # Testing multiple lists:
  arg_3 <- list('startDateLo' = startDate,
               'startDateHi' = "2013-12-31")
  arg_4 <- list(statecode="WI",
Laura A DeCicco's avatar
Laura A DeCicco committed
123
124
                characteristicName=secchi.names)
  wqp.summary <- readWQPdata(arg_3, arg_4, querySummary=TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
125
126
  expect_true("list" %in% class(wqp.summary))
  
Laura A DeCicco's avatar
Laura A DeCicco committed
127
128
129
  lakeSites <- whatWQPsites(args_2)
  expect_type(lakeSites, "list")
  
Laura A DeCicco's avatar
Laura A DeCicco committed
130
131
132
133
134
135
136
  # Test county code:
   dailyLexingtonVA <- readWQPdata(statecode = "Virginia", 
                                   countycode="Lexington", 
                                   parameterCd = "00010")
   
   expect_equal(ncol(dailyLexingtonVA),65)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
137
138
139
140
141
142
143
   bioData <- readWQPdata(statecode = "WI",
                          countycode = "Dane",
                          providers = "BIODATA")
   
   expect_equal(attr(bioData, "url"), "https://www.waterqualitydata.us/Result/search?statecode=US%3A55&countycode=US%3A55%3A025&providers=BIODATA&zip=no&sorted=no&mimeType=tsv")
   expect_gt(nrow(bioData), 1)
   
144
  # Known slow query for WQP:
Laura A DeCicco's avatar
Laura A DeCicco committed
145
146
147
  # pHDataExpanded2 <- readWQPdata(bBox=c(-90.1,42.9,-89.9,43.1),
  #                                characteristicName=nameToUse, querySummary = TRUE)
  # expect_is(pHDataExpanded2, 'list')
Laura A DeCicco's avatar
Laura A DeCicco committed
148
  
Laura A DeCicco's avatar
Laura A DeCicco committed
149
150
151
152
153
  # Super slow:
  # startDate <- as.Date("2013-01-01")
  # nutrientDaneCounty <- readWQPdata(countycode="US:55:025",startDate=startDate,
  #                        characteristicType="Nutrient")
  # expect_is(nutrientDaneCounty$ActivityStartDateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
154
})
Lindsay R Carr's avatar
Lindsay R Carr committed
155

156
test_that("WQP head query retrievals working", {
Lindsay R Carr's avatar
Lindsay R Carr committed
157
158
159
160
161
  testthat::skip_on_cran()
  nameToUse <- "pH"
  pHDataQueryResults <- readWQPdata(siteid="USGS-04024315",
                                    characteristicName=nameToUse, 
                                    querySummary=TRUE)
162
163
  expect_false(is.null(pHDataQueryResults$date))
  expect_is(pHDataQueryResults$date, 'Date')
Lindsay R Carr's avatar
Lindsay R Carr committed
164
  expect_false(is.null(pHDataQueryResults$`total-site-count`))
165
  expect_is(pHDataQueryResults$`total-site-count`, 'numeric')
Lindsay R Carr's avatar
Lindsay R Carr committed
166
  expect_false(is.null(pHDataQueryResults$`total-result-count`))
167
168
169
170
171
172
173
174
175
176
177
  expect_is(pHDataQueryResults$`total-result-count`, 'numeric')
  
  pHDataQueryResults <- readWQPqw(siteNumbers="USGS-04024315",
                                  parameterCd=nameToUse, 
                                  querySummary=TRUE)
  expect_false(is.null(pHDataQueryResults$date))
  expect_is(pHDataQueryResults$date, 'Date')
  expect_false(is.null(pHDataQueryResults$`total-site-count`))
  expect_is(pHDataQueryResults$`total-site-count`, 'numeric')
  expect_false(is.null(pHDataQueryResults$`total-result-count`))
  expect_is(pHDataQueryResults$`total-result-count`, 'numeric')
178
179
180
181
182
183
})

test_that("zeroPad handles NAs", {
  toPad <- c(1,5,55,NA)
  padded <- zeroPad(toPad,3)
  expect_true(identical(c("001","005","055",NA),padded))
Lindsay R Carr's avatar
Lindsay R Carr committed
184
})
185
186
187

test_that("Dates with no days can be handled", {
  testthat::skip_on_cran()
Laura A DeCicco's avatar
Laura A DeCicco committed
188
189
  expect_error(readNWISgwl("425957088141001", startDate = "1980-01-01"))
 })
David Watkins's avatar
David Watkins committed
190

Laura A DeCicco's avatar
Laura A DeCicco committed
191
context("whatWQPsamples")
Laura A DeCicco's avatar
Label    
Laura A DeCicco committed
192
test_that("whatWQPsamples working", {
Laura A DeCicco's avatar
Laura A DeCicco committed
193
194
195
196
197
198
  testthat::skip_on_cran()
  siteInfo <- whatWQPsamples(siteid="USGS-01594440")
  expect_true(nrow(siteInfo) > 0)
  
  })

Laura A DeCicco's avatar
Laura A DeCicco committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
context("whatWQPmetrics")
test_that("whatWQPmetrics working", {
  testthat::skip_on_cran()
  type <- "Stream"
  siteInfo <- whatWQPmetrics(countycode="US:55:025",siteType=type)
  expect_true(ncol(siteInfo) >= 21)
  
})

context("whatWQPdata")
test_that("whatWQPdata working", {
  testthat::skip_on_cran()
  
  site1 <- whatWQPdata(siteid="USGS-01594440")
  expect_is(site1, "data.frame")
  expect_equal(1, nrow(site1))
  
  type <- "Stream"
  sites <- whatWQPdata(countycode="US:55:025",siteType=type)
  expect_gt(nrow(sites), 1)
  
  lakeSites <- whatWQPdata(siteType = "Lake, Reservoir, Impoundment", statecode = "US:55")
  expect_is(lakeSites$activityCount, "numeric")
})

David Watkins's avatar
David Watkins committed
224
225
226
227
228
229
230
231
232
233
context("whatNWISsites")
test_that("whatNWISsites working", {
  testthat::skip_on_cran()
  siteListPhos <- whatNWISsites(stateCd="OH",parameterCd="00665")
  expect_true(nrow(siteListPhos) > 0)
  expect_true(is.numeric(siteListPhos$dec_lat_va))
  
  bboxSites <- whatNWISsites(bbox = c(-92.5, 45.4, -87, 47), parameterCd="00060")
  expect_true(nrow(bboxSites) > 0)
  expect_true(is.numeric(bboxSites$dec_lat_va))
Laura A DeCicco's avatar
Laura A DeCicco committed
234
})
Lindsay R Carr's avatar
Lindsay R Carr committed
235
236
237
238
239
240
241

context("readWQPdots")
test_that("readWQPdots working", {
  testthat::skip_on_cran()
  
  # bbox vector turned into single string with coords separated by semicolons
  formArgs_bbox <- dataRetrieval:::readWQPdots(bbox = c(-92.5, 45.4, -87, 47))
242
  expect_true(length(formArgs_bbox) == 2)
Lindsay R Carr's avatar
Lindsay R Carr committed
243
  expect_true(length(gregexpr(";", formArgs_bbox)[[1]]) == 3)
Lindsay R Carr's avatar
Lindsay R Carr committed
244
245
246
  
  # NWIS names (siteNumber) converted to WQP expected names (siteid)
  formArgs_site <- dataRetrieval:::readWQPdots(siteNumber="04010301")
247
  expect_true(length(formArgs_site) == 2)
Lindsay R Carr's avatar
Lindsay R Carr committed
248
249
250
251
252
  expect_true("siteid" %in% names(formArgs_site))
  expect_false("siteNumber" %in% names(formArgs_site))
  
  # NWIS names (stateCd) converted to WQP expected names (statecode)
  formArgs <- dataRetrieval:::readWQPdots(stateCd="OH",parameterCd="00665")
253
  expect_true(length(formArgs) == 3)
Lindsay R Carr's avatar
Lindsay R Carr committed
254
  expect_true("statecode" %in% names(formArgs))
Lindsay R Carr's avatar
Lindsay R Carr committed
255
256
  expect_false("stateCd" %in% names(formArgs))
})
David Watkins's avatar
tests    
David Watkins committed
257
258
259
260
261

context("NGWMN")
test_that("NGWMN functions working", {
  testthat::skip_on_cran()
  noDataSite <- "UTGS.401544112060301"
262
  noDataSite <- readNGWMNlevels(siteNumbers = noDataSite)
David Watkins's avatar
tests    
David Watkins committed
263
264
265
266
267
  expect_true(is.data.frame(noDataSite))
  
  #bounding box and a bigger request
  bboxSites <- readNGWMNdata(service = "featureOfInterest", bbox = c(30, -99, 31, 102))
  expect_gt(nrow(bboxSites), 0)
Laura A DeCicco's avatar
Laura A DeCicco committed
268
269
  siteInfo <- readNGWMNsites(bboxSites$site[1:3])
  expect_equal(nrow(siteInfo), 3)	  
David Watkins's avatar
tests    
David Watkins committed
270
271
272
  
  #one site
  site <- "USGS.430427089284901"
273
  oneSite <- readNGWMNlevels(siteNumbers = site)
David Watkins's avatar
tests    
David Watkins committed
274
275
276
277
278
279
280
281
  siteInfo <- readNGWMNsites(site)
  expect_true(is.numeric(oneSite$value))
  expect_true(is.character(oneSite$site))
  expect_true(is.data.frame(siteInfo))
  expect_true(nrow(siteInfo) > 0)
  expect_true(nrow(oneSite) > 0)
  
  #non-USGS site
282
  data <- readNGWMNlevels(siteNumbers = "MBMG.1388")
David Watkins's avatar
tests    
David Watkins committed
283
284
285
286
287
288
289
  expect_true(nrow(data) > 1)
  expect_true(is.numeric(oneSite$value))
  
  #sites with colons and NAs work
  
  na_colons <- c(NA, bboxSites$site[200:212], NA, NA)
  returnDF <- readNGWMNdata(service = "observation", 
290
                            siteNumbers = na_colons, asDateTime = FALSE)
David Watkins's avatar
tests    
David Watkins committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  expect_is(returnDF, "data.frame")
  expect_true(nrow(returnDF) > 1)
  expect_true(!is.null(attributes(returnDF)$siteInfo))
  
  sites <- c("USGS:424427089494701", NA)
  siteInfo <- readNGWMNsites(sites)
  expect_is(siteInfo, "data.frame")
  expect_true(nrow(siteInfo) == 1)
  
  #time zones
  tzSite <- "USGS.385111104214403"
  tzDataUTC <- readNGWMNlevels(tzSite, asDateTime = TRUE) 
  tzDataMT <- readNGWMNlevels(tzSite, asDateTime = TRUE, 
                              tz = "US/Mountain")
  expect_gt(nrow(tzDataMT), 1)
  expect_gt(nrow(tzDataUTC), 1)
  expect_is(tzDataUTC$dateTime, "POSIXct")
  expect_is(tzDataMT$dateTime, "POSIXct")
  expect_equal(attr(tzDataMT$dateTime, 'tzone'), "US/Mountain")
  expect_warning(tzDataUTC$dateTime == tzDataMT$dateTime)
})
Jordan Walker's avatar
Jordan Walker committed
312
313
314

context("getWebServiceData")
test_that("long urls use POST", {
315
  testthat::skip_on_cran()
Jordan Walker's avatar
Jordan Walker committed
316
317
318
319
320
321
322
323
324
325
326
327
  url <- paste0(rep("reallylongurl", 200), collapse = '')
  with_mock(
    RETRY = function(method, ...) {
      return(method == "POST")
    },
    status_code = function(resp) 200,
    headers = function(resp) list(`content-type` = "logical"),
    content = function(resp, encoding) resp,
    expect_true(getWebServiceData(url)),
    .env = "httr"
  )
})
328

Jordan Walker's avatar
Jordan Walker committed
329
test_that("ngwmn urls don't use post", {
330
  testthat::skip_on_cran()
Jordan Walker's avatar
Jordan Walker committed
331
332
333
334
335
336
337
338
339
340
341
342
  url <- paste0(rep("urlwithngwmn", 200), collapse = '')
  with_mock(
    RETRY = function(method, ...) {
      return(method == "POST")
    },
    status_code = function(resp) 200,
    headers = function(resp) list(`content-type` = "logical"),
    content = function(resp, encoding) resp,
    expect_false(getWebServiceData(url)),
    .env = "httr"
  )
})