Skip to content

Commit

Permalink
chore: update tests with metadata instead of label
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhav committed Nov 21, 2023
1 parent 6c58cdf commit 399a4fd
Showing 1 changed file with 106 additions and 10 deletions.
116 changes: 106 additions & 10 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,48 @@ test_that("xportr_write: exported data can be saved to a file", {
expect_equal(read_xpt(tmp), data_to_save)
})

test_that("xportr_write: exported data can be saved to a file with a label", {
test_that("xportr_write: exported data can still be saved to a file with a label", {
tmpdir <- tempdir()
tmp <- file.path(tmpdir, "xyz.xpt")

on.exit(unlink(tmpdir))

xportr_write(data_to_save, path = tmp, label = "Lorem ipsum dolor sit amet")
suppressWarnings(xportr_write(data_to_save, path = tmp, label = "Lorem ipsum dolor sit amet"))
expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet")
})

test_that("xportr_write: exported data can be saved to a file with a metadata", {
tmpdir <- tempdir()
tmp <- file.path(tmpdir, "xyz.xpt")

on.exit(unlink(tmpdir))

xportr_write(
data_to_save,
path = tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "Lorem ipsum dolor sit amet"
)
)
expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet")
})

test_that("xportr_write: exported data can be saved to a file with a existing metadata", {
tmpdir <- tempdir()
tmp <- file.path(tmpdir, "xyz.xpt")

on.exit(unlink(tmpdir))

df <- xportr_df_label(
data_to_save,
data.frame(
dataset = "data_to_save",
label = "Lorem ipsum dolor sit amet"
)
)

xportr_write(df, path = tmp)
expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet")
})

Expand All @@ -26,7 +61,16 @@ test_that("xportr_write: expect error when invalid multibyte string is passed in

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = "Lorizzle ipsizzle dolizzl\xe7 pizzle"))
expect_error(
xportr_write(
data_to_save,
tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "Lorizzle ipsizzle dolizzl\xe7 pizzle"
)
)
)
})

test_that("xportr_write: expect error when file name is over 8 characters long", {
Expand All @@ -35,7 +79,7 @@ test_that("xportr_write: expect error when file name is over 8 characters long",

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = "asdf"))
expect_error(xportr_write(data_to_save, tmp))
})

test_that("xportr_write: expect error when file name contains non-ASCII symbols or special characters", {
Expand All @@ -44,7 +88,7 @@ test_that("xportr_write: expect error when file name contains non-ASCII symbols

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = "asdf"))
expect_error(xportr_write(data_to_save, tmp))
})

test_that("xportr_write: expect error when label contains non-ASCII symbols or special characters", {
Expand All @@ -53,7 +97,22 @@ test_that("xportr_write: expect error when label contains non-ASCII symbols or s

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = "çtestç"))
expect_error(
xportr_write(
data_to_save,
tmp,
expect_error(
xportr_write(
data_to_save,
tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "çtestç"
)
)
)
)
)
})

test_that("xportr_write: expect error when label is over 40 characters", {
Expand All @@ -62,7 +121,16 @@ test_that("xportr_write: expect error when label is over 40 characters", {

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = paste(rep("a", 41), collapse = "")))
expect_error(
xportr_write(
data_to_save,
tmp,
metadata = data.frame(
dataset = "data_to_save",
label = paste(rep("a", 41), collapse = "")
)
)
)
})

test_that("xportr_write: expect error when an xpt validation fails with strict_checks set to TRUE", {
Expand All @@ -72,7 +140,16 @@ test_that("xportr_write: expect error when an xpt validation fails with strict_c

on.exit(unlink(tmpdir))

expect_error(xportr_write(data_to_save, tmp, label = "label", strict_checks = TRUE))
expect_error(
xportr_write(
data_to_save, tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "label"
),
strict_checks = TRUE
)
)
})

test_that("xportr_write: expect warning when an xpt validation fails with strict_checks set to FALSE", {
Expand All @@ -82,7 +159,16 @@ test_that("xportr_write: expect warning when an xpt validation fails with strict

on.exit(unlink(tmpdir))

expect_warning(xportr_write(data_to_save, tmp, label = "label", strict_checks = FALSE))
expect_warning(
xportr_write(
data_to_save, tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "label"
),
strict_checks = FALSE
)
)
})


Expand All @@ -93,8 +179,18 @@ test_that("xportr_write: Capture errors by haven and report them as such", {

on.exit(unlink(tmpdir))


expect_error(
suppressWarnings(xportr_write(data_to_save, tmp, label = "label", strict_checks = FALSE)),
suppressWarnings(
xportr_write(
data_to_save, tmp,
metadata = data.frame(
dataset = "data_to_save",
label = "label"
),
strict_checks = FALSE
)
),
"Error reported by haven"
)
})

0 comments on commit 399a4fd

Please sign in to comment.