Skip to content

Commit

Permalink
Update test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R
Browse files Browse the repository at this point in the history
Add additional tests to for the filters in  get_hipaa_disclosure_log_from_ehr_fhir_logs() function.
  • Loading branch information
saipavan10-git committed Jul 1, 2024
1 parent 5747ff2 commit 607e1e6
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions tests/testthat/test-get_hipaa_disclosure_log_from_ehr_fhir_logs.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,68 @@
library(testthat)
library(dplyr)
library(purrr)
library(DBI)
library(duckdb)
library(lubridate)

testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
# read our test data
directory_under_test_path <- "hipaa_disclosure_log"

test_tables <- c(
"redcap_ehr_fhir_logs",
"redcap_user_information",
"redcap_projects"
)

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

purrr::walk(test_tables, create_a_table_from_rds_test_data, conn, directory_under_test_path)

# Mutate the redcap_ehr_fhir_logs table after loading it
redcap_ehr_fhir_logs <- dplyr::tbl(conn, "redcap_ehr_fhir_logs") |>
dplyr::filter(.data$resource_type == "Patient" & .data$mrn != "") |>
head(n = 30) |>
dplyr::collect() |>
dplyr::mutate(
ehr_id = sample(1:3, n(), replace = TRUE),
created_at = seq.Date(from = Sys.Date() - 10, to = Sys.Date(), length.out = n())
)

# Write the mutated data back to the database
dbWriteTable(conn, "redcap_ehr_fhir_logs", redcap_ehr_fhir_logs, overwrite = TRUE)

# Required column names
required_names <- c(
"disclosure_date", "fhir_id", "mrn", "project_irb_number"
)

result <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn)

# test for the required columns
testthat::expect_contains(names(result), required_names)
# test for at least one row
testthat::expect_gt(nrow(result), 0)
# test for only distinct rows
testthat::expect_equal(
nrow(result),
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow())
result |> distinct(disclosure_date, fhir_id, mrn, project_irb_number, username) |> nrow()
)

result_filtered_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 1)
testthat::expect_true(all(result_filtered_ehr_id$ehr_id == 1))

start_date <- Sys.Date() - 5
result_filtered_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = start_date)
testthat::expect_true(all(result_filtered_date$disclosure_date >= start_date))

result_combined_filters <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 2, start_date = start_date)
testthat::expect_true(all(result_combined_filters$ehr_id == 2))
testthat::expect_true(all(result_combined_filters$disclosure_date >= start_date))

result_nonexistent_ehr_id <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, ehr_id = 9999)
testthat::expect_equal(nrow(result_nonexistent_ehr_id), 0)

future_start_date <- Sys.Date() + 1
result_future_date <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn, start_date = future_start_date)
testthat::expect_equal(nrow(result_future_date), 0)

DBI::dbDisconnect(conn, shutdown=TRUE)
DBI::dbDisconnect(conn, shutdown = TRUE)
})

0 comments on commit 607e1e6

Please sign in to comment.