Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jun 2, 2024
1 parent 80808fb commit f961b36
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
16 changes: 15 additions & 1 deletion tests/testthat/_snaps/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Code
as.list(dbGetQuery(con,
"SELECT * EXCLUDE (timestamp_tz, time_tz, timestamp_ns, timestamp_array, timestamptz_array, map, bit, \"union\", fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array) REPLACE(replace(varchar, chr(0), '') AS varchar) FROM test_all_types(use_large_enum=true)"))
"SELECT * EXCLUDE (timestamp_tz, time_tz, timestamp_ns, timestamp_array, timestamptz_array, bit, \"union\", fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array) REPLACE(replace(varchar, chr(0), '') AS varchar) FROM test_all_types(use_large_enum=true)"))
Output
$bool
[1] FALSE TRUE NA
Expand Down Expand Up @@ -207,4 +207,18 @@
NULL
$map
$map[[1]]
[1] key value
<0 rows> (or 0-length row.names)
$map[[2]]
key value
1 key1 🦆🦆🦆🦆🦆🦆
2 key2 goose
$map[[3]]
NULL

87 changes: 87 additions & 0 deletions tests/testthat/test-map.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
test_that("maps can be read", {
skip_if_not_installed("vctrs")

con <- dbConnect(duckdb())
on.exit(dbDisconnect(con, shutdown = TRUE))

res <- dbGetQuery(
con,
"SELECT map([1,2],['a','b']) AS x"
)
expect_equal(res, vctrs::data_frame(
x = list(
vctrs::data_frame(key = 1:2, value = letters[1:2])
)
))

res <- dbGetQuery(
con,
"SELECT 1 as a, map([1,2],[1.5,2.5]) AS x UNION SELECT 2, map([3,4,5],[5.5,4.5,3.5]) ORDER BY a"
)
expect_equal(res, vctrs::data_frame(
a = 1:2,
x = list(
vctrs::data_frame(key = 1:2, value = 1:2 + 0.5),
vctrs::data_frame(key = 3:5, value = 5:3 + 0.5)
)
))

res <- dbGetQuery(
con,
"SELECT 1 as a, map([1,2],[TRUE,FALSE]) AS x UNION SELECT 2, NULL ORDER BY a"
)
expect_equal(res, vctrs::data_frame(
a = 1:2,
x = list(
vctrs::data_frame(key = 1:2, value = c(TRUE, FALSE)),
NULL
)
))
})

test_that("structs give the same results via Arrow", {
skip_on_cran()
skip_if_not_installed("vctrs")
skip_if_not_installed("tibble")
skip_if_not_installed("arrow", "13.0.0")

con <- dbConnect(duckdb())
on.exit(dbDisconnect(con, shutdown = TRUE))

res <- dbGetQuery(
con,
"SELECT map([1,2],['a','b']) AS x",
arrow = TRUE
)
expect_equal(res, vctrs::data_frame(
x = structure(class = c("arrow_list", class(vctrs::list_of(logical()))), vctrs::list_of(
tibble::tibble(key = 1:2, value = letters[1:2])
))
))

res <- dbGetQuery(
con,
"SELECT 1 as a, map([1,2],[1.5,2.5]) AS x UNION SELECT 2, map([3,4,5],[5.5,4.5,3.5]) ORDER BY a",
arrow = TRUE
)
expect_equal(res, vctrs::data_frame(
a = 1:2,
x = structure(class = c("arrow_list", class(vctrs::list_of(logical()))), vctrs::list_of(
tibble::tibble(key = 1:2, value = 1:2 + 0.5),
tibble::tibble(key = 3:5, value = 5:3 + 0.5)
))
))

res <- dbGetQuery(
con,
"SELECT 1 as a, map([1,2],[TRUE,FALSE]) AS x UNION SELECT 2, NULL ORDER BY a",
arrow = TRUE
)
expect_equal(res, vctrs::data_frame(
a = 1:2,
x = structure(class = c("arrow_list", class(vctrs::list_of(logical()))), vctrs::list_of(
tibble::tibble(key = 1:2, value = c(TRUE, FALSE)),
NULL
))
))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-types.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ test_that("test_all_types() output", {

# Need to omit timestamp columns, likely due to https://bugs.r-project.org/show_bug.cgi?id=16856
expect_snapshot({
as.list(dbGetQuery(con, "SELECT * EXCLUDE (timestamp_tz, time_tz, timestamp_ns, timestamp_array, timestamptz_array, map, bit, \"union\", fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array) REPLACE(replace(varchar, chr(0), '') AS varchar) FROM test_all_types(use_large_enum=true)"))
as.list(dbGetQuery(con, "SELECT * EXCLUDE (timestamp_tz, time_tz, timestamp_ns, timestamp_array, timestamptz_array, bit, \"union\", fixed_int_array, fixed_varchar_array, fixed_nested_int_array, fixed_nested_varchar_array, fixed_struct_array, struct_of_fixed_array, fixed_array_of_int_list, list_of_fixed_int_array) REPLACE(replace(varchar, chr(0), '') AS varchar) FROM test_all_types(use_large_enum=true)"))
})
})

0 comments on commit f961b36

Please sign in to comment.