/
test-disc-ds.table.R
134 lines (101 loc) · 5.09 KB
/
test-disc-ds.table.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#-------------------------------------------------------------------------------
# Copyright (c) 2018-2022 University of Newcastle upon Tyne. All rights reserved.
#
# This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
###reminder: how to actually run a test: devtools::test(filter="smk-ds.table$")
#
# Set up - Phase 1
#
context("ds.table::disc::setup::phase 1")
connect.all.datasets()
test_that("setup", {
ds_expect_variables(c("D"))
})
#
# Tests - Phase 1
#
context("ds.table::disc::tests::phase 1::table.assign=FALSE")
test_that("table disclosure table.assign=FALSE", {
ds.asFactor(input.var.name="D$FACTOR_CHARACTER", newobj.name="factorCharacter")
ds.asFactor(input.var.name="D$FACTOR_INTEGER", newobj.name="factorInteger")
myvectors <- c('factorCharacter', 'factorInteger')
ds.dataFrame(x=myvectors, newobj="tablesource")
table.res <- ds.table(rvar='tablesource$factorCharacter', cvar='tablesource$factorInteger', table.assign=FALSE, newobj="new_table1")
expect_length(table.res, 2)
expect_length(table.res$output.list, 9)
expect_length(table.res$validity.message, 4)
expect_equal(table.res$validity.message[1], "At least one study failed for reasons identified by 'error.messages':", fixed = TRUE)
expect_equal(table.res$validity.message[2], "Study1: No errors reported from this study", fixed = TRUE)
expect_equal(table.res$validity.message[3], "Study2: Failed: at least one cell has a non-zero count less than nfilter.tab i.e. 3", fixed = TRUE)
expect_equal(table.res$validity.message[4], "Study3: No errors reported from this study", fixed = TRUE)
})
context("ds.table::disc::tests::phase 1::table.assign=TRUE")
test_that("table disclosure table.assign=TRUE", {
ds.asFactor(input.var.name="D$FACTOR_CHARACTER", newobj.name="factorCharacter")
ds.asFactor(input.var.name="D$FACTOR_INTEGER", newobj.name="factorInteger")
myvectors <- c('factorCharacter', 'factorInteger')
ds.dataFrame(x=myvectors, newobj="tablesource")
table.res <- ds.table(rvar='tablesource$factorCharacter', cvar='tablesource$factorInteger', table.assign=TRUE, newobj="new_table2")
expect_length(table.res, 0)
})
context("ds.table::disc::test rvar is big numeric")
test_that("table rvar is big numeric", {
expect_error(ds.table(rvar="D$NUMERIC"), "There are some DataSHIELD errors, list them with datashield.errors()", fixed = TRUE)
res.errors <- DSI::datashield.errors()
expect_length(res.errors, 3)
expect_match(res.errors$study1, "* Error : FAILED: this variable has too many levels and may be disclosive. It exceeds the max number of levels allowed by nfilter.levels.max: that is 40. In this study this variable has 71 factor levels")
expect_match(res.errors$study2, "* Error : FAILED: this variable has too many levels and may be disclosive. It exceeds the max number of levels allowed by nfilter.levels.max: that is 40. In this study this variable has 71 factor levels")
expect_match(res.errors$study3, "* Error : FAILED: this variable has too many levels and may be disclosive. It exceeds the max number of levels allowed by nfilter.levels.max: that is 40. In this study this variable has 71 factor levels")
})
#
# Shutdown - Phase 1
#
context("ds.table::disc::shutdown::phase 1")
test_that("setup", {
ds_expect_variables(c("D", "factorCharacter", "factorInteger", "tablesource", "new_table2"))
})
disconnect.all.datasets()
#
# Set up - Phase 2
#
context("ds.table::disc::setup::phase 2")
connect.studies.dataset.cnsim(list("GENDER", "DIS_AMI"))
test_that("setup", {
ds_expect_variables(c("D"))
})
#
# Tests - Phase 2
#
context("ds.table::disc::tests::phase 2::table.assign=FALSE")
test_that("table disclosure, table.assign=FALSE", {
table.res <- ds.table(rvar='D$GENDER', cvar="D$DIS_AMI", table.assign=FALSE, newobj="new_table3")
expect_length(table.res, 2)
expect_length(table.res$validity.message, 1)
expect_equal(table.res$validity.message, "All studies failed for reasons identified below", fixed = TRUE)
expect_length(table.res$error.message, 3)
expect_equal(table.res$error.message$sim1, "Failed: at least one cell has a non-zero count less than nfilter.tab i.e. 3", fixed = TRUE)
expect_equal(table.res$error.message$sim2, "Failed: at least one cell has a non-zero count less than nfilter.tab i.e. 3", fixed = TRUE)
expect_equal(table.res$error.message$sim3, "Failed: at least one cell has a non-zero count less than nfilter.tab i.e. 3", fixed = TRUE)
})
context("ds.table::disc::tests::phase 2::table.assign=TRUE")
test_that("table disclosure, table.assign=TRUE", {
table.res <- ds.table(rvar='D$GENDER', cvar="D$DIS_AMI", table.assign=TRUE, newobj="new_table4")
expect_length(table.res, 0)
})
#
# Shutdown - Phase 2
#
context("ds.table::disc::shutdown::phase 2")
test_that("setup", {
ds_expect_variables(c("D", "new_table4"))
})
disconnect.studies.dataset.cnsim()
#
# Done
#
context("ds.table::disc::done")