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

Unit tests for pilot3utils function #13

Merged
merged 16 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
linters: with_defaults(line_length_linter(150), object_usage_linter=NULL, cyclocomp_linter(complexity_limit = 20))
linters: linters_with_defaults(line_length_linter(150), object_usage_linter=NULL, cyclocomp_linter(complexity_limit = 20))
exclusions: list("R/data.R")
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Suggests:
fs,
ggplot2,
haven,
testthat,
testthat (>= 3.0.0),
knitr,
pharmaRTF,
r2rtf,
Expand All @@ -37,3 +37,4 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/testthat/edition: 3
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions R/example.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Retrieve paths to built-in example test files
#'
#' `testthat_examples()` retrieves path to directory of test files,
#' `testthat_example()` retrieves path to a single test file.
#'
#' @keywords internal
#' @param filename Name of test file
#' @export
#' @examples
#' dir(testthat_examples())
#' testthat_example("success")
testthat_examples <- function() {
system.file("examples", package = "testthat")
}

#' @export
#' @rdname testthat_examples
testthat_example <- function(filename) {
system.file(
"examples", paste0("test-", filename, ".R"),
package = "testthat",
mustWork = TRUE
)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions tests/testthat.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(pilot3utils)

test_check("pilot3utils")
24 changes: 24 additions & 0 deletions tests/testthat/_snaps/Tplyr_helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# nest_rowlabels works

Code
nest_rowlabels(.data)
Output
# A tibble: 12 x 7
row_label var1_placebo var1_x~1 var1_~2 ord_l~3 ord_l~4 ord_l~5
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Baseline "" "" "" 1 0 0
2 n "79" "74" "81" 1 1 1
3 Mean (SD) "24.1 (12.19)" "21.3 (~ "24.4 ~ 1 1 2
4 Median (Range) "21.0 ( 5;61)" "18.0 (~ "21.0 ~ 1 1 3
5 Week 24 "" "" "" 2 0 0
6 n "79" "74" "81" 2 1 1
7 Mean (SD) "26.7 (13.79)" "22.8 (~ "26.4 ~ 2 1 2
8 Median (Range) "24.0 ( 5;62)" "20.0 (~ "25.0 ~ 2 1 3
9 Change from Baseline "" "" "" 3 0 0
10 n "79" "74" "81" 3 1 1
11 Mean (SD) " 2.5 ( 5.80)" " 1.5 (~ " 2.0 ~ 3 1 2
12 Median (Range) " 2.0 (-11;16)" " 1.0 (~ " 2.0 ~ 3 1 3
# ... with abbreviated variable names 1: var1_xanomeline_high_dose,
# 2: var1_xanomeline_low_dose, 3: ord_layer_index, 4: ord_layer_1,
# 5: ord_layer_2

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

Code
format_dcsreas("WITHDRAWAL BY SUBJECT")
Output
[1] "Withdrew Consent"

# format_eosstt works

Code
format_eosstt("SCREENING NOT COMPLETED")
Output
[1] "NOT STARTED"

# format_sitegr1 works

Code
format_sitegr1("714")
Output
[1] "900"

# round_sas works

Code
round_sas(10.56, digits = 0)
Output
[1] 11

19 changes: 19 additions & 0 deletions tests/testthat/_snaps/eff_models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# efficacy models works

Code
efficacy_models(adas, "CHG", 24)
Output
# A tibble: 9 x 3
row_label `var1_Xanomeline Low Dose` var1_Xanomel~1
<chr> <chr> <chr>
1 "p-value(Dose Response) [1][2]" <NA> " 0.245 "
2 "" <NA> ""
3 "p-value(Xan - Placebo) [1][3]" " 0.569 " " 0.233 "
4 " Diff of LS Means (SE)" "-0.5 (0.82)" "-1.0 (0.84)"
5 " 95% CI" "(-2.1;1.1)" "(-2.7;0.7)"
6 "" "" ""
7 "p-value(Xan High - Xan Low) [1][3]" <NA> " 0.520 "
8 " Diff of LS Means (SE)" <NA> "-0.5 (0.84)"
9 " 95% CI" <NA> "(-2.2;1.1)"
# ... with abbreviated variable name 1: `var1_Xanomeline High Dose`

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

Code
fmt_num(4.25, digits = 3)
Output
[1] " 4.250"

# fmt_est works

Code
fmt_est(1.25, 0.5)
Output
[1] " 1.2 ( 0.50)"

# fmt_pval works

Code
fmt_pval(0.52, digits = 3)
Output
[1] " 0.520"

# fmt_ci works

Code
fmt_ci(-2.5, -0.09, 2.07)
Output
[1] "-2.50 (-0.09, 2.07)"

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

Code
pad_row(.data)
Output
# A tibble: 7 x 5
STUDYID USUBJID PARAMCD PARAM AVISIT
<chr> <chr> <chr> <chr> <chr>
1 "TEST01" "01-701-1015" "DIABP" "Diastolic Blood Pressure (mmHg)" "BASELINE"
2 "TEST01" "01-701-1015" "DIABP" "Diastolic Blood Pressure (mmHg)" "WEEK 2"
3 "TEST01" "01-701-1015" "SYSBP" "Systolic Blood Pressure (mmHg)" "BASELINE"
4 "TEST01" "01-701-1015" "SYSBP" "Systolic Blood Pressure (mmHg)" "WEEK 2"
5 "TEST01" "01-701-1028" "DIABP" "Diastolic Blood Pressure (mmHg)" "BASELINE"
6 "TEST01" "01-701-1028" "SYSBP" "Systolic Blood Pressure (mmHg)" "BASELINE"
7 "" "" "" "" ""

# num_fmt works

Code
num_fmt(5, digits = 0, size = 10, int_len = 3)
Output
[1] " 5 "

# str_pad works

Code
str_pad("pilot3", 7, "left")
Output
[1] " pilot3"

# nrow works

Code
nrow(matrix(1:12, 3, 4))
Output
[1] 3

# round works

Code
round(c(0.3, 1.03, 2.67, 5, 8.91), digits = 1)
Output
[1] 0.3 1.0 2.7 5.0 8.9

# format works

Code
format("GFG", width = 8, justify = "l")
Output
[1] "GFG "

41 changes: 41 additions & 0 deletions tests/testthat/test-Tplyr_helpers.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("Pilot3 can use the testthat 3e", {
local_edition(3)
expect_true(TRUE)
})

test_that("nest_rowlabels works", {
.data <- tibble::tribble(
~row_label1, ~row_label2, ~var1_placebo, ~var1_xanomeline_high_dose, ~var1_xanomeline_low_dose, ~ord_layer_index, ~ord_layer_1, ~ord_layer_2,
"Baseline", "n", "79", "74", "81", 1, 1, 1,
"Baseline", "Mean (SD)", "24.1 (12.19)", "21.3 (11.74)", "24.4 (12.92)", 1, 1, 2,
"Baseline", "Median (Range)", "21.0 ( 5;61)", "18.0 ( 3;57)", "21.0 ( 5;57)", 1, 1, 3,
"Week 24", "n", "79", "74", "81", 2, 1, 1,
"Week 24", "Mean (SD)", "26.7 (13.79)", "22.8 (12.48)", "26.4 (13.18)", 2, 1, 2,
"Week 24", "Median (Range)", "24.0 ( 5;62)", "20.0 ( 3;62)", "25.0 ( 6;62)", 2, 1, 3,
"Change from Baseline", "n", "79", "74", "81", 3, 1, 1,
"Change from Baseline", "Mean (SD)", " 2.5 ( 5.80)", " 1.5 ( 4.26)", " 2.0 ( 5.55)", 3, 1, 2,
"Change from Baseline", "Median (Range)", " 2.0 (-11;16)", " 1.0 ( -7;13)", " 2.0 (-11;17)", 3, 1, 3
)
.nest_rowlabels <- tibble::tribble(
~row_label, ~var1_placebo, ~var1_xanomeline_high_dose, ~var1_xanomeline_low_dose, ~ord_layer_index, ~ord_layer_1, ~ord_layer_2,
"Baseline", "", "", "", 1, 0, 0,
"n", "79", "74", "81", 1, 1, 1,
"Mean (SD)", "24.1 (12.19)", "21.3 (11.74)", "24.4 (12.92)", 1, 1, 2,
"Median (Range)", "21.0 ( 5;61)", "18.0 ( 3;57)", "21.0 ( 5;57)", 1, 1, 3,
"Week 24", "", "", "", 2, 0, 0,
"n", "79", "74", "81", 2, 1, 1,
"Mean (SD)", "26.7 (13.79)", "22.8 (12.48)", "26.4 (13.18)", 2, 1, 2,
"Median (Range)", "24.0 ( 5;62)", "20.0 ( 3;62)", "25.0 ( 6;62)", 2, 1, 3,
"Change from Baseline", "", "", "", 3, 0, 0,
"n", "79", "74", "81", 3, 1, 1,
"Mean (SD)", " 2.5 ( 5.80)", " 1.5 ( 4.26)", " 2.0 ( 5.55)", 3, 1, 2,
"Median (Range)", " 2.0 (-11;16)", " 1.0 ( -7;13)", " 2.0 (-11;17)", 3, 1, 3
)
expect_equal(nrow(.data), 9)
expect_equal(nrow(.nest_rowlabels), 12)
expect_equal(nrow(nest_rowlabels(.data)), 12)
expect_s3_class(nest_rowlabels(.data), "tbl_df")
expect_snapshot(nest_rowlabels(.data), dplyr::select(.nest_rowlabels))
expect_length(nrow(.data), 1)
expect_length(nrow(.nest_rowlabels), 1)
})
67 changes: 67 additions & 0 deletions tests/testthat/test-adam_functions.r
Copy link
Collaborator

@SHAESEN2 SHAESEN2 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to rename the files to make it traceable eg in this file it seems you are testing adam format functions, rather than just functions

@SHAESEN2 - yes, the filenames are autogenerated and can be changed to make traceable to a requirements traceability matrix (RTM) for pilot3utils.

The admiral package has excellent test coverage already for the functions used in pilot3. The focus in this issue has been to unit test the functions defined in the pilot3utils package source. thx.

The unit test file names can be whatever pilot3 team wishes. With test_dir( ) every source file in the
specified directory gets tested with a "dot" output representing a function 'PASS' (see below). The _snaps
directory can be used for snapshotting test results for comparison. The testthat package creates test files in the
pilot3utils package using '.R' not '.r' extension - easy enough to change extension to match the regulatory specification. The traceability aspect to match the unit test to a specific pilot3 requirement is an excellent
point. One limitation (of using snapshots) is that snapshots are not aware if an output is correct - so an expect strategy comparing unit test output against 'correct' (point of truth) is necessary. thx.

image

The snapshots appear in the _snaps directory when the expect_snapshot unit test is run. Even a slight difference in expected outcome and previous test snapshot results in an error getting issued. An example using the pilot3utils nest_rowlabels function appears below using expect_snapshot and other testthat expect suggests.
It is pretty cool but the traceability matrix is a great idea then Pilot3 can link requirement to unit test PASS and store the snapshot in the repository.

image

image

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
test_that("Pilot3 can use the testthat 3e", {
local_edition(3)
expect_true(TRUE)
})

test_that("format_dcsreas works", {
.data_format_dcsreas <- tibble::tribble(
~OUTPUT,
"Withdrew Consent"
)
expect_equal(format_dcsreas("ADVERSE EVENT"), "Adverse Event")
expect_equal(format_dcsreas("STUDY TERMINATED BY SPONSOR"), "Sponsor Decision")
expect_equal(format_dcsreas("DEATH"), "Death")
expect_equal(format_dcsreas("WITHDRAWAL BY SUBJECT"), "Withdrew Consent")
expect_snapshot(format_dcsreas("WITHDRAWAL BY SUBJECT"), dplyr::select(.data_format_dcsreas))
expect_equal(format_dcsreas("PHYSICIAN DECISION"), "Physician Decision")
expect_equal(format_dcsreas("LACK OF EFFICACY"), "Lack of Efficacy")
expect_equal(format_dcsreas("PROTOCOL VIOLATION"), "Protocol Violation")
expect_equal(format_dcsreas("LOST TO FOLLOW-UP"), "Lost to Follow-up")
expect_length(format_dcsreas("ADVERSE"), 1)
expect_length(format_dcsreas("ADVERSE EVENT"), 1)
expect_length(format_dcsreas(NA), 1)
})

test_that("format_eosstt works", {
.data_format_eosstt <- tibble::tribble(
~OUTPUT,
"NOT STARTED"
)
expect_equal(format_eosstt("SCREEN FAILURE"), "NOT STARTED")
expect_equal(format_eosstt("SCREENING FAILURE"), "DISCONTINUED")
expect_equal(format_eosstt("COMPLETED"), "COMPLETED")
expect_equal(format_eosstt("SCREENING NOT COMPLETED"), "NOT STARTED")
expect_snapshot(format_eosstt("SCREENING NOT COMPLETED"), dplyr::select(.data_format_eosstt))
expect_equal(format_eosstt(""), "DISCONTINUED")
expect_equal(format_eosstt(NA), "ONGOING")
expect_length(format_eosstt("SCREEN FAILURE"), 1)
expect_length(format_eosstt(NA), 1)
})

test_that("format_sitegr1 works", {
.data_format_sitegr1 <- tibble::tribble(
~OUTPUT,
"900"
)
expect_equal(format_sitegr1("702"), "900")
expect_equal(format_sitegr1("706"), "900")
expect_equal(format_sitegr1("707"), "900")
expect_equal(format_sitegr1("711"), "900")
expect_equal(format_sitegr1("714"), "900")
expect_snapshot(format_sitegr1("714"), dplyr::select(.data_format_sitegr1))
expect_equal(format_sitegr1("715"), "900")
expect_equal(format_sitegr1("717"), "900")
expect_length(format_sitegr1("717"), 1)
})

test_that("round_sas works", {
.data_round_sas <- tibble::tribble(
~OUTPUT,
11
)
expect_equal(round_sas(10.56, digits = 0), 11)
expect_snapshot(round_sas(10.56, digits = 0), dplyr::select(.data_round_sas))
expect_equal(round_sas(9.06, digits = 0), 9)
expect_equal(round_sas(-5.6, digits = 0), -6)
expect_length(round_sas(10.56, digits = 0), 1)
})
19 changes: 19 additions & 0 deletions tests/testthat/test-eff_models.r
Copy link
Collaborator

@SHAESEN2 SHAESEN2 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those four lines are not testing the efficacy models?

@SHAESEN2 - yes, preparing to commit the unit test for the efficacy_models function. thx.
The four lines provide a unit test to confirm pilot3utils testthat(3e) is used - 3e is required for using snapshots.

image

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("Pilot3 can use the testthat 3e", {
local_edition(3)
expect_true(TRUE)
})
test_that("efficacy models works", {
.data_model_portion <- tibble::tribble(
~row_label, ~`~var1_Xanomeline Low Dose`, ~`~var1_Xanomeline High Dose`,
"p-value(Dose Response) [1][2]", "NA", " 0.245 ",
"", "NA", "",
"p-value(Xan - Placebo) [1][3]", " 0.569 ", " 0.233 ",
" Diff of LS Means (SE)", "-0.5 (0.82)", "-1.0 (0.84)",
" 95% CI", "(-2.1;1.1)", "(-2.7;0.7)",
"", "", "",
"p-value(Xan High - Xan Low) [1][3]", "NA", " 0.520 ",
" Diff of LS Means (SE)", "NA", "-0.5 (0.84)",
" 95% CI", "NA", "(-2.2;1.1)"
)
expect_length(.data_model_portion, 3)
})
55 changes: 55 additions & 0 deletions tests/testthat/test-fmt.r
Copy link
Collaborator

@SHAESEN2 SHAESEN2 Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file we can include the character formats from before.

@SHAESEN2 - are you referring to the character formats here [earlier Issues 78, 87, and 91]. We will have to unit test those from the pilot3-adam repository.

Lmk, I could set it up testthat( ) in the pilot3-adam repository and unit test functions in the source where the 'xporter' and 'na' formatters are used from before. thx.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
test_that("Pilot3 can use the testthat 3e", {
local_edition(3)
expect_true(TRUE)
})

test_that("fmt_num works", {
.data_fmt_num <- tibble::tribble(
~OUTPUT,
" 4.250"
)
expect_equal(fmt_num(1.25, digits = 1), " 1.2")
expect_equal(fmt_num(1.25, digits = 2), " 1.25")
expect_equal(fmt_num(4.25, digits = 3), " 4.250")
expect_snapshot(fmt_num(4.25, digits = 3), dplyr::select(.data_fmt_num))
expect_equal(fmt_num(1.25, digits = 0), " 1")
expect_length(fmt_num(1.25, digits = 4), 1)
})


test_that("fmt_est works", {
.data_fmt_est <- tibble::tribble(
~OUTPUT,
" 1.2 ( 0.50)"
)
expect_equal(fmt_est(1.25, 0.5), " 1.2 ( 0.50)")
expect_snapshot(fmt_est(1.25, 0.5), dplyr::select(.data_fmt_est))
expect_equal(fmt_est(1.25, 0.01), " 1.2 ( 0.01)")
expect_equal(fmt_est(1.25, 0.07), " 1.2 ( 0.07)")
expect_length(fmt_est(1.25, 0.5), 1)
})

test_that("fmt_pval works", {
.data_fmt_est <- tibble::tribble(
~OUTPUT,
" 0.520"
)
expect_equal(fmt_pval(0.446, digits = 2), " 0.45")
expect_equal(fmt_pval(0.445, digits = 3), " 0.445")
expect_equal(fmt_pval(0.52, digits = 3), " 0.520")
expect_snapshot(fmt_pval(0.52, digits = 3), dplyr::select(.data_fmt_est))
expect_length(fmt_pval(0.445, digits = 1), 1)
expect_length(fmt_pval(0.445, digits = 4), 1)
})

test_that("fmt_ci works", {
.data_fmt_ci <- tibble::tribble(
~OUTPUT,
"-2.50 (-0.09, 2.07)"
)
expect_equal(fmt_ci(1, -0.25, 1.32), " 1.00 (-0.25, 1.32)")
expect_equal(fmt_ci(1.5, -0.04, 1.09), " 1.50 (-0.04, 1.09)")
expect_equal(fmt_ci(-2.5, -0.09, 2.07), "-2.50 (-0.09, 2.07)")
expect_snapshot(fmt_ci(-2.5, -0.09, 2.07), dplyr::select(.data_fmt_ci))
expect_length(fmt_ci(1, -0.25, 1.32), 1)
})
Loading
Loading