Merge branch 'master' into 7478-anm-spot-instances
[arvados.git] / sdk / R / tests / testthat / test-ArvadosFile.R
index fbf7acbaa245f86b7b5480f565c6fdfd332c1279..fb14888aab91b982d88dbdddca0be9589f757fb8 100644 (file)
@@ -1,7 +1,16 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
 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,14 +45,11 @@ 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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -75,13 +81,11 @@ 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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -96,14 +100,11 @@ 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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -114,36 +115,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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -161,13 +156,11 @@ 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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -191,13 +184,11 @@ 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 <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -219,9 +210,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,6 +218,8 @@ test_that(paste("move raises exception if newLocationInCollection",
                            "ball")
 
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
 
     collection <- Collection$new(api, "myUUID")
@@ -241,9 +231,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,6 +239,8 @@ test_that("move raises exception if new location contains content with the same
                            "dog")
 
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     dog <- collection$get("animal/dog")
@@ -263,9 +252,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,6 +260,8 @@ test_that("move moves arvados file inside collection tree", {
                            "ball")
 
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     dog <- collection$get("animal/dog")
@@ -284,4 +272,4 @@ test_that("move moves arvados file inside collection tree", {
 
     expect_that(dogIsNullOnOldLocation, is_true())
     expect_that(dogExistsOnNewLocation, is_true())
-}) 
+})