Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to testthat 3e #603

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Suggests:
DBItest,
magrittr,
RSQLite,
testthat,
testthat (>= 3.0.0),
tibble
LinkingTo:
Rcpp
Expand All @@ -56,3 +56,4 @@ Collate:
'hidden.R'
'utils.R'
'zzz.R'
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# odbc (development version)


* Modify `odbcDataType.Snowflake` to better reflect Snowflake Data Types documentation (@meztez, #599).
* SQL Server: Specialize syntax in sqlCreateTable to avoid failures when
writing to (new) local temp tables. (@detule, #601)
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(odbc)

test_check("odbc", reporter = "summary")
test_check("odbc")
22 changes: 22 additions & 0 deletions tests/testthat/_snaps/show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# show method prints only host if no user is defined

Code
con
Output
<OdbcConnection> localhost

# show method prints DISCONNECTED if not valid

Code
con
Output
<OdbcConnection> localhost
DISCONNECTED

# show method does not print server if it is not available

Code
con
Output
<OdbcConnection>

1 change: 0 additions & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ skip_if_no_drivers <- function() {
#' }
test_roundtrip <- function(con = DBItest:::connect(DBItest::get_default_context()), columns = "", invert = TRUE, force_sorted = FALSE) {
dbms <- dbGetInfo(con)$dbms.name
testthat::context(paste0("roundtrip[", dbms, "]"))
res <- list()
testthat::test_that(paste0("[", dbms, "] round tripping data.frames works"), {
#on.exit(try(DBI::dbRemoveTable(con, "test_table"), silent = TRUE))
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-MySQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ test_that("MySQL", {
NULL))

test_roundtrip(columns = "logical")
context("custom tests")
test_that("odbcPreviewObject", {
tblName <- "test_preview"
con <- DBItest:::connect(DBItest:::get_default_context())
Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/test-PostgreSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ test_that("PostgreSQL", {
tweaks = DBItest::tweaks(temporary_tables = FALSE, placeholder_pattern = "?"), name = "PostgreSQL")
})

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

expect_is(dbReadTable(con_default, "test")$a, "integer64")
expect_is(dbReadTable(con_integer64, "test")$a, "integer64")
expect_s3_class(dbReadTable(con_default, "test")$a, "integer64")
expect_s3_class(dbReadTable(con_integer64, "test")$a, "integer64")

expect_is(dbReadTable(con_integer, "test")$a, "integer")
expect_type(dbReadTable(con_integer, "test")$a, "integer")

expect_is(dbReadTable(con_numeric, "test")$a, "numeric")
expect_type(dbReadTable(con_numeric, "test")$a, "double")

expect_is(dbReadTable(con_character, "test")$a, "character")
expect_type(dbReadTable(con_character, "test")$a, "character")
})

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

context("DBI tests")
DBItest::test_getting_started(c(
"package_name", # Not an error
NULL))
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-SQLite.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ test_that("SQLite", {
NULL))


context("custom tests")
local({
## Test that trying to write unsupported types (like complex numbers) throws an
## informative error message
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-drivers.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("drivers")

test_that("odbcListDrivers() returns available drivers", {
skip_on_cran()
skip_if_no_drivers()
Expand Down
37 changes: 17 additions & 20 deletions tests/testthat/test-show.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
context("show")

test_that("show method prints only host if no user is defined", {
con <- new("OdbcConnection")
with_mock(
`DBI::dbGetInfo` = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
`DBI::dbIsValid` = function(x) TRUE,
{
expect_output(show(con), " localhost")
})
local_mocked_bindings(
dbGetInfo = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
dbIsValid = function(x) TRUE
)

expect_snapshot(con)
})

test_that("show method prints DISCONNECTED if not valid", {
con <- new("OdbcConnection")
with_mock(
`DBI::dbGetInfo` = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
`DBI::dbIsValid` = function(x) FALSE,
{
expect_output(show(con), " DISCONNECTED")
})
local_mocked_bindings(
dbGetInfo = function(x) c(servername = "localhost", username = "", dbname = "", dbms.name = "", db.version = ""),
dbIsValid = function(x) FALSE
)
expect_snapshot(con)
})

test_that("show method does not print server if it is not available", {

con <- new("OdbcConnection")
with_mock(
`DBI::dbGetInfo` = function(x) c(servername = "", username = "", dbname = "", dbms.name = "", db.version = ""),
`DBI::dbIsValid` = function(x) TRUE,
{
expect_output(show(con), "<OdbcConnection>$")
})
local_mocked_bindings(
dbGetInfo = function(x) c(servername = "", username = "", dbname = "", dbms.name = "", db.version = ""),
dbIsValid = function(x) TRUE
)
expect_snapshot(con)
})