diff --git a/tests/testthat/test-derive_vars_transposed.R b/tests/testthat/test-derive_vars_transposed.R index 3126af5b27..c7ddcb2b73 100644 --- a/tests/testthat/test-derive_vars_transposed.R +++ b/tests/testthat/test-derive_vars_transposed.R @@ -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")) + ) +})