Skip to content

Commit

Permalink
Testing speed up (#553)
Browse files Browse the repository at this point in the history
* Turn on parallel testing
* Reduce creation/deletion of datasets
  • Loading branch information
hadley authored Nov 3, 2023
1 parent 8e251e0 commit df16933
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 41 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ LinkingTo:
Rcpp
Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
Config/testthat/start-first: bq-table, dplyr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Expand Down
2 changes: 2 additions & 0 deletions R/bigrquery-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#' @rawNamespace import(assertthat, except = has_name)
"_PACKAGE"

the <- new_environment()

# We import rlang this way because jsonlite's unbox() is used extensively.

## usethis namespace: start
Expand Down
5 changes: 5 additions & 0 deletions R/bq-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ bq_test_dataset <- function(name = random_name(), location = "US") {
ds
}

bq_test_table <- function() {
ds <- env_cache(the, "test_dataset", bq_test_dataset())
bq_table(ds, random_name())
}

#' @export
#' @rdname bq_test_project
bq_testable <- function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bq-job.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("can control chattiness of bq_job_wait", {

test_that("informative errors on failure", {
ds <- bq_test_dataset()
tb <- bq_table(ds, "df")
tb <- bq_test_table()

fields <- bq_fields(list(bq_field("x", "integer"), bq_field("y", "string")))
bq_mtcars <- bq_table_create(tb, fields = fields)
Expand Down
10 changes: 3 additions & 7 deletions tests/testthat/test-bq-perform.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
test_that("bq_perform_upload creates job that succeeds", {
ds <- bq_test_dataset()
bq_mtcars <- bq_table(ds, "mtcars")
bq_mtcars <- bq_test_table()

job <- bq_perform_upload(bq_mtcars, mtcars)
expect_s3_class(job, "bq_job")
Expand All @@ -13,10 +12,8 @@ test_that("bq_perform_upload creates job that succeeds", {
})

test_that("bq_perform_copy creates job that succeeds", {
ds <- bq_test_dataset()

src <- as_bq_table("bigquery-public-data.moon_phases.moon_phases")
dst <- bq_table(ds, "my_moon")
dst <- bq_test_table()

job <- bq_perform_copy(src, dst)
expect_s3_class(job, "bq_job")
Expand All @@ -34,7 +31,6 @@ test_that("bq_perform_copy creates job that succeeds", {

test_that("can round trip extract + load", {
ds_public <- bq_dataset("bigquery-public-data", "moon_phases")
ds_mine <- bq_test_dataset()

tb <- bq_dataset_query(ds_public,
query = "SELECT COUNT(*) as count FROM moon_phases",
Expand All @@ -47,7 +43,7 @@ test_that("can round trip extract + load", {
job <- bq_perform_extract(tb, tmp)
bq_job_wait(job)

tb_ks <- bq_table(ds_mine, "natality_ks")
tb_ks <- bq_test_table()
job <- bq_perform_load(tb_ks, tmp)
bq_job_wait(job)

Expand Down
47 changes: 14 additions & 33 deletions tests/testthat/test-bq-table.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
test_that("can create and delete tables", {
ds <- bq_test_dataset()

bq_mtcars <- bq_table(ds, "mtcars")
bq_mtcars <- bq_test_table()
expect_false(bq_table_exists(bq_mtcars))

bq_table_create(
Expand All @@ -24,8 +22,7 @@ test_that("can retrieve table size information", {
})

test_that("can create table with schema", {
ds <- bq_test_dataset()
tb <- bq_table(ds, "df")
tb <- bq_test_table()

df <- data.frame(x = 1, y = "a")
bq_table_create(tb, "df", fields = df)
Expand All @@ -35,10 +32,10 @@ test_that("can create table with schema", {
})

test_that("can round trip to non-default location", {
asia <- bq_test_dataset(location = "asia-east1")
dallas <- bq_test_dataset(location = "us-south1")
df1 <- tibble(x = 1:10, y = letters[1:10])

bq_df <- bq_table(asia, "df")
bq_df <- bq_table(dallas, "df")
bq_table_upload(bq_df, df1)

df2 <- bq_table_download(bq_df)
Expand All @@ -49,10 +46,8 @@ test_that("can round trip to non-default location", {
})

test_that("can roundtrip via save + load", {
ds <- bq_test_dataset()

tb1 <- bq_table(bq_test_project(), "basedata", "mtcars")
tb2 <- bq_table(ds, "save_load")
tb2 <- bq_test_table()
gs <- gs_test_object()

bq_table_save(tb1, gs)
Expand All @@ -64,8 +59,7 @@ test_that("can roundtrip via save + load", {
})

test_that("can copy table from public dataset", {
ds <- bq_test_dataset()
my_natality <- bq_table(ds, "mynatality")
my_natality <- bq_test_table()

out <- bq_table_copy("publicdata.samples.natality", my_natality)
expect_equal(out, my_natality)
Expand All @@ -75,15 +69,14 @@ test_that("can copy table from public dataset", {
# data-types --------------------------------------------------------------

test_that("can round trip atomic vectors", {
ds <- bq_test_dataset()
df1 <- tibble(
lgl = c(FALSE, TRUE, NA),
int = c(-1, 1, NA),
dbl = c(-1.5, 1.5, NA),
chr = c("A", "B", NA)
)

bq_df <- bq_table(ds, "df")
bq_df <- bq_test_table()
bq_table_upload(bq_df, df1)

df2 <- bq_table_download(bq_df, bigint = "integer")
Expand All @@ -94,19 +87,18 @@ test_that("can round trip atomic vectors", {
})

test_that("can round-trip POSIXt to either TIMESTAMP or DATETIME", {
ds <- bq_test_dataset()
df <- tibble(datetime = as.POSIXct("2020-01-01 09:00", tz = "UTC"))

tb1 <- bq_table_create(
bq_table(ds, "timestamp"),
bq_test_table(),
bq_fields(list(bq_field("datetime", "TIMESTAMP")))
)
bq_table_upload(tb1, df)
df1 <- bq_table_download(tb1)
expect_equal(df1, df)

tb2 <- bq_table_create(
bq_table(ds, "datetime2"),
bq_test_table(),
bq_fields(list(bq_field("datetime", "DATETIME")))
)
bq_table_upload(tb2, df)
Expand All @@ -115,8 +107,7 @@ test_that("can round-trip POSIXt to either TIMESTAMP or DATETIME", {
})

test_that("can round trip data frame with list-cols", {
ds <- bq_test_dataset()
tb <- bq_table(ds, "complex")
tb <- bq_test_table()

df1 <- tibble::tibble(
val = 1.5,
Expand All @@ -140,8 +131,7 @@ test_that("can round trip data frame with list-cols", {
})

test_that("can create table field description", {
ds <- bq_test_dataset()
partition_table <- bq_table(ds, "table_field_description")
partition_table <- bq_test_table()

bq_table_create(
partition_table,
Expand All @@ -153,8 +143,7 @@ test_that("can create table field description", {
})

test_that("can patch table with new fields in the schema", {
ds <- bq_test_dataset()
tb <- bq_table(ds, "table_to_patch")
tb <- bq_test_table()
df <- data.frame(id = 1)
bq_table_create(tb, fields = df)

Expand All @@ -171,26 +160,18 @@ test_that("can patch table with new fields in the schema", {
test_that("can round-trip GEOGRAPHY", {
skip_if_not_installed("wk")

ds <- bq_test_dataset()
df <- tibble(geography = wk::wkt("POINT(30 10)"))

tb1 <- bq_table_create(
bq_table(ds, "geography"),
as_bq_fields(df)
)
tb1 <- bq_table_create(bq_test_table(), as_bq_fields(df))
bq_table_upload(tb1, df)
df1 <- bq_table_download(tb1)
expect_equal(df1, df)
})

test_that("can round-trip BYTES", {
ds <- bq_test_dataset()
df <- tibble(x = blob::blob(charToRaw("hi!"), charToRaw("bye")))

tb1 <- bq_table_create(
bq_table(ds, "bytes"),
as_bq_fields(df)
)
tb1 <- bq_table_create(bq_test_table(), as_bq_fields(df))
bq_table_upload(tb1, df)
df1 <- bq_table_download(tb1)
expect_equal(df1, df)
Expand Down

0 comments on commit df16933

Please sign in to comment.