Skip to content

Commit

Permalink
Test tidying (#559)
Browse files Browse the repository at this point in the history
* Align test and R names
* Convert `expect_error()` to `expect_snapshot()` (Fixes #554)
* Minor polishing
  • Loading branch information
hadley authored Nov 6, 2023
1 parent 277ed35 commit 95a1cf5
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 80 deletions.
27 changes: 0 additions & 27 deletions R/bq-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,3 @@ bq_dataset_query <- function(x, query,
bq_job_wait(job, quiet = quiet)
bq_job_table(job)
}

# Similar to bq_perform_query: except uses dryRun, and hence result
# processing is totally different
bq_query_fields <- function(query, x, ..., default_dataset = NULL, use_legacy_sql = FALSE) {
assert_that(is.string(x), is.string(query))

url <- bq_path(x, jobs = "")

query <- list(
query = unbox(query),
useLegacySql = unbox(use_legacy_sql)
)
if (!is.null(default_dataset)) {
query$defaultDataset <- datasetReference(default_dataset)
}

body <- list(configuration = list(query = query, dryRun = TRUE))

res <- bq_post(
url,
body = bq_body(body, ...),
query = list(fields = "statistics(query(schema(fields)))")
)

fields <- res$statistics$query$schema$fields
bq_fields(lapply(fields, as_bq_field))
}
2 changes: 1 addition & 1 deletion R/bq-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bq_test_init <- function(name = "basedata") {

bq_mtcars <- bq_table(basedata, "mtcars")
if (!bq_table_exists(bq_mtcars)) {
bq_table_upload(bq_mtcars, values = datasets::mtcars)
job <- bq_table_upload(bq_mtcars, values = datasets::mtcars)
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/bq-dataset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# by default can not delete dataset containing tables

Code
bq_dataset_delete(ds)
Condition
Error in `signal_reason()`:
! Dataset gargle-169921:<DATASET> is still in use [resourceInUse]

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/bq-download.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# errors when table is known to be incomplete

Code
bq_table_download(tb, n_max = 35000, page_size = 35000, bigint = "integer64")
Condition
Error in `bq_table_download()`:
! First chunk is incomplete:
x 35,000 rows were requested, but only 31,977 rows were received.
i Leave `page_size` unspecified or use an even smaller value.

41 changes: 41 additions & 0 deletions tests/testthat/_snaps/bq-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,44 @@
Output
<bq_table> x.y.z

# string coercion error on invalid number of components

Code
as_bq_table("x")
Condition
Error:
! Character <bq_table> must contain 3 components when split by `.`
Code
as_bq_table("a.b.c.d")
Condition
Error:
! Character <bq_table> must contain 3 components when split by `.`
Code
as_bq_job("x")
Condition
Error:
! Character <bq_job> must contain 3 components when split by `.`
Code
as_bq_dataset("x")
Condition
Error:
! Character <bq_dataset> must contain 2 components when split by `.`

# list coercion errors with bad names

Code
as_bq_table(list())
Condition
Error:
! List <bq_table> must have components projectId, datasetId and tableId
Code
as_bq_dataset(list())
Condition
Error:
! List <bq_dataset> must have components projectId and datasetId
Code
as_bq_job(list())
Condition
Error:
! List <bq_job> must have components projectId, jobId and location

13 changes: 13 additions & 0 deletions tests/testthat/_snaps/bq-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# error if env var not set

Code
bq_test_project()
Condition
Error:
! To run bigrquery tests you must have BIGQUERY_TEST_PROJECT envvar set to name of project which has billing set up and to which you have write access
Code
gs_test_bucket()
Condition
Error:
! To run bigrquery extract/load tests you must have BIGQUERY_TEST_BUCKET set to name of the bucket where `bq_test_project()` has write acess

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/dbi-connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
<BigQueryConnection>
Billing: p

# dataset is optional

Code
DBI::dbListTables(con)
Condition
Error:
! To list table, must supply `dataset` when creating connection

---

Code
DBI::dbReadTable(con, "natality", n_max = 10)
Condition
Error in `as_bq_table()`:
! `name` ("natality") must have 2 or 3 components if the connection doesn't have a dataset.

# can create bq_table from connection + name

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# bq_check_namespace() works

Code
bq_check_namespace("invalid package name", "FIELD_TYPE")
Condition
Error in `bq_check_namespace()`:
! Package 'invalid package name' must be installed to load BigQuery field with type 'FIELD_TYPE'

6 changes: 5 additions & 1 deletion tests/testthat/test-bq-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ test_that("by default can not delete dataset containing tables", {
ds <- bq_test_dataset()

bq_table_create(bq_table(ds, "testing"))
expect_error(bq_dataset_delete(ds))
expect_snapshot(
bq_dataset_delete(ds),
error = TRUE,
transform = function(x) gsub(ds$dataset, "<DATASET>", x)
)
})

test_that("can list tables in a dataset", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-bq-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ test_that("errors when table is known to be incomplete", {
skip_if_no_auth()

tb <- as_bq_table("bigquery-public-data.chicago_taxi_trips.taxi_trips")
expect_error(
expect_snapshot(
bq_table_download(
tb,
n_max = 35000,
page_size = 35000,
bigint = "integer64"
),
"incomplete"
error = TRUE
)
})

Expand Down
File renamed without changes.
18 changes: 11 additions & 7 deletions tests/testthat/test-bq-refs.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ test_that("objects have helpful print methods", {
})

test_that("string coercion error on invalid number of components", {
expect_error(as_bq_table("x"), "must contain 3")
expect_error(as_bq_table("a.b.c.d"), "must contain 3")
expect_error(as_bq_job("x"), "must contain 3")
expect_error(as_bq_dataset("x"), "must contain 2")
expect_snapshot(error = TRUE, {
as_bq_table("x")
as_bq_table("a.b.c.d")
as_bq_job("x")
as_bq_dataset("x")
})
})

test_that("list coercion errors with bad names", {
expect_error(as_bq_table(list()), "must have components")
expect_error(as_bq_dataset(list()), "must have components")
expect_error(as_bq_job(list()), "must have components")
expect_snapshot(error = TRUE, {
as_bq_table(list())
as_bq_dataset(list())
as_bq_job(list())
})
})
22 changes: 22 additions & 0 deletions tests/testthat/test-bq-request.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
test_that("api keys are added when present", {
key <- "my.secret.key"
withr::local_envvar(list(BIGRQUERY_API_KEY = key))

expect_equal(prepare_bq_query(NULL), list(key = key))
expect_equal(
prepare_bq_query(list(herring_color = "red")),
list(herring_color = "red", key = key)
)
})

test_that("explicit api keys override env vars", {
key <- "my.secret.key"
withr::local_envvar(list(BIGRQUERY_API_KEY = key))

expect_equal(
prepare_bq_query(list(key = "my.other.key")),
list(key = "my.other.key")
)
})

test_that("pagination warns if pages left on server", {
skip_if_no_auth()

Expand All @@ -12,3 +33,4 @@ test_that("pagination warns if pages left on server", {
)

})

17 changes: 12 additions & 5 deletions tests/testthat/test-bq-test.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
test_that("can init new dataset", {
test_that("can init and clean up dataset", {
ds <- bq_test_dataset()
bq_test_init(ds$dataset)
Sys.sleep(1)
expect_true(bq_dataset_exists(ds))

bq_test_init(ds$dataset)
expect_true(bq_table_exists(bq_table(ds, "mtcars")))

attr(ds, "env") <- NULL
gc()
expect_false(bq_dataset_exists(ds))
})

test_that("error if env var not set", {
Expand All @@ -12,6 +17,8 @@ test_that("error if env var not set", {
TESTTHAT = ""
))

expect_error(bq_test_project(), "BIGQUERY_TEST_PROJECT")
expect_error(gs_test_bucket(), "BIGQUERY_TEST_BUCKET")
expect_snapshot(error = TRUE, {
bq_test_project()
gs_test_bucket()
})
})
7 changes: 2 additions & 5 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,12 @@ test_that("can append to an existing dataset", {

test_that("dataset is optional", {
con <- DBI::dbConnect(bigquery(), project = bq_test_project())
expect_error(DBI::dbListTables(con), "`dataset`")
expect_snapshot(DBI::dbListTables(con), error = TRUE)

df <- DBI::dbReadTable(con, "publicdata.samples.natality", n_max = 10)
expect_equal(ncol(df), 31)

expect_error(
DBI::dbReadTable(con, "natality", n_max = 10),
"must have 2 or 3 components"
)
expect_snapshot(DBI::dbReadTable(con, "natality", n_max = 10), error = TRUE)
})

test_that("can create bq_table from connection + name", {
Expand Down
20 changes: 0 additions & 20 deletions tests/testthat/test-request.R

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/test-test.R

This file was deleted.

6 changes: 3 additions & 3 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

test_that("bq_check_namespace() works", {
expect_error(bq_check_namespace("bigrquery", "FIELD_TYPE"), NA)
expect_error(
expect_no_error(bq_check_namespace("bigrquery", "FIELD_TYPE"))
expect_snapshot(
bq_check_namespace("invalid package name", "FIELD_TYPE"),
"must be installed"
error = TRUE
)
})

0 comments on commit 95a1cf5

Please sign in to comment.