diff --git a/tests/testthat/test-join.R b/tests/testthat/test-join.R index 37c5b12bb4..04c75d1ec9 100644 --- a/tests/testthat/test-join.R +++ b/tests/testthat/test-join.R @@ -200,6 +200,90 @@ test_that("joins don't match NA when na_matches = 'never' (#2033)", { ) }) +test_that("`left_join(by = join_by(closest(...)))` works as expected", { + df1 <- tibble(x = 1:5) + df2 <- tibble(y = c(1, 2, 4)) + + out <- left_join(df1, df2, by = join_by(closest(x <= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 4, 4, NA)) + + out <- left_join(df1, df2, by = join_by(closest(x < y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(2, 4, 4, NA, NA)) + + out <- left_join(df1, df2, by = join_by(closest(x >= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 2, 4, 4)) + + out <- left_join(df1, df2, by = join_by(closest(x > y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(NA, 1, 2, 2, 4)) +}) + +test_that("`full_join(by = join_by(closest(...)))` works as expected", { + df1 <- tibble(x = 1:5) + df2 <- tibble(y = c(1, 2, 4)) + + out <- full_join(df1, df2, by = join_by(closest(x <= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 4, 4, NA)) + + out <- full_join(df1, df2, by = join_by(closest(x < y))) + expect_identical(out$x, c(1:5, NA)) + expect_identical(out$y, c(2, 4, 4, NA, NA, 1)) + + out <- full_join(df1, df2, by = join_by(closest(x >= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 2, 4, 4)) + + out <- full_join(df1, df2, by = join_by(closest(x > y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(NA, 1, 2, 2, 4)) +}) + +test_that("`right_join(by = join_by(closest(...)))` works as expected", { + df1 <- tibble(x = 1:5) + df2 <- tibble(y = c(1, 2, 4)) + + out <- right_join(df1, df2, by = join_by(closest(x <= y))) + expect_identical(out$x, 1:4) + expect_identical(out$y, c(1, 2, 4, 4)) + + out <- right_join(df1, df2, by = join_by(closest(x < y))) + expect_identical(out$x, c(1:3, NA)) + expect_identical(out$y, c(2, 4, 4, 1)) + + out <- right_join(df1, df2, by = join_by(closest(x >= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 2, 4, 4)) + + out <- right_join(df1, df2, by = join_by(closest(x > y))) + expect_identical(out$x, 2:5) + expect_identical(out$y, c(1, 2, 2, 4)) +}) + +test_that("`inner_join(by = join_by(closest(...)))` works as expected", { + df1 <- tibble(x = 1:5) + df2 <- tibble(y = c(1, 2, 4)) + + out <- inner_join(df1, df2, by = join_by(closest(x <= y))) + expect_identical(out$x, 1:4) + expect_identical(out$y, c(1, 2, 4, 4)) + + out <- inner_join(df1, df2, by = join_by(closest(x < y))) + expect_identical(out$x, 1:3) + expect_identical(out$y, c(2, 4, 4)) + + out <- inner_join(df1, df2, by = join_by(closest(x >= y))) + expect_identical(out$x, 1:5) + expect_identical(out$y, c(1, 2, 2, 4, 4)) + + out <- inner_join(df1, df2, by = join_by(closest(x > y))) + expect_identical(out$x, 2:5) + expect_identical(out$y, c(1, 2, 2, 4)) +}) + test_that("joins using `between(bounds =)` work as expected (#6488)", { df1 <- tibble(x = 1:5) df2 <- tibble(lower = 2, upper = 4)