tests_general.R 4.83 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
11
  
  bBoxEx <- readNWISdata(bBox=c(-83,36.5,-81,38.5), parameterCd="00010")
Laura A DeCicco's avatar
Laura A DeCicco committed
12
  expect_that(length(unique(bBoxEx$site_no)) > 1, is_true())
Laura A DeCicco's avatar
Laura A DeCicco committed
13
  # saveRDS(bBoxEx, "rds/bBoxEx.rds")
Laura A DeCicco's avatar
Laura A DeCicco committed
14
15
16
17
18
  
  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
19
  # saveRDS(waterYear, "rds/waterYear.rds")
20
  expect_is(waterYear$dateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
21
22
23
  
  siteInfo <- readNWISdata(stateCd="WI", parameterCd="00010",hasDataTypeCd="iv", 
                           service="site")
Laura A DeCicco's avatar
Laura A DeCicco committed
24
  # saveRDS(siteInfo,"rds/siteInfo.rds")
25
26
27
28
29
30
  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
31
  # saveRDS(qwData, "rds/qwData.rds")
32
  expect_is(qwData$startDateTime, "POSIXct")
Laura A DeCicco's avatar
Laura A DeCicco committed
33
  
Laura A DeCicco's avatar
Laura A DeCicco committed
34
  url <- "http://waterservices.usgs.gov/nwis/dv/?site=09037500&format=rdb&ParameterCd=00060&StatCd=00003&startDT=1985-10-02&endDT=2012-09-06"
35
36
  dv <- importRDB1(url, asDateTime = FALSE)
  
37
38
  urlEmpty <- "http://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"
  dv <- importRDB1(urlEmpty, asDateTime = FALSE)
Laura A DeCicco's avatar
Laura A DeCicco committed
39
  # saveRDS(dv, "rds/emptyDV.rds")
40
  expect_that(nrow(dv) == 0, is_true())
41
42
43
  
  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
44
  # saveRDS(dailyStat,"rds/dailyStat.rds")
David Watkins's avatar
David Watkins committed
45
  expect_that(length(dailyStat$min_va) > 1, is_true())
46
47
48
49
  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
50
  # saveRDS(waterYearStat, "rds/waterYearStat.rds")
51
52
  expect_is(waterYearStat$mean_va,"numeric")
  expect_is(waterYearStat$parameter_cd,"character")
Laura A DeCicco's avatar
Laura A DeCicco committed
53
54
55
56
57
})


test_that("General WQP retrievals working", {
  testthat::skip_on_cran()
Laura A DeCicco's avatar
Laura A DeCicco committed
58
59
60
61
  nameToUse <- "pH"
  pHData <- readWQPdata(siteid="USGS-04024315",characteristicName=nameToUse)
  expect_is(pHData$ActivityStartDateTime, 'POSIXct')
  
62
  # Known slow query for WQP:
Laura A DeCicco's avatar
Laura A DeCicco committed
63
64
65
  # 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
66
  
Laura A DeCicco's avatar
Laura A DeCicco committed
67
68
69
70
71
  # 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
72
})
Lindsay R Carr's avatar
Lindsay R Carr committed
73

74
test_that("WQP head query retrievals working", {
Lindsay R Carr's avatar
Lindsay R Carr committed
75
76
77
78
79
  testthat::skip_on_cran()
  nameToUse <- "pH"
  pHDataQueryResults <- readWQPdata(siteid="USGS-04024315",
                                    characteristicName=nameToUse, 
                                    querySummary=TRUE)
80
81
  expect_false(is.null(pHDataQueryResults$date))
  expect_is(pHDataQueryResults$date, 'Date')
Lindsay R Carr's avatar
Lindsay R Carr committed
82
  expect_false(is.null(pHDataQueryResults$`total-site-count`))
83
  expect_is(pHDataQueryResults$`total-site-count`, 'numeric')
Lindsay R Carr's avatar
Lindsay R Carr committed
84
  expect_false(is.null(pHDataQueryResults$`total-result-count`))
85
86
87
88
89
90
91
92
93
94
95
  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')
96
97
98
99
100
101
})

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
102
})
103
104
105

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