Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30 Create Unit Tests with Example Data #37

Merged
merged 27 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a2003ab
resolve merge conflict between bitbucket and github
Mar 23, 2023
705f6e6
resolve merge conflict tcplLoadData
Mar 23, 2023
2c82ef0
removed browser and added error for invalid input
Mar 24, 2023
1872c05
Merge commit '2c82ef0c604264e4f11027ace153c9f948b63e0a' into 30-creat…
Mar 24, 2023
953cc0f
resolve merge conflict tcplLoadData
Mar 24, 2023
5be2764
initial testthat files
Mar 24, 2023
3b08e0d
updated tcplConf and tcplLoadData test files for output and error mes…
Mar 27, 2023
2a8be7e
resolve merge conflict tcplLoadData
Mar 24, 2023
90a682f
initial testthat files
Mar 24, 2023
658c02f
updated tcplConf and tcplLoadData test files for output and error mes…
Mar 27, 2023
15ac4df
Merge branch '30-create-unit-tests-with-example-data' of https://gith…
Apr 6, 2023
35b1748
updated test-tcplLoadData tests to support return function and added …
Apr 6, 2023
fce3942
added helper-functions.R file to tests/testthat, added test for round…
Apr 10, 2023
3664572
added test-tcplPlot.R unit tests
Apr 11, 2023
89d8ebe
added tcplPlot unit tests, isolated error message unit tests
Apr 12, 2023
9d4e6aa
resolve remote merge conflict
Apr 13, 2023
511f122
added agg to mc and sc example data, updated unit tests for tcplPlot
Apr 13, 2023
15a7fb5
resolve remote merge conflict
Apr 14, 2023
4911e05
updated tcplLoadData test files
Apr 17, 2023
d27faa1
updated tcplPlot unit tests and created snapshot test
Apr 17, 2023
fbe088c
updated snapshot test
Apr 17, 2023
a237bee
changed aes(x=0) to x=-Inf to avoid warning message: "Transformation …
Apr 18, 2023
341ef57
added vdiffr to description
brown-jason Apr 25, 2023
63a019b
removed generated Rplots.pdf and added condition to prevent generatin…
Apr 25, 2023
dbaf1b9
Merge branch '30-create-unit-tests-with-example-data' of https://gith…
Apr 25, 2023
82b63bf
regenerated svg file for comparison
brown-jason Apr 25, 2023
ba724ea
added skip function to snapshot test to investigate in separate issue
Apr 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ Imports:
graphics,
grDevices,
sqldf,
dplyr,
tidyr,
plotly,
tcplfit2,
ggplot2,
gridExtra,
stringr
dplyr,
tidyr,
plotly,
tcplfit2,
ggplot2,
gridExtra,
stringr,
vdiffr
Suggests:
roxygen2,
knitr,
prettydoc,
rmarkdown,
htmlTable,
testthat (>= 2.1.0),
testthat (>= 3.0.0),
reshape2,
viridis,
kableExtra,
Expand All @@ -58,3 +59,4 @@ LazyData: true
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Encoding: UTF-8
Config/testthat/edition: 3
24 changes: 21 additions & 3 deletions R/tcplLoadData.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ tcplLoadData <- function(lvl, fld = NULL, val = NULL, type = "mc", add.fld = TRU
sc1 <- sc1[,c("s0id","s1id","spid","acid","aeid","apid","rowi","coli","wllt","logc","resp")]
return(sc1)
}

else if (lvl == 2L) {
sc2 <- sc_vignette[["sc2"]]
sc2 <- sc2[,c("s2id", "spid", "aeid", "bmad", "max_med", "hitc", "coff")]
return(sc2)
}
else stop("example tables for sc0, sc1, sc2 available.")
}
else if (lvl == "agg") {
sc1 <- sc_vignette[["sc1"]]
sc2 <- sc_vignette[["sc2"]]
agg <- sc1[sc2, on = c("spid","aeid")]
agg <- agg[,c("aeid","s2id","s1id","s0id","logc","resp")]
return(agg)
}
else stop("example tables for sc0, sc1, sc2, agg available.")
}

if (type == "mc" ) {
Expand Down Expand Up @@ -152,8 +160,18 @@ tcplLoadData <- function(lvl, fld = NULL, val = NULL, type = "mc", add.fld = TRU
}
return(mc5)
}
else stop("example tables for mc0, mc1, mc2, mc3, mc4, mc5 available.")
else if (lvl == "agg") {
mc3 <- mc_vignette[["mc3"]]
mc4 <- mc_vignette[["mc4"]]
agg <- mc3[mc4, on = c("spid","aeid")]
agg <- agg[, c("aeid", "m4id", "m3id", "m2id", "m1id", "m0id", "spid", "logc", "resp")]
return(agg)

}
else stop("example tables for mc0, mc1, mc2, mc3, mc4, mc5, agg available.")
}

else stop("Invalid 'lvl' and 'type' combination.")
}

