Skip to content

Commit

Permalink
Switched from checkmate to admiraldev assertions; added tests for exa…
Browse files Browse the repository at this point in the history
…mple + validations
  • Loading branch information
kamilsi committed May 20, 2024
1 parent b4fa080 commit c0f90b9
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 107 deletions.
84 changes: 24 additions & 60 deletions R/derive_blfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,65 +205,31 @@ derive_blfl <- function(sdtm_in,
baseline_visits = character(),
baseline_timepoints = character()) {
# Check assertions --------------------------------------------------------
assertion_collection <- checkmate::makeAssertCollection()
# Assert that sdtm_in is a data frame,
checkmate::assert_data_frame(sdtm_in,
col.names = "strict",
min.rows = 1L,
add = assertion_collection)

# Assert that the input dataset has a "DOMAIN" column
checkmate::assert_names(names(sdtm_in),
must.include = c("DOMAIN", sdtm.oak:::oak_id_vars()),
.var.name = "Columns of 'sdtm_inaset'",
add = assertion_collection)
# Check variables are character scalars
admiraldev::assert_character_scalar(tgt_var)
admiraldev::assert_character_scalar(ref_var)

# Assert dm_domain is data.frame
checkmate::assert_data_frame(dm_domain,
col.names = "strict",
min.rows = 1L,
add = assertion_collection)

# Check if USUBJID and reference_date is present in the DM
checkmate::assert_names(names(dm_domain),
must.include = c("USUBJID", ref_var),
.var.name = "Columns of 'dm_domain'",
add = assertion_collection)

checkmate::assert_character(tgt_var,
min.chars = 1L,
len = 1L,
add = assertion_collection)

checkmate::assert_names(tgt_var,
type = "strict",
add = assertion_collection)

checkmate::assert_character(ref_var,
min.chars = 1L,
len = 1L,
add = assertion_collection)
# Assert that sdtm_in is a data frame, contains DOMAIN and oak id vars
admiraldev::assert_data_frame(
sdtm_in,
required_vars = rlang::syms(c("DOMAIN", sdtm.oak:::oak_id_vars()))
)

checkmate::assert_names(ref_var,
type = "strict",
add = assertion_collection)

checkmate::reportAssertions(assertion_collection)
# Assert dm_domain is data.frame
admiraldev::assert_data_frame(
dm_domain,
required_vars = rlang::syms(c("USUBJID", ref_var))
)

# Get domain from input dataset
domain <- unique(sdtm_in$DOMAIN)
checkmate::assert_character(domain,
min.chars = 1L,
len = 1L,
add = assertion_collection)

admiraldev::assert_character_scalar(domain)

# Assert that tgt_var is a concatenation of domain and "BLFL" or "LOBXFL"
checkmate::assert_choice(
tgt_var,
choices = c(paste0(domain, "BLFL"),
paste0(domain, "LOBXFL")),
add = assertion_collection
)
admiraldev::assert_character_scalar(tgt_var,
values = c(paste0(domain, "BLFL"),
paste0(domain, "LOBXFL")))

# Determine domain prefixed columns
suffixes <-
Expand All @@ -274,14 +240,12 @@ derive_blfl <- function(sdtm_in,
setNames(tolower(suffixes))

# Assert that the input dataset has a "DTC" column
checkmate::assert_names(names(sdtm_in),
must.include = c(domain_prefixed_names[c("orres",
"stat",
"testcd",
"dtc")]),
.var.name = "Columns of 'sdtm_in'",
add = assertion_collection)
checkmate::reportAssertions(assertion_collection)
admiraldev::assert_data_frame(
sdtm_in,
required_vars = rlang::syms(c(domain_prefixed_names[c("orres",
"stat",
"testcd",
"dtc")])))

# End of assertions, work begins ------------------------------------------
# Create copy of input dataset for modification and processing
Expand Down
101 changes: 54 additions & 47 deletions man/derive_blfl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions tests/testthat/_snaps/derive_blfl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# derive_blfl example works

{
"type": "list",
"attributes": {
"class": {
"type": "character",
"attributes": {},
"value": ["tbl_df", "tbl", "data.frame"]
},
"row.names": {
"type": "integer",
"attributes": {},
"value": [1, 2, 3, 4, 5, 6, 7, 8, 9]
},
"names": {
"type": "character",
"attributes": {},
"value": ["DOMAIN", "oak_id", "raw_source", "patient_number", "USUBJID", "VSDTC", "VSTESTCD", "VSORRES", "VSSTAT", "VSLOBXFL"]
}
},
"value": [
{
"type": "character",
"attributes": {},
"value": ["VS", "VS", "VS", "VS", "VS", "VS", "VS", "VS", "VS"]
},
{
"type": "integer",
"attributes": {},
"value": [1, 2, 1, 2, 1, 2, 1, 1, 2]
},
{
"type": "character",
"attributes": {},
"value": ["VTLS1", "VTLS1", "VTLS1", "VTLS1", "VTLS2", "VTLS2", "VTLS1", "VTLS1", "VTLS1"]
},
{
"type": "integer",
"attributes": {},
"value": [375, 375, 375, 375, 375, 375, 376, 376, 376]
},
{
"type": "character",
"attributes": {},
"value": ["test_study-375", "test_study-375", "test_study-375", "test_study-375", "test_study-375", "test_study-375", "test_study-376", "test_study-376", "test_study-376"]
},
{
"type": "character",
"attributes": {},
"value": ["2020-09-01T13:31", "2020-10-01T11:20", "2020-09-28T10:10", "2020-10-01T13:31", "2020-09-28T10:10", "2020-09-28T10:05", "2020-09-20", "2020-09-20", "2020-09-20"]
},
{
"type": "character",
"attributes": {},
"value": ["DIABP", "DIABP", "PULSE", "PULSE", "SYSBP", "SYSBP", "DIABP", "PULSE", "PULSE"]
},
{
"type": "character",
"attributes": {},
"value": ["90", "90", "ND", "85", "120", "120", "75", null, "110"]
},
{
"type": "character",
"attributes": {},
"value": [null, null, null, null, null, null, null, "NOT DONE", null]
},
{
"type": "character",
"attributes": {},
"value": ["Y", null, null, null, null, "Y", "Y", null, "Y"]
}
]
}

# derive_blfl sdmt_in validations work

Required variable `DOMAIN` is missing

---

Required variables `oak_id`, `raw_source` and `patient_number` are missing

---

Required variables `VSORRES`, `VSSTAT`, `VSTESTCD` and `VSDTC` are missing

# derive_blfl dm_domain validations work

Required variables `USUBJID` and `RFXSTDTC` are missing

# derive_blfl tgt_var and ref_var validations work

`tgt_var` must be a character scalar but is a list

---

`ref_var` must be a character scalar but is a data frame

---

`tgt_var` must be one of 'VSBLFL' or 'VSLOBXFL' but is 'DMLOBXFL'

# derive_blfl DOMAIN validation works

`domain` must be a character scalar but is `4`

Loading

0 comments on commit c0f90b9

Please sign in to comment.