Skip to content

Commit 490c1f8

Browse files
committed
added tests
1 parent c130e19 commit 490c1f8

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#-------------------------------------------------------------------------------
2+
# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved.
3+
# Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved.
4+
#
5+
# This program and the accompanying materials
6+
# are made available under the terms of the GNU Public License v3.0.
7+
#
8+
# You should have received a copy of the GNU General Public License
9+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
10+
#-------------------------------------------------------------------------------
11+
12+
#
13+
# Set up
14+
#
15+
16+
# context("colnamesDS::smk::setup")
17+
18+
#
19+
# Tests
20+
#
21+
22+
# context("colnamesDS::smk::data.frame")
23+
test_that("simple colnamesDS, data.frame", {
24+
input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0))
25+
26+
res <- colnamesDS("input")
27+
28+
expect_equal(class(res), "character")
29+
expect_length(res, 2)
30+
expect_true("v1" %in% res)
31+
expect_true("v2" %in% res)
32+
})
33+
34+
# context("colnamesDS::smk::data.matrix")
35+
test_that("simple colnamesDS, data.matrix", {
36+
input <- data.matrix(data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)))
37+
38+
res <- colnamesDS("input")
39+
40+
expect_equal(class(res), "character")
41+
expect_length(res, 2)
42+
expect_true("v1" %in% res)
43+
expect_true("v2" %in% res)
44+
})
45+
46+
test_that("colnamesDS throws error when object does not exist", {
47+
expect_error(
48+
colnamesDS("nonexistent_object"),
49+
regexp = "does not exist"
50+
)
51+
})
52+
53+
test_that("colnamesDS throws error when object is not data.frame or matrix", {
54+
bad_input <- list(a = 1:3, b = 4:6)
55+
expect_error(
56+
colnamesDS("bad_input"),
57+
regexp = "must be of type data.frame or matrix"
58+
)
59+
})
60+
61+
#
62+
# Done
63+
#
64+
65+
# context("colnamesDS::smk::shutdown")
66+
67+
# context("colnamesDS::smk::done")

tests/testthat/test-smk-utils.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
#-------------------------------------------------------------------------------
3+
# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved.
4+
#
5+
# This program and the accompanying materials
6+
# are made available under the terms of the GNU Public License v3.0.
7+
#
8+
# You should have received a copy of the GNU General Public License
9+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
10+
#-------------------------------------------------------------------------------
11+
12+
#
13+
# Set up
14+
#
15+
16+
## When .loadServersideObject is called, the actual data exists two levels below the function,
17+
## i.e. data in global env --> ds function --> .loadServersideObject. We recreate this in
18+
## the test environment with a wrapper function.
19+
.dsFunctionWrapper <- function(x) {
20+
.loadServersideObject(x)
21+
}
22+
23+
# context("utils::smk::setup")
24+
test_that(".loadServersideObject() returns existing object", {
25+
test_df <- data.frame(a = 1:3)
26+
result <- .dsFunctionWrapper("test_df")
27+
expect_identical(result, test_df)
28+
})
29+
30+
test_that(".loadServersideObject() throws error for missing object", {
31+
expect_error(
32+
.dsFunctionWrapper("test_df"),
33+
regexp = "does not exist"
34+
)
35+
})
36+
37+
test_that(".checkClass() passes for correct class", {
38+
df <- data.frame(a = 1)
39+
expect_invisible(
40+
.checkClass(df, "df", c("data.frame", "matrix"))
41+
)
42+
})
43+
44+
test_that(".checkClass() throws informative error for wrong class", {
45+
x <- list(a = 1)
46+
expect_error(
47+
.checkClass(x, "x", c("data.frame", "matrix")),
48+
regexp = "must be of type data.frame or matrix"
49+
)
50+
})
51+
52+
# context("utils::smk::shutdown")
53+
# context("utils::smk::done")

0 commit comments

Comments
 (0)