Skip to content

Commit 7bc690d

Browse files
committed
Init commit
1 parent ec5a90b commit 7bc690d

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

tests/testthat/test-check_data.R

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
test_that("check_data errors when STUDYID missing", {
2+
df <- tibble::tibble(USUBJID = "P1")
3+
4+
expect_error(check_data(df), regexp = "STUDYID", ignore.case = TRUE)
5+
})
6+
7+
test_that("check_data returns list with studyid table", {
8+
df <- tibble::tibble(STUDYID = c("S1","S1","S2"))
9+
10+
out <- capture.output(res <- check_data(df, age_in_years = TRUE)) # capture plot output
11+
12+
expect_type(res, "list")
13+
expect_true("studyid" %in% names(res))
14+
expect_s3_class(res$studyid, "data.frame")
15+
expect_equal(res$studyid$n[res$studyid$STUDYID == "S1"], 2)
16+
})
17+
18+
test_that("SEX table included when SEX column present (counts include NA)", {
19+
df <- tibble::tibble(STUDYID = rep("S1", 4),
20+
SEX = c("M", "F", NA, "M"))
21+
22+
res <- capture.output(out <- check_data(df, age_in_years = TRUE))
23+
24+
expect_true("sex" %in% names(out))
25+
sex_df <- out$sex
26+
expect_true(all(c("SEX", "n") %in% colnames(sex_df)))
27+
# Expect 'M' count = 2
28+
expect_equal(as.integer(sex_df[which(sex_df$SEX == "M"), "n"]), 2L)
29+
# NA included
30+
expect_true(any(is.na(sex_df$SEX)))
31+
})
32+
33+
test_that("AGE branch converts ages when age_in_years = FALSE and AGEU present; age metrics computed", {
34+
df <- tibble::tibble(
35+
STUDYID = "S1",
36+
USUBJID = c("A","B","C","D"),
37+
AGE = c(6*30.417, 0.25*365.25, 20, NA), # mix: months/days/years (but convert_age_to_years expects AGEU; we'll make AGEU so conversion runs)
38+
AGEU = c("MONTHS", "DAYS", "YEARS", "YEARS")
39+
)
40+
41+
# Using age_in_years = FALSE -> function will call convert_age_to_years()
42+
out <- capture.output(res <- check_data(df, age_in_years = FALSE))
43+
44+
expect_true("age" %in% names(res))
45+
age_stats <- res$age
46+
expect_true("n_USUBJID" %in% names(age_stats))
47+
# n_USUBJID should equal number of unique USUBJID
48+
expect_equal(age_stats$n_USUBJID, length(unique(df$USUBJID)))
49+
# check thresholds: n_AGE_under_6M counts subjects < 0.5 years
50+
expect_true("n_AGE_under_6M" %in% names(age_stats))
51+
expect_equal(as.numeric(age_stats$AGE_min), 0.25)
52+
})
53+
54+
test_that("when age_in_years = TRUE, convert_age_to_years is not required", {
55+
df <- tibble::tibble(
56+
STUDYID = "S1",
57+
USUBJID = c("A","B"),
58+
AGE = c(0.2, 30)
59+
)
60+
61+
res <- capture.output(out <- check_data(df, age_in_years = TRUE))
62+
63+
expect_true("age" %in% names(out))
64+
expect_equal(out$age$AGE_min, min(df$AGE, na.rm = TRUE))
65+
})
66+
67+
test_that("testcd processing: name normalization, blanks -> NA, results precedence and summary returned", {
68+
# create a domain-like table with mixed sources
69+
df <- tibble::tibble(
70+
STUDYID = c("S1","S1","S1","S1"),
71+
USUBJID = c("a","b","c","d"),
72+
LBTESTCD = c("T1","T1","T1","T1"),
73+
LBSTRESN = c(1.5, NA, NA, NA),
74+
LBSTRESC = c(NA, "", "c_val", NA),
75+
LBMODIFY = c(NA, "mod_val", NA, NA),
76+
LBORRES = c(NA, "or_val", NA, "5.0"),
77+
LBORRESU = c(NA, "U_OR", NA, "U_OR")
78+
)
79+
80+
res <- capture.output(out <- check_data(df, age_in_years = TRUE))
81+
82+
expect_true("testcd" %in% names(out))
83+
testcd_tbl <- out$testcd
84+
expect_s3_class(testcd_tbl, "data.frame")
85+
# There should be a row for STUDYID S1 & TESTCD T1
86+
expect_true(any(testcd_tbl$STUDYID == "S1" & testcd_tbl$TESTCD == "T1"))
87+
# min should be numeric (from STRESN and ORRES)
88+
expect_true("min" %in% colnames(testcd_tbl))
89+
# n_UNITS should count unique UNITS (expect at least 1)
90+
expect_true("n_UNITS" %in% colnames(testcd_tbl))
91+
expect_true(testcd_tbl$n_UNITS[1] == 1)
92+
expect_true(testcd_tbl$UNITS[1] == "U_OR")
93+
})
94+
95+
test_that("outcome branch for INTP triggers when VISITDY/EPOCH present and TESTCD INTP exists", {
96+
df <- tibble::tibble(
97+
STUDYID = "S1",
98+
USUBJID = c("001"),
99+
PFTESTCD = c("INTP"),
100+
VISITDY = 3,
101+
EPOCH = c("FOLLOW-UP"),
102+
PFSTRESC = c("WILD TYPE")
103+
)
104+
105+
out <- capture.output(res <- check_data(df, age_in_years = TRUE))
106+
107+
expect_true("testcd" %in% names(res))
108+
expect_true("outcome" %in% names(res))
109+
expect_true(res$outcome$`n_INTP_<DAY7` == 1)
110+
})
111+
112+
test_that("outcome branch for WHOMAL01 triggers only when STRESC==ACPR and timing matches", {
113+
df <- tibble::tibble(
114+
STUDYID = c("S1","S1"),
115+
USUBJID = c("001","002"),
116+
RSTESTCD = c("WHOMAL01","WHOMAL01"),
117+
RSSTRESC = c("ACPR","NO"),
118+
VISITDY = c(1, 1),
119+
EPOCH = c("BASELINE", "BASELINE")
120+
)
121+
122+
out <- capture.output(res <- check_data(df, age_in_years = TRUE))
123+
124+
expect_true(res$outcome$`n_WHOMAL01_<DAY2` == 1)
125+
})
126+
127+
test_that("missingness vector computed and present in returned list", {
128+
df <- tibble::tibble(
129+
STUDYID = c("S","S","S"),
130+
A = c(1, NA, 3),
131+
B = c(NA, NA, NA)
132+
)
133+
134+
out <- capture.output(res <- check_data(df, age_in_years = TRUE))
135+
136+
expect_true("missingness" %in% names(res))
137+
miss <- res$missingness
138+
expect_true("A" %in% names(miss) & "B" %in% names(miss))
139+
# A proportion missing = 1/3 rounded to 3 decimals = 0.333
140+
expect_equal(as.numeric(miss["A"]), round(1/3, 3))
141+
expect_equal(as.numeric(miss["B"]), 1.000)
142+
})

0 commit comments

Comments
 (0)