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

WIP: Generated by use_dbitest() #437

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ Imports:
bit64,
blob (>= 1.2.0),
DBI (>= 1.1.0),
DBItest,
memoise,
methods,
pkgconfig,
Rcpp (>= 1.0.7)
Rcpp (>= 1.0.7),
rlang
Suggests:
callr,
DBItest (>= 1.7.0),
gert,
gh,
knitr,
rmarkdown,
hms,
rvest,
testthat,
testthat (>= 2.0.0),
xml2
LinkingTo:
plogr (>= 0.2.0),
Expand All @@ -79,6 +80,7 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
Collate:
'RSQLite-package.R'
'RcppExports.R'
'SQLiteConnection.R'
'SQLKeywords_SQLiteConnection.R'
Expand Down Expand Up @@ -147,3 +149,4 @@ Collate:
'zzz.R'
Config/autostyle/scope: line_breaks
Config/autostyle/strict: false
Config/testthat/edition: 2
50 changes: 50 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,58 @@ exportMethods(show)
exportMethods(sqlData)
import(DBI)
import(methods)
importFrom(DBItest,check_df)
importFrom(DBItest,connect)
importFrom(DBItest,dbi_generics)
importFrom(DBItest,expect_all_args_have_default_values)
importFrom(DBItest,expect_ellipsis_in_formals)
importFrom(DBItest,expect_equal_df)
importFrom(DBItest,expect_has_class_method)
importFrom(DBItest,expect_invisible_true)
importFrom(DBItest,get_default_context)
importFrom(DBItest,get_key_methods)
importFrom(DBItest,get_penguins)
importFrom(DBItest,get_pkg_path)
importFrom(DBItest,get_placeholder_funs)
importFrom(DBItest,get_texts)
importFrom(DBItest,has_utf8_or_ascii_encoding)
importFrom(DBItest,local_connection)
importFrom(DBItest,local_remove_test_table)
importFrom(DBItest,local_result)
importFrom(DBItest,new_bind_tester_extra)
importFrom(DBItest,package_name)
importFrom(DBItest,random_table_name)
importFrom(DBItest,s4_methods)
importFrom(DBItest,sql_union)
importFrom(DBItest,test_data_type)
importFrom(DBItest,test_select)
importFrom(DBItest,test_select_bind)
importFrom(DBItest,test_select_with_null)
importFrom(DBItest,test_table_roundtrip)
importFrom(DBItest,test_table_roundtrip_one)
importFrom(DBItest,trivial_df)
importFrom(DBItest,trivial_query)
importFrom(DBItest,trivial_values)
importFrom(DBItest,try_silent)
importFrom(DBItest,unrowname)
importFrom(Rcpp,sourceCpp)
importFrom(bit64,integer64)
importFrom(bit64,is.integer64)
importFrom(blob,blob)
importFrom(rlang,"%||%")
importFrom(rlang,":=")
importFrom(rlang,abort)
importFrom(rlang,as_function)
importFrom(rlang,enexpr)
importFrom(rlang,enquo)
importFrom(rlang,enquos)
importFrom(rlang,eval_tidy)
importFrom(rlang,expr)
importFrom(rlang,has_length)
importFrom(rlang,is_interactive)
importFrom(rlang,list2)
importFrom(rlang,local_options)
importFrom(rlang,quo)
importFrom(rlang,seq2)
importFrom(rlang,set_names)
useDynLib(RSQLite, .registration = TRUE)
56 changes: 56 additions & 0 deletions R/RSQLite-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom DBItest check_df
#' @importFrom DBItest connect
#' @importFrom DBItest dbi_generics
#' @importFrom DBItest expect_all_args_have_default_values
#' @importFrom DBItest expect_ellipsis_in_formals
#' @importFrom DBItest expect_equal_df
#' @importFrom DBItest expect_has_class_method
#' @importFrom DBItest expect_invisible_true
#' @importFrom DBItest get_default_context
#' @importFrom DBItest get_key_methods
#' @importFrom DBItest get_penguins
#' @importFrom DBItest get_pkg_path
#' @importFrom DBItest get_placeholder_funs
#' @importFrom DBItest get_texts
#' @importFrom DBItest has_utf8_or_ascii_encoding
#' @importFrom DBItest local_connection
#' @importFrom DBItest local_remove_test_table
#' @importFrom DBItest local_result
#' @importFrom DBItest new_bind_tester_extra
#' @importFrom DBItest package_name
#' @importFrom DBItest random_table_name
#' @importFrom DBItest s4_methods
#' @importFrom DBItest sql_union
#' @importFrom DBItest test_data_type
#' @importFrom DBItest test_select
#' @importFrom DBItest test_select_bind
#' @importFrom DBItest test_select_with_null
#' @importFrom DBItest test_table_roundtrip
#' @importFrom DBItest test_table_roundtrip_one
#' @importFrom DBItest trivial_df
#' @importFrom DBItest trivial_query
#' @importFrom DBItest trivial_values
#' @importFrom DBItest try_silent
#' @importFrom DBItest unrowname
#' @importFrom rlang %||%
#' @importFrom rlang :=
#' @importFrom rlang abort
#' @importFrom rlang as_function
#' @importFrom rlang enexpr
#' @importFrom rlang enquo
#' @importFrom rlang enquos
#' @importFrom rlang eval_tidy
#' @importFrom rlang expr
#' @importFrom rlang has_length
#' @importFrom rlang is_interactive
#' @importFrom rlang list2
#' @importFrom rlang local_options
#' @importFrom rlang quo
#' @importFrom rlang seq2
#' @importFrom rlang set_names
## usethis namespace: end
NULL
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/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(RSQLite)

