Skip to content

Commit

Permalink
Avoid testthat deprecated features and upgrade to testthat 3rd edition
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Dec 10, 2024
1 parent 27b1f4d commit 30bd0b3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ Imports:
Suggests:
knitr,
rmarkdown,
testthat,
testthat (>= 3.0.0),
withr
VignetteBuilder:
knitr
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Config/testthat/edition: 3
8 changes: 8 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 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(dbflobr)
library(RSQLite)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("check_sqlite_connection", {

test_that("check_table_name", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(x = 1:2)
expect_true(DBI::dbCreateTable(conn, "local", local))
Expand All @@ -41,7 +41,7 @@ test_that("check_table_name", {

test_that("check_column_name", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(test = 1:2)
expect_true(DBI::dbCreateTable(conn, "local", local))
Expand All @@ -68,7 +68,7 @@ test_that("check_column_name", {

test_that("check_column_blob", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(test = 1:2)
expect_true(DBI::dbCreateTable(conn, "local", local))
Expand All @@ -85,7 +85,7 @@ test_that("check_column_blob", {

test_that("check_key", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

df <- data.frame(
char = c("a", "b", "b"),
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-db.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# borrowed from readwritesqlite
test_that("unquoted table names case insensitive in RSQLite", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(x = as.character(1:3))

Expand All @@ -20,7 +20,7 @@ test_that("unquoted table names case insensitive in RSQLite", {

test_that("columns exist", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(x = as.character(1:3))

Expand All @@ -32,7 +32,7 @@ test_that("columns exist", {

test_that("columns type blob", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(x = as.character(1:3))
expect_true(DBI::dbCreateTable(conn, "local", local))
Expand All @@ -49,7 +49,7 @@ test_that("columns type blob", {

test_that("filter key", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

df <- data.frame(
"char" = c("a", "b", "b"),
Expand All @@ -64,7 +64,7 @@ test_that("filter key", {
## safe_key
key <- df[1, ]
x <- safe_key(key, conn)
expect_is(x, "glue")
expect_s3_class(x, "glue")
expect_length(x, 1L)

x <- filter_key("df", key, conn)
Expand All @@ -76,7 +76,7 @@ test_that("filter key", {
# borrowed from readwritesqlite
test_that("table_info", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

local <- data.frame(
logical = TRUE,
Expand All @@ -87,7 +87,7 @@ test_that("table_info", {
expect_true(DBI::dbCreateTable(conn, "local", local))

table_info <- table_info("local", conn)
expect_is(table_info, "data.frame")
expect_s3_class(table_info, "data.frame")
expect_identical(
colnames(table_info),
c("cid", "name", "type", "notnull", "dflt_value", "pk")
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-import.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("import_flobs works", {
write.csv(df, file.path(inner_path, "b_-_3_-_2.csv"))

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -114,7 +114,7 @@ test_that("import_all_flobs works", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -186,7 +186,7 @@ test_that("import_all_flobs requires unique", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -252,7 +252,7 @@ test_that("import_all_flobs is actually recursive", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -294,7 +294,7 @@ test_that("import_flobs works with subdirectory", {
write.csv(df, file.path(path, "b_-_3", "data.csv"))

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -363,7 +363,7 @@ test_that("import_flobs does not recurse beyond 1", {
write.csv(df, file.path(path, "extra", "b_-_3", "data.csv"))

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -400,7 +400,7 @@ test_that("import_flobs sub = TRUE", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -454,7 +454,7 @@ test_that("import_flobs sub = NA", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -490,7 +490,7 @@ test_that("import_flobs sub = NA", {

unlink(file.path(path, "dump", "df", "geometry", "b_-_1", "b_-_1.pdf"))

expect_is(read_flob("geometry", "df", conn = conn, key = data.frame(char = "b", num = 1)), "flob")
expect_s3_class(read_flob("geometry", "df", conn = conn, key = data.frame(char = "b", num = 1)), "flob")

expect_identical(
import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE, replace = TRUE),
Expand All @@ -504,7 +504,7 @@ test_that("import_all_flobs works", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -572,7 +572,7 @@ test_that("import_all_flobs works with .", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-read-write.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("write_flob works", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

df <- data.frame(
char = c("a", "b", "b"),
Expand Down Expand Up @@ -67,13 +67,13 @@ test_that("write_flob works", {
),
class = "chk_error"
)
expect_is(write_flob(flob, "flob",
expect_s3_class(write_flob(flob, "flob",
table_name = "df",
exists = TRUE, key = key, conn = conn
), "flob")

df2 <- DBI::dbReadTable(conn, "df")
expect_equal(df2$flob[1], flob, check.names = FALSE, check.attributes = FALSE)
expect_equal(df2$flob[1], flob, ignore_attr = c("names", "class", "ptype"))

### read flob
expect_error(
Expand Down Expand Up @@ -111,7 +111,7 @@ test_that("write_flob works", {
class = "chk_error"
)

expect_is(delete_flob("flob",
expect_s3_class(delete_flob("flob",
table_name = "df",
key = key, conn = conn
), "flob")
Expand All @@ -127,7 +127,7 @@ test_that("write_flob works", {

test_that("write_flob column exists", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))
expect_true(DBI::dbWriteTable(conn, "df", data.frame(Index = 1)))

key <- data.frame(Index = 1)
Expand All @@ -137,9 +137,9 @@ test_that("write_flob column exists", {
write_flob(flob, "New", "df", key, conn, exists = TRUE),
class = "chk_error"
)
expect_is(write_flob(flob, "New", "df", key, conn), "flob")
expect_is(write_flob(flob, "New", "df", key, conn), "flob")
expect_is(write_flob(flob, "New", "df", key, conn, exists = TRUE), "flob")
expect_s3_class(write_flob(flob, "New", "df", key, conn), "flob")
expect_s3_class(write_flob(flob, "New", "df", key, conn), "flob")
expect_s3_class(write_flob(flob, "New", "df", key, conn, exists = TRUE), "flob")
expect_error(
write_flob(flob, "New", "df", key, conn, exists = FALSE),
class = "chk_error"
Expand All @@ -148,7 +148,7 @@ test_that("write_flob column exists", {

test_that("add_blob_column works", {
conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

df <- data.frame(x = 1)

Expand All @@ -163,7 +163,7 @@ test_that("add_blob_column works", {

df2 <- DBI::dbReadTable(conn, "df")
expect_identical(colnames(df2), c(colnames(df), c("flob")))
expect_is(df2$flob, "blob")
expect_s3_class(df2$flob, "blob")

result <- DBI::dbSendQuery(conn = conn, statement = paste("SELECT flob FROM df LIMIT 1"))
expect_identical(DBI::dbColumnInfo(result)$type, "list")
Expand All @@ -172,6 +172,6 @@ test_that("add_blob_column works", {
## flob
flob <- flobr::flob_obj
x <- collapse_flob(flob)
expect_is(x, "character")
expect_type(x, "character")
expect_length(x, 1L)
})
6 changes: 3 additions & 3 deletions tests/testthat/test-save.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("save_flobs works", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -104,7 +104,7 @@ test_that("save_flobs works with sub", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

# 2 column pk
DBI::dbExecute(
Expand Down Expand Up @@ -216,7 +216,7 @@ test_that("save_flob's slob compatibility", {
path <- withr::local_tempdir()

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
teardown(DBI::dbDisconnect(conn))
withr::defer(DBI::dbDisconnect(conn))

DBI::dbExecute(
conn,
Expand Down

0 comments on commit 30bd0b3

Please sign in to comment.