From e48e358004b86eb06b54a1a92b751510fe78dbb4 Mon Sep 17 00:00:00 2001
From: Richard Martin-Nielsen <richard.martin@alumni.utoronto.ca>
Date: Mon, 27 Sep 2021 20:26:59 +0300
Subject: [PATCH] Add tests for the download_JSON method

---
 NAMESPACE                          |  2 ++
 R/test-DataClass.R                 | 43 ++++++++++++++++++++++++++++++
 man/expect_clean_cols.Rd           |  1 +
 man/expect_columns_contain_data.Rd |  1 +
 man/expect_processed_cols.Rd       |  1 +
 man/test_cleaning.Rd               |  1 +
 man/test_download.Rd               |  1 +
 man/test_download_JSON.Rd          | 33 +++++++++++++++++++++++
 man/test_processing.Rd             |  1 +
 man/test_return.Rd                 |  1 +
 10 files changed, 85 insertions(+)
 create mode 100644 man/test_download_JSON.Rd

diff --git a/NAMESPACE b/NAMESPACE
index 35d1f272..30a995e7 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -41,6 +41,7 @@ export(start_using_memoise)
 export(stop_using_memoise)
 export(test_cleaning)
 export(test_download)
+export(test_download_JSON)
 export(test_processing)
 export(test_return)
 importFrom(R6,R6Class)
@@ -74,6 +75,7 @@ importFrom(dplyr,pull)
 importFrom(dplyr,recode)
 importFrom(dplyr,rename)
 importFrom(dplyr,select)
+importFrom(dplyr,slice_head)
 importFrom(dplyr,slice_tail)
 importFrom(dplyr,starts_with)
 importFrom(dplyr,summarise)
diff --git a/R/test-DataClass.R b/R/test-DataClass.R
index e6f7f2fd..f0f48282 100644
--- a/R/test-DataClass.R
+++ b/R/test-DataClass.R
@@ -121,6 +121,49 @@ test_download <- function(DataClass_obj, download, snapshot_path) {
   }
 }
 
+
+#' Test download method for JSON files works correctly
+#' @description Test data can be downloaded if `download = TRUE`, or a requested
+#' snapshot file is not found, and store a snap shot in the `snapshot_dir`. If
+#' an existing snapshot file is found then load this data to use in future tests
+#' @param DataClass_obj The R6Class object to perform checks on.
+#' Must be a `DataClass` or `DataClass` child object.
+#' @param download Logical check to download or use a snapshot of the data
+#' @param snapshot_path character_array the path to save the downloaded
+#' snapshot to.
+#' @importFrom purrr map walk
+#' @importFrom dplyr slice_head
+#' @family tests
+#' @concept tests
+#' @export
+test_download_JSON <- function(DataClass_obj, download, snapshot_path) {
+  if (!file.exists(snapshot_path)) {
+    download <- TRUE
+  }
+  if (download) {
+    testthat::test_that(
+      paste0(DataClass_obj$data_name, " downloads sucessfully"),
+      {
+        DataClass_obj$download_JSON()
+        walk(DataClass_obj$data$raw, function(data) {
+          testthat::expect_s3_class(data, "data.frame")
+          testthat::expect_true(nrow(data) > 0)
+          testthat::expect_true(ncol(data) >= 2
+                                || typeof(data[[1]]) == "list")
+        })
+      }
+    )
+    DataClass_obj$data$raw <- map(vn$data$raw,
+    slice_head,
+    n = 2
+    )
+
+    saveRDS(DataClass_obj$data$raw, snapshot_path)
+  } else {
+    DataClass_obj$data$raw <- readRDS(snapshot_path)
+  }
+}
+
 #' Test clean method works correctly
 #' @description Test data can be cleaned properly. The clean method is invoked
 #' to generate clean data. This data is checked to ensure it is a data.frame,
diff --git a/man/expect_clean_cols.Rd b/man/expect_clean_cols.Rd
index 4cbfc03e..c5165fef 100644
--- a/man/expect_clean_cols.Rd
+++ b/man/expect_clean_cols.Rd
@@ -20,6 +20,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_columns_contain_data}()},
 \code{\link{expect_processed_cols}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_processing}()},
 \code{\link{test_return}()}