Expand Down
60 changes: 60 additions & 0 deletions tests/testthat/test-dbitest-compliance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Created by DBItest::use_dbitest(), do not edit by hand

ctx <- get_default_context()

con <- local_connection(ctx)

test_that("compliance", {
key_methods <- get_key_methods()
expect_identical(names(key_methods), c("Driver", "Connection", "Result"))
pkg <- package_name(ctx)
where <- asNamespace(pkg)
sapply(
names(key_methods),
function(name) {
dbi_class <- paste0("DBI", name)
classes <- Filter(
function(class) {
extends(class, dbi_class) && `@`(getClass(class), virtual) == FALSE
},
getClasses(where)
)
expect_gte(length(classes), 1)
class <- classes[[1]]
mapply(
function(method, args) {
expect_has_class_method(method, class, args, where)
},
names(key_methods[[name]]),
key_methods[[name]]
)
}
)
})

test_that("reexport", {
pkg <- package_name(ctx)
where <- asNamespace(pkg)
dbi_names <- dbi_generics(`$`(`$`(ctx, tweaks), dbitest_version))
exported_names <- suppressWarnings(
`::`(callr, r)(
function(pkg) {
tryCatch(getNamespaceExports(getNamespace(pkg)), error = function(e) character())
},
args = list(pkg = pkg)
)
)
if (length(exported_names) == 0) {
skip("reexport: package must be installed for this test")
}
missing <- setdiff(dbi_names, exported_names)
expect_equal(paste(missing, collapse = ", "), "")
})

test_that("ellipsis", {
pkg <- package_name(ctx)
where <- asNamespace(pkg)
methods <- s4_methods(where, function(x) x == "DBI")
methods <- methods[grep("^db", names(methods))]
Map(expect_ellipsis_in_formals, methods, names(methods))
})
32 changes: 32 additions & 0 deletions tests/testthat/test-dbitest-connection.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Created by DBItest::use_dbitest(), do not edit by hand

ctx <- get_default_context()

con <- local_connection(ctx)

test_that("disconnect_formals", {
# <establish formals of described functions>
expect_equal(names(formals(dbDisconnect)), c("conn", "..."))
})

test_that("can_disconnect", {
#' @return
con <- connect(ctx)
#' `dbDisconnect()` returns `TRUE`, invisibly.
expect_invisible_true(dbDisconnect(con))
})

test_that("data_type_connection", {
test_data_type(ctx, con)
})

test_that("get_info_connection", {
info <- dbGetInfo(con)
expect_type(info, "list")
info_names <- names(info)
necessary_names <- c("db.version", "dbname", "username", "host", "port")
for (name in necessary_names) {
eval(bquote(expect_true(.(name) %in% info_names)))
}
expect_false("password" %in% info_names)
})
98 changes: 98 additions & 0 deletions tests/testthat/test-dbitest-driver.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Created by DBItest::use_dbitest(), do not edit by hand

ctx <- get_default_context()

con <- local_connection(ctx)

