forked from SwissClinicalTrialOrganisation/secuTrialR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-factorize_secutrial.R
132 lines (108 loc) · 6.33 KB
/
test-factorize_secutrial.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
context("factorize")
skip_on_cran()
## lookup table tests
lookup_long_loc <- system.file("extdata", "sT_exports", "lnames",
"s_export_CSV-xls_PLAYG_20201203-180930.zip",
package = "secuTrialR")
lookup_short_loc <- system.file("extdata", "sT_exports", "snames",
"s_export_CSV-xls_PLAYG_20201204-122705.zip",
package = "secuTrialR")
test_that("lookup table factorization", {
# warnings are not related to the lookup but related to incomplete dates
expect_warning(sT_exp_lookup_long <- read_secuTrial(data_dir = lookup_long_loc))
expect_warning(sT_exp_lookup_short <- read_secuTrial(data_dir = lookup_short_loc))
expect_equal(sT_exp_lookup_long$emnpplaygtherapy_sub$therapy_first_lvl.factor,
sT_exp_lookup_short$etherapy_sub$therapy_first_lvl.factor)
expect_equal(sT_exp_lookup_long$emnpplaygtherapy_sub$therapy_second_lvl.factor,
sT_exp_lookup_short$etherapy_sub$therapy_second_lvl.factor)
expect_equal(levels(sT_exp_lookup_long$emnpplaygtherapy_sub$therapy_first_lvl.factor),
c("Bacterial infection therapy", "Pain therapy"))
expect_equal(levels(sT_exp_lookup_long$emnpplaygtherapy_sub$therapy_second_lvl.factor),
c("Ampicillin", "Paracetamol", "Penicillin", "Ibuprofen", "Flucloxacillin"))
})
# BMD
short_export_location <- system.file("extdata", "sT_exports", "BMD",
"s_export_CSV-xls_BMD_short_en_utf8.zip",
package = "secuTrialR")
long_export_location <- system.file("extdata", "sT_exports", "BMD",
"s_export_CSV-xls_BMD_long_en_utf8.zip",
package = "secuTrialR")
sT_export_short <- read_secuTrial_raw(data_dir = short_export_location)
sT_export_long <- read_secuTrial_raw(data_dir = long_export_location)
test_that("separate table warning", {
expect_error(factorize_secuTrial(sT_export_short))
expect_error(factorize_secuTrial(sT_export_long))
})
# CTU05
ctu05_l <- read_secuTrial_raw(system.file("extdata", "sT_exports", "lnames",
"s_export_CSV-xls_CTU05_long_ref_miss_en_utf8.zip",
package = "secuTrialR"))
ctu05_s <- read_secuTrial_raw(system.file("extdata", "sT_exports", "snames",
"s_export_CSV-xls_CTU05_short_ref_miss_en_utf8.zip",
package = "secuTrialR"))
test_that("error on factorize", expect_warning(factorize_secuTrial(ctu05_l), regexp = NA))
test_that("warning on factorize", expect_error(factorize_secuTrial(ctu05_l), regexp = NA))
sAF <- options()$stringsAsFactors
options(stringsAsFactors = FALSE)
fact_ctu05_l <- factorize_secuTrial(ctu05_l)
fact_ctu05_s <- factorize_secuTrial(ctu05_s)
w_l <- sapply(fact_ctu05_l$ctu05ae, class) == "factor"
w_s <- sapply(fact_ctu05_s$ae, class) == "factor"
test_that("number of factors in AE form", {
expect_equal(sum(w_l), 21)
expect_equal(sum(w_s), 21)
})
test_that("Levels in liver cirrhosis", {
expect_equal(levels(fact_ctu05_l$ctu05baseline$liver_cirrh_type.factor), c("C", "B", "A"))
expect_equal(levels(fact_ctu05_s$baseline$liver_cirrh_type.factor), c("C", "B", "A"))
})
test_that("Levels in follow-up", {
expect_equal(as.vector(table(fact_ctu05_l$ctu05outcome$follow_up.factor)), c(5, 5, 2))
expect_equal(levels(fact_ctu05_l$ctu05outcome$follow_up.factor), c("unknown", "ongoing consultation", "death"))
expect_equal(as.vector(table(fact_ctu05_s$outcome$follow_up.factor)), c(5, 5, 2))
expect_equal(levels(fact_ctu05_s$outcome$follow_up.factor), c("unknown", "ongoing consultation", "death"))
})
test_that("Levels in SAE", {
expect_equal(levels(fact_ctu05_l$ctu05sae$sae_drug_relation.factor), c("not assessable",
"possible",
"unlikely",
"probable",
"unrelated",
"definitely"))
expect_equal(as.vector(table(fact_ctu05_l$ctu05sae$sae_drug_relation.factor)), c(1, 1, 0, 0, 0, 0))
expect_equal(levels(fact_ctu05_s$sae$sae_drug_relation.factor), c("not assessable",
"possible",
"unlikely",
"probable",
"unrelated",
"definitely"))
expect_equal(as.vector(table(fact_ctu05_s$sae$sae_drug_relation.factor)), c(1, 1, 0, 0, 0, 0))
})
test_that("Levels in meta variables", {
expect_equal(levels(fact_ctu05_l$ctu05baseline$mnpfcs0.factor), c("empty", "partly filled", "completely filled"))
expect_equal(as.vector(table(fact_ctu05_l$ctu05baseline$mnpfcs0.factor)), c(0, 3, 14))
expect_equal(as.vector(table(fact_ctu05_l$ctu05baseline$sigstatus.factor))[1], 17)
expect_equal(levels(fact_ctu05_s$baseline$mnpfcs0.factor), c("empty", "partly filled", "completely filled"))
expect_equal(as.vector(table(fact_ctu05_s$baseline$mnpfcs0.factor)), c(0, 3, 14))
expect_equal(as.vector(table(fact_ctu05_s$baseline$sigstatus.factor))[1], 17)
})
# factorization of reprtitions (subforms)
test_that("Factorization of repetitions working.", {
expect_true(all.equal(fact_ctu05_s$esurgeries, fact_ctu05_l$emnpctu05surgeries))
})
# warnings for trying to refactorize
test_that("refactorize warning", {
expect_warning(factorize_secuTrial(fact_ctu05_l))
expect_warning(factorize_secuTrial(fact_ctu05_s))
})
# manually adding duplicate factor levels to cl table for mnpptnid
reference_line <- as.vector(ctu05_l$cl[158, ])
duplicate_1 <- reference_line
duplicate_2 <- reference_line
duplicate_1$code <- 1987
duplicate_2$code <- 2019
ctu05_l$cl <- rbind(ctu05_l$cl, duplicate_1, duplicate_2)
test_that("Exception for duplicated factor levels in working.", {
expect_true(factorize_secuTrial(ctu05_l)$export_options$factorized)
})
options(stringsAsFactors = sAF)