1test_that("automatically finds common variables", {
2  expect_message(vars <- join_cols(c("x", "y"), c("x", "z")))
3  expect_named(vars$x$key, "x")
4  expect_named(vars$y$key, "x")
5})
6
7test_that("key vars are found", {
8  vars <- join_cols(c("x", "y"), c("x", "z"), by = "x")
9  expect_equal(vars$x$key, c(x = 1L))
10  expect_equal(vars$y$key, c(x = 1L))
11
12  vars <- join_cols(c("a", "x", "b"), c("x", "a"), by = "x")
13  expect_equal(vars$x$key, c(x = 2L))
14  expect_equal(vars$y$key, c(x = 1L))
15
16  vars <- join_cols(c("x", "y"), c("a", "x", "z"), by = c("y" = "z"))
17  expect_equal(vars$x$key, c(y = 2L))
18  expect_equal(vars$y$key, c(y = 3L))
19})
20
21test_that("y key matches order and names of x key", {
22  vars <- join_cols(c("x", "y", "z"), c("c", "b", "a"), by = c("x" = "a", "y" = "b"))
23  expect_equal(vars$x$key, c(x = 1L, y = 2L))
24  expect_equal(vars$y$key, c(x = 3L, y = 2L))
25})
26
27test_that("duplicate column names are given suffixes", {
28  vars <- join_cols(c("x", "y"), c("x", "y"), by = "x")
29  expect_equal(vars$x$out, c("x" = 1, "y.x" = 2))
30  expect_equal(vars$y$out, c("y.y" = 2))
31
32  # including join vars when keep = TRUE
33  vars <- join_cols(c("x", "y"), c("x", "y"), by = "x", keep = TRUE)
34  expect_equal(vars$x$out, c("x.x" = 1, "y.x" = 2))
35  expect_equal(vars$y$out, c("x.y" = 1, "y.y" = 2))
36
37  # suffixes don't create duplicates
38  vars <- join_cols(c("x", "y", "y.x"), c("x", "y"), by = "x")
39  expect_equal(vars$x$out, c("x" = 1, "y.x" = 2, "y.x.x" = 3))
40  expect_equal(vars$y$out, c("y.y" = 2))
41
42  # but not when they're the join vars
43  vars <- join_cols(c("A", "A.x"), c("B", "A.x", "A"), by = "A.x")
44  expect_named(vars$x$out, c("A.x.x", "A.x"))
45  expect_named(vars$y$out, c("B", "A.y"))
46
47  # or when no suffix is requested
48  vars <- join_cols(c("x", "y"), c("x", "y"), by = "x", suffix = c("", ".y"))
49  expect_equal(vars$x$out, c("x" = 1, "y" = 2))
50  expect_equal(vars$y$out, c("y.y" = 2))
51})
52
53test_that("NA names are preserved", {
54  vars <- join_cols(c("x", NA), c("x", "z"), by = "x")
55  expect_named(vars$x$out, c("x", NA))
56
57  vars <- join_cols(c("x", NA), c("x", NA), by = "x")
58  expect_named(vars$x$out, c("x", "NA.x"))
59  expect_named(vars$y$out, "NA.y")
60})
61
62test_that("by columns omited from y" , {
63  vars <- join_cols(c("x", "y"), c("x", "y"), by = c("x" = "y"))
64  expect_equal(vars$x$out, c("x" = 1, "y" = 2))
65  expect_equal(vars$y$out, c("x.y" = 1))
66
67  # unless specifically requested
68  vars <- join_cols(c("x", "y"), c("x", "y"), by = c("x" = "y"), keep = TRUE)
69  expect_equal(vars$x$out, c("x.x" = 1, "y.x" = 2))
70  expect_equal(vars$y$out, c("x.y" = 1, "y.y" = 2))
71})
72
73test_that("emits useful messages", {
74  # names
75  expect_snapshot(error = TRUE, join_cols(c("x", "y"), c("y", "y")))
76  expect_snapshot(error = TRUE, join_cols(c("y", "y"), c("x", "y")))
77
78  # common by
79  xy <- c("x", "y")
80  expect_snapshot(vars <- join_cols(xy, xy))
81
82  # by errors
83  expect_snapshot(error = TRUE, join_cols(xy, c("a", "b")))
84
85  expect_snapshot(error = TRUE, join_cols(xy, xy, by = FALSE))
86  expect_snapshot(error = TRUE, join_cols(xy, xy, by = list(1, 2)))
87  expect_snapshot(error = TRUE, join_cols(xy, xy, by = c("x", "x")))
88  expect_snapshot(error = TRUE, join_cols(xy, xy, by = c("x", NA)))
89  expect_snapshot(error = TRUE, join_cols(xy, xy, by = c("aaa", "bbb")))
90
91  # suffixes
92  expect_snapshot(error = TRUE, join_cols(xy, xy, by = "x", suffix = "x"))
93  expect_snapshot(error = TRUE, join_cols(xy, xy, by = "x", suffix = c("", NA)))
94})
95