@@ -4,12 +4,12 @@ test_that("findings domain: STRESN → STRESC → MODIFY → ORRES precedence, T
44 USUBJID = c(" S1" , " S2" , " S3" , " S4" ),
55 LBTESTCD = c(" HGB" , " ALT" , " ALT" , " HGB" ),
66 LBSTDY = c(" 1" , " 2" , " 3" , " 4" ),
7- LBORRES = c(" fourteen" , " sechs" , " 0" , NA_character_ ),
8- LBORRESU = c(" U_ORRES" , " U_ORRES" , " U_ORRES" , NA_character_ ),
9- LBMODIFY = c(" 14_mod" , " six" , NA_character_ , NA_character_ ),
10- LBSTRESN = c(14 , NA_real_ , NA_real_ , NA_real_ ),
11- LBSTRESU = c(" U" , NA_character_ , NA_character_ , NA_character_ ),
12- LBSTRESC = c(" 14_char" , NA_character_ , NA_character_ , NA_character_ )
7+ LBORRES = c(" fourteen" , " sechs" , " 0" , " " ),
8+ LBORRESU = c(" U_ORRES" , " U_ORRES" , " U_ORRES" , " " ),
9+ LBMODIFY = c(" 14_mod" , " six" , NA_character_ , " " ),
10+ LBSTRESN = c(14 , NA_real_ , NA_real_ , " " ),
11+ LBSTRESU = c(" U" , NA_character_ , NA_character_ , " " ),
12+ LBSTRESC = c(" 14_char" , NA_character_ , NA_character_ , " " )
1313 )
1414
1515 out <- prepare_domain(" LB" , lb , print_messages = FALSE )
@@ -28,7 +28,7 @@ test_that("findings domain: STRESN → STRESC → MODIFY → ORRES precedence, T
2828 expect_equal(out $ TIME_SOURCE [1 ], " STDY" )
2929})
3030
31- test_that(" include_LOC/include_METHOD produce expected column-name patterns when both present " , {
31+ test_that(" include_LOC/include_METHOD produce expected column-name patterns" , {
3232 lb <- tibble :: tibble(
3333 STUDYID = " ST1" , USUBJID = " S1" ,
3434 LBTESTCD = " HGB" ,
@@ -38,19 +38,28 @@ test_that("include_LOC/include_METHOD produce expected column-name patterns when
3838 LBLOC = " L1" , LBMETHOD = " M1"
3939 )
4040
41- out <- prepare_domain(" LB" , lb , include_LOC = TRUE , include_METHOD = TRUE , print_messages = FALSE )
41+ out_none <- prepare_domain(" LB" , lb , include_LOC = FALSE , include_METHOD = FALSE , variables_include = c(" HGB" ), timing_variables = c(" LBSTDY" ), print_messages = FALSE )
42+ out_loc <- prepare_domain(" LB" , lb , include_LOC = TRUE , include_METHOD = FALSE , variables_include = c(" HGB" ), timing_variables = c(" LBSTDY" ), print_messages = FALSE )
43+ out_method <- prepare_domain(" LB" , lb , include_LOC = FALSE , include_METHOD = TRUE , variables_include = c(" HGB" ), timing_variables = c(" LBSTDY" ), print_messages = FALSE )
44+ out_both <- prepare_domain(" LB" , lb , include_LOC = TRUE , include_METHOD = TRUE , variables_include = c(" HGB" ), timing_variables = c(" LBSTDY" ), print_messages = FALSE )
4245
4346 # When both included, names_glue = "{TESTCD}_{LOC}_{METHOD}_{UNITS}_{.value}"
4447 # After removing _RESULTS we expect column "HGB_L1_M1_U"
45- expect_true(" HGB_L1_M1_U" %in% colnames(out ))
46- expect_equal(as.character(out $ HGB_L1_M1_U [1 ]), " 7" )
48+ expect_true(" HGB_L1_M1_U" %in% colnames(out_both ))
49+ expect_true(" HGB_L1_U" %in% colnames(out_loc ))
50+ expect_true(" HGB_M1_U" %in% colnames(out_method ))
51+ expect_true(" HGB_U" %in% colnames(out_none ))
52+
53+ expect_equal(as.character(out_both $ HGB_L1_M1_U [1 ]), " 7" )
54+ expect_equal(as.character(out_loc $ HGB_L1_U [1 ]), " 7" )
55+ expect_equal(as.character(out_method $ HGB_M1_U [1 ]), " 7" )
56+ expect_equal(as.character(out_none $ HGB_U [1 ]), " 7" )
4757})
4858
4959test_that(" include_LOC warns and is reset to FALSE if domain does not have LOC" , {
5060 lb <- tibble :: tibble(
5161 STUDYID = " ST1" , USUBJID = " S1" ,
52- LBTESTCD = " HGB" ,
53- LBSTDY = " 1" ,
62+ LBTESTCD = " HGB" , LBSTDY = " 1" ,
5463 LBSTRESN = 7 , LBSTRESU = " U" ,
5564 LBORRES = " seven" , LBORRESU = " units"
5665 )
@@ -61,16 +70,42 @@ test_that("include_LOC warns and is reset to FALSE if domain does not have LOC",
6170 expect_false(any(grepl(" _L" , colnames(out )) & grepl(" _U" , colnames(out ))))
6271})
6372
73+ test_that(" include_METHOD warns and is reset to FALSE if domain does not have METHOD" , {
74+ lb <- tibble :: tibble(
75+ STUDYID = " ST1" , USUBJID = " S1" ,
76+ LBTESTCD = " HGB" , LBSTDY = " 1" ,
77+ LBSTRESN = 7 , LBSTRESU = " U" ,
78+ LBORRES = " seven" , LBORRESU = " units"
79+ )
80+
81+ expect_warning(out <- prepare_domain(" LB" , lb , include_METHOD = TRUE , print_messages = FALSE ),
82+ regexp = " does not have a method" , ignore.case = TRUE )
83+ # no METHOD token present in column names
84+ expect_false(any(grepl(" _M" , colnames(out )) & grepl(" _U" , colnames(out ))))
85+ })
86+
87+ test_that(" spaces in generated column names are replaced with underscores" , {
88+ lb <- tibble :: tibble(
89+ STUDYID = " S" , USUBJID = " P1" ,
90+ LBTESTCD = " TEST" , LBSTDY = " 1" ,
91+ LBSTRESN = 5 , LBSTRESU = " per litre" ,
92+ LBORRES = " five" , LBORRESU = " pL"
93+ )
94+ out <- prepare_domain(" LB" , lb , variables_include = c(" TEST" ), print_messages = FALSE )
95+ # generated column should use underscore rather than space
96+ expect_true(any(grepl(" per_litre" , colnames(out ))))
97+ })
98+
6499test_that(" variables_include filters to requested TESTCDs before pivot" , {
65100 lb <- tibble :: tibble(
66- STUDYID = " ST1" , USUBJID = c(" S1" ," S1" ),
67- LBTESTCD = c(" HGB" ," ALT" ),
68- LBSTDY = c(" 1" ," 1" ),
69- LBSTRESN = c(10 , 99 ), LBSTRESU = c(" U" ," U" ),
70- LBORRES = c(" ten" , " ninty-nine" ), LBORRESU = " units"
101+ STUDYID = " ST1" , USUBJID = c(" S1" ," S1" , " S2 " ),
102+ LBTESTCD = c(" HGB" ," ALT" , " HGB " ),
103+ LBSTDY = c(" 1" ," 1" , " 1 " ),
104+ LBSTRESN = c(10 , 99 , 22 ), LBSTRESU = c(" U" ," U " , " U" ),
105+ LBORRES = c(" ten" , " ninty-nine" , " twenty two " ), LBORRESU = " units"
71106 )
72107
73- out <- prepare_domain(" LB" , lb , variables_include = c(" HGB " ), print_messages = FALSE )
108+ out <- prepare_domain(" LB" , lb , variables_include = c(" hgB " ), print_messages = FALSE )
74109
75110 # ALT should be filtered out before pivot
76111 expect_false(any(grepl(" ^ALT" , colnames(out ))))
@@ -143,16 +178,18 @@ test_that("event domain: EVENT chosen from DECOD/MODIFY/TERM and PRESP/OCCUR def
143178
144179test_that(" DS domain returns STUDYID, USUBJID, TIME, TIME_SOURCE, EVENT columns" , {
145180 ds <- tibble :: tibble(
146- STUDYID = " S" , USUBJID = " P1" ,
147- DSTERM = " treatment completed" ,
148- DSDECOD = " TREATED" ,
149- DSSTDY = " 10"
181+ STUDYID = " S" , USUBJID = c(" A" ," B" ," C" ),
182+ DSTERM = c(" t1" ," t2" ," t3" ),
183+ DSMODIFY = c(NA , " MOD" , NA ),
184+ DSDECOD = c(NA , NA , " DEC" ),
185+ DSSTDY = 28
150186 )
151187
152188 out <- prepare_domain(" DS" , ds , print_messages = FALSE )
153189 expect_true(all(c(" STUDYID" , " USUBJID" , " TIME" , " TIME_SOURCE" , " EVENT" ) %in% colnames(out )))
154- # TIME_SOURCE prefix replaced
155190 expect_equal(out $ TIME_SOURCE [1 ], " STDY" )
191+ expect_true(" MOD" %in% out $ EVENT )
192+ expect_true(" DEC" %in% out $ EVENT )
156193})
157194
158195test_that(" special domain DM: convert blanks to NA and variables_include selection works" , {
@@ -185,3 +222,12 @@ test_that("check print_messages parameter option", {
185222 expect_output(prepare_domain(" SA" , sa , variables_include = c(" FEVER" ), print_messages = TRUE ),
186223 regexp = " Number of rows where values_fn has been used to pick record" )
187224})
225+
226+ test_that(" prepare_domain errors when required STUDYID or USUBJID missing" , {
227+ df_missing <- tibble :: tibble(USUBJID = " P1" ) # STUDYID missing
228+ expect_error(prepare_domain(" LB" , df_missing ), regexp = " STUDYID|USUBJID" , ignore.case = TRUE )
229+
230+ df_missing2 <- tibble :: tibble(STUDYID = " S1" ) # USUBJID missing
231+ expect_error(prepare_domain(" LB" , df_missing2 ), regexp = " STUDYID|USUBJID" , ignore.case = TRUE )
232+ })
233+
0 commit comments