Skip to content

Commit ea2b935

Browse files
hadleydetule
andauthored
Update to testthat 3e (#603)
* Update to testthat 3e * Eliminate some expect_is() calls * fixup tests * SQL Server: Index local temp tables (#600) Update handling ( listing, writing ) of local temp tables. --------- Co-authored-by: Hadley Wickham <[email protected]> * SQL Server: Handle temporary flag in sqlCreateTable (#601) * SQL Server: Handle temporary flag in sqlCreateTable * code-review: better warning + testthat usage * code-review: simplify sql server specific method * code-review: Add missing _snaps * Update news --------- Co-authored-by: detule <[email protected]>
1 parent 427a54d commit ea2b935

File tree

10 files changed

+56
-34
lines changed

10 files changed

+56
-34
lines changed

DESCRIPTION

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Suggests:
3131
DBItest,
3232
magrittr,
3333
RSQLite,
34-
testthat,
34+
testthat (>= 3.0.0),
3535
tibble
3636
LinkingTo:
3737
Rcpp
@@ -56,3 +56,4 @@ Collate:
5656
'hidden.R'
5757
'utils.R'
5858
'zzz.R'
59+
Config/testthat/edition: 3

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# odbc (development version)
22

3+
34
* Modify `odbcDataType.Snowflake` to better reflect Snowflake Data Types documentation (@meztez, #599).
45
* SQL Server: Specialize syntax in sqlCreateTable to avoid failures when
56
writing to (new) local temp tables. (@detule, #601)

tests/testthat.R

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
19
library(testthat)
210
library(odbc)
311

4-
test_check("odbc", reporter = "summary")
12+
test_check("odbc")

tests/testthat/_snaps/show.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# show method prints only host if no user is defined
2+
3+
Code
4+
con
5+
Output
6+
<OdbcConnection> localhost
7+
8+
# show method prints DISCONNECTED if not valid
9+
10+
Code
11+
con
12+
Output
13+
<OdbcConnection> localhost
14+
DISCONNECTED
15+
16+
# show method does not print server if it is not available
17+
18+
Code
19+
con
20+
Output
21+
<OdbcConnection>
22+

tests/testthat/helper.R

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ skip_if_no_drivers <- function() {
5656
#' }
5757
test_roundtrip <- function(con = DBItest:::connect(DBItest::get_default_context()), columns = "", invert = TRUE, force_sorted = FALSE) {
5858
dbms <- dbGetInfo(con)$dbms.name
59-
testthat::context(paste0("roundtrip[", dbms, "]"))
6059
res <- list()
6160
testthat::test_that(paste0("[", dbms, "] round tripping data.frames works"), {
6261
#on.exit(try(DBI::dbRemoveTable(con, "test_table"), silent = TRUE))

tests/testthat/test-MySQL.R

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ test_that("MySQL", {
7474
NULL))
7575

7676
test_roundtrip(columns = "logical")
77-
context("custom tests")
7877
test_that("odbcPreviewObject", {
7978
tblName <- "test_preview"
8079
con <- DBItest:::connect(DBItest:::get_default_context())

tests/testthat/test-PostgreSQL.R

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ test_that("PostgreSQL", {
44
tweaks = DBItest::tweaks(temporary_tables = FALSE, placeholder_pattern = "?"), name = "PostgreSQL")
55
})
66

7-
context("custom tests")
87
test_that("show method works as expected with real connection", {
98
skip_on_os("windows")
109
con <- DBItest:::connect(DBItest:::get_default_context())
@@ -28,14 +27,14 @@ test_that("PostgreSQL", {
2827
dbWriteTable(con_default, "test", data.frame(a = 1:10L), field.types = c(a = "BIGINT"))
2928
on.exit(dbRemoveTable(con_default, "test"))
3029

31-
expect_is(dbReadTable(con_default, "test")$a, "integer64")
32-
expect_is(dbReadTable(con_integer64, "test")$a, "integer64")
30+
expect_s3_class(dbReadTable(con_default, "test")$a, "integer64")
31+
expect_s3_class(dbReadTable(con_integer64, "test")$a, "integer64")
3332

34-
expect_is(dbReadTable(con_integer, "test")$a, "integer")
33+
expect_type(dbReadTable(con_integer, "test")$a, "integer")
3534

36-
expect_is(dbReadTable(con_numeric, "test")$a, "numeric")
35+
expect_type(dbReadTable(con_numeric, "test")$a, "double")
3736

38-
expect_is(dbReadTable(con_character, "test")$a, "character")
37+
expect_type(dbReadTable(con_character, "test")$a, "character")
3938
})
4039

4140
# This test checks whether when writing to a table and using
@@ -74,7 +73,6 @@ test_that("PostgreSQL", {
7473
expect_equal(nrow(res), 3)
7574
})
7675

77-
context("DBI tests")
7876
DBItest::test_getting_started(c(
7977
"package_name", # Not an error
8078
NULL))

tests/testthat/test-SQLite.R

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ test_that("SQLite", {
118118
NULL))
119119

120120

121-
context("custom tests")
122121
local({
123122
## Test that trying to write unsupported types (like complex numbers) throws an
124123
## informative error message

tests/testthat/test-drivers.R

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
context("drivers")
2-
31
test_that("odbcListDrivers() returns available drivers", {
42
skip_on_cran()
53
skip_if_no_drivers()

tests/testthat/test-show.R

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
context("show")
2-
31
test_that("show method prints only host if no user is defined", {
42
con <- new("OdbcConnection")
5-
with_mock(
6-
`DBI::dbGetInfo` = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
7-
`DBI::dbIsValid` = function(x) TRUE,
8-
{
9-
expect_output(show(con), " localhost")
10-
})
3+
local_mocked_bindings(
4+
dbGetInfo = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
5+
dbIsValid = function(x) TRUE
6+
)
7+
8+
expect_snapshot(con)
119
})
1210

1311
test_that("show method prints DISCONNECTED if not valid", {
1412
con <- new("OdbcConnection")
15-
with_mock(
16-
`DBI::dbGetInfo` = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
17-
`DBI::dbIsValid` = function(x) FALSE,
18-
{
19-
expect_output(show(con), " DISCONNECTED")
20-
})
13+
local_mocked_bindings(
14+
dbGetInfo = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
15+
dbIsValid = function(x) FALSE
16+
)
17+
expect_snapshot(con)
2118
})
2219

2320
test_that("show method does not print server if it is not available", {
21+
2422
con <- new("OdbcConnection")
25-
with_mock(
26-
`DBI::dbGetInfo` = function(x) c(servername = "", username = "", dbname = "", dbms.name = "", db.version = ""),
27-
`DBI::dbIsValid` = function(x) TRUE,
28-
{
29-
expect_output(show(con), "<OdbcConnection>$")
30-
})
23+
local_mocked_bindings(
24+
dbGetInfo = function(x) c(servername = "", username = "", dbname = "", dbms.name = "", db.version = ""),
25+
dbIsValid = function(x) TRUE
26+
)
27+
expect_snapshot(con)
3128
})

0 commit comments

Comments
 (0)