Added additional unit tests to Arvados class
authorFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 23 Jan 2018 11:38:18 +0000 (12:38 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 23 Jan 2018 11:38:18 +0000 (12:38 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/R/Arvados.R
sdk/R/R/HttpRequest.R
sdk/R/README
sdk/R/tests/testthat/fakes/FakeRESTService.R
sdk/R/tests/testthat/test-Arvados.R

index cb36e49a74b74537c032aa74afd3f12dc63d52d8..a22ff73bef300ff409496d4246636a9b37e6ab42 100644 (file)
@@ -79,7 +79,6 @@ Arvados <- R6::R6Class(
         updateCollection = function(uuid, newContent)
         {
             body <- list(list())
-            #test if this is needed
             names(body) <- c("collection")
             body$collection <- newContent
 
index cc4d8683a954b3cb6ddacc01a4569e63b7f636fe..5cee567437dfa33fcd68c7ba7704e9c3b0852ae3 100644 (file)
@@ -64,7 +64,6 @@ HttpRequest <- R6::R6Class(
             h <- curl::new_handle()
             curl::handle_setopt(h, customrequest = "MOVE")
             curl::handle_setheaders(h, .list = headers)
-            print(url)
 
             propfindResponse <- curl::curl_fetch_memory(url, h)
         }
@@ -76,7 +75,7 @@ HttpRequest <- R6::R6Class(
         {
             finalQuery <- NULL
 
-            if(!is.null(filters))
+        if(!is.null(filters))
             {
                 filters <- sapply(filters, function(filter)
                 {
index cc073d5303087806fb3aedc50fa69385b0fb1416..6b8cd3f46dc63aff7ce6df4c71f2b60413ad687b 100644 (file)
@@ -43,6 +43,10 @@ collectionList <- arv$listCollections(list(list("name", "like", "Test%")), limit
 collectionList$items_available # count of total number of items (may be more than returned due to paging)
 collectionList$items # items which match the filter criteria
 
+#Next example will list all collections even when the number of items is greater than maximum API limit
+
+collectionList <- arv$listAllCollections(list(list("name", "like", "Test%")))
+
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Delete a collection:
@@ -203,6 +207,10 @@ arv$getProject("uuid")
 projects <- arv$listProjects(list(list("owner_uuid", "=", "aaaaa-j7d0g-ccccccccccccccc"))) # list subprojects of a project
 arv$listProjects(list(list("name","like","Example%"))) # list projects which have names beginning with Example
 
+#Next example will list all projects even when the number of items is greater than maximum API limit
+
+collectionList <- arv$listAllProjects(list(list("name","like","Example%")))
+
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Delete a project:
index c71a3ef5ae1ca4f6509b8ba61d60adb113d6f5db..9c7203f75618fa21deda6974043a1167ecdc9cf1 100644 (file)
@@ -19,11 +19,15 @@ FakeRESTService <- R6::R6Class(
         readCallCount                 = NULL,
         writeCallCount                = NULL,
         writeBuffer                   = NULL,
+        filtersAreConfiguredCorrectly = NULL,
+        bodyIsConfiguredCorrectly     = NULL,
+        expectedFilterContent         = NULL,
 
         collectionContent = NULL,
-        returnContent = NULL,
+        returnContent     = NULL,
 
-        initialize = function(collectionContent = NULL, returnContent = NULL)
+        initialize = function(collectionContent = NULL, returnContent = NULL, 
+                              expectedFilterContent = NULL)
         {
             self$getResourceCallCount    <- 0
             self$createResourceCallCount <- 0
@@ -39,9 +43,12 @@ FakeRESTService <- R6::R6Class(
             self$getResourceSizeCallCount      <- 0
             self$readCallCount                 <- 0
             self$writeCallCount                <- 0
+            self$filtersAreConfiguredCorrectly <- FALSE
+            self$bodyIsConfiguredCorrectly     <- FALSE
 
-            self$collectionContent <- collectionContent
-            self$returnContent <- returnContent
+            self$collectionContent     <- collectionContent
+            self$returnContent         <- returnContent
+            self$expectedFilterContent <- expectedFilterContent
         },
 
         getWebDavHostName = function()
@@ -57,12 +64,22 @@ FakeRESTService <- R6::R6Class(
         listResources = function(resource, filters = NULL, limit = 100, offset = 0)
         {
             self$listResourcesCallCount <- self$listResourcesCallCount + 1
+
+            if(!is.null(self$expectedFilterContent) && !is.null(filters))
+               if(all.equal(filters, self$expectedFilterContent))
+                    self$filtersAreConfiguredCorrectly <- TRUE
+
             self$returnContent
         },
 
         fetchAllItems = function(resourceURL, filters)
         {
             self$fetchAllItemsCallCount <- self$fetchAllItemsCallCount + 1
+
+            if(!is.null(self$expectedFilterContent) && !is.null(filters))
+               if(all.equal(filters, self$expectedFilterContent))
+                    self$filtersAreConfiguredCorrectly <- TRUE
+
             self$returnContent
         },
 
@@ -75,12 +92,22 @@ FakeRESTService <- R6::R6Class(
         updateResource = function(resource, uuid, newContent)
         {
             self$updateResourceCallCount <- self$updateResourceCallCount + 1
+
+            if(!is.null(self$returnContent) && !is.null(newContent))
+               if(all.equal(newContent, self$returnContent))
+                    self$bodyIsConfiguredCorrectly <- TRUE
+
             self$returnContent
         },
 
         createResource = function(resource, content)
         {
             self$createResourceCallCount <- self$createResourceCallCount + 1
+
+            if(!is.null(self$returnContent) && !is.null(content))
+               if(all.equal(content, self$returnContent))
+                    self$bodyIsConfiguredCorrectly <- TRUE
+
             self$returnContent
         },
 
index 8ebad95d0c1ac79cdfff7ab2c5e3cc3741f6d5ad..25cf88f42253e4fccf85961c1e9183ab59b2ee30 100644 (file)
@@ -56,7 +56,7 @@ test_that("getCollection delegates operation to RESTService class", {
     expect_that(fakeREST$getResourceCallCount, equals(1))
 }) 
 
-test_that("listCollection delegates operation to RESTService class", {
+test_that("listCollections delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
     fakeREST <- FakeRESTService$new()
@@ -67,7 +67,20 @@ test_that("listCollection delegates operation to RESTService class", {
     expect_that(fakeREST$listResourcesCallCount, equals(1))
 }) 
 
-test_that("listAllCollection delegates operation to RESTService class", {
+test_that("listCollections filter paramerter must be named 'collection'", {
+
+    filters <- list(list("name", "like", "MyCollection"))
+    names(filters) <- c("collection")
+    fakeREST <- FakeRESTService$new(expectedFilterContent = filters)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$listCollections(list(list("name", "like", "MyCollection")))
+
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
+
+test_that("listAllCollections delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
     fakeREST <- FakeRESTService$new()
@@ -78,6 +91,19 @@ test_that("listAllCollection delegates operation to RESTService class", {
     expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
 }) 
 
+test_that("listAllCollections filter paramerter must be named 'collection'", {
+
+    filters <- list(list("name", "like", "MyCollection"))
+    names(filters) <- c("collection")
+    fakeREST <- FakeRESTService$new(expectedFilterContent = filters)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$listAllCollections(list(list("name", "like", "MyCollection")))
+
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
+
 test_that("deleteCollection delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -103,6 +129,22 @@ test_that("updateCollection delegates operation to RESTService class", {
     expect_that(fakeREST$updateResourceCallCount, equals(1))
 }) 
 
+test_that("updateCollection adds content to request parameter named 'collection'", {
+
+    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+    body <- list(list())
+    names(body) <- c("collection")
+    body$collection <- list(name = "MyCollection", desc = "No description")
+    fakeREST <- FakeRESTService$new(returnContent = body)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$updateCollection(collectionUUID, 
+                         list(name = "MyCollection", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
+}) 
+
 test_that("createCollection delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -115,6 +157,20 @@ test_that("createCollection delegates operation to RESTService class", {
     expect_that(fakeREST$createResourceCallCount, equals(1))
 }) 
 
+test_that("createCollection adds content to request parameter named 'collection'", {
+
+    body <- list(list())
+    names(body) <- c("collection")
+    body$collection <- list(name = "MyCollection", desc = "No description")
+    fakeREST <- FakeRESTService$new(returnContent = body)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$createCollection(list(name = "MyCollection", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
+}) 
+
 test_that("getProject delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -138,6 +194,21 @@ test_that("listProjects delegates operation to RESTService class", {
     expect_that(fakeREST$listResourcesCallCount, equals(1))
 }) 
 
+test_that("listProjects filter contains additional 'group_class' field by default", {
+
+    filters <- list(list("name", "like", "MyProject"))
+    names(filters) <- c("groups")
+    filters[[length(filters) + 1]] <- list("group_class", "=", "project")
+
+    fakeREST <- FakeRESTService$new(expectedFilterContent = filters)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$listProjects(list(list("name", "like", "MyProject")))
+
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
+
 test_that("listAllProjects delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -149,6 +220,21 @@ test_that("listAllProjects delegates operation to RESTService class", {
     expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
 }) 
 
+test_that("listAllProjects filter contains additional 'group_class' field by default", {
+
+    filters <- list(list("name", "like", "MyProject"))
+    names(filters) <- c("groups")
+    filters[[length(filters) + 1]] <- list("group_class", "=", "project")
+
+    fakeREST <- FakeRESTService$new(expectedFilterContent = filters)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$listAllProjects(list(list("name", "like", "MyProject")))
+
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
+
 test_that("deleteProject delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -174,6 +260,23 @@ test_that("updateProject delegates operation to RESTService class", {
     expect_that(fakeREST$updateResourceCallCount, equals(1))
 }) 
 
+test_that("updateProject adds content to request parameter named 'group'", {
+
+    projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+    body <- list(list())
+    names(body) <- c("group")
+    body$group <- list(name = "MyProject", desc = "No description")
+
+    fakeREST <- FakeRESTService$new(returnContent = body)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$updateProject(projectUUID,
+                      list(name = "MyProject", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
+}) 
+
 test_that("createProject delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
@@ -185,3 +288,19 @@ test_that("createProject delegates operation to RESTService class", {
 
     expect_that(fakeREST$createResourceCallCount, equals(1))
 }) 
+
+test_that("createProject request body contains 'goup_class' filed", {
+
+    body <- list(list())
+    names(body) <- c("group")
+    body$group <- c("group_class" = "project",
+                    list(name = "MyProject", desc = "No description"))
+
+    fakeREST <- FakeRESTService$new(returnContent = body)
+    arv <- Arvados$new("token", "hostName")
+    arv$setRESTService(fakeREST)
+
+    arv$createProject(list(name = "MyProject", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
+})