Skip to content

Commit 7e80df4

Browse files
committed
Merge branch 'v7.0-dev-colnames' of github.com:datashield/dsBaseClient into v7.0-dev-colnames
2 parents 1613471 + 50e1caf commit 7e80df4

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Suggests:
8181
DescTools,
8282
DSOpal,
8383
DSMolgenisArmadillo,
84-
DSLite
84+
DSLite,
85+
mockery
8586
RoxygenNote: 7.3.3
8687
Encoding: UTF-8
8788
Language: en-GB
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
### Testing instructions
1414
- [ ] Writen client-side unit tests for unhappy flow
1515
- [ ] Run `devtools::test(filter = "smk-|disc|arg")` and check it passes
16-
- [ ] Run `devtools::check(args = '--no-tests')` and check it passes (we run tests separately to skip performance checks)
16+
- [ ] Run `devtools::check(args = '--no-tests')` and check it passes
1717
- [ ] Run `devtools::build()` and check it builds without errors
18+
- [ ] Check that the continuous integration checks pass on the pull request branch. Note that the performance test relating to your function may fail, as failure is also triggered by a dramatic improval in performance!
1819

1920
## Instructions & checklist for PR reviewers
2021
- [ ] Checkout this branch as well as the corresponding branch of dsBase
@@ -23,3 +24,4 @@
2324
- [ ] Run `devtools::test(filter = "smk-|disc|arg")` and check it passes
2425
- [ ] Run `devtools::check(args = '--no-tests')` and check it passes (we run tests separately to skip performance checks)
2526
- [ ] Run `devtools::build()` and check it builds without errors
27+
- [ ] Check that the continuous integration checks pass on the pull request branch (see above note on performance checks)

tests/testthat/test-smk-utils.R

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
library(testthat)
2+
library(DSI)
3+
library(cli)
4+
5+
library(testthat)
6+
library(mockery)
7+
library(cli)
8+
library(testthat)
9+
library(mockery)
10+
library(cli)
11+
12+
if (!isClass("DSConnection")) {
13+
setClass("DSConnection", contains = "VIRTUAL")
14+
}
15+
16+
setClass("MockDSConnection", contains = "DSConnection")
17+
mock_ds_conn <- new("MockDSConnection")
18+
19+
test_that(".get_datasources retrieves connections when input is NULL", {
20+
21+
mock_connections <- list(
22+
server1 = mock_ds_conn,
23+
server2 = mock_ds_conn
24+
)
25+
26+
stub(.get_datasources, "datashield.connections_find", mock_connections)
27+
28+
result <- .get_datasources(NULL)
29+
30+
expect_type(result, "list")
31+
expect_length(result, 2)
32+
expect_named(result, c("server1", "server2"))
33+
expect_true(is(result$server1, "DSConnection"))
34+
})
35+
36+
test_that(".get_datasources returns input when provided", {
37+
38+
input_datasources <- list(A = "connA", B = "connB")
39+
40+
result <- with_mocked_bindings(
41+
datashield.connections_find = function() stop("Should not be called!"),
42+
.get_datasources(input_datasources),
43+
.package = "dsBaseClient"
44+
)
45+
46+
expect_equal(result, input_datasources)
47+
})
48+
49+
test_that(".verify_datasources passes with valid DSConnection list", {
50+
51+
valid_datasources <- list(
52+
conn_list1 = mock_ds_conn,
53+
conn_list2 = mock_ds_conn
54+
)
55+
56+
expect_no_error(.verify_datasources(valid_datasources))
57+
expect_null(.verify_datasources(valid_datasources))
58+
})
59+
60+
test_that(".verify_datasources aborts with invalid object types", {
61+
62+
invalid_datasources <- list(
63+
conn_list1 = mock_ds_conn,
64+
conn_list2 = "not_a_connection"
65+
)
66+
67+
expect_error(
68+
.verify_datasources(invalid_datasources)
69+
)
70+
})
71+
72+
test_that(".set_datasources works with valid input", {
73+
74+
input_datasources <- list(
75+
mock_ds_conn
76+
)
77+
78+
result <- with_mocked_bindings(
79+
.get_datasources = function(d) d,
80+
.verify_datasources = function(d) {},
81+
.set_datasources(input_datasources),
82+
.package = "dsBaseClient"
83+
)
84+
85+
expect_equal(result, input_datasources)
86+
})
87+
88+
test_that(".set_datasources calls .get_datasources and .verify_datasources", {
89+
90+
get_called <- FALSE
91+
verify_called <- FALSE
92+
93+
mock_get <- function(d) {
94+
get_called <<- TRUE
95+
return(list(list(mock_ds_conn)))
96+
}
97+
98+
mock_verify <- function(d) {
99+
verify_called <<- TRUE
100+
}
101+
102+
with_mocked_bindings(
103+
.get_datasources = mock_get,
104+
.verify_datasources = mock_verify,
105+
.set_datasources(NULL),
106+
.package = "dsBaseClient"
107+
)
108+
109+
expect_true(get_called)
110+
expect_true(verify_called)
111+
})
112+
113+
test_that(".set_datasources aborts if verification fails", {
114+
115+
expect_error(
116+
with_mocked_bindings(
117+
.get_datasources = function(d) list(list("bad_conn")),
118+
.verify_datasources = function(d) cli::cli_abort("Verification failed mock"),
119+
.set_datasources(NULL),
120+
.package = "dsBaseClient"
121+
)
122+
)
123+
})
124+
125+
test_that(".check_df_name_provided passes when df is not NULL", {
126+
127+
expect_no_error(.check_df_name_provided("D"))
128+
expect_null(.check_df_name_provided("D"))
129+
130+
expect_no_error(.check_df_name_provided(data.frame(a=1)))
131+
})
132+
133+
test_that(".check_df_name_provided aborts when df is NULL", {
134+
135+
expect_error(
136+
.check_df_name_provided(NULL)
137+
)
138+
})
139+

0 commit comments

Comments
 (0)