diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index 51b569b6..4229c06e 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -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") }) @@ -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", { @@ -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", { @@ -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", { @@ -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", { @@ -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", { @@ -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", { @@ -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 + ) + ) }) @@ -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" ) })