Skip to content

Commit 5590ed4

Browse files
authored
Merge pull request #162 from ctsit/use_params_in_get_hipaa_disclosure_log_from_ehr_fhir_logs
Use parameters in get_hipaa_disclosure_log_from_ehr_fhir_logs.R
2 parents c35c8c7 + 06500e1 commit 5590ed4

2 files changed

+53
-7
lines changed

R/get_hipaa_disclosure_log_from_ehr_fhir_logs.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs <- function(
3030
conn,
3131
ehr_id = NA_real_,
3232
start_date = as.Date(NA)) {
33+
34+
# rename parameters for local use
35+
ehr_id_local <- ehr_id
36+
3337
# make DBI objects for joins
3438
user_information <- dplyr::tbl(conn, "redcap_user_information") |>
3539
dplyr::select(
@@ -49,8 +53,11 @@ get_hipaa_disclosure_log_from_ehr_fhir_logs <- function(
4953
"project_irb_number"
5054
)
5155

52-
disclosures <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
56+
disclosures <-
57+
dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
5358
dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |>
59+
dplyr::filter(is.na(start_date) | .data$created_at >= start_date) |>
60+
dplyr::filter(is.na(ehr_id_local) | ehr_id_local == .data$ehr_id) |>
5461
dplyr::left_join(user_information, by = c("user_id" = "ui_id")) |>
5562
dplyr::left_join(projects, by = c("project_id")) |>
5663
dplyr::collect() |>
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
1+
library(testthat)
2+
library(dplyr)
3+
library(purrr)
4+
library(DBI)
5+
library(duckdb)
6+
library(lubridate)
7+
18
testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
29
# read our test data
310
directory_under_test_path <- "hipaa_disclosure_log"
11+
412
test_tables <- c(
513
"redcap_ehr_fhir_logs",
614
"redcap_user_information",
715
"redcap_projects"
816
)
917

1018
conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:")
11-
purrr::walk(test_tables, create_a_table_from_rds_test_data, conn, "hipaa_disclosure_log")
1219

20+
purrr::walk(test_tables, create_a_table_from_rds_test_data, conn, directory_under_test_path)
21+
22+
# Mutate the redcap_ehr_fhir_logs table after loading it
23+
redcap_ehr_fhir_logs <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
24+
dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |>
25+
head(n = 30) |>
26+
dplyr::collect() |>
27+
dplyr::mutate(
28+
ehr_id = sample(1:3, n(), replace = TRUE),
29+
created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = n())
30+
)
31+
32+
# Write the mutated data back to the database
33+
duckdb_register(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs)
34+
35+
# Required column names
1336
required_names <- c(
1437
"disclosure_date", "fhir_id", "mrn", "project_irb_number"
1538
)
1639

1740
result <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn)
1841

19-
# test for the required columns
2042
testthat::expect_contains(names(result), required_names)
21-
# test for at least one row
2243
testthat::expect_gt(nrow(result), 0)
23-
# test for only distinct rows
2444
testthat::expect_equal(
2545
nrow(result),
26-
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow())
46+
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow()
47+
)
48+
49+
result_filtered_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 1)
50+
testthat::expect_true(all(result_filtered_ehr_id$ehr_id == 1))
51+
52+
start_date <- Sys.Date() - 5
53+
result_filtered_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date)
54+
testthat::expect_true(all(result_filtered_date$disclosure_date >= start_date))
55+
56+
result_combined_filters <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 2, start_date = start_date)
57+
testthat::expect_true(all(result_combined_filters$ehr_id == 2))
58+
testthat::expect_true(all(result_combined_filters$disclosure_date >= start_date))
59+
60+
result_nonexistent_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 9999)
61+
testthat::expect_equal(nrow(result_nonexistent_ehr_id), 0)
62+
63+
future_start_date <- Sys.Date() + 1
64+
result_future_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = future_start_date)
65+
testthat::expect_equal(nrow(result_future_date), 0)
2766

28-
DBI::dbDisconnect(conn, shutdown=TRUE)
67+
DBI::dbDisconnect(conn, shutdown = TRUE)
2968
})

0 commit comments

Comments
 (0)