Skip to content

Commit

Permalink
Created tests for new functions (export_rda and extract_caps_alttext)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbreitbart-NOAA committed Jan 22, 2025
1 parent a5191ee commit b45412c
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
96 changes: 96 additions & 0 deletions tests/testthat/test-export_rda.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
test_that("export_rda works for figures", {
# read in sample dataset
dat <- utils::read.csv(
system.file(
"resources",
"sample_data",
"petrale_sole-after_2020.csv",
package = "satf"
)
)

topic_label <- "biomass"
fig_or_table <- "figure"

# run write_captions.R
satf::write_captions(dat = dat,
dir = getwd(),
year = 2022)

# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table,
dir = getwd())

# make a simple plot
library(ggplot2)
final <- ggplot2::ggplot(Orange, aes(circumference, age)) +
ggplot2::geom_point()

# export rda
export_rda(
final = final,
caps_alttext = caps_alttext,
rda_dir = getwd(),
topic_label = topic_label,
fig_or_table = fig_or_table
)

# expect that both rda_files dir and the biomass_figure.rda file exist
expect_true(dir.exists(fs::path(getwd(), "rda_files")))
expect_true(file.exists(fs::path(
getwd(), "rda_files", "biomass_figure.rda"
)))

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
unlink(fs::path(getwd(), "rda_files"), recursive = T)

})

test_that("export_rda works for tables", {
# read in sample dataset
dat <- utils::read.csv(
system.file(
"resources",
"sample_data",
"petrale_sole-after_2020.csv",
package = "satf"
)
)

topic_label <- "bnc"
fig_or_table <- "table"

# run write_captions.R
satf::write_captions(dat = dat,
dir = getwd(),
year = 2022)

# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table,
dir = getwd())

# make a simple table
final <- flextable::flextable(data = data.frame(x = c(1, 2, 3),
y = c(4, 5, 6)))

# export rda
export_rda(
final = final,
caps_alttext = caps_alttext,
rda_dir = getwd(),
topic_label = topic_label,
fig_or_table = fig_or_table
)

# expect that both rda_files dir and the bnc_table.rda file exist
expect_true(dir.exists(fs::path(getwd(), "rda_files")))
expect_true(file.exists(fs::path(getwd(), "rda_files", "bnc_table.rda")))

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
unlink(fs::path(getwd(), "rda_files"), recursive = T)

})
66 changes: 66 additions & 0 deletions tests/testthat/test-extract_caps_alttext.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
test_that("extract_caps_alttext works for figures", {

# read in sample dataset
dat <- utils::read.csv(
system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf")
)

topic_label <- "biomass"
fig_or_table <- "figure"

# run write_captions.R
satf::write_captions(dat = dat,
dir = getwd(),
year = 2022)

# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table,
dir = getwd())


# expect that the caps_alttext is a list with two objects (caption, alt text)
expect_true(length(caps_alttext) == 2)

# expect the first 4 words of the caption
expect_true("Biomass (B) time series." == stringr::word(caps_alttext[[1]], 1, 4))

# expect the first 3 words of the alt text
expect_true("Line graph showing" == stringr::word(caps_alttext[[2]], 1, 3))

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))

})

test_that("extract_caps_alttext works for tables", {

# read in sample dataset
dat <- utils::read.csv(
system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf")
)

topic_label <- "bnc"
fig_or_table <- "table"

# run write_captions.R
satf::write_captions(dat = dat,
dir = getwd(),
year = 2022)

# extract this plot's caption and alt text
caps_alttext <- extract_caps_alttext(topic_label = topic_label,
fig_or_table = fig_or_table,
dir = getwd())


# expect that the caps_alttext is a list with one object (caption)
expect_true(length(caps_alttext) == 1)

# expect the first 4 words of the caption
expect_true("Historical biomass, abundance, and" == stringr::word(caps_alttext, 1, 4))

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))

})

0 comments on commit b45412c

Please sign in to comment.