test_that("constructor", {
pkg_name <- package_name(ctx)
default_constructor_name <- gsub("^R", "", pkg_name)
constructor_name <- `$`(`$`(ctx, tweaks), constructor_name) %||% default_constructor_name
pkg_env <- getNamespace(pkg_name)
eval(bquote(expect_true(.(constructor_name) %in% getNamespaceExports(pkg_env))))
eval(bquote(expect_true(exists(.(constructor_name), mode = "function", pkg_env))))
constructor <- get(constructor_name, mode = "function", pkg_env)
expect_all_args_have_default_values(constructor)
if (!isTRUE(`$`(`$`(ctx, tweaks), constructor_relax_args))) {
expect_arglist_is_empty(constructor)
}
})

test_that("data_type_formals", {
# <establish formals of described function>
expect_equal(names(formals(dbDataType)), c("dbObj", "obj", "..."))
})

test_that("data_type_driver", {
test_data_type(ctx, ctx$drv)
})

test_that("get_info_driver", {
info <- dbGetInfo(`$`(ctx, drv))
expect_type(info, "list")
info_names <- names(info)
necessary_names <- c("driver.version", "client.version")
for (name in necessary_names) {
eval(bquote(expect_true(.(name) %in% info_names)))
}
})

test_that("connect_formals", {
# <establish formals of described functions>
expect_equal(names(formals(dbConnect)), c("drv", "..."))
})

test_that("connect_can_connect", {
#' @return
con <- expect_visible(connect(ctx))
#' `dbConnect()` returns an S4 object that inherits from [DBIConnection-class].
expect_s4_class(con, "DBIConnection")
dbDisconnect(con)
#' This object is used to communicate with the database engine.
})

test_that("connect_format", {
#'
#' A [format()] method is defined for the connection object.
desc <- format(con)
#' It returns a string that consists of a single line of text.
expect_type(desc, "character")
expect_length(desc, 1)
expect_false(grepl("\n", desc, fixed = TRUE))
})

test_that("connect_bigint_integer", {
#' In addition, DBI supports the `bigint` argument that governs how
#' 64-bit integer data is returned. The following values are supported:
#' - `"integer"`: always return as `integer`, silently overflow
con <- local_connection(ctx, bigint = "integer")
res <- dbGetQuery(con, "SELECT 10000000000")
expect_type(res[[1]], "integer")
})

test_that("connect_bigint_numeric", {
#' - `"numeric"`: always return as `numeric`, silently round
con <- local_connection(ctx, bigint = "numeric")
res <- dbGetQuery(con, "SELECT 10000000000")
expect_type(res[[1]], "double")
expect_equal(res[[1]], 1e10)
})

test_that("connect_bigint_character", {
#' - `"character"`: always return the decimal representation as `character`
con <- local_connection(ctx, bigint = "character")
res <- dbGetQuery(con, "SELECT 10000000000")
expect_type(res[[1]], "character")
expect_equal(res[[1]], "10000000000")
})

test_that("connect_bigint_integer64", {
#' - `"integer64"`: return as a data type that can be coerced using
#' [as.integer()] (with warning on overflow), [as.numeric()]
#' and [as.character()]
con <- local_connection(ctx, bigint = "integer64")
res <- dbGetQuery(con, "SELECT 10000000000")
expect_warning(expect_true(is.na(as.integer(res[[1]]))))
expect_equal(as.numeric(res[[1]]), 1e10)
expect_equal(as.character(res[[1]]), "10000000000")
})
28 changes: 28 additions & 0 deletions tests/testthat/test-dbitest-getting_started.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Created by DBItest::use_dbitest(), do not edit by hand

ctx <- get_default_context()

con <- local_connection(ctx)

test_that("package_dependencies", {
#' A DBI backend is an R package
pkg_path <- get_pkg_path(ctx)

pkg_deps_df <- desc::desc_get_deps(pkg_path)
pkg_imports <- pkg_deps_df$package[pkg_deps_df$type == "Imports"]

#' which imports the \pkg{DBI}
expect_true("DBI" %in% pkg_imports)
#' and \pkg{methods}
expect_true("methods" %in% pkg_imports)
#' packages.
})

test_that("package_name", {
pkg_name <- package_name(ctx)

#' For better or worse, the names of many existing backends start with
#' \sQuote{R}, e.g., \pkg{RSQLite}, \pkg{RMySQL}, \pkg{RSQLServer}; it is up
#' to the backend author to adopt this convention or not.
expect_match(pkg_name, "^R")
})
Loading