Skip to content

Commit

Permalink
Merge pull request #199 from pharmaverse/LYW_unit_tests
Browse files Browse the repository at this point in the history
Lyw unit tests
  • Loading branch information
harriscw committed Aug 18, 2023
2 parents 0f56c6b + 3558f23 commit 5515783
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/testthat/test-check_dm_actarm_arm.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test_that("Returns true when no errors present",{
DM <- data.frame(USUBJID = 1:5,
ARM = c(letters[1:5]),
ACTARM = letters[1:5],
stringsAsFactors = FALSE)


expect_true(check_dm_actarm_arm(DM))

})

test_that("Returns false when errors present - 1",{
DM <- data.frame(USUBJID = 1:5,
ARM = c(letters[1:3], letters[5:6]),
ACTARM = letters[1:5],
stringsAsFactors = FALSE)

expect_false(check_dm_actarm_arm(DM))
})

test_that("Returns false when errors present - 2", {
DM <- data.frame(USUBJID = 1:5,
ACTARM = c(letters[1:3], letters[5:6]),
ARM = letters[1:5],
stringsAsFactors = FALSE)

expect_false(check_dm_actarm_arm(DM))
})

test_that("Returns false when expected column not present", {
DM <- data.frame(USUBJID = 1:5,
ARM = c(letters[1:5]),
ACTARM = letters[1:5],
stringsAsFactors = FALSE)

DM$ARM <- NULL

expect_false(check_dm_actarm_arm(DM))
})
47 changes: 47 additions & 0 deletions tests/testthat/test-check_dm_age_missing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
test_that("Returns true when no errors present", {
DM <- data.frame(
USUBJID = 1:10,
AGE = c(50,60,50,89,19,33,50,40,22,25)
)

expect_true(check_dm_age_missing(DM))
})

test_that("Returns false when errors present - 1", {
DM <- data.frame(
USUBJID = 1:10,
AGE =c(50,60,50,89,NA,33,50,40,22,25)
)

expect_false(check_dm_age_missing(DM))
})

test_that("Returns false when errors present - 2", {
DM <- data.frame(
USUBJID = 1:10,
AGE =c(50,60,50,89,91,33,50,40,22,25)
)

expect_false(check_dm_age_missing(DM))
})

test_that("Returns false when errors present - 3", {
DM <- data.frame(
USUBJID = 1:10,
AGE =c(50,60,50,89,17,33,50,40,22,25)
)

expect_false(check_dm_age_missing(DM))
})


test_that("Returns false when expected column not present", {
DM <- data.frame(
USUBJID = 1:10,
AGE = c(50,60,50,89,19,33,50,40,22,25)
)

DM$AGE = NULL

expect_false(check_dm_age_missing(DM))
})
80 changes: 80 additions & 0 deletions tests/testthat/test-check_dm_dthfl_dthdtc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
test_that("Returns true when no errors present", {

DM <- data.frame(
USUBJID = 1:7,
DTHFL = 1:7,
DTHDTC = 1:7
)

DM$DTHFL[1] = ""
DM$DTHDTC[1] = "2020-01-01"
DM$DTHFL[2] = "N"
DM$DTHDTC[2] = "2020-01-01"
DM$DTHFL[3] = "Y"
DM$DTHDTC[3] = "2020-01-01"
DM$DTHFL[4] = "Y"
DM$DTHDTC[4] = ""
DM$DTHFL[5] = "N"
DM$DTHDTC[5] = ""
DM$DTHFL[6] = "Y"
DM$DTHDTC[6] = "2020"
DM$DTHFL[7] = ""
DM$DTHDTC[7] = ""

expect_true(check_dm_dthfl_dthdtc(DM[3,]))

})

test_that("Returns false when errors present", {
DM <- data.frame(
USUBJID = 1:7,
DTHFL = 1:7,
DTHDTC = 1:7
)

DM$DTHFL[1] = ""
DM$DTHDTC[1] = "2020-01-01"
DM$DTHFL[2] = "N"
DM$DTHDTC[2] = "2020-01-01"
DM$DTHFL[3] = "Y"
DM$DTHDTC[3] = "2020-01-01"
DM$DTHFL[4] = "Y"
DM$DTHDTC[4] = ""
DM$DTHFL[5] = "N"
DM$DTHDTC[5] = ""
DM$DTHFL[6] = "Y"
DM$DTHDTC[6] = "2020"
DM$DTHFL[7] = ""
DM$DTHDTC[7] = ""

expect_false(check_dm_dthfl_dthdtc(DM))
})

test_that("Returns false when expected column not present", {

DM <- data.frame(
USUBJID = 1:7,
DTHFL = 1:7,
DTHDTC = 1:7
)

DM$DTHFL[1] = ""
DM$DTHDTC[1] = "2020-01-01"
DM$DTHFL[2] = "N"
DM$DTHDTC[2] = "2020-01-01"
DM$DTHFL[3] = "Y"
DM$DTHDTC[3] = "2020-01-01"
DM$DTHFL[4] = "Y"
DM$DTHDTC[4] = ""
DM$DTHFL[5] = "N"
DM$DTHDTC[5] = ""
DM$DTHFL[6] = "Y"
DM$DTHDTC[6] = "2020"
DM$DTHFL[7] = ""
DM$DTHDTC[7] = ""

DM$DTHDTC <- NULL
expect_false(check_dm_dthfl_dthdtc(DM[3,]))

})

187 changes: 187 additions & 0 deletions tests/testthat/test-check_dm_usubjid_ae_usubjid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
test_that("Returns true when no errors present", {

USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

expect_true(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))

})

test_that("Returns false when errors present - 1", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)
AE$USUBJID[3]=NA
AE$USUBJID[8]=NA
AE$USUBJID[10]=NA

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))

})

test_that("Returns false when errors present - 2", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)
AE$USUBJID[3]=NA
AE$USUBJID[8]=NA
AE$USUBJID[10]=NA

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

EX$EXOCCUR[3]="N"

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))
})

test_that("Returns false when expected column not present - 1", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

EX$EXDOSE=NULL

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))
})

test_that("Returns false when expected column not present - 2", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

DS$DSDECOD=NULL

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))
})

test_that("Returns false when expected column not present - 3", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

AE$USUBJID=NULL

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))
})

test_that("Returns false when expected column not present - 4", {
USUBJID<- c(1:10)
DM=data.frame(USUBJID)
AE=data.frame(USUBJID)

EX <- data.frame(
USUBJID = c(1:8, 6, 8, 10, 10, 10, 10),
EXOCCUR = rep("Y", times=14),
EXDOSE = rep(c(1,2), times=7),
EXSTDTC = c(rep("2012-01-01", 10),"2012-02-04","2012-02-04", "", "2012-02-07"),
EXTRT = "GDC",
stringsAsFactors=FALSE
)

DS <- data.frame(
USUBJID = c(2,8,8),
DSDECOD = rep("DEATH", times=3),
DSSTDTC = c("2012-12-01", NA, "2013-07-01"),
stringsAsFactors=FALSE
)

DM$USUBJID=NULL

expect_false(check_dm_usubjid_ae_usubjid(DM, AE, DS, EX))
})

Loading

0 comments on commit 5515783

Please sign in to comment.