tests_imports.R 7.85 KB
Newer Older
Laura A DeCicco's avatar
Laura A DeCicco committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
context("importRDB_noCRAN")

test_that("External importRDB1 tests", {
  testthat::skip_on_cran()
  
  siteNumber <- "02177000"
  startDate <- "2012-09-01"
  endDate <- "2012-10-01"
  offering <- "00003"
  property <- "00060"
   
  obs_url <- constructNWISURL(siteNumber,property,
            startDate,endDate,"dv",format="tsv")
  data <- importRDB1(obs_url)
Laura A DeCicco's avatar
Laura A DeCicco committed
15
  expect_is(data$datetime, 'Date') 
Laura A DeCicco's avatar
Laura A DeCicco committed
16
17
18
19
20
21
22
23
24
25
26
  
  urlMultiPcodes <- constructNWISURL("04085427",c("00060","00010"),
          startDate,endDate,"dv",statCd=c("00003","00001"),"tsv")
  multiData <- importRDB1(urlMultiPcodes)
  pCodeCols <- grep("X", colnames(multiData))
  expect_that(length(pCodeCols)/2 > 2, is_true() )
  
  unitDataURL <- constructNWISURL(siteNumber,property,
          "2013-11-03","2013-11-03","uv",format="tsv") #includes timezone switch
  unitData <- importRDB1(unitDataURL, asDateTime=TRUE)
  
Laura A DeCicco's avatar
Laura A DeCicco committed
27
28
29
  # Need to think of a way to automatically check timezone conversion:
#   expect_that(as.numeric(unitData[which(unitData$tz_cd == "EST")[1],"datetime"]),
#               equals(as.numeric(as.POSIXct("2013-11-03 01:00:00", tz="UTC")+60*60*5)))
Laura A DeCicco's avatar
Laura A DeCicco committed
30
31
32
33
34
  
  
  qwURL <- constructNWISURL(c('04024430','04024000'),
           c('34247','30234','32104','34220'),
          "2010-11-03","","qw",format="rdb") 
Laura A DeCicco's avatar
Laura A DeCicco committed
35
  qwData <- importRDB1(qwURL, tz="America/Chicago")
36
37
  expect_is(qwData$sample_dt, 'Date')
  expect_is(qwData$startDateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
38
  
Laura A DeCicco's avatar
Laura A DeCicco committed
39
40
41
42
43
44
45
46
47
48
  #This data got deleted:
#   iceSite <- '04024430'
#   start <- "2014-11-09"
#   end <- "2014-11-28"
#   urlIce <- constructNWISURL(iceSite,"00060",start, end,"uv",format="tsv")
#   ice <- importRDB1(urlIce)
#   expect_that(sum(is.na(ice$X01_00060)) > 0, is_true())
#   
#   iceNoConvert <- importRDB1(urlIce, convertType=FALSE)
#   expect_that(sum(iceNoConvert$X01_00060 == "Ice") > 0, is_true())
Laura A DeCicco's avatar
Laura A DeCicco committed
49
50
  
  
Laura A DeCicco's avatar
Laura A DeCicco committed
51
  site <- "05427850"
David Watkins's avatar
David Watkins committed
52

53
54
55
56
57
  url <- constructNWISURL(site,"00060","2015-01-01", "","dv",
                          format="tsv",
                          statCd = "laksjd")
  # And....now there's data there:
  expect_error(importRDB1(url))
Laura A DeCicco's avatar
Laura A DeCicco committed
58
59
60
61
62
63
64
65
66
67
})

context("importRDB")
test_that("CRAN-friendly importRDB test", {
  filePath <- system.file("extdata", package="dataRetrieval")
  fileName <- "RDB1Example.txt"
  fullPath <- file.path(filePath, fileName)
  importUserRDB <- importRDB1(fullPath)
  
  # default is to turn dates to characters
Laura A DeCicco's avatar
Laura A DeCicco committed
68
  expect_is(importUserRDB$datetime, 'Date')
Laura A DeCicco's avatar
Laura A DeCicco committed
69
70
71
  
})

72
73
74
75
76
77
context("importWaterML1")
test_that("CRAN-friendly importWaterML1 test", {
  filePath <- system.file("extdata", package="dataRetrieval")
  fileName <- "WaterML1Example.xml"
  fullPath <- file.path(filePath, fileName)
  importUserWML1 <- importWaterML1(fullPath, asDateTime = TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
78
  # saveRDS(importUserWML1, "rds/importUserWML1.rds")
79
80
81
82
83
  # default is to turn dates to characters
  expect_is(importUserWML1$dateTime, 'POSIXct')
  
})

84
85
86
87
88
89
90
91
92
93
94
test_that("External importWaterML1 test", {
  testthat::skip_on_cran()

  siteNumber <- "02177000"
  startDate <- "2012-09-01"
  endDate <- "2012-10-01"
  offering <- '00003'
  property <- '00060'
  obs_url <- constructNWISURL(siteNumber,property,startDate,endDate,'dv')

  data <- importWaterML1(obs_url,TRUE)
Laura A DeCicco's avatar
Laura A DeCicco committed
95
  # saveRDS(data, "rds/dvWML1.rds")
96
97
98
99
100
101
102
103
  expect_is(data$dateTime, 'POSIXct')

  groundWaterSite <- "431049071324301"
  startGW <- "2013-10-01"
  endGW <- "2014-06-30"
  groundwaterExampleURL <- constructNWISURL(groundWaterSite, NA,
           startGW,endGW, service="gwlevels")
  groundWater <- importWaterML1(groundwaterExampleURL)
Laura A DeCicco's avatar
Laura A DeCicco committed
104
  # saveRDS(groundWater, "rds/groundwater.rds")
105

106
  expect_is(groundWater$dateTime, 'character')
107
108
109
110
111
112
113
114
115
116

  unitDataURL <- constructNWISURL(siteNumber,property,
          "2013-11-03","2013-11-03",'uv')
  unitData <- importWaterML1(unitDataURL,TRUE)
  expect_is(unitData$dateTime, 'POSIXct')

  # Two sites, two pcodes, one site has two data descriptors
  siteNumber <- c('01480015',"04085427") #one site seems to have lost it's 2nd dd
  obs_url <- constructNWISURL(siteNumber,c("00060","00010"),startDate,endDate,'dv')
  data <- importWaterML1(obs_url)
Laura A DeCicco's avatar
Laura A DeCicco committed
117
  # saveRDS(data, "rds/twoWML1.rds")
118
119
120
121
122
123
124
125
126
  expect_that(length(unique(data$site_no)) == 2, is_true())
  expect_that(ncol(data) == 8, is_true()) # 3 data, 3 remark codes, and 4 (agency, site, dateTime, tz)

  inactiveSite <- "05212700"
  inactiveSite <- constructNWISURL(inactiveSite, "00060", "2014-01-01", "2014-01-10",'dv')
  inactiveSite <- importWaterML1(inactiveSite)
  expect_that(nrow(inactiveSite) == 0, is_true())

  inactiveAndActive <- c("07334200","05212700")
127
128
  inactiveAndActive <- constructNWISURL(inactiveAndActive, "00060", 
                                        "2014-01-01", "2014-12-31",'dv')
129
  inactiveAndActive <- importWaterML1(inactiveAndActive)
Laura A DeCicco's avatar
Laura A DeCicco committed
130
  # saveRDS(inactiveAndActive, "rds/inactiveAndActive.rds")
131
132
133
  # 
  expect_true(length(unique(inactiveAndActive$site_no)) < 2)
  
David Watkins's avatar
David Watkins committed
134
135
136
137
  
  #raw XML
  url <- constructNWISURL(service = 'dv', siteNumber = '02319300', parameterCd = "00060", 
                          startDate = "2014-01-01", endDate = "2014-01-01")
Laura A DeCicco's avatar
Laura A DeCicco committed
138
139
140
141
142
143
  # Will put back when https is converted:
  # raw <- content(GET(url), as = 'raw')
  # rawParsed <- importWaterML1(raw)
  # expect_true(nrow(rawParsed) > 0)
  # expect_true(data.class(rawParsed$X_00060_00003) == "numeric")
  # 
David Watkins's avatar
David Watkins committed
144
145
146
147
148
  #no data
  url <- constructNWISURL("05212700", "00060", "2014-01-01", "2014-01-10",'dv', statCd = "00001")
  noData <- importWaterML1(url) 
  expect_true(class(attr(noData,"url"))=="character")
  expect_true(all(dim(noData)==0))
David Watkins's avatar
David Watkins committed
149
  
David Watkins's avatar
David Watkins committed
150
151
152
153
154
  url <- constructNWISURL(service = 'iv', site = c('02319300','02171500'), 
                          startDate = "2015-04-04", endDate = "2015-04-05")
  data <- importWaterML1(url, tz = "America/New_York", asDateTime = TRUE)
  expect_true(data.class(data$dateTime) == "POSIXct")
  expect_true(nrow(data) > 0)
Laura A DeCicco's avatar
Laura A DeCicco committed
155
156
157
158
159
160
161
162
  
  expect_error(readNWISdata(sites="05114000", 
                              service="iv",
                              parameterCd="00060",
                              startDate="2014-05-01T00:00",
                              endDate="2014-05-01T12:00",
                              tz="blah"))

163
164
165
  arg.list <- list(sites="05114000", 
                   parameterCd="00060",
                   startDate="2014-05-01T00:00",
166
                   endDate="2014-05-01T12:00")
167
168
  
  chi_iv <- readNWISdata(arg.list, 
169
170
                         service="iv",
                         tz="America/Chicago")
171
172
173
174
  
  expect_true(all(chi_iv$tz_cd == "America/Chicago"))
  expect_equal(chi_iv$dateTime[1], as.POSIXct("2014-05-01T00:00", format = "%Y-%m-%dT%H:%M", tz="America/Chicago"))
  expect_equal(chi_iv$dateTime[nrow(chi_iv)], as.POSIXct("2014-05-01T12:00", format = "%Y-%m-%dT%H:%M", tz="America/Chicago"))
Laura A DeCicco's avatar
Laura A DeCicco committed
175
  
176
})
Laura A DeCicco's avatar
Laura A DeCicco committed
177

178
context("importWaterML2")
179

180
181
182
183
184
test_that("importWaterML2 internal test", {
  filePath <- system.file("extdata", package="dataRetrieval")
  fileName <- "WaterML2Example.xml"
  fullPath <- file.path(filePath, fileName)
  UserData <- importWaterML2(fullPath)
Laura A DeCicco's avatar
Laura A DeCicco committed
185
  # saveRDS(UserData, "rds/UserData.rds")
186
  expect_is(UserData$value, 'numeric')
Laura A DeCicco's avatar
Laura A DeCicco committed
187
  # expect_is(UserData$qualifier, 'character')
188
  
189
190
})

191
192
test_that("importWaterML2 external test", {
  testthat::skip_on_cran()
David Watkins's avatar
David Watkins committed
193
194
  url <- "https://waterservices.usgs.gov/nwis/iv/?format=waterml,2.0&sites=01646500&parameterCd=00060,00065"
  exData <- importWaterML2(url)
Laura A DeCicco's avatar
Laura A DeCicco committed
195
  # saveRDS(data, "rds/externalML2.rds")
David Watkins's avatar
David Watkins committed
196
197
  expect_is(exData$value, 'numeric')
  expect_gt(nrow(exData),0)
198
199
})
  
200

201

Laura A DeCicco's avatar
Laura A DeCicco committed
202
203
204
205
context("importWQP_noCRAN")

test_that("External WQP tests", {
  testthat::skip_on_cran()
206
  expect_that(1==1, is_true())   
Laura A DeCicco's avatar
Laura A DeCicco committed
207
208
209
210
  rawSampleURL <- constructWQPURL('USGS-01594440','01075', '', '')
  rawSample <- importWQP(rawSampleURL)
  expect_is(rawSample$ActivityStartDateTime, 'POSIXct')
  
Laura A DeCicco's avatar
Laura A DeCicco committed
211
212
  url2 <- constructWQPURL('USGS-01594440','01075', '', '', zip = FALSE)
  rawSample2 <- suppressWarnings(importWQP(url2, FALSE))
Laura A DeCicco's avatar
Laura A DeCicco committed
213
214
215
216
217
  expect_is(rawSample2$ActivityStartDateTime, 'POSIXct')
  
  STORETex <- constructWQPURL('WIDNR_WQX-10032762','Specific conductance', '', '')
  STORETdata <- importWQP(STORETex)
  expect_is(STORETdata$ActivityStartDateTime, 'POSIXct')
Laura A DeCicco's avatar
Laura A DeCicco committed
218
})
David Watkins's avatar
David Watkins committed
219