Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #2575 - derive_vars_transposed() -- test -- left-join's relationship argument #2597

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
)
})
Loading