This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
forked from RMI-PACTA/r2dii.match
/
test-prioritize.R
208 lines (180 loc) · 5.04 KB
/
test-prioritize.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
library(dplyr)
test_that("prioritize works with loanbook_demo and ald_demo", {
expect_error(
loanbook_demo %>%
slice(4:5) %>%
match_name(ald_demo) %>%
prioritize(),
NA
)
})
test_that("prioritize errors gracefully if data lacks crucial columns", {
expect_error(prioritize(tibble(bad = 1)), "must have.*names")
matched <- tibble(id = "a", level = "a", score = 1)
expect_error(prioritize(matched), NA)
expect_error(
prioritize(select(matched, -id)),
"must have.*names"
)
expect_error(
prioritize(select(matched, -level)),
"must have.*names"
)
expect_error(
prioritize(select(matched, -score)),
"must have.*names"
)
})
test_that("prioritize errors gracefully with bad `priority`", {
matched <- tibble(id = "a", level = c("z", "a"), score = 1)
expect_warning(
prioritize(matched, priority = c("bad1", "bab2")),
"[Ii]gnoring.*levels"
)
expect_warning(
prioritize(matched, priority = c("bad1", "bab2")),
"[Uu]nknown.*bad1.*bab2"
)
})
test_that("prioritize picks score equal to 1", {
# styler: off
matched <- tibble::tribble(
~id, ~level, ~score,
"aa", "direct_loantaker", 1,
"bb", "direct_loantaker", 0.9,
)
# styler: on
expect_equal(min(prioritize(matched)$score), 1L)
})
test_that("prioritize picks priority level per loan", {
# styler: off
matched <- tibble::tribble(
~id, ~level, ~score,
"aa", "ultimate_parent", 1,
"aa", "direct_loantaker", 1, # pick this
"bb", "intermediate_parent", 1, # pick this
"bb", "ultimate_parent", 1,
)
# styler: on
out <- prioritize(matched)
expect_equal(
out$level, c("direct_loantaker", "intermediate_parent")
)
})
test_that("prioritize picks the highetst level per loan", {
# styler: off
matched <- tibble::tribble(
~id, ~level, ~score,
"aa", "ultimate_parent", 1,
"aa", "direct_loantaker", 1, # pick this
"bb", "intermediate_parent", 1, # pick this
"bb", "ultimate_parent", 1,
)
# styler: on
out <- prioritize(matched)
expect_equal(
out$level, c("direct_loantaker", "intermediate_parent")
)
})
test_that("prioritize takes a `priority` function
or lambda", {
level <- c("direct_loantaker", "ultimate_parent")
matched <- tibble(id = "aa", level, score = 1)
out <- prioritize(matched, priority = NULL)
expect_equal(out$level, "direct_loantaker")
# Reverse with function
out <- prioritize(matched, priority = rev)
expect_equal(out$level, "ultimate_parent")
# Reverse with lambda
out <- prioritize(matched, priority = ~ rev(.x))
expect_equal(out$level, "ultimate_parent")
})
test_that("prioritize is sensitive to `priority`", {
matched <- tibble(id = "aa", level = c("z", "a"), score = 1)
expect_equal(
prioritize(matched, priority = "z")$level,
"z"
)
})
test_that("prioritize ignores other groups", {
# styler: off
matched <- tibble::tribble(
~id, ~level, ~score, ~other_id,
"a", "z", 1, 1,
"a", "a", 1, 2,
"b", "z", 1, 3,
"b", "a", 1, 4,
) %>%
group_by(other_id)
# styler: on
out <- prioritize(matched, priority = "z")
expect_equal(
out$level,
c("z", "z")
)
})
test_that("prioritize previous preserves groups", {
# styler: off
matched <- tibble::tribble(
~id, ~level, ~score, ~other_id,
"a", "z", 1, 1,
"a", "a", 1, 2,
"b", "z", 1, 3,
"b", "a", 1, 4,
) %>%
group_by(other_id, score)
# styler: on
out <- prioritize(matched, priority = "z")
expect_true(dplyr::is_grouped_df(out))
expect_equal(dplyr::group_vars(out), c("other_id", "score"))
})
test_that("prioritize_level otputs expected vector", {
matched <- tibble(
level = c(
"intermediate_parent_1",
"direct_loantaker",
"direct_loantaker",
"direct_loantaker",
"ultimate_parent",
"intermediate_parent_2"
)
)
expect_equal(
prioritize_level(matched),
c(
"direct_loantaker",
"intermediate_parent_1",
"intermediate_parent_2",
"ultimate_parent"
)
)
})
test_that("prioritize_at with ungrouped data picks the highest priority row", {
out <- tibble(x = c("a", "z")) %>%
prioritize_at(.at = "x", priority = c("z", "a"))
expect_equal(out$x, "z")
})
test_that("prioritize_at with grouped data picks one row per group", {
out <- tibble(
x = c(1, 2, 2),
y = c("a", "a", "z")
) %>%
group_by(x) %>%
prioritize_at(.at = "y", priority = c("z", "a")) %>%
arrange(x)
expect_equal(out$y, c("a", "z"))
})
test_that("prioritize_at does not warn if a group has not all priority items", {
expect_warning(
tibble(x = c("a", "z"), y = x) %>%
group_by(y) %>%
prioritize_at(.at = "x", priority = c("z", "a")),
NA
)
})
test_that("prioritize_at accepts `priority = NULL`", {
expect_error(
tibble(x = "a") %>% prioritize_at("x"),
NA
)
})