Skip to content

Commit

Permalink
Merge pull request #30 from olivroy/lin
Browse files Browse the repository at this point in the history
Remove dependencies + upgrade to testthat 3
  • Loading branch information
joethorley authored Dec 11, 2024
2 parents 3c3da21 + 1d3c321 commit 2523a15
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 91 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ Description: Reads and writes files to SQLite databases
<https://www.sqlite.org/index.html> as flobs (a flob is a blob that
preserves the file extension).
License: MIT + file LICENSE
URL: https://github.com/poissonconsulting/dbflobr
URL: https://github.com/poissonconsulting/dbflobr, https://poissonconsulting.github.io/dbflobr/
BugReports: https://github.com/poissonconsulting/dbflobr/issues
Depends:
R (>= 3.5)
Imports:
blob,
chk,
clisymbols,
crayon,
cli,
DBI,
flobr,
glue,
rlang,
RSQLite
Suggests:
covr,
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
Config/Needs/website: poissonconsulting/poissontemplate
38 changes: 27 additions & 11 deletions R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import_flobs <- function(column_name, table_name, conn,
add_blob_column(column_name, table_name, conn)
}

ui_line(glue("Writing files to database"))
cli::cli_inform("Writing files to database")

success <- rep(FALSE, length = length(files))
names(success) <- names(files)
Expand All @@ -81,7 +81,9 @@ import_flobs <- function(column_name, table_name, conn,
values <- parse_filename(names(files)[i], sep)

if (is_length_unequal(values, key)) {
ui_oops(glue("File {i}: can't write {names(files)[i]} to database. The number of hyphen-separated values must be identical to the number of columns in `key`."))
cli::cli_inform(c(
"x" = "File {i}: can't write {names(files)[i]} to database. The number of hyphen-separated values must be identical to the number of columns in `key`."
))
next
}

Expand All @@ -91,7 +93,9 @@ import_flobs <- function(column_name, table_name, conn,

y <- try(read_flob(column_name, table_name, key[i, , drop = FALSE], conn), silent = TRUE)
if (!replace && !is_try_error(y)) {
ui_oops(glue("File {i}: can't write {names(files)[i]} to database. Flob already exists in that location and replace = FALSE"))
cli::cli_inform(c(
"x" = "File {i}: can't write {names(files)[i]} to database. Flob already exists in that location and replace = FALSE"
))
next
}

Expand All @@ -107,15 +111,21 @@ import_flobs <- function(column_name, table_name, conn,

if (!is_try_error(x)) {
success[i] <- TRUE
ui_done(glue("File {i}: {names(files)[i]} written to database"))
cli::cli_inform(c(
"v" = "File {i}: {names(files)[i]} written to database"
))
} else {
ui_oops(glue("File {i}: can't write {names(files)[i]} to database"))
cli::cli_inform(c(
"x" = "File {i}: can't write {names(files)[i]} to database"
))
}
next
}

if (is_try_error(y)) {
ui_done(glue("File {i}: {names(files)[i]} already absent from database."))
cli::cli_inform(c(
"v" = "File {i}: {names(files)[i]} already absent from database."
))
success[i] <- TRUE
next
}
Expand All @@ -127,9 +137,13 @@ import_flobs <- function(column_name, table_name, conn,
))
if (!is_try_error(x)) {
success[i] <- TRUE
ui_done(glue("File {i}: {names(files)[i]} deleted in database."))
cli::cli_inform(c(
"v" = "File {i}: {names(files)[i]} deleted in database."
))
} else {
ui_oops(glue("File {i}: can't delete {names(files)[i]} in database."))
cli::cli_inform(c(
"v" = "File {i}: can't delete {names(files)[i]} in database."
))
}
}
return(invisible(success))
Expand Down Expand Up @@ -178,15 +192,17 @@ import_all_flobs <- function(conn, dir = ".", sep = "_-_", pattern = ".*",
table_name <- x[1]
column_name <- x[2]
inner_dir <- file.path(dir, table_name, column_name)
ui_line(glue("Table name: {ui_value(table_name)}"))
ui_line(glue("Column name: {ui_value(column_name)}"))
cli::cli_inform(c(
"Table name: {.val {table_name}}",
"Column name: {.val {column_name}}"
))
success[[i]] <- import_flobs(
column_name = x[2], table_name = x[1],
conn = conn, dir = inner_dir, sep = sep,
pattern = pattern, sub = sub,
exists = exists, replace = replace
)
ui_line("")
cli::cat_line()
}

names(success) <- sapply(dirs, glue_collapse, "/")
Expand Down
18 changes: 12 additions & 6 deletions R/save.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su
sql <- glue("SELECT {sql_pk(pk)} FROM ('{table_name}');")
values <- get_query(sql, conn)

ui_line(glue("Saving files to {ui_value(dir)}"))
cli::cli_inform(("Saving files to {.path {dir}}"))

success <- vector()
success_names <- vector()
Expand Down Expand Up @@ -82,12 +82,16 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su
dir.create(file.path(dir, new_file), recursive = TRUE)
flobr::unflob(x, dir = file.path(dir, new_file), name = new_file, ext = ext, slob = NA, check = FALSE)
}
ui_done(glue("Row {i}: file {file} renamed to {new_file_ext}"))
cli::cli_inform(c(
"v" = "Row {i}: file {file} renamed to {new_file_ext}"
))
} else {
if (is.na(sub)) {
dir.create(file.path(dir, new_file), recursive = TRUE)
}
ui_oops(glue("Row {i}: no file found"))
cli::cli_inform(c(
"x" = "Row {i}: no file found"
))
}
}
names(success) <- success_names
Expand Down Expand Up @@ -146,13 +150,15 @@ save_all_flobs <- function(table_name = NULL, conn, dir = ".", sep = "_-_",
if (!dir.exists(path)) {
dir.create(path, recursive = TRUE)
}
ui_line(glue("Table name: {ui_value(i)}"))
ui_line(glue("Column name: {ui_value(j)}"))
cli::cli_inform(c(
"Table name: {.val {i}}",
"Column name: {.val {j}}"
))
success[[name]] <- save_flobs(j, i, conn, path,
sep = sep, sub = sub,
replace = replace
)
ui_line("")
cli::cat_line()
}
}
return(invisible(success))
Expand Down
25 changes: 0 additions & 25 deletions R/ui.R

This file was deleted.

2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ knitr::opts_chunk$set(
)
```

# dbflobr <img src="man/figures/logo.png" style='text-align: right; />
# dbflobr <img src="man/figures/logo.png" align="right" />

<!-- badges: start -->
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# dbflobr \<img src=man/figures/logo.png” style=’text-align: right; /\>
# dbflobr <img src="man/figures/logo.png" align="right" />

<!-- badges: start -->

Expand Down
6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
url: https://poissonconsulting.github.io/dbflobr/

destination: docs

template:
bootstrap: 5
package: poissontemplate
1 change: 1 addition & 0 deletions man/dbflobr-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading

0 comments on commit 2523a15

Please sign in to comment.