Skip to content

Commit

Permalink
add more tests for datanames() and do not extract datanames() from jo…
Browse files Browse the repository at this point in the history
…in_keys as they might refer to datanames that do not exist in env
  • Loading branch information
m7pr committed Jul 31, 2024
1 parent a3ece0e commit b395c29
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion R/teal_data-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ setClass(
new_teal_data <- function(data,
code = character(0),
join_keys = join_keys(),
datanames = union(names(data), names(join_keys))) {
datanames = names(data)) {
checkmate::assert_list(data)
checkmate::assert_class(join_keys, "join_keys")
if (is.null(datanames)) datanames <- character(0) # todo: allow to specify
Expand Down
54 changes: 52 additions & 2 deletions tests/testthat/test-datanames.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ testthat::test_that("datanames<- called on qenv.error does not change qenv.error

# topological_order ----

testthat::test_that("datanames are set in topological in constructor if join_keys are specified", {
testthat::test_that("datanames are set in topological order in constructor if join_keys are specified", {
data <-
teal_data(b = data.frame(), a = data.frame(), join_keys = join_keys(join_key("a", "b", "id")))
testthat::expect_identical(
Expand All @@ -53,6 +53,25 @@ testthat::test_that("datanames are set in topological in constructor if join_key
)
})

testthat::test_that("datanames return parent if in constructor it was provided in join_keys and exists in env", {
data <-
teal_data(b = data.frame(), a = data.frame(), join_keys = join_keys(join_key("a", "b", "id")))
datanames(data) <- "b"
testthat::expect_identical(
datanames(data),
c("a", "b")
)
})

testthat::test_that("datanames do not return parent if in constructor it was provided in join_keys but do not exists in env", {

Check warning on line 66 in tests/testthat/test-datanames.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-datanames.R,line=66,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
data <-
teal_data(b = data.frame(), join_keys = join_keys(join_key("a", "b", "id")))
testthat::expect_identical(
datanames(data),
"b"
)
})

testthat::test_that("datanames return topological order of datasets once join_keys are specified", {
data <- within(teal_data(), {
ADTTE <- teal.data::rADTTE

Check warning on line 77 in tests/testthat/test-datanames.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-datanames.R,line=77,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
Expand All @@ -67,7 +86,7 @@ testthat::test_that("datanames return topological order of datasets once join_ke
)
})

testthat::test_that("datanames return topological order of datasets after datanames are caled after join_keys", {
testthat::test_that("datanames return topological order of datasets after datanames are called after join_keys", {
data <- within(teal_data(), {
ADTTE <- teal.data::rADTTE

Check warning on line 91 in tests/testthat/test-datanames.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-datanames.R,line=91,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
iris <- iris
Expand All @@ -82,3 +101,34 @@ testthat::test_that("datanames return topological order of datasets after datana
c("ADSL", "ADTTE", "iris")
)
})


testthat::test_that("datanames return parent if join_keys were provided and parent exists in env", {
data <- within(teal_data(), {
ADTTE <- teal.data::rADTTE

Check warning on line 108 in tests/testthat/test-datanames.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-datanames.R,line=108,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
iris <- iris
ADSL <- teal.data::rADSL

Check warning on line 110 in tests/testthat/test-datanames.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-datanames.R,line=110,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
})

join_keys(data) <- default_cdisc_join_keys[c("ADSL", "ADTTE")]
datanames(data) <- c("ADTTE", "iris")

testthat::expect_identical(
datanames(data),
c("ADSL", "ADTTE", "iris")
)
})

testthat::test_that("datanames do not return parent if join_keys were provided and parent did not exists in env", {
data <- teal_data(
ADTTE = teal.data::rADTTE,
iris = iris
)

join_keys(data) <- default_cdisc_join_keys[c("ADSL", "ADTTE")]

testthat::expect_identical(
datanames(data),
c("ADTTE", "iris")
)
})
14 changes: 8 additions & 6 deletions tests/testthat/test-teal_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ testthat::test_that("teal_data initializes teal_data object with @datanames take
)
})

testthat::test_that("teal_data initializes teal_data object with @datanames taken from passed join_keys", {
testthat::test_that(
"teal_data initializes teal_data object without @datanames taken from join_keys if objects did not exist in env", {

Check warning on line 32 in tests/testthat/test-teal_data.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-teal_data.R,line=32,col=2,[indentation_linter] Hanging indent should be 20 spaces but is 2 spaces.
testthat::expect_identical(

Check warning on line 33 in tests/testthat/test-teal_data.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-teal_data.R,line=33,col=2,[indentation_linter] Indentation should be 22 spaces but is 2 spaces.
teal_data(join_keys = join_keys(join_key("parent", "child", "id")))@datanames,
c("parent", "child")
datanames(teal_data(join_keys = join_keys(join_key("parent", "child", "id")))),
character(0)
)
})

testthat::test_that("teal_data initializes teal_data object with @datanames taken from join_keys and passed objects", {
testthat::test_that(
"teal_data initializes teal_data object with @datanames taken only from passed objects and not join_keys", {

Check warning on line 40 in tests/testthat/test-teal_data.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-teal_data.R,line=40,col=2,[indentation_linter] Hanging indent should be 20 spaces but is 2 spaces.
testthat::expect_identical(
teal_data(iris = iris, join_keys = join_keys(join_key("parent", "child", "id")))@datanames,
c("iris", "parent", "child")
datanames(teal_data(iris = iris, join_keys = join_keys(join_key("parent", "child", "id")))),
"iris"
)
})

Expand Down

0 comments on commit b395c29

Please sign in to comment.