if (drvr != "example"){
Expand Down
2 changes: 1 addition & 1 deletion R/tcplPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ tcplggplot <- function(dat, lvl = 5, verbose = FALSE) {
geom_vline(aes(xintercept = dat$ac50, color = "AC50", linetype = "AC50")) +
geom_segment(aes(x=dat$bmd, xend=dat$bmd, y=-Inf, yend=dat$bmr, color = "BMD", linetype = "BMD")) +
geom_hline(aes(yintercept = dat$coff, color = "Cutoff", linetype = "Cutoff")) +
geom_segment(aes(x=0, xend=dat$bmd, y = dat$bmr, yend=dat$bmr, color = "BMD", linetype = "BMD")) +
geom_segment(x=-Inf, aes(xend=dat$bmd, y = dat$bmr, yend=dat$bmr, color = "BMD", linetype = "BMD")) +
geom_point() +
scale_x_continuous(limits = l3_range, trans = "log10") +
scale_color_viridis_d("", direction = -1, guide = guide_legend(reverse = TRUE, order = 2), end = 0.9) +
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(tcpl)

Expand Down
790 changes: 790 additions & 0 deletions tests/testthat/_snaps/tcplPlot/test-output-482273.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/testthat/helper-functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#helper-functions.R
#round_n
round_n <- function(x, n=3) {
if (!is.na(x)) {
if (x >= 1000 | x<=0.0005) {
# if x>=1000, convert value to scientific notation
formatC(x, format = "e", digits = 1)
} else { # else, round the value to 3 decimal places
format(round(x, n), nsmall = 3)
}
} else {
return(NA)
}
}
round_n <- Vectorize(round_n)
13 changes: 13 additions & 0 deletions tests/testthat/test-tcplConf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("example=driver updates option TCPL_DRVR", {
tcplConf(drvr = "example")
expect_equal("example",
getOption("TCPL_DRVR"))
})

test_that("unsupported driver error message", {
drvr <- "test"
expect_error(
tcplConf(drvr),
paste0(drvr," is not a supported database driver. Must be 'MySQL', 'tcplLite', or 'example'.")
)
})
161 changes: 161 additions & 0 deletions tests/testthat/test-tcplLoadData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
tcplConf(drvr = "example")

test_that("sc0 loads sc0 data table", {
sc0 <- tcplLoadData(lvl = 0, type = "sc")
expect_true(
is.data.table(sc0)
)
sc0 <- tcplLoadData(lvl = 0L, type = "sc")
expect_true(
is.data.table(sc0)
)
})

test_that("sc1 loads sc1 data table", {
sc1 <- tcplLoadData(lvl = 1, type = "sc")
expect_true(
is.data.table(sc1)
)
sc1 <- tcplLoadData(lvl = 1L, type = "sc")
expect_true(
is.data.table(sc1)
)
})

test_that("sc2 loads sc2 data table", {
sc2 <- tcplLoadData(lvl = 2, type = "sc")
expect_true(
is.data.table(sc2)
)
sc2 <- tcplLoadData(lvl = 2L, type = "sc")
expect_true(
is.data.table(sc2)
)
})

test_that("lvl = 'agg' loads sc agg data table", {
agg <- tcplLoadData(lvl = "agg", type = "sc")
expect_true(
is.data.table(agg)
)
})

test_that("mc0 loads mc0 data table", {
mc0 <- tcplLoadData(lvl = 0, type = "mc")
expect_true(
is.data.table(mc0)
)
mc0 <- tcplLoadData(lvl = 0L, type = "mc")
expect_true(
is.data.table(mc0)
)
})

test_that("mc1 loads mc1 data table", {
mc1 <- tcplLoadData(lvl = 1, type = "mc")
expect_true(
is.data.table(mc1)
)
mc1 <- tcplLoadData(lvl = 1L, type = "mc")
expect_true(
is.data.table(mc1)
)
})

test_that("mc2 loads mc2 data table", {
mc2 <- tcplLoadData(lvl = 2, type = "mc")
expect_true(
is.data.table(mc2)
)
mc2 <- tcplLoadData(lvl = 2L, type = "mc")
expect_true(
is.data.table(mc2)
)
})

test_that("mc3 loads mc3 data table", {
mc3 <- tcplLoadData(lvl = 3, type = "mc")
expect_true(
is.data.table(mc3)
)
mc3 <- tcplLoadData(lvl = 3L, type = "mc")
expect_true(
is.data.table(mc3)
)
})

test_that("mc4 loads mc4 data table", {
mc4 <- tcplLoadData(lvl = 4, type = "mc")
expect_true(
is.data.table(mc4)
)
mc4 <- tcplLoadData(lvl = 4L, type = "mc")
expect_true(
is.data.table(mc4)
)
})

test_that("mc4 !add.fld loads mc4 data table", {
mc4 <- tcplLoadData(lvl = 4, type = "mc", add.fld=FALSE)
expect_true(
is.data.table(mc4)
)
mc4 <- tcplLoadData(lvl = 4L, type = "mc", add.fld=FALSE)
expect_true(
is.data.table(mc4)
)
})

test_that("mc5 loads mc5 data table", {
mc5 <- tcplLoadData(lvl = 5, type = "mc")
expect_true(
is.data.table(mc5)
)
mc5 <- tcplLoadData(lvl = 5L, type = "mc")
expect_true(
is.data.table(mc5)
)
})

test_that("mc5 !add.fld loads mc5 data table", {
mc5 <- tcplLoadData(lvl = 5, type = "mc", add.fld=FALSE)
expect_true(
is.data.table(mc5)
)
mc5 <- tcplLoadData(lvl = 5L, type = "mc", add.fld=FALSE)
expect_true(
is.data.table(mc5)
)
})

test_that("lvl = 'agg' loads mc agg data table", {
agg <- tcplLoadData(lvl = "agg", type = "mc")
expect_true(
is.data.table(agg)
)
})

test_that("invalid input error message", {
expect_error(
tcplLoadData(lvl = "sc", type = 0),
"Invalid 'lvl' and 'type' combination."
)
})

test_that("sc example error message appears", {
expect_error(
tcplLoadData(lvl = 4, type = "sc"),
"example tables for sc0, sc1, sc2, agg available."
)
})

test_that("mc example error message appears", {
expect_error(
tcplLoadData(lvl = 6, type = "mc"),
"example tables for mc0, mc1, mc2, mc3, mc4, mc5, agg available."
)
})

# test_that("description", {
# expect_*(code)
# })
Loading