2411: add copyright headers to our R files.
[arvados.git] / sdk / R / tests / testthat / test-CollectionTree.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 context("CollectionTree")
6
7 test_that("constructor creates file tree from character array properly", {
8
9     collection <- "myCollection"
10     characterArray <- c("animal", 
11                         "animal/dog",
12                         "boat")
13
14     collectionTree <- CollectionTree$new(characterArray, collection)
15
16     root   <- collectionTree$getTree()
17     animal <- collectionTree$getElement("animal")
18     dog    <- collectionTree$getElement("animal/dog")
19     boat   <- collectionTree$getElement("boat")
20
21     rootHasNoParent             <- is.null(root$getParent())
22     rootIsOfTypeSubcollection   <- "Subcollection" %in% class(root)
23     animalIsOfTypeSubcollection <- "Subcollection" %in% class(animal)
24     dogIsOfTypeArvadosFile      <- "ArvadosFile" %in% class(dog)
25     boatIsOfTypeArvadosFile     <- "ArvadosFile" %in% class(boat)
26     animalsParentIsRoot         <- animal$getParent()$getName() == root$getName()
27     animalContainsDog           <- animal$getFirst()$getName() == dog$getName()
28     dogsParentIsAnimal          <- dog$getParent()$getName() == animal$getName()
29     boatsParentIsRoot           <- boat$getParent()$getName() == root$getName()
30
31     allElementsBelongToSameCollection <- root$getCollection()   == "myCollection" &&
32                                          animal$getCollection() == "myCollection" &&
33                                          dog$getCollection()    == "myCollection" &&
34                                          boat$getCollection()   == "myCollection"
35
36     expect_that(root$getName(), equals(""))
37     expect_that(rootIsOfTypeSubcollection, is_true())
38     expect_that(rootHasNoParent, is_true())
39     expect_that(animalIsOfTypeSubcollection, is_true())
40     expect_that(animalsParentIsRoot, is_true())
41     expect_that(animalContainsDog, is_true())
42     expect_that(dogIsOfTypeArvadosFile, is_true())
43     expect_that(dogsParentIsAnimal, is_true())
44     expect_that(boatIsOfTypeArvadosFile, is_true())
45     expect_that(boatsParentIsRoot, is_true())
46     expect_that(allElementsBelongToSameCollection, is_true())
47 }) 
48
49 test_that("getElement returns element from tree if element exists on specified path", {
50
51     collection <- "myCollection"
52     characterArray <- c("animal", 
53                         "animal/dog",
54                         "boat")
55
56     collectionTree <- CollectionTree$new(characterArray, collection)
57
58     dog <- collectionTree$getElement("animal/dog")
59
60     expect_that(dog$getName(), equals("dog"))
61 }) 
62
63 test_that("getElement returns NULL from tree if element doesn't exists on specified path", {
64
65     collection <- "myCollection"
66     characterArray <- c("animal", 
67                         "animal/dog",
68                         "boat")
69
70     collectionTree <- CollectionTree$new(characterArray, collection)
71
72     fish <- collectionTree$getElement("animal/fish")
73     fishIsNULL <- is.null(fish)
74
75     expect_that(fishIsNULL, is_true())
76 }) 
77
78 test_that("getElement trims ./ from start of relativePath", {
79
80     collection <- "myCollection"
81     characterArray <- c("animal", 
82                         "animal/dog",
83                         "boat")
84
85     collectionTree <- CollectionTree$new(characterArray, collection)
86
87     dog <- collectionTree$getElement("animal/dog")
88     dogWithDotSlash <- collectionTree$getElement("./animal/dog")
89
90     expect_that(dogWithDotSlash$getName(), equals(dog$getName()))
91 }) 
92
93 test_that("getElement trims / from end of relativePath", {
94
95     collection <- "myCollection"
96     characterArray <- c("animal", 
97                         "animal/dog",
98                         "boat")
99
100     collectionTree <- CollectionTree$new(characterArray, collection)
101
102     animal <- collectionTree$getElement("animal")
103     animalWithSlash <- collectionTree$getElement("animal/")
104
105     expect_that(animalWithSlash$getName(), equals(animal$getName()))
106 })