Skip to content

Commit bdb7e75

Browse files
committed
Updated Unit-Tests to be compatible with CRAN checks for MacOs
1 parent af70894 commit bdb7e75

6 files changed

Lines changed: 253 additions & 51 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: cellKey
22
Type: Package
3-
Date: 2023-03-09
3+
Date: 2023-03-13
44
Title: Consistent Perturbation of Statistical Frequency- And Magnitude Tables
5-
Version: 1.0.0
5+
Version: 1.0.1
66
Authors@R: c(
77
person(
88
given="Bernhard", family="Meindl",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# cellKey 1.0.1
2+
- Updated Unit-Tests to be compatible with CRAN checks for MacOs
3+
14
# cellKey 1.0.0
25
- first version on CRAN
36
- updated due to changes in Package `ptable`

tests/testthat/test_ck_generate_rkeys.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ ck1 <- ck_generate_rkeys(dat = x, nr_digits = 8, seed = NULL)
1414
ck2 <- ck_generate_rkeys(dat = x, nr_digits = 10, seed = 5)
1515

1616
test_that("recordkeys are correctly computed", {
17-
expect_equal(digest::sha1(ck1), "a291a306de5db64e3fbcb94478982733a17ede20")
18-
expect_equal(digest::sha1(ck2), "5677f580e6ed9fa59ab670b0e7badc67a37bf14c")
17+
expect_identical(length(ck1), 4580L)
18+
expect_identical(ck1[1], 0.97131759)
19+
expect_identical(ck1[4580], 0.88019983)
20+
expect_identical(round(mean(ck1), digits = 3), 0.501)
21+
22+
23+
expect_identical(length(ck2), 4580L)
24+
expect_identical(ck2[1], 0.2002144526)
25+
expect_identical(ck2[4580], 0.1878750164)
26+
expect_identical(round(mean(ck2), digits = 3), 0.508)
1927
})

tests/testthat/test_countvars.R

Lines changed: 129 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
context("Testing Frequency Tables")
2+
23
set.seed(120, sample.kind = "Reject")
34
dat <- ck_create_testdata()
45

@@ -11,22 +12,29 @@ dim_sex <- hier_create(root = "Total", nodes = c("male", "female"))
1112
dim_age <- hier_create(root = "Total", paste0("age_group", 1:6))
1213
dims <- list(sex = dim_sex, age = dim_age)
1314
test_that("dims-hash is ok", {
14-
expect_identical(digest::sha1(dim_sex), "fea2001f35be84e90b30f6773af75f03c11fbf7a")
15-
expect_identical(digest::sha1(dim_age), "a7648dc3f484720911f0de0e6ac563b69fd20c42")
16-
expect_identical(digest::sha1(dims), "62748837ca3246a33081dd35f50d06334caa3119")
15+
expect_identical(class(dims), "list")
16+
expect_identical(nrow(dim_sex), 3L)
17+
expect_identical(max(dim_sex$level), 2)
18+
expect_identical(nrow(dim_age), 7L)
19+
expect_identical(max(dim_age$level), 2)
1720
})
1821

1922
## test generation of destatis rkeys
2023
rk1 <- ck_generate_rkeys(dat = dat, nr_digits = 5)
2124
rk2 <- ck_generate_rkeys(dat = dat, nr_digits = 5)
2225
test_that("check rkey generation and seed is ok", {
2326
expect_identical(rk1, rk2)
24-
expect_identical(digest::sha1(rk1), "4de74ed6170e2142ef552ee6722921db8d091d0c")
27+
expect_identical(round(mean(rk1), digits = 3), 0.501)
2528
})
2629
dat$rec_key <- rk1
2730
test_that("checking dimension and structure of generated testdata is ok", {
28-
expect_identical(digest::sha1(dat), "fb66a8be3e9044c8fecdb13c6fab5fe9ec456c25")
2931
expect_true(is.data.table(dat))
32+
expect_identical(round(mean(dat$sampling_weight), digits = 3), 59.719)
33+
expect_identical(round(mean(dat$household_weights), digits = 3), 21.834)
34+
35+
expect_identical(nrow(dat), 4580L)
36+
expect_identical(ncol(dat), 16L)
37+
expect_identical(sum(dat$sex == "male"), 2296L)
3038
})
3139

3240
## perturbation parameters for count variables
@@ -54,8 +62,13 @@ test_that("ck_params_cnts() is ok", {
5462
test_that("checking perturbation parameters for counts", {
5563
expect_is(params_cnts, "ck_params")
5664
expect_equal(params_cnts$type, "cnts")
57-
expect_is(params_cnts$params$ptable, "data.table")
58-
expect_identical(digest::sha1(params_cnts), "cedf56d7064f15e55da506b1922c4cac6035765f")
65+
dt <- params_cnts$params$ptable
66+
expect_is(dt, "data.table")
67+
expect_identical(dim(dt), c(66L, 7L))
68+
expect_identical(round(mean(dt$p), digits = 3), 0.136)
69+
expect_identical(round(mean(dt$lb), digits = 3), 0.562)
70+
expect_identical(round(mean(dt$ub), digits = 3), 0.698)
71+
expect_identical(round(mean(dt$v), digits = 3), 1.045)
5972
})
6073

6174
countvars <- NULL
@@ -79,9 +92,11 @@ expect_message(tab$perturb("total"), "Variable 'total' was already perturbed!")
7992
res_freqtab <- tab$freqtab("total")
8093
test_that("check ck_define_table() with already existing rec-keys", {
8194
expect_is(tab, "cellkey_obj")
95+
expect_identical(dim(res_freqtab), c(21L, 7L))
8296
expect_identical(res_freqtab$uwc[3], 1143)
8397
expect_identical(res_freqtab$puwc[3], 1147)
84-
expect_identical(digest::sha1(res_freqtab), "05e71f630a385f7e428ce1fec21b5f6026bb921a")
98+
expect_identical(round(mean(res_freqtab$pwc), digits = 3), 52096.24)
99+
expect_identical(round(mean(res_freqtab$puwc), digits = 3), 872.333)
85100
})
86101

87102
dat$rec_key <- NULL
@@ -99,23 +114,71 @@ expect_message(tab$perturb("total"), "Variable 'total' was already perturbed!")
99114

100115
test_that("ck_define_table() with new record keys is ok", {
101116
expect_is(tab, "cellkey_obj")
102-
expect_identical(digest::sha1(tab$freqtab("total")), "05e71f630a385f7e428ce1fec21b5f6026bb921a")
117+
dt <- tab$freqtab("total")
118+
expect_identical(dt$uwc[3], 1143)
119+
expect_identical(dt$puwc[3], 1147)
120+
expect_identical(round(mean(dt$pwc), digits = 3), 52096.24)
121+
expect_identical(round(mean(dt$puwc), digits = 3), 872.333)
103122
})
104123

105124
freqtab <- tab$freqtab("total")
106125
test_that("weighted version of ck_perturb() is ok", {
107-
expect_identical(digest::sha1(freqtab), "05e71f630a385f7e428ce1fec21b5f6026bb921a")
108-
expect_identical(digest::sha1(tab$mod_cnts()), "ee05433bb69fbf66094cf14e5c50320b3060eab3")
126+
expect_identical(freqtab$uwc[3], 1143)
127+
expect_identical(freqtab$puwc[3], 1147)
128+
expect_identical(round(mean(freqtab$pwc), digits = 3), 52096.24)
129+
expect_identical(round(mean(freqtab$puwc), digits = 3), 872.333)
130+
131+
dt <- tab$mod_cnts()
132+
expect_identical(dim(dt), c(21L, 6L))
133+
expect_identical(round(mean(dt$ckey), digits = 3), 0.456)
134+
expect_identical(round(mean(dt$pert), digits = 3), -0.048)
135+
expect_identical(range(dt$row_nr), c(40, 65))
109136
})
110137

111138
mm <- tab$measures_cnts("Total")
112139
test_that("ck_cnt_measures() [exclude_zeros = TRUE] is ok", {
113-
expect_identical(digest::sha1(mm), "89f4aba98334930446c2fb97b0812b7ece98ef6d")
140+
expect_identical(range(as.numeric(mm$overview$noise)), c(1, 7))
141+
expect_identical(range(as.numeric(mm$overview$cnt)), c(1, 9))
142+
expect_identical(round(mean(as.numeric(mm$overview$pct)), digits = 3), 0.143)
143+
144+
expect_identical(range(as.numeric(mm$measures$d1)), c(0, 4))
145+
expect_identical(range(as.numeric(mm$measures$d2)), c(0, 0.429))
146+
expect_identical(range(as.numeric(mm$measures$d3)), c(0, 0.517))
147+
148+
expect_identical(range(mm$cumdistr_d1$cnt), c(9L, 21L))
149+
expect_identical(round(range(mm$cumdistr_d1$pct), digits = 3), c(0.429, 1))
150+
151+
expect_identical(range(mm$cumdistr_d2$cnt), c(19L, 21L))
152+
expect_identical(round(range(mm$cumdistr_d2$pct), digits = 3), c(0.905, 1))
153+
154+
expect_identical(range(mm$cumdistr_d3$cnt), c(12L, 21L))
155+
expect_identical(round(range(mm$cumdistr_d3$pct), digits = 3), c(0.571, 1))
156+
expect_identical(mm$false_nonzero, 0L)
157+
expect_identical(mm$false_zero, 0L)
158+
expect_identical(mm$exclude_zeros, TRUE)
114159
})
115160

116161
mm <- tab$measures_cnts("Total", exclude_zeros = FALSE)
117162
test_that("ck_cnt_measures() [exclude_zeros = FALSE] is ok", {
118-
expect_identical(digest::sha1(mm), "9fb4ffe32ebc420d8ccecb6f3dab6e1431396fa0")
163+
expect_identical(range(as.numeric(mm$overview$noise)), c(1, 7))
164+
expect_identical(range(as.numeric(mm$overview$cnt)), c(1, 9))
165+
expect_identical(round(mean(as.numeric(mm$overview$pct)), digits = 3), 0.143)
166+
167+
expect_identical(range(as.numeric(mm$measures$d1)), c(0, 4))
168+
expect_identical(range(as.numeric(mm$measures$d2)), c(0, 0.429))
169+
expect_identical(range(as.numeric(mm$measures$d3)), c(0, 0.517))
170+
171+
expect_identical(range(mm$cumdistr_d1$cnt), c(9L, 21L))
172+
expect_identical(round(range(mm$cumdistr_d1$pct), digits = 3), c(0.429, 1))
173+
174+
expect_identical(range(mm$cumdistr_d2$cnt), c(19L, 21L))
175+
expect_identical(round(range(mm$cumdistr_d2$pct), digits = 3), c(0.905, 1))
176+
177+
expect_identical(range(mm$cumdistr_d3$cnt), c(12L, 21L))
178+
expect_identical(round(range(mm$cumdistr_d3$pct), digits = 3), c(0.571, 1))
179+
expect_identical(mm$false_nonzero, 0L)
180+
expect_identical(mm$false_zero, 0L)
181+
expect_identical(mm$exclude_zeros, FALSE)
119182
})
120183

121184
# no weights
@@ -133,8 +196,17 @@ tab$perturb("total")
133196
freqtab <- tab$freqtab("total")
134197

135198
test_that("checking unweighted version of perturb()", {
136-
expect_identical(digest::sha1(freqtab), "31831ab589f3bc7bdd20c4b6fd1ea1916e3edfef")
137-
expect_identical(digest::sha1(tab$mod_cnts()), "ee05433bb69fbf66094cf14e5c50320b3060eab3")
199+
expect_identical(dim(freqtab), c(21L, 7L))
200+
expect_identical(freqtab$uwc[3], 1143)
201+
expect_identical(freqtab$puwc[3], 1147)
202+
expect_identical(freqtab$pwc, freqtab$puwc)
203+
expect_identical(round(mean(freqtab$pwc), digits = 3), 872.333)
204+
205+
dt <- tab$mod_cnts()
206+
expect_identical(dim(dt), c(21L, 6L))
207+
expect_identical(round(mean(dt$ckey), digits = 3), 0.456)
208+
expect_identical(round(mean(dt$pert), digits = 3), -0.048)
209+
expect_identical(range(dt$row_nr), c(40, 65))
138210
})
139211

140212
context("Testing multiple countvars")
@@ -153,16 +225,52 @@ tab <- ck_setup(
153225
tab$params_cnts_set(params_cnts, v = NULL)
154226
tab$perturb(c("total", "cnt_males", "cnt_highincome"))
155227
test_that("check tabulation of cnt_males is ok", {
156-
expect_identical(digest::sha1(tab$freqtab("cnt_males")), "a7260c1f65a089d8849084a7e84c946be997e986")
157-
expect_identical(digest::sha1(tab$measures_cnts("cnt_males")), "b692361c523b0d37aa38bf252a360414cd941e2b")
228+
dt <- tab$freqtab("cnt_males")
229+
expect_identical(dt$uwc[3], 571)
230+
expect_identical(dt$puwc[3], 571)
231+
expect_identical(round(mean(dt$pwc), digits = 3), 25757.65)
232+
expect_identical(round(mean(dt$puwc), digits = 3), 437.048)
233+
234+
expect_identical(round(range(dt$puwc), digits = 3), c(0, 2297))
235+
expect_identical(round(range(dt$pwc), digits = 3), c(0, 135387.941))
236+
237+
mm <- tab$measures_cnts("cnt_males")
238+
expect_identical(round(range(mm$overview$pct), digits = 3), c(0.095, 0.810))
239+
expect_identical(round(range(mm$measures$d1), digits = 3), c(0, 4))
240+
expect_identical(round(range(mm$measures$d2), digits = 3), c(0, 0.048))
241+
expect_identical(round(range(mm$measures$d3), digits = 3), c(0, 0.221))
158242
})
159243

160244
test_that("check tabulation of cnt_highincome is ok", {
161-
expect_identical(digest::sha1(tab$freqtab("cnt_highincome")), "72c684edc0cebef0343bce02d0075050d5286a5c")
162-
expect_identical(digest::sha1(tab$measures_cnts("cnt_highincome")), "639f20e406ecfdd44c84e0ae04674321c2a602b1")
245+
dt <- tab$freqtab("cnt_highincome")
246+
expect_identical(dt$uwc[3], 123)
247+
expect_identical(dt$puwc[3], 123)
248+
expect_identical(round(mean(dt$pwc), digits = 3), 5063.68)
249+
expect_identical(round(mean(dt$puwc), digits = 3), 84.286)
250+
251+
expect_identical(round(range(dt$puwc), digits = 3), c(0, 444))
252+
expect_identical(round(range(dt$pwc), digits = 3), c(0, 26671.928))
253+
254+
mm <- tab$measures_cnts("cnt_highincome")
255+
expect_identical(round(range(mm$overview$pct), digits = 3), c(0.048, 0.667))
256+
expect_identical(round(range(mm$measures$d1), digits = 3), c(0, 3))
257+
expect_identical(round(range(mm$measures$d2), digits = 3), c(0, 0.158))
258+
expect_identical(round(range(mm$measures$d3), digits = 3), c(0, 0.359))
163259
})
164260

165261
test_that("check tabulation of multiple count variables is ok", {
166-
tt <- tab$freqtab(c("total", "cnt_males", "cnt_highincome"))
167-
expect_identical(digest::sha1(tt), "1984413d2bb4ebff2d268b243b300cd8494b55fb")
262+
dt <- tab$freqtab(c("total", "cnt_males", "cnt_highincome"))
263+
expect_identical(dim(dt), c(63L, 7L))
264+
expect_identical(dt$uwc[3], 1143)
265+
expect_identical(dt$puwc[3], 1148)
266+
expect_identical(round(mean(dt$pwc), digits = 3), 27640.21)
267+
expect_identical(round(mean(dt$puwc), digits = 3), 464.587)
268+
expect_identical(round(range(dt$puwc), digits = 3), c(0, 4582))
269+
expect_identical(round(range(dt$pwc), digits = 3), c(0, 273633.438))
270+
271+
mm <- tab$measures_cnts("cnt_highincome")
272+
expect_identical(round(range(mm$overview$pct), digits = 3), c(0.048, 0.667))
273+
expect_identical(round(range(mm$measures$d1), digits = 3), c(0, 3))
274+
expect_identical(round(range(mm$measures$d2), digits = 3), c(0, 0.158))
275+
expect_identical(round(range(mm$measures$d3), digits = 3), c(0, 0.359))
168276
})

tests/testthat/test_numvars.R

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ x$rkey <- ck_generate_rkeys(dat = x, nr_digits = 8)
2424

2525
test_that("checking dimension and structure of generated testdata is ok", {
2626
expect_true(is.data.table(x))
27-
expect_identical(digest::sha1(x), "4c804693f7d573e9dfa24bcc312ba2b61b490ada")
28-
expect_identical(digest::sha1(dims), "62748837ca3246a33081dd35f50d06334caa3119")
27+
expect_identical(dim(x), c(4580L, 21L))
28+
expect_identical(round(mean(x$rkey), digits = 3), 0.5)
29+
expect_identical(round(mean(x$household_weights), digits = 3), 21.834)
30+
expect_identical(range(x$mixed), c(-20L, 10L))
31+
expect_identical(sum(x$cnt_males), 2296)
32+
expect_identical(sum(x$cnt_females), 2284)
33+
expect_identical(sum(x$cnt_highincome), 445)
2934
})
3035

3136
## perturbation parameters for count variables
@@ -85,7 +90,27 @@ p2 <- ck_params_nums(
8590
test_that("checking perturbation parameters", {
8691
expect_is(p1, "ck_params")
8792
expect_equal(p1$type, "params_m_flex")
88-
expect_identical(digest::sha1(p1), "53299f7eab1d919060e12311955ed3eb02f0b38b")
93+
94+
pp <- p1$params
95+
96+
expect_identical(pp$type, "top_contr")
97+
expect_identical(pp$top_k, 3)
98+
expect_identical(dim(pp$ptab), c(96L, 7L))
99+
expect_identical(round(mean(pp$ptab$p), digits = 3), 0.042)
100+
101+
expect_identical(pp$mu_c, 2.5)
102+
expect_identical(pp$m_fixed_sq, NA)
103+
expect_identical(pp$zs, 0)
104+
expect_identical(pp$E, 1.34)
105+
expect_identical(pp$mult_params$fp, 1000)
106+
expect_identical(pp$mult_params$p_small, 0.2)
107+
expect_identical(pp$mult_params$p_large, 0.03)
108+
expect_identical(pp$mult_params$epsilon, c(1, 0.5, 0.3))
109+
110+
expect_identical(pp$same_key, FALSE)
111+
expect_identical(pp$use_zero_rkeys, TRUE)
112+
expect_identical(pp$even_odd, FALSE)
113+
expect_identical(pp$separation, FALSE)
89114
})
90115

91116
# set up problem
@@ -114,8 +139,23 @@ expect_message(tab$perturb("income"), "Numeric variable 'income' was perturbed."
114139
expect_message(tab$perturb("savings"), "Numeric variable 'savings' was perturbed.")
115140

116141
test_that("variable was correctly perturbed", {
117-
expect_equal(digest::sha1(tab$numtab("income", mean_before_sum = FALSE)), "29eec69ec43987831d951b7e6ecd4d423b27f5e2")
118-
expect_equal(digest::sha1(tab$numtab("savings", mean_before_sum = FALSE)), "77abfc53132ec03b5a523764eefb52e00dca897f")
119-
expect_equal(digest::sha1(tab$numtab("income", mean_before_sum = TRUE)), "98f4cdbe6b4b362aa0b9ab31337fc39c4d807c1e")
120-
expect_equal(digest::sha1(tab$numtab("savings", mean_before_sum = TRUE)), "39a0fa3ddfd8b45778549711d9f9f58c843b0912")
142+
dt <- tab$numtab("income", mean_before_sum = FALSE)
143+
expect_identical(dim(dt), c(21L, 6L))
144+
expect_equal(round(mean(dt$pws), digits = 3), 13181967.1)
145+
expect_equal(round(mean(dt$ws), digits = 3), 13184145.52)
146+
147+
dt <- tab$numtab("savings", mean_before_sum = FALSE)
148+
expect_identical(dim(dt), c(21L, 6L))
149+
expect_equal(round(mean(dt$pws), digits = 3), 1306306.6)
150+
expect_equal(round(mean(dt$ws), digits = 3), 1306303.048)
151+
152+
dt <- tab$numtab("income", mean_before_sum = TRUE)
153+
expect_identical(dim(dt), c(21L, 6L))
154+
expect_equal(round(mean(dt$pws), digits = 3), 13179895.45)
155+
expect_equal(round(mean(dt$ws), digits = 3), 13184145.52)
156+
157+
dt <- tab$numtab("savings", mean_before_sum = TRUE)
158+
expect_identical(dim(dt), c(21L, 6L))
159+
expect_equal(round(mean(dt$pws), digits = 3), 1306310.272)
160+
expect_equal(round(mean(dt$ws), digits = 3), 1306303.048)
121161
})

0 commit comments

Comments
 (0)