Added support for unit testing.
authorFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 9 Jan 2018 16:21:18 +0000 (17:21 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 9 Jan 2018 16:21:18 +0000 (17:21 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/DESCRIPTION
sdk/R/R/Arvados.R
sdk/R/tests/testthat.R [new file with mode: 0644]
sdk/R/tests/testthat/HttpRequestStub.R [new file with mode: 0644]
sdk/R/tests/testthat/test-Arvados.R [new file with mode: 0644]

index 0e586e91bd3bbb1c8476775428f22fbb0be6f2d9..ce96f05b27c6c018f64bfe6198982ad686a5b024 100644 (file)
@@ -17,3 +17,4 @@ Imports:
     jsonlite,
     curl,
     XML
+Suggests: testthat
index e08ce61d4c8a91de0704a4268b094839e800764e..b58b287b0dffbf95a26ec7ac51aa220d52e2eee6 100644 (file)
@@ -30,9 +30,6 @@ Arvados <- R6::R6Class(
                 stop("Please provide host name and authentification token or set
                      ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
 
-            discoveryDocumentURL <- paste0("https://", host,
-                                           "/discovery/v1/apis/arvados/v1/rest")
-
             version <- "v1"
             host  <- paste0("https://", host, "/arvados/", version, "/")
 
@@ -40,22 +37,31 @@ Arvados <- R6::R6Class(
             private$httpParser <- HttpParser$new()
             private$token      <- token
             private$host       <- host
-            
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            serverResponse <- private$http$GET(discoveryDocumentURL, headers)
-
-            discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
-            private$webDavHostName <- discoveryDocument$keepWebServiceUrl
-
-            if(is.null(private$webDavHostName))
-                stop("Unable to find WebDAV server.")
         },
 
         getToken    = function() private$token,
         getHostName = function() private$host,
 
-        getWebDavHostName = function() private$webDavHostName,
+        getWebDavHostName = function()
+        {
+            if(is.null(private$webDavHostName))
+            {
+                discoveryDocumentURL <- paste0("https://", host,
+                                               "/discovery/v1/apis/arvados/v1/rest")
+
+                headers <- list(Authorization = paste("OAuth2", private$token))
+
+                serverResponse <- private$http$GET(discoveryDocumentURL, headers)
+
+                discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
+                private$webDavHostName <- discoveryDocument$keepWebServiceUrl
+
+                if(is.null(private$webDavHostName))
+                    stop("Unable to find WebDAV server.")
+            }
+
+            private$webDavHostName
+        },
 
         getCollection = function(uuid) 
         {
diff --git a/sdk/R/tests/testthat.R b/sdk/R/tests/testthat.R
new file mode 100644 (file)
index 0000000..18ef411
--- /dev/null
@@ -0,0 +1,4 @@
+library(testthat)
+library(ArvadosR)
+
+test_check("ArvadosR")
diff --git a/sdk/R/tests/testthat/HttpRequestStub.R b/sdk/R/tests/testthat/HttpRequestStub.R
new file mode 100644 (file)
index 0000000..31f8759
--- /dev/null
@@ -0,0 +1,52 @@
+HttpRequest <- R6::R6Class(
+
+    "HttrRequestStub",
+
+    public = list(
+
+        validContentTypes = NULL,
+
+        content = NULL,
+
+        initialize = function(returnContent) 
+        {
+            self$validContentTypes <- c("text", "raw")
+            self$content <- returnContent
+        },
+
+        GET = function(url, headers = NULL, queryFilters = NULL, limit = NULL, offset = NULL)
+        {
+            return self$content
+        },
+
+        PUT = function(url, headers = NULL, body = NULL,
+                       queryFilters = NULL, limit = NULL, offset = NULL)
+        {
+            return self$content
+        },
+
+        POST = function(url, headers = NULL, body = NULL,
+                        queryFilters = NULL, limit = NULL, offset = NULL)
+        {
+            return self$content
+        },
+
+        DELETE = function(url, headers = NULL, body = NULL,
+                          queryFilters = NULL, limit = NULL, offset = NULL)
+        {
+            return self$content
+        },
+
+        PROPFIND = function(url, headers = NULL)
+        {
+            return self$content
+        },
+
+        MOVE = function(url, headers = NULL)
+        {
+            return self$content
+        }
+    ),
+
+    cloneable = FALSE
+)
diff --git a/sdk/R/tests/testthat/test-Arvados.R b/sdk/R/tests/testthat/test-Arvados.R
new file mode 100644 (file)
index 0000000..35b4152
--- /dev/null
@@ -0,0 +1,35 @@
+context("Arvados API")
+
+test_that("Arvados constructor will use environment variables if no parameters are passed to it", {
+
+    Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
+    Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
+
+    arv <- Arvados$new()
+
+    Sys.unsetenv("ARVADOS_API_HOST")
+    Sys.unsetenv("ARVADOS_API_TOKEN")
+
+    expect_that("https://environment_api_host/arvados/v1/",
+                equals(arv$getHostName())) 
+
+    expect_that("environment_api_token",
+                equals(arv$getToken())) 
+}) 
+
+test_that("Arvados constructor preferes constructor fields over environment variables", {
+
+    Sys.setenv(ARVADOS_API_HOST  = "environment_api_host")
+    Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
+
+    arv <- Arvados$new("constructor_api_token", "constructor_api_host")
+
+    Sys.unsetenv("ARVADOS_API_HOST")
+    Sys.unsetenv("ARVADOS_API_TOKEN")
+
+    expect_that("https://constructor_api_host/arvados/v1/",
+                equals(arv$getHostName())) 
+
+    expect_that("constructor_api_token",
+                equals(arv$getToken())) 
+})