Skip to content

Commit

Permalink
Use test_path to make tests easier to run interactively
Browse files Browse the repository at this point in the history
Avoid partial matching warning in incorporate()

allow overwrite when file_copy()
  • Loading branch information
olivroy committed Nov 26, 2024
1 parent a85658b commit 6be4589
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
7 changes: 6 additions & 1 deletion R/incorporate.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ incorporate <- function(informant) {

# Get the target table for this informant object
# TODO: extend the materialize table function to use an agent or informant
tbl <- informant$tbl
if (rlang::has_name(informant, "tbl")) {
# Avoid partial matching
tbl <- informant$tbl
} else {
tbl <- informant$tbl_name
}
tbl_name <- informant$tbl_name
read_fn <- informant$read_fn

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-get_informant_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("Getting an information report is possible", {
# `incorporate()` the snippets into the info text
informant <-
create_informant(
tbl = ~ readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc"),
tbl = ~ readr::read_csv(file = test_path("test_table.csv"), col_types = "TDdcddlc"),
tbl_name = "test_table",
label = "An example."
) %>%
Expand Down Expand Up @@ -54,7 +54,7 @@ test_that("Getting a more advanced information report is possible", {

informant <-
create_informant(
tbl = ~ readr::read_csv(file = "penguins.csv", col_types = "ccddddcd"),
tbl = ~ readr::read_csv(file = test_path("penguins.csv"), col_types = "ccddddcd"),
tbl_name = "penguins",
label = "The `penguins` dataset from the **palmerpenguins** 📦."
) %>%
Expand Down
23 changes: 10 additions & 13 deletions tests/testthat/test-incorporate_with_informant.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("Incorporating an informant yields the correct results", {
# `incorporate()` the snippets into the info text
informant <-
create_informant(
tbl = ~ readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc"),
tbl = ~ readr::read_csv(file = testthat::test_path("test_table.csv"), col_types = "TDdcddlc"),
tbl_name = "test_table",
label = "An example."
) %>%
Expand Down Expand Up @@ -41,14 +41,11 @@ test_that("Incorporating an informant yields the correct results", {

# Expect that names in an informant after using
# `incorporate()` match a prescribed set of names
expect_true(
all(
names(informant_inc) ==
c(
"tbl", "read_fn", "tbl_name", "info_label",
"meta_snippets", "lang", "locale",
"metadata", "metadata_rev"
)
expect_named(
informant_inc,
c(
"tbl", "read_fn", "tbl_name", "info_label", "meta_snippets", "lang",
"locale", "metadata", "metadata_rev"
)
)

Expand All @@ -74,7 +71,7 @@ test_that("Incorporating an informant yields the correct results", {
expect_no_error(
informant %>%
set_tbl(
tbl = function() readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc")
tbl = function() readr::read_csv(file = testthat::test_path("test_table.csv"), col_types = "TDdcddlc")
) %>%
incorporate()
)
Expand All @@ -87,7 +84,7 @@ test_that("Incorporating an informant yields the correct results", {
informant_inc_2 <-
informant_inc %>%
set_tbl(
tbl = ~ readr::read_csv(file = "test_table_2.csv", col_types = "TDdcddlcd")
tbl = ~ readr::read_csv(file = test_path("test_table_2.csv"), col_types = "TDdcddlcd")
) %>%
incorporate()

Expand All @@ -109,7 +106,7 @@ test_that("Incorporating an informant from YAML yields the correct results", {
# add information with some other `info_*()` functions
informant <-
create_informant(
tbl = ~ readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc"),
tbl = ~ readr::read_csv(file = test_path("test_table.csv"), col_types = "TDdcddlc"),
tbl_name = "test_table",
label = "An example."
) %>%
Expand Down Expand Up @@ -182,7 +179,7 @@ test_that("Incorporating an informant from YAML yields the correct results", {
expect_no_error(
informant_inc_yaml %>%
set_tbl(
tbl = function() readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc")
tbl = function() readr::read_csv(file = test_path("test_table.csv"), col_types = "TDdcddlc")
) %>%
incorporate()
)
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-yaml_exec.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ test_that("The `yaml_exec()` function effectively processes .yml files", {
# Copy the YAML files over to the working directory
fs::file_copy(
path = c(path_agent, path_informant, path_tbl_store),
new_path = getwd()
new_path = getwd(),
overwrite = TRUE
)

# Read YAML files from the working directory, write output
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-yaml_read_informant.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("Reading an informant from YAML is possible", {
# add information with some other `info_*()` functions
informant <-
create_informant(
tbl = ~ readr::read_csv(file = "test_table.csv", col_types = "TDdcddlc"),
tbl = ~ readr::read_csv(file = test_path("test_table.csv"), col_types = "TDdcddlc"),
tbl_name = "test_table",
label = "An example."
) %>%
Expand Down

0 comments on commit 6be4589

Please sign in to comment.