Skip to content

Commit

Permalink
test(db_joins): Test all dplyr joins work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Oct 13, 2024
1 parent ddb1f54 commit d2c6f7d
Showing 1 changed file with 64 additions and 22 deletions.
86 changes: 64 additions & 22 deletions tests/testthat/test-db_joins.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
test_that("*_join() works with character `by` and `na_by`", {
for (conn in get_test_conns()) {

# Define two test datasets
x <- get_table(conn, "__mtcars") |>
dplyr::select(name, mpg, cyl, hp, vs, am, gear, carb)

y <- get_table(conn, "__mtcars") |>
dplyr::select(name, drat, wt, qsec)


# Test the implemented joins
q <- dplyr::left_join(x, y, by = "name") |> dplyr::collect()
qr <- dplyr::left_join(dplyr::collect(x), dplyr::collect(y), by = "name")
expect_equal(q, qr)

q <- dplyr::right_join(x, y, by = "name") |> dplyr::collect()
qr <- dplyr::right_join(dplyr::collect(x), dplyr::collect(y), by = "name")
expect_equal(q, qr)

q <- dplyr::inner_join(x, y, by = "name") |> dplyr::collect()
qr <- dplyr::inner_join(dplyr::collect(x), dplyr::collect(y), by = "name")
expect_equal(q, qr)


# Create two more synthetic test data set with NA data

# First test case
Expand Down Expand Up @@ -144,3 +122,67 @@ test_that("*_join() works with `dplyr::join_by()`", {
connection_clean_up(conn)
}
})


test_that("*_join() does not break any dplyr joins", {
for (conn in get_test_conns()) {

# Define two test datasets
x <- get_table(conn, "__mtcars") |>
dplyr::select(name, mpg, cyl, hp, vs, am, gear, carb)

y <- get_table(conn, "__mtcars") |>
dplyr::select(name, drat, wt, qsec)

# Test the standard joins
# left_join
qr <- dplyr::left_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::left_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::left_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

# right_join
qr <- dplyr::right_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::right_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::right_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

# inner_join
qr <- dplyr::inner_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::inner_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::inner_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

# full_join
qr <- dplyr::full_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::full_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::full_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

# semi_join
qr <- dplyr::semi_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::semi_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::semi_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

# anti_join
qr <- dplyr::anti_join(dplyr::collect(x), dplyr::collect(y), by = "name")
q <- dplyr::anti_join(x, y, by = "name") |> dplyr::collect()
expect_equal(q, qr)

q <- dplyr::anti_join(x, y, by = dplyr::join_by(x$name == y$name)) |> dplyr::collect()
expect_equal(q, qr)

connection_clean_up(conn)
}
})

0 comments on commit d2c6f7d

Please sign in to comment.