Skip to content

Commit

Permalink
Merge pull request #161 from Merck/test-style
Browse files Browse the repository at this point in the history
Remove `library()` calls from tests
  • Loading branch information
wangben718 authored Feb 15, 2024
2 parents fc90c66 + a56777b commit 174a74d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 48 deletions.
8 changes: 0 additions & 8 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
library(testthat)
library(metalite)

ae_summary <- function(...) {
paste("results of", deparse(match.call(), nlines = 1))
}
ae_specific <- function(...) {
paste("results of", deparse(match.call(), nlines = 1))
}


test_check("metalite")
6 changes: 3 additions & 3 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Create two example functions
ae_summary <- function(...) {
ae_summary <<- function(...) {
paste("results of", deparse(match.call(), nlines = 1))
}
ae_specific <- function(...) {

ae_specific <<- function(...) {
paste("results of", deparse(match.call(), nlines = 1))
}
2 changes: 0 additions & 2 deletions tests/testthat/test-independent-testing-collect_n_subject.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library(r2rtf)

test_that("test n_subject can calculate number of records by group in diffrent `use_na` situation", {
df1 <- data.frame(
id = c(1, 2, 3, 4, 5, 6, 7),
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-independent-testing-meta_adam.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(r2rtf)


test_that("meta_adam testing", {
test <- meta_adam(
population = r2rtf::r2rtf_adsl,
Expand Down
8 changes: 2 additions & 6 deletions tests/testthat/test-independent-testing-meta_add_total.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
library(testthat)
library(dplyr)

test_that("Multple names for total column has to throw error", {
expect_error(meta_add_total(meta = meta_example(), total = c("Total", "Sum")))
})


test_that("number of distinct treatment arms after adding", {
x <- meta_add_total(meta = meta_example(), total = "Sum")
y <- meta_example()
expect_equal(n_distinct(x$data_population$TRTA), n_distinct(y$data_population$TRTA) + 1)
expect_equal(n_distinct(x$data_observation$TRTA), n_distinct(y$data_observation$TRTA) + 1)
expect_equal(dplyr::n_distinct(x$data_population$TRTA), dplyr::n_distinct(y$data_population$TRTA) + 1)
expect_equal(dplyr::n_distinct(x$data_observation$TRTA), dplyr::n_distinct(y$data_observation$TRTA) + 1)
expect_true("Sum" %in% x$data_observation$TRTA)
expect_false("Sum" %in% y$data_observation$TRTA)
expect_true("Sum" %in% x$data_population$TRTA)
Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/test-independent-testing-meta_split.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
library(dplyr)

test_that("variable 'RACE' checking", {
expect_equal(names(meta_example() |> meta_split("RACE")), sort(unique(meta_example()$data_population$RACE)))
})


test_that("variable 'RACE = WHITE' checking in data_population", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_population %>% filter(RACE == "WHITE")
z <- meta_example()$data_population %>% dplyr::filter(RACE == "WHITE")
expect_equal(x$WHITE$data_population$USUBJID, unlist(as.list(z$USUBJID)))
})


test_that("variable 'RACE = BLACK OR AFRICAN AMERICAN' checking in data_population", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_population %>% filter(RACE == "BLACK OR AFRICAN AMERICAN")
z <- meta_example()$data_population %>% dplyr::filter(RACE == "BLACK OR AFRICAN AMERICAN")
expect_equal(x$`BLACK OR AFRICAN AMERICAN`$data_population$USUBJID, unlist(as.list(z$USUBJID)))
})


test_that("variable 'RACE = AMERICAN INDIAN OR ALASKA NATIVE' checking in data_population", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_population %>% filter(RACE == "AMERICAN INDIAN OR ALASKA NATIVE")
z <- meta_example()$data_population %>% dplyr::filter(RACE == "AMERICAN INDIAN OR ALASKA NATIVE")
expect_equal(x$`AMERICAN INDIAN OR ALASKA NATIVE`$data_population$USUBJID, unlist(as.list(z$USUBJID)))
})


test_that("variable 'RACE = WHITE' checking in data_observation", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_observation %>% filter(RACE == "WHITE")
z <- meta_example()$data_observation %>% dplyr::filter(RACE == "WHITE")
expect_equal(x$WHITE$data_observation$USUBJID, unlist(as.list(z$USUBJID)))
})


test_that("variable 'RACE = BLACK OR AFRICAN AMERICAN' checking in data_observation", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_observation %>% filter(RACE == "BLACK OR AFRICAN AMERICAN")
z <- meta_example()$data_observation %>% dplyr::filter(RACE == "BLACK OR AFRICAN AMERICAN")
expect_equal(x$`BLACK OR AFRICAN AMERICAN`$data_observation$USUBJID, unlist(as.list(z$USUBJID)))
})


test_that("variable 'RACE = AMERICAN INDIAN OR ALASKA NATIVE' checking in data_observation", {
x <- meta_example() |> meta_split("RACE")
z <- meta_example()$data_observation %>% filter(RACE == "AMERICAN INDIAN OR ALASKA NATIVE")
z <- meta_example()$data_observation %>% dplyr::filter(RACE == "AMERICAN INDIAN OR ALASKA NATIVE")
expect_equal(x$`AMERICAN INDIAN OR ALASKA NATIVE`$data_observation$USUBJID, unlist(as.list(z$USUBJID)))
})
22 changes: 7 additions & 15 deletions tests/testthat/test-independent-testing-outdata.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
library(dplyr)
library(metalite)

test_that("Its class is 'outdata'", {
meta <- meta_example()
output <- outdata(meta_example(), "apat", "wk12", "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1, order = 1:3)
output <- outdata(meta_example(), "apat", "wk12", "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1, order = 1:3)

expect_equal(class(output), "outdata")
expect_equal(length(output), 8)
Expand All @@ -17,7 +14,7 @@ test_that("Expecting an error", {

test_that("Its class is 'outdata'", {
meta <- meta_example()
x <- list(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1, order = 1:3)
x <- list(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1, order = 1:3)
output <- metalite:::new_outdata(x)
expect_equal(class(output), "outdata")
})
Expand All @@ -27,14 +24,9 @@ test_that("Expecting an error", {

meta <- meta_example()

expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = "Placebo", order = 1:3)))


expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1, order = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"))))

expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = 1, observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1, order = 1:3)))

expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1:2, order = 1:3)))

expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = c("apat", "ITT"), observation = "wk12", parameter = "rel", n = meta$data_population %>% group_by(TRTA) %>% summarize(n = n()), group = "TRTA", reference_group = 1, order = 1:3)))
expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = "Placebo", order = 1:3)))
expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1, order = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"))))
expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = 1, observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1, order = 1:3)))
expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = "apat", observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1:2, order = 1:3)))
expect_error(metalite:::validate_outdata(outdata(meta = meta_example(), population = c("apat", "ITT"), observation = "wk12", parameter = "rel", n = meta$data_population %>% dplyr::group_by(TRTA) %>% dplyr::summarize(n = dplyr::n()), group = "TRTA", reference_group = 1, order = 1:3)))
})
3 changes: 0 additions & 3 deletions tests/testthat/test-independent-testing-plan.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(dplyr)


test_that("error is thrown", {
expect_error(metalite::plan(c("ae_summary", "ae_specific"), population = "apat", observation = c("wk12", "wk24"), parameter = "any;rel"))
})
Expand Down

0 comments on commit 174a74d

Please sign in to comment.