Skip to content

Commit

Permalink
Add tests for closest() (#6882)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Jul 17, 2023
1 parent aea3dc9 commit 3125697
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/testthat/test-join.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3125697

Please sign in to comment.