Merge branch '12315-exclude-child-procs'
[arvados.git] / sdk / R / tests / testthat / test-Arvados.R
index 16b679805747ac53fac26b8c59ea9105407a8ce8..25cf88f42253e4fccf85961c1e9183ab59b2ee30 100644 (file)
@@ -1,7 +1,6 @@
 context("Arvados API")
 
-source("fakes/FakeHttpRequest.R")
-source("fakes/FakeHttpParser.R")
+source("fakes/FakeRESTService.R")
 
 test_that("Constructor will use environment variables if no parameters are passed to it", {
 
@@ -42,177 +41,266 @@ test_that("Constructor raises exception if fields and environment variables are
     expect_that(Arvados$new(),
                 throws_error(paste0("Please provide host name and authentification token",
                                     " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
-                                    " environmental variables.")))
+                                    " environment variables.")))
 }) 
 
-test_that("getWebDavHostName calls REST service properly", {
+test_that("getCollection delegates operation to RESTService class", {
 
-    hostName <- "hostName"
-    token    <- "token"
-    arv      <- Arvados$new(token, hostName)
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
 
-    serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
-    expectedURL    <- paste0("https://", hostName,
-                             "/discovery/v1/apis/arvados/v1/rest")
+    arv$getCollection(collectionUUID)
 
-    httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
-    arv$setHttpClient(httpRequest)
-    arv$setHttpParser(FakeHttpParser$new())
+    expect_that(fakeREST$getResourceCallCount, equals(1))
+}) 
 
-    webDAVHostName <- arv$getWebDavHostName()
+test_that("listCollections delegates operation to RESTService class", {
 
-    expect_that(httpRequest$URLIsProperlyConfigured, is_true())
-    expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
-    expect_that(httpRequest$numberOfGETRequests, equals(1))
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+
+    arv$listCollections()
+
+    expect_that(fakeREST$listResourcesCallCount, equals(1))
 }) 
 
-test_that("getWebDavHostName returns webDAV host name properly", {
+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)
 
-    serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
+    arv$listCollections(list(list("name", "like", "MyCollection")))
 
-    httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
-    arv$setHttpClient(httpRequest)
-    arv$setHttpParser(FakeHttpParser$new())
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
 
-    expect_that("https://myWebDavServer.com", equals(arv$getWebDavHostName())) 
+test_that("listAllCollections delegates operation to RESTService class", {
+
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+
+    arv$listAllCollections()
+
+    expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
 }) 
 
-test_that("getCollection calls REST service properly", {
+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)
 
-    serverResponse <- NULL
-    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    expectedURL    <- paste0(arv$getHostName(), "collections/", collectionUUID)
+    arv$listAllCollections(list(list("name", "like", "MyCollection")))
 
-    httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
-    arv$setHttpClient(httpRequest)
-    arv$setHttpParser(FakeHttpParser$new())
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
 
-    arv$getCollection(collectionUUID)
+test_that("deleteCollection delegates operation to RESTService class", {
 
-    expect_that(httpRequest$URLIsProperlyConfigured, is_true())
-    expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
-    expect_that(httpRequest$numberOfGETRequests, equals(1))
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+
+    arv$deleteCollection(collectionUUID)
+
+    expect_that(fakeREST$deleteResourceCallCount, equals(1))
 }) 
 
-test_that("getCollection parses server response", {
+test_that("updateCollection delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    newCollectionContent <- list(newName = "Brand new shiny name")
+    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+
+    arv$updateCollection(collectionUUID, newCollectionContent)
+
+    expect_that(fakeREST$updateResourceCallCount, equals(1))
+}) 
 
-    httpParser <- FakeHttpParser$new()
-    arv$setHttpParser(httpParser)
-    arv$setHttpClient(FakeHttpRequest$new())
+test_that("updateCollection adds content to request parameter named 'collection'", {
 
     collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    arv$getCollection(collectionUUID)
+    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)
 
-    expect_that(httpParser$parserCallCount, equals(1))
+    arv$updateCollection(collectionUUID, 
+                         list(name = "MyCollection", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
 }) 
 
-test_that("getCollection raises exception if response contains errors field", {
+test_that("createCollection delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
-    
-    serverResponse <- list(errors = 404)
-    arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
-    arv$setHttpParser(FakeHttpParser$new())
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    collectionContent <- list(newName = "Brand new shiny name")
 
-    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    
-    expect_that(arv$getCollection(collectionUUID), 
-                throws_error(404))
+    arv$createCollection(collectionContent)
+
+    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("listCollections calls REST service properly", {
+test_that("getProject delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
 
-    serverResponse <- NULL
-    expectedURL    <- paste0(arv$getHostName(), "collections")
+    arv$getCollection(projectUUID)
+
+    expect_that(fakeREST$getResourceCallCount, equals(1))
+}) 
 
-    httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
-    arv$setHttpClient(httpRequest)
-    arv$setHttpParser(FakeHttpParser$new())
+test_that("listProjects delegates operation to RESTService class", {
+
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
 
     arv$listCollections()
 
-    expect_that(httpRequest$URLIsProperlyConfigured, is_true())
-    expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
-    expect_that(httpRequest$numberOfGETRequests, equals(1))
+    expect_that(fakeREST$listResourcesCallCount, equals(1))
 }) 
 
-test_that("listCollections parses server response", {
+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)
 
-    httpParser <- FakeHttpParser$new()
-    arv$setHttpParser(httpParser)
-    arv$setHttpClient(FakeHttpRequest$new())
+    arv$listProjects(list(list("name", "like", "MyProject")))
 
-    arv$listCollections()
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
+}) 
+
+test_that("listAllProjects delegates operation to RESTService class", {
+
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
 
-    expect_that(httpParser$parserCallCount, equals(1))
+    arv$listAllProjects()
+
+    expect_that(fakeREST$fetchAllItemsCallCount, equals(1))
 }) 
 
-test_that("listCollections raises exception if response contains errors field", {
+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")
-    
-    serverResponse <- list(errors = 404)
-    expectedURL <- NULL
-    arv$setHttpClient(FakeHttpRequest$new(expectedURL, serverResponse))
-    arv$setHttpParser(FakeHttpParser$new())
+    arv$setRESTService(fakeREST)
 
-    expect_that(arv$listCollections(), 
-                throws_error(404))
+    arv$listAllProjects(list(list("name", "like", "MyProject")))
+
+    expect_that(fakeREST$filtersAreConfiguredCorrectly, is_true())
 }) 
 
-test_that("deleteCollection calls REST service properly", {
+test_that("deleteProject delegates operation to RESTService class", {
 
     arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
 
-    serverResponse <- NULL
-    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    expectedURL    <- paste0(arv$getHostName(), "collections/", collectionUUID)
+    arv$deleteCollection(projectUUID)
 
-    httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
-    arv$setHttpClient(httpRequest)
-    arv$setHttpParser(FakeHttpParser$new())
+    expect_that(fakeREST$deleteResourceCallCount, equals(1))
+}) 
 
-    arv$deleteCollection(collectionUUID)
+test_that("updateProject delegates operation to RESTService class", {
+
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    newProjectContent <- list(newName = "Brand new shiny name")
+    projectUUID <- "aaaaa-j7d0g-ccccccccccccccc"
 
-    expect_that(httpRequest$URLIsProperlyConfigured, is_true())
-    expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
-    expect_that(httpRequest$numberOfDELETERequests, equals(1))
+    arv$updateCollection(projectUUID, newProjectContent)
+
+    expect_that(fakeREST$updateResourceCallCount, equals(1))
 }) 
 
-test_that("deleteCollection parses server response", {
+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)
 
-    httpParser <- FakeHttpParser$new()
-    arv$setHttpParser(httpParser)
-    arv$setHttpClient(FakeHttpRequest$new())
+    arv$updateProject(projectUUID,
+                      list(name = "MyProject", desc = "No description"))
 
-    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    arv$deleteCollection(collectionUUID)
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
+}) 
+
+test_that("createProject delegates operation to RESTService class", {
+
+    arv <- Arvados$new("token", "hostName")
+    fakeREST <- FakeRESTService$new()
+    arv$setRESTService(fakeREST)
+    projectContent <- list(newName = "Brand new shiny name")
 
-    expect_that(httpParser$parserCallCount, equals(1))
+    arv$createCollection(projectContent)
+
+    expect_that(fakeREST$createResourceCallCount, equals(1))
 }) 
 
-test_that("getCollection raises exception if response contains errors field", {
+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")
-    
-    serverResponse <- list(errors = 404)
-    arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
-    arv$setHttpParser(FakeHttpParser$new())
+    arv$setRESTService(fakeREST)
 
-    collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
-    
-    expect_that(arv$deleteCollection(collectionUUID), 
-                throws_error(404))
+    arv$createProject(list(name = "MyProject", desc = "No description"))
+
+    expect_that(fakeREST$bodyIsConfiguredCorrectly, is_true())
 })