library(testthat) library(data.table) library(DTwrappers2) # Ensure you're using the 3rd edition of testthat local_test_context() # Define a setup function for your test fixture setup_iris_fixture <- function() { iris_dt <- data.table(iris) iris_dt$SL.1000 <- iris_dt$Sepal.Length * 1000 + rnorm(n = nrow(iris_dt), mean = 0, sd = 25) assign("iris_dt", iris_dt, envir = globalenv()) # Assign to the global environment } # Start your test file with this setup call setup_iris_fixture() describe("dt.character.coercion.culprits functionality", { describe("Basic functionality", { it("returns a data.table with expected columns", { result <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.8, the.variables = ".") expect_true(is.data.table(result)) expect_true(all(c("variable", "character.coercion.culprits") %in% names(result))) }) }) describe("Threshold handling", { it("adjusts output based on the threshold", { result_low_threshold <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.1, the.variables = ".") result_high_threshold <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.9, the.variables = ".") expect_true(nrow(result_low_threshold) >= nrow(result_high_threshold)) }) }) describe("Variable selection", { it("only processes specified variables", { result_specific_vars <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.5, the.variables = c("Sepal.Width", "Petal.Width")) expect_equal(length(unique(result_specific_vars$variable)), 2) expect_true(all(result_specific_vars$variable %in% c("Sepal.Width", "Petal.Width"))) }) }) describe("Grouping functionality", { it("correctly groups the results", { result_grouping <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.5, the.variables = ".", grouping.variables = "Species") expect_true("Species" %in% names(result_grouping)) expect_equal(length(unique(result_grouping$Species)), length(unique(iris$Species))) }) }) describe("Return type handling", { it("handles different return types correctly", { result_code <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.5, the.variables = ".", return.as = "code") result_all <- dt.character.coercion.culprits(dt.name = "iris", threshold.for.numeric = 0.5, the.variables = ".", return.as = "all") expect_is(result_code, "character") expect_is(result_all, "list") expect_true("result" %in% names(result_all) && "code" %in% names(result_all)) }) }) })