-
Notifications
You must be signed in to change notification settings - Fork 18
/
test-match_names.R
489 lines (407 loc) · 13.2 KB
/
test-match_names.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
context("match names")
hol_ott_id <- 924443
############################################################################
## check_args_match_names ##
############################################################################
context("check_args_match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
rsp <- tnrs_match_names(names = c("holothuria", "diadema", "fromia"))
}
test_that(
"error generated if object provided isn't created by tnrs_match_names",
expect_error(
rotl:::check_args_match_names(letters),
"was not created using"
)
)
test_that("error generated if no argument is provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp),
"You must specify"
)
})
test_that("error generated if row_number and taxon_name are provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp,
row_number = 1,
taxon_name = "holothuria"
),
"must use only one of "
)
})
test_that("error generated if row_number and ott_id are provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp,
row_number = 1,
ott_id = hol_ott_id
),
"must use only one of"
)
})
test_that("error generated if ott_id and taxon_name are provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp,
taxon_name = "holothuria",
ott_id = hol_ott_id
),
"must use only one of"
)
})
test_that("error generated if row_number is not numeric", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, row_number = TRUE),
"must be a numeric"
)
})
test_that("error generated if ott_id is not numeric", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, ott_id = TRUE),
"must look like a number"
)
})
test_that("error generated if taxon_name is not character", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, taxon_name = TRUE),
"must be a character"
)
})
test_that("error generated if row_number if not one of the row", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, row_number = 10),
"is not a valid row number"
)
expect_error(
rotl:::check_args_match_names(rsp, row_number = 1.5),
"is not a valid row number"
)
expect_error(
rotl:::check_args_match_names(rsp, row_number = 0),
"is not a valid row number"
)
})
test_that("error generated if invalid taxon_name", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, taxon_name = "echinodermata"),
"Can't find"
)
expect_error(
rotl:::check_args_match_names(rsp, taxon_name = NA_character_),
"Can't find"
)
})
test_that("error generated if invalid ott id", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, ott_id = 66666),
"Can't find"
)
})
test_that("error generated if more than 1 value for row_number is provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, row_number = c(1, 2, 3)),
"You must supply a single element"
)
})
test_that("error generated if more than 1 value for taxon_name is provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, taxon_name = c("holothuria", "diadema")),
"You must supply a single element"
)
})
test_that("error generated if more than 1 value for ott_id is provided", {
skip_on_cran()
expect_error(
rotl:::check_args_match_names(rsp, ott_id = c(5004030, 4930522, 240396)),
"only 1 element should be provided"
)
})
############################################################################
## inspect.match_names ##
############################################################################
context("inspect.match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
rsp <- tnrs_match_names(names = c("holothuria", "diadema", "fromia"))
expect_warning(rsp_na <- tnrs_match_names(names = c(
"diadema", "fluffy",
"hemichordata", "escherichia"
)))
diadema_ids <- c(4930522, 631176, 643831, 6356093, 4024672)
}
test_that("correct data is being returned when asked to lookup by taxon name", {
skip_on_cran()
tt <- inspect(rsp, taxon_name = "diadema")[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
})
test_that("correct data is being returned when asked to lookup by ott_id", {
skip_on_cran()
tt <- inspect(rsp, ott_id = ott_id(rsp)[2])[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
})
test_that("correct data is being returned when asked to lookup by row number", {
skip_on_cran()
tt <- inspect(rsp, row_number = 2)[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
})
## with missing data
test_that("correct data is being returned when asked to lookup by taxon name (with missing data)", {
skip_on_cran()
tt <- inspect(rsp_na, taxon_name = "diadema")[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
expect_true(is.na(inspect(rsp_na, taxon_name = "fluffy")[["ott_id"]]))
})
test_that("correct data is being returned when asked to lookup by ott_id (with missing data)", {
skip_on_cran()
tt <- inspect(rsp_na, ott_id = ott_id(rsp)[2])[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
})
test_that("correct data is being returned when asked to lookup by row number (with missing data)", {
skip_on_cran()
tt <- inspect(rsp_na, row_number = 1)[["ott_id"]]
expect_true(all(tt %in% diadema_ids))
expect_true(is.na(inspect(rsp_na, row_number = 2)[["ott_id"]]))
})
############################################################################
## synonyms.match_names ##
############################################################################
context("list_synonym_match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
tax_rsp <- c("Holothuria", "Diadema", "Fromia")
rsp <- tnrs_match_names(names = tax_rsp)
tax_rsp_na <- c("Holothuria", "Diadema", "fluffy", "Fromia")
expect_warning(rsp_na <- tnrs_match_names(names = tax_rsp_na))
}
test_that("synonyms", {
skip_on_cran()
tt <- synonyms(rsp)
expect_true(inherits(tt, "list"))
expect_equal(
names(tt),
c("Holothuria", "Diadema", "Fromia")
)
})
test_that("correct synonyms are being returned when asked to look up by taxon name", {
skip_on_cran()
tt <- synonyms(rsp, taxon_name = "holothuria")
expect_true(any(grepl("^Holothuria", names(tt))))
})
test_that("holothuria is present in each element of the list", {
skip_on_cran()
tt <- synonyms(rsp, taxon_name = "holothuria")
expect_true(all(sapply(tt, function(x) any(grepl("holothuria", x, ignore.case = TRUE)))))
expect_true(any(grepl("Halodeima", tt[["Holothuria"]])))
})
test_that("correct synonyms are being returned when asked to look up by row number", {
skip_on_cran()
tt <- synonyms(rsp, row_number = 1)
expect_true(any(grepl("^Holothuria", names(tt))))
expect_true(any(grepl("Halodeima", tt[["Holothuria"]])))
})
test_that("correct synonyms are being returned when asked to look up by ott id", {
skip_on_cran()
tt <- synonyms(rsp, ott_id = 924443)
expect_true(any(grepl("^Holothuria", names(tt))))
expect_true(any(grepl("Halodeima", tt[["Holothuria"]])))
})
## with missing data
test_that("synonyms", {
skip_on_cran()
tt <- synonyms(rsp_na)
expect_true(inherits(tt, "list"))
expect_equal(
names(tt),
c("Holothuria", "Diadema", "Fromia")
)
})
test_that("correct synonyms are being returned when asked to look up by taxon name", {
skip_on_cran()
tt <- synonyms(rsp_na, taxon_name = "holothuria")
expect_true(any(grepl("^Holothuria", names(tt))))
expect_true(is.na(synonyms(rsp_na, taxon_name = "fluffy")[[1]]))
})
test_that("correct synonyms are being returned when asked to look up by row number", {
skip_on_cran()
tt <- synonyms(rsp_na, row_number = 1)
expect_true(any(grepl("^Holothuria", names(tt))))
expect_true(any(grepl("Halodeima", tt[["Holothuria"]])))
expect_true(is.na(synonyms(rsp_na, row_number = 3)[[1]]))
})
test_that("correct synonyms are being returned when asked to look up by ott id", {
skip_on_cran()
tt <- synonyms(rsp_na, ott_id = 924443)
expect_true(any(grepl("^Holothuria", names(tt))))
expect_true(any(grepl("Halodeima", tt[["Holothuria"]])))
})
############################################################################
## update.match_names ##
############################################################################
context("update.match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
rsp <- tnrs_match_names(names = c("holothuria", "diadema", "fromia"))
}
test_that("error message if missing both new arguments", {
skip_on_cran()
expect_error(
update(rsp, row_number = 1),
"You must specify either"
)
})
test_that("error message if both new arguments are provided", {
skip_on_cran()
expect_error(
update(rsp,
row_number = 1,
new_row_number = 1,
new_ott_id = 6666
),
"You must use only"
)
})
test_that("error message if wrong new row number provided", {
skip_on_cran()
expect_error(
update(rsp,
row_number = 1,
new_row_number = 10
),
"is not a valid row number"
)
expect_error(
update(rsp,
row_number = 1,
new_row_number = 1.5
),
"is not a valid row number"
)
})
test_that("error message if wrong new ott id provided", {
skip_on_cran()
expect_error(
update(rsp,
row_number = 1,
new_ott_id = 66666
),
"Can't find"
)
})
test_that("it works correctly when providing a new row number", {
skip_on_cran()
new_rsp <- update(
rsp,
row_number = 2,
new_row_number = 2
)
expect_equal(
new_rsp[new_rsp$search_string == "diadema", "ott_id"],
4024672
)
})
test_that("it works correctly when providing a new ott id", {
skip_on_cran()
new_rsp <- update(rsp,
row_number = 2,
new_ott_id = 631176
)
expect_equal(
new_rsp[new_rsp$search_string == "diadema", "ott_id"],
631176L
)
})
test_that("it produces warning when trying to update with unmatched name", {
skip_on_cran()
expect_warning(new_rsp <- update(rsp_na, row_number = 3, new_row_number = 1))
expect_identical(new_rsp, rsp_na)
})
############################################################################
## flags method ##
############################################################################
context("flags method for class match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
tax_rsp <- c(
"Tyrannosaurus", "Helicoplacus", "Ctenocystis",
"Holothuria", "Echinoidea"
)
rsp <- tnrs_match_names(tax_rsp)
}
test_that("flags with no arguments", {
skip_on_cran()
flags_rsp <- flags(rsp)
expect_equal(length(flags_rsp), 5)
expect_equivalent(
sapply(flags_rsp, length),
c(1, 2, 2, 0, 0)
)
})
test_that("flags with row number", {
skip_on_cran()
flags_rsp <- flags(rsp, 1)
expect_true(inherits(flags_rsp, "list"))
expect_equal(length(flags_rsp), 1)
expect_equal(length(flags_rsp[[1]]), 1)
expect_true(inherits(flags_rsp[[1]], "character"))
expect_equal(names(flags_rsp), tax_rsp[1])
})
test_that("flags with taxon name", {
skip_on_cran()
flags_rsp <- flags(rsp, taxon_name = "Tyrannosaurus")
expect_true(inherits(flags_rsp, "list"))
expect_equal(length(flags_rsp), 1)
expect_equal(length(flags_rsp[[1]]), 1)
expect_true(inherits(flags_rsp[[1]], "character"))
expect_equal(names(flags_rsp), tax_rsp[1])
})
test_that("flags with ott id", {
skip_on_cran()
flags_rsp <- flags(rsp, ott_id = 664348)
expect_true(inherits(flags_rsp, "list"))
expect_equal(length(flags_rsp), 1)
expect_equal(length(flags_rsp[[1]]), 1)
expect_true(inherits(flags_rsp[[1]], "character"))
expect_equal(names(flags_rsp), tax_rsp[1])
})
############################################################################
## ott_id method ##
############################################################################
context("ott_id method for class match_names")
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
tax_rsp <- c(
"Tyrannosaurus", "Helicoplacus", "Ctenocystis",
"Holothuria", "Echinoidea"
)
rsp <- tnrs_match_names(tax_rsp)
}
test_that("ott_id with no arguments", {
skip_on_cran()
expect_true(inherits(ott_id(rsp), "list"))
expect_true(inherits(ott_id(rsp), "otl_ott_id"))
expect_equal(names(ott_id(rsp)), tax_rsp)
expect_equal(ott_id(rsp)[["Holothuria"]][[1]], hol_ott_id)
})
test_that("ott_id with row number", {
skip_on_cran()
expect_equal(length(ott_id(rsp, 4)), 1)
expect_true(inherits(ott_id(rsp, 4), "list"))
expect_equivalent(ott_id(rsp, 4)[[1]], hol_ott_id)
})
test_that("ott_id with taxon name", {
skip_on_cran()
expect_equal(length(ott_id(rsp, taxon_name = "Holothuria")), 1)
expect_true(inherits(ott_id(rsp, taxon_name = "Holothuria"), "list"))
expect_equivalent(ott_id(rsp, taxon_name = "Holothuria")[[1]], hol_ott_id)
})
test_that("ott_id with ott id", {
skip_on_cran()
expect_equal(length(ott_id(rsp, ott_id = hol_ott_id)), 1)
expect_true(inherits(ott_id(rsp, ott_id = hol_ott_id), "list"))
expect_equivalent(ott_id(rsp, ott_id = hol_ott_id)[[1]], hol_ott_id)
})