From d6c7bab06cb4c272a18d4d3e05b110e061db7f5d Mon Sep 17 00:00:00 2001
From: Luke Zappia <lazappi@users.noreply.github.com>
Date: Wed, 4 Oct 2017 20:08:23 +1100
Subject: [PATCH] Add ZINBParams tests

---
 R/AllClasses.R                    |  3 ++-
 R/ZINBParams-methods.R            | 17 +++++++++++------
 tests/testthat/test-PhenoParams.R |  2 +-
 tests/testthat/test-ZINBParams.R  | 13 +++++++++++++
 4 files changed, 27 insertions(+), 8 deletions(-)
 create mode 100644 tests/testthat/test-ZINBParams.R

diff --git a/R/AllClasses.R b/R/AllClasses.R
index 00f74fc..7c768e9 100644
--- a/R/AllClasses.R
+++ b/R/AllClasses.R
@@ -559,7 +559,8 @@ setClass("PhenoParams",
 #'
 #' The majority of the parameters for this simulation are stored in a
 #' \code{\link[zinbwave]{ZinbModel}} object. Please refer to the documentation
-#' for this class for details about all the parameters.
+#' for this class and its constructor(\code{\link[zinbwave]{zinbModel}}) for
+#' details about all the parameters.
 #'
 #' The parameters not shown in brackets can be estimated from real data using
 #' \code{\link{zinbEstimate}}. For details of the ZINB-WaVE simulation
diff --git a/R/ZINBParams-methods.R b/R/ZINBParams-methods.R
index 391b34b..91b6863 100644
--- a/R/ZINBParams-methods.R
+++ b/R/ZINBParams-methods.R
@@ -38,14 +38,19 @@ setValidity("ZINBParams", function(object) {
 setMethod("setParam", "ZINBParams", function(object, name, value) {
     checkmate::assertString(name)
 
-    if (name %in% names(getSlots("ZinbModel"))) {
-        model <- getParam(object, "model")
-        slot(model, name) <- value
-        object <- setParam(object, "model", model)
-    } else {
-        object <- callNextMethod()
+    if (name %in% c("nGenes", "nCells")) {
+        stop(name, " cannot be set directly, set model instead")
+    }
+
+    if (name == "model") {
+        object <- setParamUnchecked(object, "nGenes",
+                                    zinbwave::nFeatures(value))
+        object <- setParamUnchecked(object, "nCells",
+                                    zinbwave::nSamples(value))
     }
 
+    object <- cellNextMethod()
+
     return(object)
 })
 
diff --git a/tests/testthat/test-PhenoParams.R b/tests/testthat/test-PhenoParams.R
index 535724a..d5a252d 100644
--- a/tests/testthat/test-PhenoParams.R
+++ b/tests/testthat/test-PhenoParams.R
@@ -1,7 +1,7 @@
 context("PhenoParams")
 
 test_that("constructor is valid", {
-    expect_true(validObject(newSCDDParams()))
+    expect_true(validObject(newPhenoParams()))
 })
 
 test_that("nGenes checks work", {
diff --git a/tests/testthat/test-ZINBParams.R b/tests/testthat/test-ZINBParams.R
new file mode 100644
index 0000000..4a4cfde
--- /dev/null
+++ b/tests/testthat/test-ZINBParams.R
@@ -0,0 +1,13 @@
+context("ZINBParams")
+
+test_that("constructor is valid", {
+    expect_true(validObject(newZINBParams()))
+})
+
+test_that("nGenes checks work", {
+    params <- newZINBParams()
+    expect_error(setParam(params, "nGenes", 1),
+                 "nGenes cannot be set directly")
+    expect_error(setParam(params, "nCells", 1),
+                 "nGenes cannot be set directly")
+})
-- 
GitLab