|
| 1 | +library(testthat) |
| 2 | +library(dplyr) |
| 3 | +library(purrr) |
| 4 | +library(DBI) |
| 5 | +library(duckdb) |
| 6 | +library(lubridate) |
| 7 | + |
1 | 8 | testthat::test_that("get_hipaa_disclosure_log_from_ehr_fhir_logs works", {
|
2 | 9 | # read our test data
|
3 | 10 | directory_under_test_path <- "hipaa_disclosure_log"
|
| 11 | + |
4 | 12 | test_tables <- c(
|
5 | 13 | "redcap_ehr_fhir_logs",
|
6 | 14 | "redcap_user_information",
|
7 | 15 | "redcap_projects"
|
8 | 16 | )
|
9 | 17 |
|
10 | 18 | conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:")
|
11 |
| - purrr::walk(test_tables, create_a_table_from_rds_test_data, conn, "hipaa_disclosure_log") |
12 | 19 |
|
| 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 |
13 | 36 | required_names <- c(
|
14 | 37 | "disclosure_date", "fhir_id", "mrn", "project_irb_number"
|
15 | 38 | )
|
16 | 39 |
|
17 | 40 | result <- get_hipaa_disclosure_log_from_ehr_fhir_logs(conn)
|
18 | 41 |
|
19 |
| - # test for the required columns |
20 | 42 | testthat::expect_contains(names(result), required_names)
|
21 |
| - # test for at least one row |
22 | 43 | testthat::expect_gt(nrow(result), 0)
|
23 |
| - # test for only distinct rows |
24 | 44 | testthat::expect_equal(
|
25 | 45 | 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) |
27 | 66 |
|
28 |
| - DBI::dbDisconnect(conn, shutdown=TRUE) |
| 67 | + DBI::dbDisconnect(conn, shutdown = TRUE) |
29 | 68 | })
|
0 commit comments