Skip to content

Commit

Permalink
feat: add join_lpr3 with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
signekb committed Jun 19, 2024
1 parent 2668ef2 commit 154e6b4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
20 changes: 20 additions & 0 deletions R/joins.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ join_lpr2 <- function(lpr_diag, lpr_adm) {
)
}

#' Join together the LPR3 (`diagnoser` and `kontakter`) registers.
#'
#' @param diagnoser The diagnosis register.
#' @param kontakter The contacts register.
#'
#' @return The same class as the input, defaults to a [tibble::tibble()].
#' @keywords internal
#'
#' @examples
#' register_data$diagnoser |>
#' join_lpr3(register_data$kontakter)
join_lpr3 <- function(diagnoser, kontakter) {
verify_required_variables(diagnoser, "diagnoser")
verify_required_variables(kontakter, "kontakter")
dplyr::full_join(
column_names_to_lower(kontakter),
column_names_to_lower(diagnoser),
by = "dw_ek_kontakt"
)
}
65 changes: 65 additions & 0 deletions tests/testthat/test-joins.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# join_lpr2 -----------------------------------------------------------------

actual_lpr_diag <- tibble::tibble(
recnum = c(1:2),
c_diag = 1:2,
Expand Down Expand Up @@ -56,3 +58,66 @@ test_that("joining works for data.table", {

expect_contains(class(actual), "data.table")
})

# join_lpr3 -----------------------------------------------------------------

actual_diagnoser <- tibble::tibble(
dw_ek_kontakt = 1:2,
diagnosekode = c("DA071","DD075"),
diagnosetype = c("A", "B"),
senere_afkraefter = c("Nej", "Ja")
)

actual_kontakter <- tibble::tibble(
cpr = c(1, 1, 2),
dw_ek_kontakt = 1:3,
dato_start = c("20230101", "20220101", "20200101"),
hovedspaciale_ans = c("Neurologi", "Akut medicin", "Kardiologi"),
)

expected_lpr3 <- tibble::tibble(
cpr = c(1, 1, 2),
dw_ek_kontakt = c(1,2,3),
dato_start = c("20230101", "20220101", "20200101"),
hovedspaciale_ans = c("Neurologi", "Akut medicin", "Kardiologi"),
diagnosekode = c("DA071","DD075", NA),
diagnosetype = c("A", "B", NA),
senere_afkraefter = c("Nej", "Ja", NA),
)

test_that("joining LPR3 correctly", {
actual <- join_lpr3(
actual_diagnoser,
actual_kontakter
)

expect_equal(actual, expected_lpr3)
})

test_that("joining works for DuckDB Database", {
actual <- arrow::to_duckdb(actual_diagnoser) |>
join_lpr3(arrow::to_duckdb(actual_kontakter))

expect_contains(class(actual), "tbl_duckdb_connection")
})

test_that("joining works for Arrow Tables (from Parquet)", {
actual <- arrow::as_arrow_table(actual_diagnoser) |>
join_lpr3(arrow::as_arrow_table(actual_kontakter))

expect_contains(class(actual), "arrow_dplyr_query")
})

test_that("joining works for data.frame", {
actual <- as.data.frame(actual_diagnoser) |>
join_lpr3(as.data.frame(actual_kontakter))

expect_contains(class(actual), "data.frame")
})

test_that("joining works for data.table", {
actual <- data.table::as.data.table(actual_diagnoser) |>
join_lpr3(data.table::as.data.table(actual_kontakter))

expect_contains(class(actual), "data.table")
})

0 comments on commit 154e6b4

Please sign in to comment.