Tweak arvbox build order, API server depends on arvados-server
[arvados.git] / sdk / R / tests / testthat / test-util.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 context("Utility function")
6
7 test_that("listAll always returns all resource items from server", {
8
9     serverResponseLimit <- 3
10     itemsAvailable <- 8
11     items <- list("collection1", "collection2", "collection3", "collection4",
12                   "collection5", "collection6", "collection7", "collection8")
13
14     testFunction <- function(offset, ...)
15     {
16         response <- list()
17         response$items_available <- itemsAvailable
18
19         maxIndex <- offset + serverResponseLimit
20         lastElementIndex <- if(maxIndex < itemsAvailable) maxIndex else itemsAvailable
21
22         response$items <- items[(offset + 1):lastElementIndex]
23         response
24     }
25
26     result <- listAll(testFunction)
27
28     expect_that(length(result), equals(8))
29 })
30
31 test_that("trimFromStart trims string correctly if string starts with trimCharacters", {
32
33     sample <- "./something/random"
34     trimCharacters <- "./something/"
35
36     result <- trimFromStart(sample, trimCharacters)
37
38     expect_that(result, equals("random"))
39 })
40
41 test_that("trimFromStart returns original string if string doesn't starts with trimCharacters", {
42
43     sample <- "./something/random"
44     trimCharacters <- "./nothing/"
45
46     result <- trimFromStart(sample, trimCharacters)
47
48     expect_that(result, equals("./something/random"))
49 })
50
51 test_that("trimFromEnd trims string correctly if string ends with trimCharacters", {
52
53     sample <- "./something/random"
54     trimCharacters <- "/random"
55
56     result <- trimFromEnd(sample, trimCharacters)
57
58     expect_that(result, equals("./something"))
59 })
60
61 test_that("trimFromEnd returns original string if string doesn't end with trimCharacters", {
62
63     sample <- "./something/random"
64     trimCharacters <- "specific"
65
66     result <- trimFromStart(sample, trimCharacters)
67
68     expect_that(result, equals("./something/random"))
69 })
70
71 test_that("RListToPythonList converts nested R list to char representation of Python list", {
72
73     sample <- list("insert", list("random", list("text")), list("here"))
74
75     result              <- RListToPythonList(sample)
76     resultWithSeparator <- RListToPythonList(sample, separator = ",+")
77
78     expect_that(result, equals("[\"insert\", [\"random\", \"text\"], \"here\"]"))
79     expect_that(resultWithSeparator,
80                 equals("[\"insert\",+[\"random\",+\"text\"],+\"here\"]"))
81 })
82
83 test_that("appendToStartIfNotExist appends characters to beginning of a string", {
84
85     sample <- "New Year"
86     charactersToAppend <- "Happy "
87
88     result <- appendToStartIfNotExist(sample, charactersToAppend)
89
90     expect_that(result, equals("Happy New Year"))
91 })
92
93 test_that(paste("appendToStartIfNotExist returns original string if string",
94                 "doesn't start with specified characters"), {
95
96     sample <- "Happy New Year"
97     charactersToAppend <- "Happy"
98
99     result <- appendToStartIfNotExist(sample, charactersToAppend)
100
101     expect_that(result, equals("Happy New Year"))
102 })
103
104 test_that(paste("splitToPathAndName splits relative path to file/folder",
105                 "name and rest of the path"), {
106
107     relativePath <- "path/to/my/file.exe"
108
109     result <- splitToPathAndName( relativePath)
110
111     expect_that(result$name, equals("file.exe"))
112     expect_that(result$path, equals("path/to/my"))
113 })