From a0a482abef763fddd837ac363469b24ea8037a18 Mon Sep 17 00:00:00 2001 From: pegeler Date: Thu, 28 Jun 2018 17:20:14 -0400 Subject: [PATCH] Completed working test suite and added CI --- R/.Rbuildignore | 2 + R/.travis.yml | 5 ++ R/appveyor.yml | 45 ++++++++++++++ R/tests/testthat/data/create-ref-data.R | 48 ++++++++++++++- ...helper-ExampleProject_R_2018-06-07_1129.r} | 59 ++++++++++--------- R/tests/testthat/helper-paths.R | 9 +++ R/tests/testthat/test-API.R | 10 +--- R/tests/testthat/test-csv-exports.R | 20 +++---- R/tests/testthat/test-longitudinal.R | 7 +-- README.md | 3 + 10 files changed, 153 insertions(+), 55 deletions(-) create mode 100644 R/.travis.yml create mode 100644 R/appveyor.yml rename R/tests/testthat/{data/ExampleProject_R_2018-06-07_1129.r => helper-ExampleProject_R_2018-06-07_1129.r} (64%) create mode 100644 R/tests/testthat/helper-paths.R diff --git a/R/.Rbuildignore b/R/.Rbuildignore index 91114bf..e148253 100644 --- a/R/.Rbuildignore +++ b/R/.Rbuildignore @@ -1,2 +1,4 @@ ^.*\.Rproj$ ^\.Rproj\.user$ +^\.travis\.yml$ +^appveyor\.yml$ diff --git a/R/.travis.yml b/R/.travis.yml new file mode 100644 index 0000000..8d139ac --- /dev/null +++ b/R/.travis.yml @@ -0,0 +1,5 @@ +# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r + +language: R +sudo: false +cache: packages diff --git a/R/appveyor.yml b/R/appveyor.yml new file mode 100644 index 0000000..c6c1438 --- /dev/null +++ b/R/appveyor.yml @@ -0,0 +1,45 @@ +# DO NOT CHANGE the "init" and "install" sections below + +# Download script file from GitHub +init: + ps: | + $ErrorActionPreference = "Stop" + Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" + Import-Module '..\appveyor-tool.ps1' + +install: + ps: Bootstrap + +cache: + - C:\RLibrary + +# Adapt as necessary starting from here + +build_script: + - travis-tool.sh install_deps + +test_script: + - travis-tool.sh run_tests + +on_failure: + - 7z a failure.zip *.Rcheck\* + - appveyor PushArtifact failure.zip + +artifacts: + - path: '*.Rcheck\**\*.log' + name: Logs + + - path: '*.Rcheck\**\*.out' + name: Logs + + - path: '*.Rcheck\**\*.fail' + name: Logs + + - path: '*.Rcheck\**\*.Rout' + name: Logs + + - path: '\*_*.tar.gz' + name: Bits + + - path: '\*_*.zip' + name: Bits diff --git a/R/tests/testthat/data/create-ref-data.R b/R/tests/testthat/data/create-ref-data.R index be65877..7b9442c 100644 --- a/R/tests/testthat/data/create-ref-data.R +++ b/R/tests/testthat/data/create-ref-data.R @@ -1,3 +1,49 @@ +# Setup ------------------------------------------------------------------- + +devtools::load_all() + +library(digest) +library(magrittr) +library(jsonlite) + + +ref_data_location <- function(x) file.path("tests","testthat","data", x) + +# RCurl ------------------------------------------------------------------- + +REDCap_split( + ref_data_location("ExampleProject_records.json") %>% fromJSON, + ref_data_location("ExampleProject_metadata.json") %>% fromJSON + ) %>% digest + + +# Basic CSV --------------------------------------------------------------- + +REDCap_split( + ref_data_location("ExampleProject_DATA_2018-06-07_1129.csv") %>% read.csv, + ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>% read.csv + ) %>% digest + +# REDCap R Export --------------------------------------------------------- + +source("tests/testthat/helper-ExampleProject_R_2018-06-07_1129.r") + +REDCap_split( + ref_data_location("ExampleProject_DATA_2018-06-07_1129.csv") %>% + read.csv %>% + REDCap_process_csv, + ref_data_location("ExampleProject_DataDictionary_2018-06-07.csv") %>% read.csv + ) %>% digest + # Longitudinal data from @pbchase; Issue #7 ------------------------------- -# Something will go here. +file_paths <- sapply( + c( + records = "WARRIORtestForSoftwa_DATA_2018-06-21_1431.csv", + metadata = "WARRIORtestForSoftwareUpgrades_DataDictionary_2018-06-21.csv" + ), ref_data_location +) + +redcap <- lapply(file_paths, read.csv, stringsAsFactors = FALSE) +redcap[["metadata"]] <- with(redcap, metadata[metadata[,1] > "",]) +with(redcap, REDCap_split(records, metadata)) %>% digest diff --git a/R/tests/testthat/data/ExampleProject_R_2018-06-07_1129.r b/R/tests/testthat/helper-ExampleProject_R_2018-06-07_1129.r similarity index 64% rename from R/tests/testthat/data/ExampleProject_R_2018-06-07_1129.r rename to R/tests/testthat/helper-ExampleProject_R_2018-06-07_1129.r index 6c6a6cf..45200aa 100644 --- a/R/tests/testthat/data/ExampleProject_R_2018-06-07_1129.r +++ b/R/tests/testthat/helper-ExampleProject_R_2018-06-07_1129.r @@ -1,36 +1,37 @@ REDCap_process_csv <- function(data) { #Load Hmisc library - if (!require(Hmisc)) + if (!requireNamespace("Hmisc", quietly = TRUE)) { stop("This test requires the 'Hmisc' package") + } - label(data$row)="Name" - label(data$redcap_repeat_instrument)="Repeat Instrument" - label(data$redcap_repeat_instance)="Repeat Instance" - label(data$mpg)="Miles/(US) gallon" - label(data$cyl)="Number of cylinders" - label(data$disp)="Displacement" - label(data$hp)="Gross horsepower" - label(data$drat)="Rear axle ratio" - label(data$wt)="Weight" - label(data$qsec)="1/4 mile time" - label(data$vs)="V engine?" - label(data$am)="Transmission" - label(data$gear)="Number of forward gears" - label(data$carb)="Number of carburetors" - label(data$color_available___red)="Colors Available (choice=Red)" - label(data$color_available___green)="Colors Available (choice=Green)" - label(data$color_available___blue)="Colors Available (choice=Blue)" - label(data$color_available___black)="Colors Available (choice=Black)" - label(data$motor_trend_cars_complete)="Complete?" - label(data$letter_group___a)="Which group? (choice=A)" - label(data$letter_group___b)="Which group? (choice=B)" - label(data$letter_group___c)="Which group? (choice=C)" - label(data$choice)="Choose one" - label(data$grouping_complete)="Complete?" - label(data$price)="Sale price" - label(data$color)="Color" - label(data$customer)="Customer Name" - label(data$sale_complete)="Complete?" + Hmisc::label(data$row)="Name" + Hmisc::label(data$redcap_repeat_instrument)="Repeat Instrument" + Hmisc::label(data$redcap_repeat_instance)="Repeat Instance" + Hmisc::label(data$mpg)="Miles/(US) gallon" + Hmisc::label(data$cyl)="Number of cylinders" + Hmisc::label(data$disp)="Displacement" + Hmisc::label(data$hp)="Gross horsepower" + Hmisc::label(data$drat)="Rear axle ratio" + Hmisc::label(data$wt)="Weight" + Hmisc::label(data$qsec)="1/4 mile time" + Hmisc::label(data$vs)="V engine?" + Hmisc::label(data$am)="Transmission" + Hmisc::label(data$gear)="Number of forward gears" + Hmisc::label(data$carb)="Number of carburetors" + Hmisc::label(data$color_available___red)="Colors Available (choice=Red)" + Hmisc::label(data$color_available___green)="Colors Available (choice=Green)" + Hmisc::label(data$color_available___blue)="Colors Available (choice=Blue)" + Hmisc::label(data$color_available___black)="Colors Available (choice=Black)" + Hmisc::label(data$motor_trend_cars_complete)="Complete?" + Hmisc::label(data$letter_group___a)="Which group? (choice=A)" + Hmisc::label(data$letter_group___b)="Which group? (choice=B)" + Hmisc::label(data$letter_group___c)="Which group? (choice=C)" + Hmisc::label(data$choice)="Choose one" + Hmisc::label(data$grouping_complete)="Complete?" + Hmisc::label(data$price)="Sale price" + Hmisc::label(data$color)="Color" + Hmisc::label(data$customer)="Customer Name" + Hmisc::label(data$sale_complete)="Complete?" #Setting Units diff --git a/R/tests/testthat/helper-paths.R b/R/tests/testthat/helper-paths.R new file mode 100644 index 0000000..19c8ff1 --- /dev/null +++ b/R/tests/testthat/helper-paths.R @@ -0,0 +1,9 @@ +# get_data_location <- function(x) { +# system.file( +# "testdata", +# x, +# package = "REDCapRITS" +# ) +# } + +get_data_location <- function(x) file.path("data", x) diff --git a/R/tests/testthat/test-API.R b/R/tests/testthat/test-API.R index a01af3f..b666859 100644 --- a/R/tests/testthat/test-API.R +++ b/R/tests/testthat/test-API.R @@ -1,22 +1,16 @@ context("Reading in JSON") -# Set up the path ---------------------------------------------------------- - -data_dir <- system.file("tests", "testthat", "data", package = "REDCapRITS") - # Check the RCurl export --------------------------------------------------- test_that("JSON character vector from RCurl matches reference", { metadata <- jsonlite::fromJSON( - file.path( - data_dir, + get_data_location( "ExampleProject_metadata.json" ) ) records <- jsonlite::fromJSON( - file.path( - data_dir, + get_data_location( "ExampleProject_records.json" ) ) diff --git a/R/tests/testthat/test-csv-exports.R b/R/tests/testthat/test-csv-exports.R index 61e6ba7..098e585 100644 --- a/R/tests/testthat/test-csv-exports.R +++ b/R/tests/testthat/test-csv-exports.R @@ -1,19 +1,14 @@ context("CSV Exports") # Set up the path and data ------------------------------------------------- - -data_dir <- system.file("tests", "testthat", "data", package = "REDCapRITS") - metadata <- read.csv( - file.path( - data_dir, + get_data_location( "ExampleProject_DataDictionary_2018-06-07.csv" ) ) records <- read.csv( - file.path( - data_dir, + get_data_location( "ExampleProject_DATA_2018-06-07_1129.csv" ) ) @@ -26,10 +21,11 @@ test_that("CSV export matches reference", { }) # Test that R code enhanced CSV export matches reference -------------------- -test_that("R code enhanced export matches reference", { - source(file.path(data_dir, "ExampleProject_R_2018-06-07_1129.r")) +if (requireNamespace("Hmisc", quietly = TRUE)) { + test_that("R code enhanced export matches reference", { + redcap_output_csv2 <- REDCap_split(REDCap_process_csv(records), metadata) - redcap_output_csv2 <- REDCap_split(REDCap_process_csv(records), metadata) + expect_known_hash(redcap_output_csv2, "34f82cab35bf8aae47d08cd96f743e6b") + }) +} - expect_known_hash(redcap_output_csv2, "34f82cab35bf8aae47d08cd96f743e6b") -}) diff --git a/R/tests/testthat/test-longitudinal.R b/R/tests/testthat/test-longitudinal.R index 3ba492f..c64cc14 100644 --- a/R/tests/testthat/test-longitudinal.R +++ b/R/tests/testthat/test-longitudinal.R @@ -1,17 +1,14 @@ context("Longitudinal data") test_that("CSV export matches reference", { - # Reading in the files - file_paths <- file.path( - system.file("tests", "testthat", "data", package = "REDCapRITS"), + file_paths <- sapply( c( records = "WARRIORtestForSoftwa_DATA_2018-06-21_1431.csv", metadata = "WARRIORtestForSoftwareUpgrades_DataDictionary_2018-06-21.csv" - ) + ), get_data_location ) redcap <- lapply(file_paths, read.csv, stringsAsFactors = FALSE) - names(redcap) <- c("records", "metadata") redcap[["metadata"]] <- with(redcap, metadata[metadata[,1] > "",]) redcap_output <- with(redcap, REDCap_split(records, metadata)) diff --git a/README.md b/README.md index 560be13..e96aeee 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,9 @@ welcome your contributions! ## Instructions ### R +[![Travis-CI Build Status](https://travis-ci.org/SpectrumHealthResearch/REDCapRITS.svg?branch=master)](https://travis-ci.org/SpectrumHealthResearch/REDCapRITS) +[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/SpectrumHealthResearch/REDCapRITS?branch=master&svg=true)](https://ci.appveyor.com/project/SpectrumHealthResearch/REDCapRITS) + #### Installation First you must install the package. To do so, execute the following in your R console: