Skip to content

Commit

Permalink
Fix a bug in qc(), improve tests, and cleanup .gitlab-ci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorIA committed Feb 7, 2019
1 parent 3290e14 commit 7e253db
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
6 changes: 1 addition & 5 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
before_script:
- export _R_CHECK_CRAN_INCOMING_=FALSE
- export _R_CHECK_FORCE_SUGGESTS_=TRUE

r-base:
test:
stage: test
image: r-base
script:
Expand Down
10 changes: 5 additions & 5 deletions R/qc.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ qc <- function(dat) {
stop("You've passed a one-year table. We need (many) additional years of data for context.")
}

if (grepl("Observations", colnames(dat)[15])) {
observations <- select(dat, 15) %>% unlist()
if (grepl("Observations", colnames(dat)[ncol(dat)])) {
observations <- select(dat, ncol(dat)) %>% unlist()
} else {
observations <- rep(NA, nrow(dat))
}
Expand Down Expand Up @@ -139,8 +139,8 @@ qc <- function(dat) {


if (any(grepl("Nivel Medio", names(dat)))) {
Observations <- rep('', nrow(dat))
Observations[!is.na(dat$`Nivel Medio (m)`) &
observations <- rep('', nrow(dat))
observations[!is.na(dat$`Nivel Medio (m)`) &
dat$`Nivel Medio (m)` < 0] <- paste(
"Level err:",
dat$`Nivel Medio (m)`[!is.na(dat$`Nivel Medio (m)`) &
Expand All @@ -149,7 +149,7 @@ qc <- function(dat) {
dat$`Nivel Medio (m)`[dat$`Nivel Medio (m)` < 0] <- NA
while (any(dat$`Nivel Medio (m)` > 10 * mean(dat$`Nivel Medio (m)`, na.rm = TRUE), na.rm = TRUE)) {
co <- which.max(dat$`Nivel Medio (m)`)
Observations[co] <- paste("Level err:",
observations[co] <- paste("Level err:",
dat$`Nivel Medio (m)`[co],
"-> NA")
dat$`Nivel Medio (m)`[co] <- NA
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-map_stations.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library("testthat")
library("senamhiR")
df <- station_search("Lima")
df <- station_search(target = "000401")

context("Test `map_stations()`")

Expand Down
27 changes: 21 additions & 6 deletions tests/testthat/test-qc.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
library("testthat")
library("senamhiR")
indat <- senamhiR("000280")
indat <- senamhiR(c("000280", "230715", "47270400"))

context("Test `qc()`")

##FIXME, make more interesting tests
## test qc corrections
test_that("qc can fix errors", {
out <- qc(indat)
test_that("qc can fix temperature errors", {
out <- qc(indat[[1]])
expect_identical(names(out)[15], "Observations")
expect_that(out, is_a("tbl_df"))
expect_output(str(out), "15 variables")
expect_equal(out$`Tmin (C)`[19660], 23.2)
expect_identical(out$Observations[19660], "Tmin dps: 232 -> 23.2 (1.03)")
expect_identical(out$Observations[19558], "Tmin err: 221.2 -> NA")
})

test_that("qc can fix CON level errors", {
out <- qc(indat[[2]])
expect_identical(names(out)[7], "Observations")
expect_that(out, is_a("tbl_df"))
expect_output(str(out), "7 variables")
expect_identical(out$Observations[3262], "Level err: 1.83 -> NA")
})

test_that("qc can fix SUT level errors", {
out <- qc(indat[[3]])
expect_identical(names(out)[11], "Observations")
expect_that(out, is_a("tbl_df"))
expect_output(str(out), "11 variables")
expect_identical(out$Observations[1255], "Level err: -123.51 -> NA")
})

## should fail if not enough context
test_that("qc fails if the data set is just one year", {
expect_error(qc(indat[format(indat$Fecha, format = "%Y") == 2013,]), "You've passed a one-year table. We need (many) additional years of data for context.", fixed=TRUE)
expect_error(qc(indat[[1]][format(indat[[1]]$Fecha, format = "%Y") == 2013,]), "You've passed a one-year table. We need (many) additional years of data for context.", fixed=TRUE)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-station_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("station_search() can locate a station by period", {
test_that("station_search() can locate a station by period (years)", {
df <- station_search(period = 55)
expect_that(df, is_a("tbl_df"))
expect_output(str(df), "335 obs")
expect_output(str(df), "3[0-9]{2} obs")
expect_output(str(df), "14 variables")
})

Expand Down

0 comments on commit 7e253db

Please sign in to comment.