Merge branch '18094-update-uuid-endpoint-removal' into main. Closes #18094
[arvados.git] / sdk / R / tests / testthat / test-Subcollection.R
index 45d0b02a17d224ea0ffea936f51184bb3746c72e..a95247f77c7d457d9f368ef321a027a980652e85 100644 (file)
@@ -1,8 +1,12 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
 source("fakes/FakeRESTService.R")
 
 context("Subcollection")
 
-test_that("getRelativePath returns relative path properly", {
+test_that("getRelativePath returns path relative to the tree root", {
 
     animal <- Subcollection$new("animal")
 
@@ -11,9 +15,9 @@ test_that("getRelativePath returns relative path properly", {
 
     expect_that(animal$getRelativePath(), equals("animal"))
     expect_that(fish$getRelativePath(), equals("animal/fish"))
-}) 
+})
 
-test_that(paste("getFileListing by default returns path of all files",
+test_that(paste("getFileListing by default returns sorted path of all files",
                 "relative to the current subcollection"), {
 
     animal   <- Subcollection$new("animal")
@@ -26,15 +30,17 @@ test_that(paste("getFileListing by default returns path of all files",
     fish$add(blueFish)
 
     result <- animal$getFileListing()
-    expectedResult <- c("animal/fish/shark", "animal/fish/blueFish")
+
+    #expect sorted array
+    expectedResult <- c("animal/fish/blueFish", "animal/fish/shark")
 
     resultsMatch <- length(expectedResult) == length(result) &&
                     all(expectedResult == result)
 
-    expect_that(resultsMatch, is_true())
-}) 
+    expect_true(resultsMatch)
+})
 
-test_that(paste("getFileListing returns names of all direct children",
+test_that(paste("getFileListing returns sorted names of all direct children",
                 "if fullPath is set to FALSE"), {
 
     animal <- Subcollection$new("animal")
@@ -47,13 +53,13 @@ test_that(paste("getFileListing returns names of all direct children",
     fish$add(shark)
 
     result <- animal$getFileListing(fullPath = FALSE)
-    expectedResult <- c("fish", "dog")
+    expectedResult <- c("dog", "fish")
 
     resultsMatch <- length(expectedResult) == length(result) &&
                     all(expectedResult == result)
 
-    expect_that(resultsMatch, is_true())
-}) 
+    expect_true(resultsMatch)
+})
 
 test_that("add adds content to inside collection tree", {
 
@@ -67,11 +73,20 @@ test_that("add adds content to inside collection tree", {
     animalContainsFish <- animal$get("fish")$getName() == fish$getName()
     animalContainsDog  <- animal$get("dog")$getName()  == dog$getName()
 
-    expect_that(animalContainsFish, is_true())
-    expect_that(animalContainsDog, is_true())
-}) 
+    expect_true(animalContainsFish)
+    expect_true(animalContainsDog)
+})
+
+test_that("add raises exception if content name is empty string", {
+
+    animal     <- Subcollection$new("animal")
+    rootFolder <- Subcollection$new("")
+
+    expect_that(animal$add(rootFolder),
+                throws_error("Content has invalid name.", fixed = TRUE))
+})
 
-test_that(paste("add raises exception if ArvadosFile/Subcollection", 
+test_that(paste("add raises exception if ArvadosFile/Subcollection",
                 "with same name already exists in the subcollection"), {
 
     animal     <- Subcollection$new("animal")
@@ -87,9 +102,9 @@ test_that(paste("add raises exception if ArvadosFile/Subcollection",
     expect_that(animal$add(thirdFish),
                 throws_error(paste("Subcollection already contains ArvadosFile or",
                                    "Subcollection with same name."), fixed = TRUE))
-}) 
+})
 
-test_that(paste("add raises exception if passed argument is", 
+test_that(paste("add raises exception if passed argument is",
                 "not ArvadosFile or Subcollection"), {
 
     animal <- Subcollection$new("animal")
@@ -98,17 +113,15 @@ test_that(paste("add raises exception if passed argument is",
     expect_that(animal$add(number),
                 throws_error(paste("Expected AravodsFile or Subcollection object,",
                                    "got (numeric)."), fixed = TRUE))
-}) 
+})
 
-test_that(paste("add post content to a REST service", 
+test_that(paste("add post content to a REST service",
                 "if subcollection belongs to a collection"), {
-    
-    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")
@@ -118,7 +131,7 @@ test_that(paste("add post content to a REST service",
     animal$add(dog)
 
     expect_that(fakeREST$createCallCount, equals(1))
-}) 
+})
 
 test_that("remove removes content from subcollection", {
 
@@ -130,10 +143,10 @@ test_that("remove removes content from subcollection", {
 
     returnValueAfterRemovalIsNull <- is.null(animal$get("fish"))
 
-    expect_that(returnValueAfterRemovalIsNull, is_true())
-}) 
+    expect_true(returnValueAfterRemovalIsNull)
+})
 
-test_that(paste("remove raises exception", 
+test_that(paste("remove raises exception",
                 "if content to remove doesn't exist in the subcollection"), {
 
     animal <- Subcollection$new("animal")
@@ -141,7 +154,7 @@ test_that(paste("remove raises exception",
     expect_that(animal$remove("fish"),
                 throws_error(paste("Subcollection doesn't contains ArvadosFile",
                                    "or Subcollection with specified name.")))
-}) 
+})
 
 test_that("remove raises exception if passed argument is not character vector", {
 
@@ -151,18 +164,15 @@ test_that("remove raises exception if passed argument is not character vector",
     expect_that(animal$remove(number),
                 throws_error(paste("Expected character,",
                                    "got (numeric)."), fixed = TRUE))
-}) 
+})
 
-test_that(paste("remove removes content from REST service", 
+test_that(paste("remove removes content from REST service",
                 "if subcollection belongs to a collection"), {
-    
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
 
     collectionContent <- c("animal", "animal/fish", "animal/dog")
-
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     animal <- collection$get("animal")
@@ -170,9 +180,9 @@ test_that(paste("remove removes content from REST service",
     animal$remove("fish")
 
     expect_that(fakeREST$deleteCallCount, equals(1))
-}) 
+})
 
-test_that(paste("get returns ArvadosFile or Subcollection", 
+test_that(paste("get returns ArvadosFile or Subcollection",
                 "if file or folder with given name exists"), {
 
     animal <- Subcollection$new("animal")
@@ -188,14 +198,14 @@ test_that(paste("get returns ArvadosFile or Subcollection",
     returnedFishIsSubcollection <- "Subcollection" %in% class(returnedFish)
     returnedDogIsArvadosFile    <- "ArvadosFile"   %in% class(returnedDog)
 
-    expect_that(returnedFishIsSubcollection, is_true())
+    expect_true(returnedFishIsSubcollection)
     expect_that(returnedFish$getName(), equals("fish"))
 
-    expect_that(returnedDogIsArvadosFile, is_true())
+    expect_true(returnedDogIsArvadosFile)
     expect_that(returnedDog$getName(), equals("dog"))
-}) 
+})
 
-test_that(paste("get returns NULL if file or folder", 
+test_that(paste("get returns NULL if file or folder",
                 "with given name doesn't exists"), {
 
     animal <- Subcollection$new("animal")
@@ -205,8 +215,8 @@ test_that(paste("get returns NULL if file or folder",
 
     returnedDogIsNull <- is.null(animal$get("dog"))
 
-    expect_that(returnedDogIsNull, is_true())
-}) 
+    expect_true(returnedDogIsNull)
+})
 
 test_that("getFirst returns first child in the subcollection", {
 
@@ -216,7 +226,7 @@ test_that("getFirst returns first child in the subcollection", {
     animal$add(fish)
 
     expect_that(animal$getFirst()$getName(), equals("fish"))
-}) 
+})
 
 test_that("getFirst returns NULL if subcollection contains no children", {
 
@@ -224,8 +234,8 @@ test_that("getFirst returns NULL if subcollection contains no children", {
 
     returnedElementIsNull <- is.null(animal$getFirst())
 
-    expect_that(returnedElementIsNull, is_true())
-}) 
+    expect_true(returnedElementIsNull)
+})
 
 test_that(paste("setCollection by default sets collection",
                 "filed of subcollection and all its children"), {
@@ -238,7 +248,7 @@ test_that(paste("setCollection by default sets collection",
 
     expect_that(animal$getCollection(), equals("myCollection"))
     expect_that(fish$getCollection(), equals("myCollection"))
-}) 
+})
 
 test_that(paste("setCollection sets collection filed of subcollection only",
                 "if parameter setRecursively is set to FALSE"), {
@@ -251,8 +261,8 @@ test_that(paste("setCollection sets collection filed of subcollection only",
     fishCollectionIsNull <- is.null(fish$getCollection())
 
     expect_that(animal$getCollection(), equals("myCollection"))
-    expect_that(fishCollectionIsNull, is_true())
-}) 
+    expect_true(fishCollectionIsNull)
+})
 
 test_that(paste("move raises exception if subcollection",
                 "doesn't belong to any collection"), {
@@ -261,44 +271,57 @@ test_that(paste("move raises exception if subcollection",
 
     expect_that(animal$move("new/location"),
                 throws_error("Subcollection doesn't belong to any collection"))
-}) 
+})
 
-test_that(paste("move raises exception if newLocationInCollection",
-                "parameter is invalid"), {
+test_that("move raises exception if new location contains content with the same name", {
+
+    collectionContent <- c("animal",
+                           "animal/fish",
+                           "animal/dog",
+                           "animal/fish/shark",
+                           "fish")
+    fakeREST <- FakeRESTService$new(collectionContent)
 
     api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
+    api$setRESTService(fakeREST)
+    collection <- Collection$new(api, "myUUID")
+    fish <- collection$get("animal/fish")
+
+    expect_that(fish$move("fish"),
+                throws_error("Destination already contains content with same name."))
+
+})
+
+test_that(paste("move raises exception if newLocationInCollection",
+                "parameter is invalid"), {
 
     collectionContent <- c("animal",
                            "animal/fish",
                            "animal/dog",
                            "animal/fish/shark",
                            "ball")
-
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
 
     collection <- Collection$new(api, "myUUID")
-    dog <- collection$get("animal/dog")
+    fish <- collection$get("animal/fish")
 
-    expect_that(dog$move("objects/dog"),
-                throws_error("Unable to get destination subcollection"))
-}) 
+    expect_that(fish$move("objects/dog"),
+                throws_error("Unable to get destination subcollection."))
+})
 
 test_that("move moves subcollection inside collection tree", {
 
-    api <- Arvados$new("myToken", "myHostName")
-    api$setHttpClient(FakeHttpRequest$new())
-    api$setHttpParser(FakeHttpParser$new())
-
     collectionContent <- c("animal",
                            "animal/fish",
                            "animal/dog",
                            "animal/fish/shark",
                            "ball")
-
     fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
     collection <- Collection$new(api, "myUUID")
     fish <- collection$get("animal/fish")
@@ -307,9 +330,9 @@ test_that("move moves subcollection inside collection tree", {
     fishIsNullOnOldLocation <- is.null(collection$get("animal/fish"))
     fishExistsOnNewLocation <- !is.null(collection$get("fish"))
 
-    expect_that(fishIsNullOnOldLocation, is_true())
-    expect_that(fishExistsOnNewLocation, is_true())
-}) 
+    expect_true(fishIsNullOnOldLocation)
+    expect_true(fishExistsOnNewLocation)
+})
 
 test_that(paste("getSizeInBytes returns zero if subcollection",
                 "is not part of a collection"), {
@@ -317,19 +340,16 @@ test_that(paste("getSizeInBytes returns zero if subcollection",
     animal <- Subcollection$new("animal")
 
     expect_that(animal$getSizeInBytes(), equals(0))
-}) 
+})
 
 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")
     animal <- collection$get("animal")
@@ -337,4 +357,88 @@ test_that(paste("getSizeInBytes delegates size calculation",
     resourceSize <- animal$getSizeInBytes()
 
     expect_that(resourceSize, equals(100))
-}) 
+})
+
+#########################
+test_that(paste("copy raises exception if subcollection",
+                "doesn't belong to any collection"), {
+
+    animal <- Subcollection$new("animal")
+
+    expect_that(animal$copy("new/location"),
+                throws_error("Subcollection doesn't belong to any collection."))
+})
+
+test_that("copy raises exception if new location contains content with the same name", {
+
+    collectionContent <- c("animal",
+                           "animal/fish",
+                           "animal/dog",
+                           "animal/fish/shark",
+                           "fish")
+    fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
+    api$setRESTService(fakeREST)
+    collection <- Collection$new(api, "myUUID")
+    fish <- collection$get("animal/fish")
+
+    expect_that(fish$copy("fish"),
+                throws_error("Destination already contains content with same name."))
+
+})
+
+test_that(paste("copy raises exception if location parameter is invalid"), {
+
+    collectionContent <- c("animal",
+                           "animal/fish",
+                           "animal/dog",
+                           "animal/fish/shark",
+                           "ball")
+    fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
+    api$setRESTService(fakeREST)
+
+    collection <- Collection$new(api, "myUUID")
+    fish <- collection$get("animal/fish")
+
+    expect_that(fish$copy("objects/dog"),
+                throws_error("Unable to get destination subcollection."))
+})
+
+test_that("copy copies subcollection inside collection tree", {
+
+    collectionContent <- c("animal",
+                           "animal/fish",
+                           "animal/dog",
+                           "animal/fish/shark",
+                           "ball")
+    fakeREST <- FakeRESTService$new(collectionContent)
+
+    api <- Arvados$new("myToken", "myHostName")
+    api$setRESTService(fakeREST)
+    collection <- Collection$new(api, "myUUID")
+    fish <- collection$get("animal/fish")
+
+    fish$copy("fish")
+    fishExistsOnOldLocation <- !is.null(collection$get("animal/fish"))
+    fishExistsOnNewLocation <- !is.null(collection$get("fish"))
+
+    expect_true(fishExistsOnOldLocation)
+    expect_true(fishExistsOnNewLocation)
+})
+
+test_that("duplicate performs deep cloning of Subcollection", {
+    foo <- ArvadosFile$new("foo")
+    bar <- ArvadosFile$new("bar")
+    sub <- Subcollection$new("qux")
+    sub$add(foo)
+    sub$add(bar)
+
+    newSub1 <- sub$duplicate()
+    newSub2 <- sub$duplicate("quux")
+
+    expect_that(newSub1$getFileListing(), equals(sub$getFileListing()))
+    expect_that(sort(newSub2$getFileListing()), equals(c("quux/bar", "quux/foo")))
+})