From b3d49b80a39814039a4612c6cde698c9e96cd663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=BAria=20Mercad=C3=A9-Besora?= <61558739+nmercadeb@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:46:27 +0000 Subject: [PATCH] issue #376 --- R/exitAtColumnDate.R | 7 +++++++ tests/testthat/test-entryAtColumnDate.R | 2 +- tests/testthat/test-exitAtColumnDate.R | 4 ++-- tests/testthat/test-requireIsEntry.R | 7 +++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/R/exitAtColumnDate.R b/R/exitAtColumnDate.R index 31a9945..62b4014 100644 --- a/R/exitAtColumnDate.R +++ b/R/exitAtColumnDate.R @@ -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( diff --git a/tests/testthat/test-entryAtColumnDate.R b/tests/testthat/test-entryAtColumnDate.R index db3ed4a..dc1e72f 100644 --- a/tests/testthat/test-entryAtColumnDate.R +++ b/tests/testthat/test-entryAtColumnDate.R @@ -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") == diff --git a/tests/testthat/test-exitAtColumnDate.R b/tests/testthat/test-exitAtColumnDate.R index f661546..94b4e33 100644 --- a/tests/testthat/test-exitAtColumnDate.R +++ b/tests/testthat/test-exitAtColumnDate.R @@ -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") == diff --git a/tests/testthat/test-requireIsEntry.R b/tests/testthat/test-requireIsEntry.R index 4c553ad..65215ea 100644 --- a/tests/testthat/test-requireIsEntry.R +++ b/tests/testthat/test-requireIsEntry.R @@ -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") == @@ -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) })