diff --git a/man/expect_columns_contain_data.Rd b/man/expect_columns_contain_data.Rd
index 75439ae5..3c97b657 100644
--- a/man/expect_columns_contain_data.Rd
+++ b/man/expect_columns_contain_data.Rd
@@ -19,6 +19,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_clean_cols}()},
 \code{\link{expect_processed_cols}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_processing}()},
 \code{\link{test_return}()}
diff --git a/man/expect_processed_cols.Rd b/man/expect_processed_cols.Rd
index ad96cf9e..ae79949d 100644
--- a/man/expect_processed_cols.Rd
+++ b/man/expect_processed_cols.Rd
@@ -23,6 +23,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_clean_cols}()},
 \code{\link{expect_columns_contain_data}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_processing}()},
 \code{\link{test_return}()}
diff --git a/man/test_cleaning.Rd b/man/test_cleaning.Rd
index c7f7414d..4194c5a0 100644
--- a/man/test_cleaning.Rd
+++ b/man/test_cleaning.Rd
@@ -22,6 +22,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_clean_cols}()},
 \code{\link{expect_columns_contain_data}()},
 \code{\link{expect_processed_cols}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_processing}()},
 \code{\link{test_return}()}
diff --git a/man/test_download.Rd b/man/test_download.Rd
index fb9c32ea..1cd01985 100644
--- a/man/test_download.Rd
+++ b/man/test_download.Rd
@@ -26,6 +26,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_columns_contain_data}()},
 \code{\link{expect_processed_cols}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_processing}()},
 \code{\link{test_return}()}
 }
diff --git a/man/test_download_JSON.Rd b/man/test_download_JSON.Rd
new file mode 100644
index 00000000..8d05564f
--- /dev/null
+++ b/man/test_download_JSON.Rd
@@ -0,0 +1,33 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/test-DataClass.R
+\name{test_download_JSON}
+\alias{test_download_JSON}
+\title{Test download method for JSON files works correctly}
+\usage{
+test_download_JSON(DataClass_obj, download, snapshot_path)
+}
+\arguments{
+\item{DataClass_obj}{The R6Class object to perform checks on.
+Must be a \code{DataClass} or \code{DataClass} child object.}
+
+\item{download}{Logical check to download or use a snapshot of the data}
+
+\item{snapshot_path}{character_array the path to save the downloaded
+snapshot to.}
+}
+\description{
+Test data can be downloaded if \code{download = TRUE}, or a requested
+snapshot file is not found, and store a snap shot in the \code{snapshot_dir}. If
+an existing snapshot file is found then load this data to use in future tests
+}
+\seealso{
+Functions used for testing data is cleaned and processed correctly
+\code{\link{expect_clean_cols}()},
+\code{\link{expect_columns_contain_data}()},
+\code{\link{expect_processed_cols}()},
+\code{\link{test_cleaning}()},
+\code{\link{test_download}()},
+\code{\link{test_processing}()},
+\code{\link{test_return}()}
+}
+\concept{tests}
diff --git a/man/test_processing.Rd b/man/test_processing.Rd
index 6b7ebb31..3b698d3c 100644
--- a/man/test_processing.Rd
+++ b/man/test_processing.Rd
@@ -25,6 +25,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_columns_contain_data}()},
 \code{\link{expect_processed_cols}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_return}()}
 }
diff --git a/man/test_return.Rd b/man/test_return.Rd
index 69715cf9..02f66f34 100644
--- a/man/test_return.Rd
+++ b/man/test_return.Rd
@@ -22,6 +22,7 @@ Functions used for testing data is cleaned and processed correctly
 \code{\link{expect_columns_contain_data}()},
 \code{\link{expect_processed_cols}()},
 \code{\link{test_cleaning}()},
+\code{\link{test_download_JSON}()},
 \code{\link{test_download}()},
 \code{\link{test_processing}()}
 }