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

Fix bug when reason clumn name for entry/exit is already in cohort #379

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions R/exitAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ exitAtColumnDate <- function(cohort,
reason <- "entry_reason"
}

if (reason %in% colnames(cohort) & returnReason) {
cli::cli_inform("Column {reason} will be overwritten.")
cohort <- cohort |> dplyr::select(!dplyr::all_of(reason))
} else if (reason %in% colnames(cohort) & !returnReason) {
reason <- "date_column_name_1234"
}

# check NA
checkNA <- cohort |>
dplyr::filter(dplyr::if_all(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-entryAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test_that("test indexes - postgres", {
cohort_end_date = as.Date("2009-01-03"),
other_date = as.Date("2009-01-01")))
cdm$my_cohort <- omopgenerics::newCohortTable(cdm$my_cohort)
cdm$my_cohort <- entryAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = FALSE)
cdm$my_cohort <- entryAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-exitAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ test_that("test indexes - postgres", {
cohort_end_date = as.Date("2009-01-03"),
other_date = as.Date("2009-01-01")))
cdm$my_cohort <- omopgenerics::newCohortTable(cdm$my_cohort)
cdm$my_cohort <- exitAtLastDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = FALSE)
cdm$my_cohort <- exitAtLastDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- exitAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_start_date", "cohort_end_date"), returnReason = FALSE)
cdm$my_cohort <- exitAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_start_date", "cohort_end_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-requireIsEntry.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ test_that("test indexes - postgres", {
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- requireIsEntry(cdm$my_cohort, entryRange = c(1,Inf))
expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- requireIsFirstEntry(cdm$my_cohort)
expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand All @@ -258,5 +264,6 @@ test_that("test indexes - postgres", {
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

omopgenerics::dropTable(cdm = cdm, name = dplyr::starts_with("my_cohort"))
CDMConnector::cdm_disconnect(cdm = cdm)
})
Loading