Skip to content

Commit

Permalink
Closes #2575 - derive_vars_transposed() -- test -- left-join's `relat…
Browse files Browse the repository at this point in the history
…ionship` argument (#2597)

* 2575 - derive_vars_transposed() -- test -- left-join's relationship =

* 2575 test-derive_vars_transposed -- lint
  • Loading branch information
jimrothstein authored Dec 10, 2024
1 parent 6351451 commit 6772320
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions tests/testthat/test-derive_vars_transposed.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,97 @@ test_that("derive_vars_transposed Test 5: ATC variables are merged properly", {
keys = c("STUDYID", "USUBJID", "CMDECOD", "ATC4CD")
)
})

## Test 6: `Relationship` argument handled by left_join ----
test_that("derive_vars_transposed Test 6: left_join throws error when argument specified by
`relationship` is incorrect", {
cm <- tibble::tribble(
~STUDYID, ~USUBJID, ~CMGRPID, ~CMREFID, ~CMDECOD,
"STUDY01", "BP40257-1001", "14", "1192056", "PARACETAMOL",
"STUDY01", "BP40257-1001", "18", "2007001", "SOLUMEDROL",
"STUDY01", "BP40257-1002", "19", "2791596", "SPIRONOLACTONE"
)
facm <- tibble::tribble(
~STUDYID, ~USUBJID, ~FAGRPID, ~FAREFID, ~FATESTCD, ~FASTRESC,
"STUDY01", "BP40257-1001", "1", "1192056", "CMATC1CD", "N",
"STUDY01", "BP40257-1001", "1", "1192056", "CMATC2CD", "N02",
"STUDY01", "BP40257-1001", "1", "1192056", "CMATC3CD", "N02B",
"STUDY01", "BP40257-1001", "1", "1192056", "CMATC4CD", "N02BE",
"STUDY01", "BP40257-1001", "1", "2007001", "CMATC1CD", "D",
"STUDY01", "BP40257-1001", "1", "2007001", "CMATC2CD", "D10",
"STUDY01", "BP40257-1001", "1", "2007001", "CMATC3CD", "D10A",
"STUDY01", "BP40257-1001", "1", "2007001", "CMATC4CD", "D10AA",
"STUDY01", "BP40257-1001", "2", "2007001", "CMATC1CD", "D",
"STUDY01", "BP40257-1001", "2", "2007001", "CMATC2CD", "D07",
"STUDY01", "BP40257-1001", "2", "2007001", "CMATC3CD", "D07A",
"STUDY01", "BP40257-1001", "2", "2007001", "CMATC4CD", "D07AA",
"STUDY01", "BP40257-1001", "3", "2007001", "CMATC1CD", "H",
"STUDY01", "BP40257-1001", "3", "2007001", "CMATC2CD", "H02",
"STUDY01", "BP40257-1001", "3", "2007001", "CMATC3CD", "H02A",
"STUDY01", "BP40257-1001", "3", "2007001", "CMATC4CD", "H02AB",
"STUDY01", "BP40257-1002", "1", "2791596", "CMATC1CD", "C",
"STUDY01", "BP40257-1002", "1", "2791596", "CMATC2CD", "C03",
"STUDY01", "BP40257-1002", "1", "2791596", "CMATC3CD", "C03D",
"STUDY01", "BP40257-1002", "1", "2791596", "CMATC4CD", "C03DA"
)

dup <- tibble(
STUDYID = "STUDYID01",
USUBJID = "BP40257-1001",
FAGRPID = "1",
FAREFID = "1192056"
)

facm1 <- bind_rows(facm, dup)

expect_no_error(
cm %>%
derive_vars_transposed(
facm,
by_vars = exprs(USUBJID, CMREFID = FAREFID),
id_vars = exprs(FAGRPID),
key_var = FATESTCD,
value_var = FASTRESC,
relationship = "one-to-many"
) %>%
select(USUBJID, CMDECOD, starts_with("CMATC"))
)

expect_error(
cm %>%
derive_vars_transposed(
facm,
by_vars = exprs(USUBJID, CMREFID = FAREFID),
id_vars = exprs(FAGRPID),
key_var = FATESTCD,
value_var = FASTRESC,
relationship = "one-to-one"
) %>%
select(USUBJID, CMDECOD, starts_with("CMATC"))
)
expect_error(
cm %>%
derive_vars_transposed(
facm,
by_vars = exprs(USUBJID, CMREFID = FAREFID),
id_vars = exprs(FAGRPID),
key_var = FATESTCD,
value_var = FASTRESC,
relationship = "many-to-one"
) %>%
select(USUBJID, CMDECOD, starts_with("CMATC"))
)

expect_error(
cm %>%
derive_vars_transposed(
facm1,
by_vars = exprs(USUBJID, CMREFID = FAREFID),
id_vars = exprs(FAGRPID),
key_var = FATESTCD,
value_var = FASTRESC,
relationship = "one-to-one"
) %>%
select(USUBJID, CMDECOD, starts_with("CMATC"))
)
})

0 comments on commit 6772320

Please sign in to comment.