From 512e2fee65d669ae01edf7c04970473e14df895a Mon Sep 17 00:00:00 2001 From: Conor Anderson Date: Sun, 8 Oct 2017 16:39:43 -0400 Subject: [PATCH] Add some initial tests. Overhaul the build process. --- .gitlab-ci.yml | 21 +-- DESCRIPTION | 7 +- R/data.R | 2 +- R/qc.R | 4 + R/senamhiR.R | 4 + R/station_search.R | 6 + R/zzz.R | 2 +- README.Rmd | 2 +- README.md | 202 +++++++++++++-------------- man/catalogue.Rd | 2 +- man/qc.Rd | 1 - tests/testthat.R | 8 ++ tests/testthat/test-map_stations.R | 15 ++ tests/testthat/test-qc.R | 22 +++ tests/testthat/test-quick_audit.R | 37 +++++ tests/testthat/test-senamhiR.R | 27 ++++ tests/testthat/test-station_search.R | 54 +++++++ 17 files changed, 299 insertions(+), 117 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-map_stations.R create mode 100644 tests/testthat/test-qc.R create mode 100644 tests/testthat/test-quick_audit.R create mode 100644 tests/testthat/test-senamhiR.R create mode 100644 tests/testthat/test-station_search.R diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f243843..08ddc15 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,11 +1,14 @@ -image: conoria/alpine-r +before_script: + - export _R_CHECK_CRAN_INCOMING_=FALSE + - export _R_CHECK_FORCE_SUGGESTS_=TRUE -variables: - BUILD_FLAGS: "--no-build-vignettes --no-manual" - -test: +r-base: + stage: test + image: r-base script: - - apk add --no-cache mariadb-dev - - R CMD build . ${BUILD_FLAGS} - - Rscript -e 'devtools::install_deps(dependencies = TRUE)' - - R CMD check $(ls -1t *.tar.gz | head -n 1) ${BUILD_FLAGS} --as-cran + - apt-get update -qq + - apt-get install -y --no-install-recommends libmariadbclient-dev libcurl4-openssl-dev libssh2-1-dev libssl-dev libxml2-dev git + - Rscript -e 'install.packages(c("devtools", "roxygen2", "testthat", "covr"), repos = "https://cran.rstudio.com/")' + - Rscript -e 'devtools::install_deps()' + - Rscript -e 'devtools::check()' + - Rscript -e 'covr::codecov(type = "all", quiet = FALSE)' diff --git a/DESCRIPTION b/DESCRIPTION index d294983..2dc4666 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: senamhiR Type: Package Title: A Collection of Functions to Obtain Peruvian Climate Data -Version: 0.4.1 -Date: 2017-03-24 +Version: 0.4.2 +Date: 2017-10-08 Authors@R: c(person(given = c("Conor", "I."), family = "Anderson", role = c("aut","cre"), email = "conor.anderson@mail.utoronto.ca"), person(given = c("William", "A."), family = "Gough", role = "ths", @@ -27,6 +27,9 @@ Imports: utils, XML, zoo +Suggests: + testthat, + covr License: GPL (>= 3) URL: https://gitlab.com/ConorIA/senamhiR/ BugReports: https://gitlab.com/ConorIA/senamhiR/issues diff --git a/R/data.R b/R/data.R index 072048d..f522005 100644 --- a/R/data.R +++ b/R/data.R @@ -20,6 +20,6 @@ #' } #' #' @examples -#' catalogue +#' \dontrun{catalogue} "catalogue" diff --git a/R/qc.R b/R/qc.R index 2938c6e..d7b1c57 100644 --- a/R/qc.R +++ b/R/qc.R @@ -26,6 +26,10 @@ qc <- function(dat) { } } + if (length(unique(format(dat$Fecha, format = "%Y"))) == 1) { + 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 } else { diff --git a/R/senamhiR.R b/R/senamhiR.R index d304e0c..2ab38c4 100644 --- a/R/senamhiR.R +++ b/R/senamhiR.R @@ -24,6 +24,10 @@ senamhiR <- function(station, year, tasks, fallback, write_mode = "z") { station <- trimws(unlist(strsplit(station, split = ","))) } + if (!station %in% catalogue$StationID) { + stop("The station requested is not a valid station.") + } + # If tasks is not specified (good), use MySQL if (missing(tasks)) { dataout <- list() diff --git a/R/station_search.R b/R/station_search.R index ffdb919..5b0d20d 100644 --- a/R/station_search.R +++ b/R/station_search.R @@ -50,12 +50,18 @@ station_search <- function(name = NULL, ignore.case = TRUE, glob = FALSE, region # If `region` is not NULL, filter by name if (!is.null(region)) { index <- index[which(catalogue$Region == toupper(region))] + if (length(index) == 0) { + stop("No data found for that region. Did you spell it correctly?") + } } # If `config` is not NULL, filter by name if (!is.null(config)) { index <- index[grep(config, catalogue$Configuration[index], ignore.case = ignore.case, ...)] + if (length(index) == 0) { + stop("No data found for that config. Did you pass \"m\" or \"h\"?") + } } # Make a table with the info we want diff --git a/R/zzz.R b/R/zzz.R index 3695a5e..3c10d0c 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,3 +1,3 @@ -.onLoad <- function(libname = find.package("senamhiR"), pkgname = "senamhiR") { +.onAttach <- function(libname = find.package("senamhiR"), pkgname = "senamhiR") { packageStartupMessage("The information accessed by this package was compiled and maintained by Peru's National Meteorology and Hydrology Service (Senamhi). The use of this data is of your sole responsibility.") } \ No newline at end of file diff --git a/README.Rmd b/README.Rmd index 8850c3d..fab8e6d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -10,7 +10,7 @@ knitr::opts_chunk$set(echo = TRUE) library(senamhiR) ``` -[![build status](https://gitlab.com/ConorIA/senamhiR/badges/master/build.svg)](https://gitlab.com/ConorIA/senamhiR/commits/master) [![Build status](https://ci.appveyor.com/api/projects/status/60kbu1b7wkf7akqn?svg=true)](https://ci.appveyor.com/project/ConorIA/senamhir-bxb45) +[![build status](https://gitlab.com/ConorIA/senamhiR/badges/master/build.svg)](https://gitlab.com/ConorIA/senamhiR/commits/master) [![Build status](https://ci.appveyor.com/api/projects/status/60kbu1b7wkf7akqn?svg=true)](https://ci.appveyor.com/project/ConorIA/senamhir-bxb45) [![codecov](https://codecov.io/gl/ConorIA/senamhiR/branch/master/graph/badge.svg)](https://codecov.io/gl/ConorIA/senamhiR) The package provides an automated solution for the acquisition of archived Peruvian climate and hydrology data directly within R. The data was compiled from the Senamhi website, and contains all of the data that was available as of March 2017. This data was originally converted from HTML, and is stored in a MySQL database in tibble format. diff --git a/README.md b/README.md index 88d00c2..cf07fb7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ -[![build status](https://gitlab.com/ConorIA/senamhiR/badges/master/build.svg)](https://gitlab.com/ConorIA/senamhiR/commits/master) [![Build status](https://ci.appveyor.com/api/projects/status/60kbu1b7wkf7akqn?svg=true)](https://ci.appveyor.com/project/ConorIA/senamhir-bxb45) +[![build status](https://gitlab.com/ConorIA/senamhiR/badges/master/build.svg)](https://gitlab.com/ConorIA/senamhiR/commits/master) [![Build status](https://ci.appveyor.com/api/projects/status/60kbu1b7wkf7akqn?svg=true)](https://ci.appveyor.com/project/ConorIA/senamhir-bxb45) [![codecov](https://codecov.io/gl/ConorIA/senamhiR/branch/master/graph/badge.svg)](https://codecov.io/gl/ConorIA/senamhiR) The package provides an automated solution for the acquisition of archived Peruvian climate and hydrology data directly within R. The data was compiled from the Senamhi website, and contains all of the data that was available as of March 2017. This data was originally converted from HTML, and is stored in a MySQL database in tibble format. @@ -37,18 +37,18 @@ station_search("Santa") ``` ``` -## # A tibble: 42 × 12 +## # A tibble: 42 x 12 ## Station StationID Type Configuration `Data Start` ## -## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 -## 2 SANTA 000433 CON M 1964 -## 3 SANTA RITA 000829 CON M 1977 -## 4 SANTA ELENA 000834 CON M 1963 -## 5 SANTA ISABEL DE SIGUAS 158201 CON M 1964 -## 6 SANTA CRUZ DE HOSPICIO 113248 SUT M 2015 -## 7 SANTA CRUZ 000351 CON M 1963 -## 8 SANTA CATALINA DE PULAN 153200 CON M 1963 -## 9 HACIENDA SANTA INES 000766 CON M 1954 +## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 +## 2 SANTA 000433 CON M 1964 +## 3 SANTA RITA 000829 CON M 1977 +## 4 SANTA ELENA 000834 CON M 1963 +## 5 SANTA ISABEL DE SIGUAS 158201 CON M 1964 +## 6 SANTA CRUZ DE HOSPICIO 113248 SUT M 2015 +## 7 SANTA CRUZ 000351 CON M 1963 +## 8 SANTA CATALINA DE PULAN 153200 CON M 1963 +## 9 HACIENDA SANTA INES 000766 CON M 1954 ## 10 SANTAROSA LLIHUA 151505 CON M 1980 ## # ... with 32 more rows, and 7 more variables: `Data End` , `Station ## # Status` , Latitude , Longitude , Region , @@ -65,18 +65,18 @@ station_search("San*", glob = TRUE) ``` ``` -## # A tibble: 135 × 12 +## # A tibble: 135 x 12 ## Station StationID Type Configuration `Data Start` ## -## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 -## 2 SAN RAFAEL 152222 CON M 1965 -## 3 SAN LORENZO # 5 000430 CON M 1966 -## 4 SAN JACINTO DE NEPENA 000424 CON M 1956 -## 5 SAN JACINTO 201901 CON H 1947 -## 6 SAN DIEGO 000420 CON M 1960 -## 7 SANTIAGO ANTUNEZ DE MAYOLO 000426 CON M 1998 -## 8 SANTA 000433 CON M 1964 -## 9 SAN PEDRO 211404 CON H 2009 +## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 +## 2 SAN RAFAEL 152222 CON M 1965 +## 3 SAN LORENZO # 5 000430 CON M 1966 +## 4 SAN JACINTO DE NEPENA 000424 CON M 1956 +## 5 SAN JACINTO 201901 CON H 1947 +## 6 SAN DIEGO 000420 CON M 1960 +## 7 SANTIAGO ANTUNEZ DE MAYOLO 000426 CON M 1998 +## 8 SANTA 000433 CON M 1964 +## 9 SAN PEDRO 211404 CON H 2009 ## 10 SANTA RITA 000829 CON M 1977 ## # ... with 125 more rows, and 7 more variables: `Data End` , `Station ## # Status` , Latitude , Longitude , Region , @@ -92,18 +92,18 @@ station_search(region = "SAN MARTIN") ``` ``` -## # A tibble: 72 × 12 +## # A tibble: 72 x 12 ## Station StationID Type Configuration `Data Start` `Data End` ## -## 1 MOYOBAMBA 000378 CON M 1946 2016 -## 2 NARANJILLO 000219 CON M 1975 2016 -## 3 NAVARRO 000386 CON M 1964 2016 -## 4 NARANJILLO 4724851A SUT M1 2000 2016 -## 5 EL PORVENIR 4723013A SUT M1 2001 2016 -## 6 NUEVO LIMA 153312 CON M 1963 2016 -## 7 SHEPTE 153301 CON M 1963 1985 -## 8 TINGO DE PONAZA 153318 CON M 1963 2005 -## 9 TINGO DE PONAZA 000297 CON M 1998 2016 +## 1 MOYOBAMBA 000378 CON M 1946 2016 +## 2 NARANJILLO 000219 CON M 1975 2016 +## 3 NAVARRO 000386 CON M 1964 2016 +## 4 NARANJILLO 4724851A SUT M1 2000 2016 +## 5 EL PORVENIR 4723013A SUT M1 2001 2016 +## 6 NUEVO LIMA 153312 CON M 1963 2016 +## 7 SHEPTE 153301 CON M 1963 1985 +## 8 TINGO DE PONAZA 153318 CON M 1963 2005 +## 9 TINGO DE PONAZA 000297 CON M 1998 2016 ## 10 PUEBLO LIBRE 152228 CON M 1996 1998 ## # ... with 62 more rows, and 6 more variables: `Station Status` , ## # Latitude , Longitude , Region , Province , @@ -116,18 +116,18 @@ station_search("Santa", baseline = 1971:2000) ``` ``` -## # A tibble: 10 × 12 +## # A tibble: 10 x 12 ## Station StationID Type Configuration `Data Start` ## -## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 -## 2 SANTA CRUZ 000351 CON M 1963 -## 3 SANTA EULALIA 155213 CON M 1963 -## 4 SANTA CRUZ 155202 CON M 1963 -## 5 SANTA ROSA 000536 CON M 1967 -## 6 SANTA MARIA DE NANAY 152409 CON M 1963 -## 7 SANTA RITA DE CASTILLA 152401 CON M 1963 -## 8 SANTA CLOTILDE 000177 CON M 1963 -## 9 SANTA CRUZ 152303 CON M 1963 +## 1 SANTA MARIA DE NIEVA 000256 CON M 1951 +## 2 SANTA CRUZ 000351 CON M 1963 +## 3 SANTA EULALIA 155213 CON M 1963 +## 4 SANTA CRUZ 155202 CON M 1963 +## 5 SANTA ROSA 000536 CON M 1967 +## 6 SANTA MARIA DE NANAY 152409 CON M 1963 +## 7 SANTA RITA DE CASTILLA 152401 CON M 1963 +## 8 SANTA CLOTILDE 000177 CON M 1963 +## 9 SANTA CRUZ 152303 CON M 1963 ## 10 SANTA ROSA 000823 CON M 1970 ## # ... with 7 more variables: `Data End` , `Station Status` , ## # Latitude , Longitude , Region , Province , @@ -140,18 +140,18 @@ station_search(target = "000401", dist = 0:100) ``` ``` -## # A tibble: 57 × 13 +## # A tibble: 57 x 13 ## Station StationID Type Configuration `Data Start` `Data End` ## -## 1 TARAPOTO 000401 CON M 1998 2016 -## 2 CUNUMBUQUE 153311 CON M 1963 2016 -## 3 CUMBAZA 221801 CON H 1968 2015 -## 4 LAMAS 000383 CON M 1963 2016 -## 5 SAN ANTONIO 153314 CON M 1963 2016 -## 6 SHANAO 221802 CON H 1965 2015 -## 7 SHANAO 153328 CON M 2002 2016 -## 8 SHANAO 210006 SUT H 2016 2017 -## 9 TABALOSOS 000322 CON M 1963 2016 +## 1 TARAPOTO 000401 CON M 1998 2016 +## 2 CUNUMBUQUE 153311 CON M 1963 2016 +## 3 CUMBAZA 221801 CON H 1968 2015 +## 4 LAMAS 000383 CON M 1963 2016 +## 5 SAN ANTONIO 153314 CON M 1963 2016 +## 6 SHANAO 221802 CON H 1965 2015 +## 7 SHANAO 153328 CON M 2002 2016 +## 8 SHANAO 210006 SUT H 2016 2017 +## 9 TABALOSOS 000322 CON M 1963 2016 ## 10 EL PORVENIR 000310 CON M 1964 2016 ## # ... with 47 more rows, and 7 more variables: `Station Status` , ## # Latitude , Longitude , Region , Province , @@ -164,18 +164,18 @@ station_search(target = c(-13.163333, -72.545556), dist = 0:50) ``` ``` -## # A tibble: 19 × 13 +## # A tibble: 19 x 13 ## Station StationID Type Configuration `Data Start` `Data End` ## -## 1 MACHU PICCHU 000679 CON M 1964 2016 -## 2 HUYRO 000678 CON M 1964 1981 -## 3 CHILCA 472A9204 SUT H 2015 2016 -## 4 ECHARATE 000716 CON M 1981 1982 -## 5 MARANURA 000676 CON M 1970 1978 -## 6 OLLANTAYTAMBO 47295014 SUT M 2011 2013 -## 7 QUILLABAMBA 4729B3E6 SUT M1 2001 2016 -## 8 QUILLABAMBA 000606 CON M 1964 2016 -## 9 OCOBAMBA 000681 CON M 1964 1983 +## 1 MACHU PICCHU 000679 CON M 1964 2016 +## 2 HUYRO 000678 CON M 1964 1981 +## 3 CHILCA 472A9204 SUT H 2015 2016 +## 4 ECHARATE 000716 CON M 1981 1982 +## 5 MARANURA 000676 CON M 1970 1978 +## 6 OLLANTAYTAMBO 47295014 SUT M 2011 2013 +## 7 QUILLABAMBA 4729B3E6 SUT M1 2001 2016 +## 8 QUILLABAMBA 000606 CON M 1964 2016 +## 9 OCOBAMBA 000681 CON M 1964 1983 ## 10 MOLLEPATA 000680 CON M 1963 1978 ## 11 CUNYAC 156224 CON M 2002 2016 ## 12 ECHARATE 156300 CON M 1963 1981 @@ -209,18 +209,18 @@ requ ``` ``` -## # A tibble: 10,957 × 14 +## # A tibble: 10,957 x 14 ## Fecha `Tmean (C)` `Tmax (C)` `Tmin (C)` `TBS07 (C)` `TBS13 (C)` ## -## 1 1981-01-01 29.0 35.0 23.0 24.8 30.2 -## 2 1981-01-02 28.1 34.0 22.2 24.2 30.0 -## 3 1981-01-03 26.1 29.0 23.2 24.6 25.2 -## 4 1981-01-04 26.1 30.2 22.0 24.6 28.0 -## 5 1981-01-05 27.7 33.0 22.4 24.0 25.0 -## 6 1981-01-06 29.1 35.2 23.0 25.0 30.8 -## 7 1981-01-07 28.3 33.6 23.0 25.4 30.8 -## 8 1981-01-08 30.1 37.4 22.8 25.4 35.0 -## 9 1981-01-09 29.0 35.0 23.0 27.0 35.0 +## 1 1981-01-01 29.0 35.0 23.0 24.8 30.2 +## 2 1981-01-02 28.1 34.0 22.2 24.2 30.0 +## 3 1981-01-03 26.1 29.0 23.2 24.6 25.2 +## 4 1981-01-04 26.1 30.2 22.0 24.6 28.0 +## 5 1981-01-05 27.7 33.0 22.4 24.0 25.0 +## 6 1981-01-06 29.1 35.2 23.0 25.0 30.8 +## 7 1981-01-07 28.3 33.6 23.0 25.4 30.8 +## 8 1981-01-08 30.1 37.4 22.8 25.4 35.0 +## 9 1981-01-09 29.0 35.0 23.0 27.0 35.0 ## 10 1981-01-10 29.0 35.6 22.4 24.8 34.4 ## # ... with 10,947 more rows, and 8 more variables: `TBS19 (C)` , ## # `TBH07 (C)` , `TBH13 (C)` , `TBH19 (C)` , `Prec07 @@ -264,18 +264,18 @@ quick_audit(requ, c("Tmean", "Tmax", "Tmin")) ``` ``` -## # A tibble: 30 × 4 +## # A tibble: 30 x 4 ## Year `Tmean (C) pct NA` `Tmax (C) pct NA` `Tmin (C) pct NA` ## -## 1 1981 8.4931507 8.4931507 8.4931507 -## 2 1982 0.0000000 0.0000000 0.0000000 -## 3 1983 41.9178082 41.9178082 41.9178082 -## 4 1984 17.2131148 8.1967213 17.2131148 -## 5 1985 7.6712329 0.2739726 7.6712329 -## 6 1986 0.8219178 0.8219178 0.8219178 -## 7 1987 17.8082192 17.8082192 17.8082192 -## 8 1988 8.4699454 8.4699454 8.4699454 -## 9 1989 0.0000000 0.0000000 0.0000000 +## 1 1981 8.4931507 8.4931507 8.4931507 +## 2 1982 0.0000000 0.0000000 0.0000000 +## 3 1983 41.9178082 41.9178082 41.9178082 +## 4 1984 17.2131148 8.1967213 17.2131148 +## 5 1985 7.6712329 0.2739726 7.6712329 +## 6 1986 0.8219178 0.8219178 0.8219178 +## 7 1987 17.8082192 17.8082192 17.8082192 +## 8 1988 8.4699454 8.4699454 8.4699454 +## 9 1989 0.0000000 0.0000000 0.0000000 ## 10 1990 0.0000000 0.0000000 0.0000000 ## # ... with 20 more rows ``` @@ -289,18 +289,18 @@ quick_audit(toca, "Tmean", by = "month", report = "n") ``` ``` -## # A tibble: 12 × 5 +## # A tibble: 12 x 5 ## Year Month `Year-month` `Tmean (C) consec NA` `Tmean (C) tot NA` ## -## 1 1980 01 Jan 1980 0 0 -## 2 1980 02 Feb 1980 0 0 -## 3 1980 03 Mar 1980 2 3 -## 4 1980 04 Apr 1980 4 4 -## 5 1980 05 May 1980 0 0 -## 6 1980 06 Jun 1980 0 0 -## 7 1980 07 Jul 1980 0 0 -## 8 1980 08 Aug 1980 0 0 -## 9 1980 09 Sep 1980 1 1 +## 1 1980 01 Jan 1980 0 0 +## 2 1980 02 Feb 1980 0 0 +## 3 1980 03 Mar 1980 2 3 +## 4 1980 04 Apr 1980 4 4 +## 5 1980 05 May 1980 0 0 +## 6 1980 06 Jun 1980 0 0 +## 7 1980 07 Jul 1980 0 0 +## 8 1980 08 Aug 1980 0 0 +## 9 1980 09 Sep 1980 1 1 ## 10 1980 10 Oct 1980 0 0 ## 11 1980 11 Nov 1980 1 1 ## 12 1980 12 Dec 1980 0 0 @@ -317,18 +317,18 @@ quick_audit(toca, "Tmean", by = "month", report = "n") ``` ``` -## # A tibble: 12 × 5 +## # A tibble: 12 x 5 ## Year Month `Year-month` `Tmean (C) consec NA` `Tmean (C) tot NA` ## -## 1 1980 01 Jan 1980 0 0 -## 2 1980 02 Feb 1980 0 0 -## 3 1980 03 Mar 1980 2 3 -## 4 1980 04 Apr 1980 4 4 -## 5 1980 05 May 1980 0 0 -## 6 1980 06 Jun 1980 0 0 -## 7 1980 07 Jul 1980 0 0 -## 8 1980 08 Aug 1980 0 0 -## 9 1980 09 Sep 1980 1 1 +## 1 1980 01 Jan 1980 0 0 +## 2 1980 02 Feb 1980 0 0 +## 3 1980 03 Mar 1980 2 3 +## 4 1980 04 Apr 1980 4 4 +## 5 1980 05 May 1980 0 0 +## 6 1980 06 Jun 1980 0 0 +## 7 1980 07 Jul 1980 0 0 +## 8 1980 08 Aug 1980 0 0 +## 9 1980 09 Sep 1980 1 1 ## 10 1980 10 Oct 1980 0 0 ## 11 1980 11 Nov 1980 1 1 ## 12 1980 12 Dec 1980 0 0 diff --git a/man/catalogue.Rd b/man/catalogue.Rd index cb0d63d..b143854 100644 --- a/man/catalogue.Rd +++ b/man/catalogue.Rd @@ -28,6 +28,6 @@ catalogue A \code{data.frame} containing the identifiers, names, and other characteristics of climate and hydro stations in Peru. } \examples{ -catalogue +\dontrun{catalogue} } \keyword{datasets} diff --git a/man/qc.Rd b/man/qc.Rd index 55b3f99..487f6c5 100644 --- a/man/qc.Rd +++ b/man/qc.Rd @@ -19,4 +19,3 @@ For now, this script only performs action on the three main temperature variable \author{ Conor I. Anderson } - diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..03ae859 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,8 @@ +## Test `senamhiR` using the `testthat` package + +## Setup +library("testthat") +library("senamhiR") + +## Runs the tests in tests/testthat +test_check("senamhiR") diff --git a/tests/testthat/test-map_stations.R b/tests/testthat/test-map_stations.R new file mode 100644 index 0000000..4053f0b --- /dev/null +++ b/tests/testthat/test-map_stations.R @@ -0,0 +1,15 @@ +library("testthat") +library("senamhiR") +df <- station_search("Lima") + +context("Test `map_stations()`") + +## test a map of one station +test_that("map_stations() can map a single station", { + map_stations("000401") +}) + +## test a map of one searched stations +test_that("map_stations() can map a station search result", { + map_stations(df) +}) diff --git a/tests/testthat/test-qc.R b/tests/testthat/test-qc.R new file mode 100644 index 0000000..c804854 --- /dev/null +++ b/tests/testthat/test-qc.R @@ -0,0 +1,22 @@ +library("testthat") +library("senamhiR") +indat <- senamhiR("000280") + +context("Test `qc()`") + +##FIXME, make more interesting tests +## test qc corrections +test_that("qc can fix errors", { + out <- qc(indat) + expect_identical(names(out)[15], "Observations") + expect_that(out, is_a("tbl_df")) + expect_output(str(out), "20820 obs") + 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.04)") +}) + +## 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) +}) diff --git a/tests/testthat/test-quick_audit.R b/tests/testthat/test-quick_audit.R new file mode 100644 index 0000000..5fc8368 --- /dev/null +++ b/tests/testthat/test-quick_audit.R @@ -0,0 +1,37 @@ +library("testthat") +library("senamhiR") +indat <- senamhiR("000401", 1998:2000) + +context("Test `quick_audit()`") + +## test quick audit by year and return percent missing values +test_that("quick_audit() can audit by year", { + df <- quick_audit(indat, variables = c("Tmean", "Tmax", "Tmin"), by = "year") + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "3 obs") + expect_output(str(df), "4 variables") + expect_equal(df$`Tmean (C) pct NA`[1], 83.28767) +}) + +## test quick audit by month and return number of missing values +test_that("quick_audit() can audit by month", { + df <- quick_audit(indat, variables = c("Tmean", "Tmax", "Tmin"), by = "month", report = "n") + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "36 obs") + expect_output(str(df), "9 variables") + expect_equal(df$`Tmean (C) tot NA`[3], 31) +}) + +## test quick_audit with missing variables and in reverse +test_that("quick_audit() can audit with missing variables", { + df <- quick_audit(indat, reverse = TRUE) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "3 obs") + expect_output(str(df), "14 variables") + expect_lt(df$`Tmean (C) pct present`[1], 16.71233) +}) + +## test quick_audit warns if "year" or "month" not set correctly +test_that("quick_audit() will fail if `by` is a typo", { + expect_warning(quick_audit(indat, by = "mnoth"), "By was neither \"month\" nor \"year\". Defaulting to year.", fixed=TRUE) +}) diff --git a/tests/testthat/test-senamhiR.R b/tests/testthat/test-senamhiR.R new file mode 100644 index 0000000..ae990fa --- /dev/null +++ b/tests/testthat/test-senamhiR.R @@ -0,0 +1,27 @@ +library("testthat") +library("senamhiR") + +context("Test `senamhiR()`") + +## test senamhiR download +test_that("senamhiR can download data", { + out <- senamhiR("000401") + expect_identical(names(out)[12], "Prec19 (mm)") + expect_that(out, is_a("tbl_df")) + expect_output(str(out), "6940 obs") + expect_output(str(out), "14 variables") +}) + +## test senamhiR download by year +test_that("senamhiR can filter by year", { + out <- senamhiR("000401", 1998:2000) + expect_identical(names(out)[12], "Prec19 (mm)") + expect_that(out, is_a("tbl_df")) + expect_output(str(out), "1096 obs") + expect_output(str(out), "14 variables") +}) + +## should fail when no correct station is given +test_that("senamhiR() fails when an incorrect station is requested", { + expect_error(senamhiR("foo"), "The station requested is not a valid station.", fixed=TRUE) +}) diff --git a/tests/testthat/test-station_search.R b/tests/testthat/test-station_search.R new file mode 100644 index 0000000..ea3dd33 --- /dev/null +++ b/tests/testthat/test-station_search.R @@ -0,0 +1,54 @@ +library("testthat") +library("senamhiR") + +context("Test `station_search()`") + +## test finding a station by name with regex +test_that("station_search() can locate a station by name regex", { + df <- station_search("Tara*", glob = TRUE) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "4 obs") + expect_output(str(df), "12 variables") +}) + +## test finding a station by baseline +test_that("station_search() can locate a station by baseline", { + df <- station_search(baseline = 1965:2015) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "346 obs") + expect_output(str(df), "12 variables") +}) + +## test finding a station by region +test_that("station_search() can locate a station by region", { + df <- station_search(region = "TACNA") + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "56 obs") + expect_output(str(df), "12 variables") +}) + +## test finding a station by distance from target +test_that("station_search() can locate a station by distance from target", { + df <- station_search(target = "000410", dist = 0:10) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "2 obs") + expect_output(str(df), "13 variables") +}) + +## test finding a station by distance from coordinates +test_that("station_search() can locate a station by distance for coordinates", { + df <- station_search(target = c(-6.50, -76.47), dist = 0:10) + expect_that(df, is_a("tbl_df")) + expect_output(str(df), "2 obs") + expect_output(str(df), "13 variables") +}) + +## station_search should fail if we spell the region incorrectly +test_that("station_search() fails if passed an incorrect region name", { + expect_error(station_search(region = "Saint Martin"), "No data found for that region. Did you spell it correctly?", fixed=TRUE) +}) + +## station_search should fail if we spell the region incorrectly +test_that("station_search() fails if passed an incorrect region name", { + expect_error(station_search(config = "Q"), "No data found for that config. Did you pass \"m\" or \"h\"?", fixed=TRUE) +})