Merge branch 'master' of git.curoverse.com:arvados into 13076-r-autogen-api
[arvados.git] / sdk / R / tests / testthat / test-ArvadosFile.R
index fbf7acbaa245f86b7b5480f565c6fdfd332c1279..67ed0f6240938562ff5cb7894201205926c3c494 100644 (file)
@@ -2,6 +2,11 @@ source("fakes/FakeRESTService.R")
 
 context("ArvadosFile")
 
+test_that("constructor raises error if  file name is empty string", {
+
+    expect_that(ArvadosFile$new(""), throws_error("Invalid name."))
+}) 
+
 test_that("getFileListing always returns file name", {
 
     dog <- ArvadosFile$new("dog")
@@ -36,16 +41,12 @@ test_that(paste("getSizeInBytes returns zero if arvadosFile",
 test_that(paste("getSizeInBytes delegates size calculation",
                 "to REST service class"), {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
-
     collectionContent <- c("animal", "animal/fish")
     returnSize <- 100
-
     fakeREST <- FakeRESTService$new(collectionContent, returnSize)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
 
     resourceSize <- fish$getSizeInBytes()
@@ -75,15 +76,12 @@ test_that("read raises exception if file doesn't belong to a collection", {
 
 test_that("read raises exception offset or length is negative number", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal", "animal/fish")
-
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
 
     expect_that(fish$read(contentType = "text", offset = -1),
@@ -96,16 +94,12 @@ test_that("read raises exception offset or length is negative number", {
 
 test_that("read delegates reading operation to REST service class", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
-
     collectionContent <- c("animal", "animal/fish")
     readContent <- "my file"
-
     fakeREST <- FakeRESTService$new(collectionContent, readContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
     
     fileContent <- fish$read("text")
@@ -114,38 +108,30 @@ test_that("read delegates reading operation to REST service class", {
     expect_that(fakeREST$readCallCount, equals(1))
 }) 
 
-test_that(paste("connect returns textConnection opened",
-                "in read mode when 'r' is passed as argument"), {
-
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
+test_that(paste("connection delegates connection creation ro RESTService class",
+                "which returns curl connection opened in read mode when", 
+                "'r' of 'rb' is passed as argument"), {
 
     collectionContent <- c("animal", "animal/fish")
-    readContent <- "file content"
+    fakeREST <- FakeRESTService$new(collectionContent)
 
-    fakeREST <- FakeRESTService$new(collectionContent, readContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
 
     connection <- fish$connection("r")
 
-    expect_that(readLines(connection), equals("file content"))
+    expect_that(fakeREST$getConnectionCallCount, equals(1))
 }) 
 
-test_that(paste("connect returns textConnection opened",
+test_that(paste("connection returns textConnection opened",
                 "in write mode when 'w' is passed as argument"), {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
-
     collectionContent <- c("animal", "animal/fish")
-
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
 
     connection <- fish$connection("w")
@@ -161,15 +147,12 @@ test_that(paste("connect returns textConnection opened",
 
 test_that("flush sends data stored in a connection to a REST server", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal", "animal/fish")
-
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
 
     connection <- fish$connection("w")
@@ -191,15 +174,12 @@ test_that("write raises exception if file doesn't belong to a collection", {
 
 test_that("write delegates writing operation to REST service class", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal", "animal/fish")
-
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     fish <- collection$get("animal/fish")
     
     fileContent <- fish$write("new file content")
@@ -219,9 +199,6 @@ test_that(paste("move raises exception if arvados file",
 test_that(paste("move raises exception if newLocationInCollection",
                 "parameter is invalid"), {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal",
                            "animal/fish",
@@ -230,9 +207,9 @@ test_that(paste("move raises exception if newLocationInCollection",
                            "ball")
 
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
 
-    collection <- Collection$new(api, "myUUID")
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     dog <- collection$get("animal/dog")
 
     expect_that(dog$move("objects/dog"),
@@ -241,9 +218,6 @@ test_that(paste("move raises exception if newLocationInCollection",
 
 test_that("move raises exception if new location contains content with the same name", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal",
                            "animal/fish",
@@ -252,8 +226,9 @@ test_that("move raises exception if new location contains content with the same
                            "dog")
 
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     dog <- collection$get("animal/dog")
 
     expect_that(dog$move("dog"),
@@ -263,9 +238,6 @@ test_that("move raises exception if new location contains content with the same
 
 test_that("move moves arvados file inside collection tree", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal",
                            "animal/fish",
@@ -274,8 +246,9 @@ test_that("move moves arvados file inside collection tree", {
                            "ball")
 
     fakeREST <- FakeRESTService$new(collectionContent)
-    api$setRESTService(fakeREST)
-    collection <- Collection$new(api, "myUUID")
+
+    collection <- Collection$new("fakeUUID")
+    collection$setRESTService(fakeREST)
     dog <- collection$get("animal/dog")
 
     dog$move("dog")