Added unit tests for HttpRequest class.
authorFuad Muhic <fmuhic@capeannenterprises.com>
Wed, 24 Jan 2018 15:17:36 +0000 (16:17 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Wed, 24 Jan 2018 15:17:36 +0000 (16:17 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/R/util.R
sdk/R/tests/testthat/test-HttpRequest.R [new file with mode: 0644]

index f6a582ff177b3b9aea4234486868142e19f10c59..65b5a4e52f8da088e3236d2bb44f362e5eb4e9d2 100644 (file)
@@ -14,22 +14,23 @@ trimFromEnd <- function(sample, trimCharacters)
     sample
 }
 
-RListToPythonList <- function(sample, separator = ", ")
+RListToPythonList <- function(RList, separator = ", ")
 {
-    pythonArrayContent <- sapply(sample, function(sampleUnit)
+    pythonArrayContent <- sapply(RList, function(elementInList)
     {
-        if((is.vector(sampleUnit) || is.list(sampleUnit)) &&
-            length(sampleUnit) > 1)
+        if((is.vector(elementInList) || is.list(elementInList)) &&
+            length(elementInList) > 1)
         {
-            return(RListToPythonList(sampleUnit, separator))
+            return(RListToPythonList(elementInList, separator))
         }
         else
         {
-            return(paste0("\"", sampleUnit, "\""))
+            return(paste0("\"", elementInList, "\""))
         }
     })
 
-    return(paste0("[", paste0(pythonArrayContent, collapse = separator), "]"))
+    pythonArray <- paste0("[", paste0(pythonArrayContent, collapse = separator), "]")
+    pythonArray
 }
 
 appendToStartIfNotExist <- function(sample, characters)
diff --git a/sdk/R/tests/testthat/test-HttpRequest.R b/sdk/R/tests/testthat/test-HttpRequest.R
new file mode 100644 (file)
index 0000000..427ec34
--- /dev/null
@@ -0,0 +1,21 @@
+context("Http Parser")
+
+
+test_that(paste("createQuery generates and encodes query portion of http",
+                "request based on filters, limit and offset parameters"), {
+
+    http <- HttpRequest$new()
+    filters <- list(list("color", "=", "red"))
+    limit <- 20
+    offset <- 50
+    expect_that(http$createQuery(filters, limit, offset),
+                equals(paste0("/?filters=%5B%5B%22color%22%2C%22%3D%22%2C%22red",
+                              "%22%5D%5D&limit=20&offset=50")))
+}) 
+
+test_that(paste("createQuery generates and empty string",
+                "when filters, limit and offset parameters are set to NULL"), {
+
+    http <- HttpRequest$new()
+    expect_that(http$createQuery(NULL, NULL, NULL), equals(""))
+})