|
| 1 | +# In a rules file, `keys: []` means "no key", exactly like an absent field or |
| 2 | +# `keys: ~`: yaml::read_yaml() reads [] as list(), which is not NULL, and must |
| 3 | +# not be mistaken for an empty key. The `key` argument, R code, stays strict. |
| 4 | + |
| 5 | +# legacy = TRUE adds a singular `key: [f2]` field, which a present `keys` |
| 6 | +# field must beat whatever its value: without it, the no-key spellings could |
| 7 | +# not be told apart from one another. |
| 8 | +write_rules_with <- function(key_line, legacy = FALSE) { |
| 9 | + path <- tempfile(fileext = ".yaml") |
| 10 | + writeLines(c( |
| 11 | + "version: 1", |
| 12 | + "defaults:", |
| 13 | + key_line, |
| 14 | + if (legacy) " key: [f2]", |
| 15 | + " label: essai", |
| 16 | + "by_type:", |
| 17 | + " character:", |
| 18 | + " equal_mode: exact" |
| 19 | + ), con = path) |
| 20 | + path |
| 21 | +} |
| 22 | + |
| 23 | +tbl <- data.frame(f2 = "OUI", f3 = "NON", stringsAsFactors = FALSE) |
| 24 | + |
| 25 | +test_that("keys: [] in the rules is read as no key: identical tables pass", { |
| 26 | + path <- write_rules_with(" keys: []") |
| 27 | + on.exit(unlink(path), add = TRUE) |
| 28 | + expect_no_error(res <- compare_datasets_from_yaml(tbl, tbl, path = path)) |
| 29 | + expect_true(res$all_passed) |
| 30 | +}) |
| 31 | + |
| 32 | +test_that("keys: [] behaves like keys: ~ and like an absent keys field", { |
| 33 | + paths <- c(write_rules_with(" keys: []"), write_rules_with(" keys: ~"), |
| 34 | + write_rules_with(" na_equal: yes")) |
| 35 | + on.exit(unlink(paths), add = TRUE) |
| 36 | + cand <- data.frame(f2 = "OUI", f3 = "non", stringsAsFactors = FALSE) |
| 37 | + covs <- lapply(paths, FUN = function(p) compare_datasets_from_yaml(tbl, cand, path = p)$coverage) |
| 38 | + expect_false(all(covs[[1]]$n_failed == 0)) |
| 39 | + expect_identical(covs[[1]], covs[[2]]) |
| 40 | + expect_identical(covs[[1]], covs[[3]]) |
| 41 | +}) |
| 42 | + |
| 43 | +test_that("keys: [] is positional: every column is compared, unequal counts error", { |
| 44 | + path <- write_rules_with(" keys: []") |
| 45 | + on.exit(unlink(path), add = TRUE) |
| 46 | + res <- compare_datasets_from_yaml(tbl, tbl, path = path) |
| 47 | + expect_setequal(res$coverage$column[res$coverage$check == "equality"], c("f2", "f3")) |
| 48 | + expect_error( |
| 49 | + compare_datasets_from_yaml(tbl, rbind(tbl, tbl), path = path), |
| 50 | + regexp = "same number of rows" |
| 51 | + ) |
| 52 | +}) |
| 53 | + |
| 54 | +test_that("legacy singular key: [] is read as no key as well", { |
| 55 | + path <- write_rules_with(" key: []") |
| 56 | + on.exit(unlink(path), add = TRUE) |
| 57 | + expect_true(compare_datasets_from_yaml(tbl, tbl, path = path)$all_passed) |
| 58 | +}) |
| 59 | + |
| 60 | +test_that("a present keys field wins over a legacy key field, whatever its no-key spelling", { |
| 61 | + paths <- c(write_rules_with(" keys: []", legacy = TRUE), write_rules_with(" keys: ~", legacy = TRUE)) |
| 62 | + on.exit(unlink(paths), add = TRUE) |
| 63 | + covs <- lapply(paths, FUN = function(p) compare_datasets_from_yaml(tbl, tbl, path = p)$coverage) |
| 64 | + for (cov in covs) { |
| 65 | + expect_setequal(cov$column[cov$check == "equality"], c("f2", "f3")) |
| 66 | + } |
| 67 | + expect_identical(covs[[1]], covs[[2]]) |
| 68 | +}) |
| 69 | + |
| 70 | +test_that("an absent keys field still falls back to the legacy key field", { |
| 71 | + path <- write_rules_with(" na_equal: yes", legacy = TRUE) |
| 72 | + on.exit(unlink(path), add = TRUE) |
| 73 | + res <- compare_datasets_from_yaml(tbl, tbl, path = path) |
| 74 | + expect_identical(res$coverage$column[res$coverage$check == "equality"], "f3") |
| 75 | +}) |
| 76 | + |
| 77 | +test_that("lazy path: keys: [] is positional as well", { |
| 78 | + skip_if_not_installed("duckdb") |
| 79 | + skip_if_not_installed("DBI") |
| 80 | + con <- DBI::dbConnect(duckdb::duckdb()) |
| 81 | + on.exit(DBI::dbDisconnect(con, shutdown = TRUE), add = TRUE) |
| 82 | + duckdb::duckdb_register(con, name = "ref_kel", df = tbl) |
| 83 | + duckdb::duckdb_register(con, name = "cand_kel", df = tbl) |
| 84 | + path <- write_rules_with(" keys: []") |
| 85 | + on.exit(unlink(path), add = TRUE) |
| 86 | + res <- suppressMessages( |
| 87 | + compare_datasets_from_yaml(dplyr::tbl(con, "ref_kel"), dplyr::tbl(con, "cand_kel"), path = path) |
| 88 | + ) |
| 89 | + expect_true(res$all_passed) |
| 90 | + expect_setequal(res$coverage$column[res$coverage$check == "equality"], c("f2", "f3")) |
| 91 | +}) |
| 92 | + |
| 93 | +test_that("the key argument stays strict: an empty vector is an error, not a positional comparison", { |
| 94 | + path <- write_rules_with(" keys: [f2]") |
| 95 | + on.exit(unlink(path), add = TRUE) |
| 96 | + expect_error( |
| 97 | + compare_datasets_from_yaml(tbl, tbl, key = character(0), path = path), |
| 98 | + regexp = "non-empty character vector" |
| 99 | + ) |
| 100 | + expect_error( |
| 101 | + write_rules_template(tbl, key = character(0), path = tempfile(fileext = ".yaml")), |
| 102 | + regexp = "non-empty character vector" |
| 103 | + ) |
| 104 | +}) |
0 commit comments