forked from insightsengineering/tern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-count_values.R
119 lines (91 loc) · 3.18 KB
/
test-count_values.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
testthat::test_that("s_count_values works for character input without NAs", {
x <- c("a", "b", "a")
# Value which is present in vector.
result <- s_count_values(x, values = "a")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
# Value which is not present.
result <- s_count_values(x, values = "c")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values works for character input with NAs", {
x <- c("a", "b", "a", NA, "c", "b")
# Default behavior does not count NAs for `n`.
result <- s_count_values(x, values = "a")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
# Count the NAs with `na.rm` argument set.
result <- s_count_values(x, values = c("bla", "c"), na.rm = FALSE)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values can pass options to s_summary's logical method", {
x <- c("a", "b", "a")
result <- s_count_values(x, .N_row = 10, values = "a", denom = "N_row")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values for factor gives same result as for character", {
x <- c("a", "b", "a")
result <- s_count_values(factor(x), .N_row = 10, values = "a", denom = "N_row")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values for factor gives the same result as for character for values not in factor level", {
x <- c("a", "b", "a")
result <- s_count_values(factor(x), values = "x")
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("count_values works as expected with a single value", {
result <- basic_table() %>%
count_values("Species", values = "setosa") %>%
build_table(iris)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("count_values works as expected with multiple values and variables", {
df <- data.frame(
x = c("a", "b", "a", "c"),
y = c("b", "a", "a", "f"),
stringsAsFactors = FALSE
)
result <- basic_table() %>%
count_values(
c("x", "y"),
values = c("a", "f")
) %>%
build_table(df)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values for logical vector", {
x <- c(TRUE, FALSE, TRUE)
# Value which is present in vector.
result <- s_count_values(x)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("s_count_values for logical vector with NA", {
x <- c(TRUE, FALSE, TRUE, NA)
# Value which is present in vector.
result <- s_count_values(x)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})
testthat::test_that("count_values works as expected with multiple values and variables", {
df <- data.frame(
x = c(TRUE, FALSE, TRUE, FALSE),
y = c("b", "a", "a", "f"),
stringsAsFactors = FALSE
)
result <- basic_table() %>%
count_values(
"x",
values = TRUE
) %>%
build_table(df)
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})