diff --git a/R/check.R b/R/check.R index 78692f2..fbb4f51 100644 --- a/R/check.R +++ b/R/check.R @@ -63,10 +63,10 @@ check_flob_query <- function(x, slob = FALSE) { abort_chk("Can't find flob in that location.") } - if(vld_false(slob)){ + if (vld_false(slob)) { class(x) <- c("flob", "blob") flobr::chk_flob(x) # this is a problem when x is a blob and blob = FALSE - } else if (vld_true(slob)){ + } else if (vld_true(slob)) { class(x) <- "blob" flobr::chk_slob(x) class(x) <- "list" @@ -74,7 +74,7 @@ check_flob_query <- function(x, slob = FALSE) { names(x) <- NULL } else { class(x) <- c("flob", "blob") - if(!flobr::vld_flob(x)){ + if (!flobr::vld_flob(x)) { class(x) <- c("blob") chkor_vld(vld_slob(x), flobr::vld_flob(x)) class(x) <- "list" @@ -85,9 +85,9 @@ check_flob_query <- function(x, slob = FALSE) { invisible(x) } -check_pk <- function(table_name, conn){ +check_pk <- function(table_name, conn) { pk <- table_pk(table_name, conn) - if(!length(pk)){ + if (!length(pk)) { abort_chk("Table `", table_name, "` must have a primary key.") } return(pk) diff --git a/R/db.R b/R/db.R index 9d68ae3..a6640e9 100644 --- a/R/db.R +++ b/R/db.R @@ -34,18 +34,18 @@ table_info <- function(table_name, conn) { table_info } -table_pk <- function(table_name, conn){ +table_pk <- function(table_name, conn) { info <- table_info(table_name, conn) info$name[info$pk > 0] } -table_pk_df <- function(table_name, conn){ +table_pk_df <- function(table_name, conn) { info <- table_info(table_name, conn) pk <- info$name[info$pk > 0] key <- data.frame(matrix(ncol = length(pk), nrow = 1, dimnames = list(NULL, pk))) - for(i in pk){ + for (i in pk) { type <- info$type[info$name == i] - x <- switch (type, + x <- switch(type, "TEXT" = character(), "INTEGER" = integer(), "BOOLEAN" = logical(), @@ -54,10 +54,10 @@ table_pk_df <- function(table_name, conn){ ) key[i] <- x } - key[0,,drop = FALSE] + key[0, , drop = FALSE] } -sql_pk <- function(x){ +sql_pk <- function(x) { paste0("`", paste(x, collapse = "`, `"), "`") } @@ -70,7 +70,7 @@ is_column_blob <- function(column_name, table_name, conn) { toupper(table_column_type(column_name, table_name, conn)) == "BLOB" } -blob_columns <- function(table_name, conn){ +blob_columns <- function(table_name, conn) { table_info <- table_info(table_name, conn) table_info$name[table_info$type == "BLOB"] } diff --git a/R/dbflob.R b/R/dbflob.R index f2365c9..450296b 100644 --- a/R/dbflob.R +++ b/R/dbflob.R @@ -1,6 +1,6 @@ #' Write flob #' -#' Write a \code{\link[flobr]{flob}} to a SQLite database. +#' Write a [flobr::flob()] to a SQLite database. #' #' @param flob A flob. #' @param column_name A string of the name of the BLOB column. @@ -50,7 +50,7 @@ write_flob <- function(flob, column_name, table_name, key, conn, exists = NA) { #' Read flob #' -#' Read a \code{\link[flobr]{flob}} from a SQLite database. +#' Read a [flobr::flob()] from a SQLite database. #' #' @inheritParams write_flob #' @param slob A logical scalar specifying whether to process as slobs (serialized blobs) instead of flobs. @@ -137,11 +137,10 @@ add_blob_column <- function(column_name, table_name, conn) { sql <- "ALTER TABLE ?table_name ADD ?column_name BLOB" sql <- sql_interpolate(sql, conn, - table_name = table_name, - column_name = column_name + table_name = table_name, + column_name = column_name ) execute(sql, conn) invisible(TRUE) } - diff --git a/R/import.R b/R/import.R index 3f2b9f2..49a90d6 100644 --- a/R/import.R +++ b/R/import.R @@ -1,6 +1,6 @@ #' Import flobs. #' -#' Import \code{\link[flobr]{flob}}s to SQLite database column from directory. +#' Import [flobr::flob()]s to SQLite database column from directory. #' Values in file name are matched to table primary key to determine where to write flob. #' #' @inheritParams write_flob @@ -22,7 +22,7 @@ #' conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") #' DBI::dbGetQuery(conn, "CREATE TABLE Table1 (CharColumn TEXT PRIMARY KEY NOT NULL)") #' DBI::dbWriteTable(conn, "Table1", data.frame(CharColumn = c("a", "b")), append = TRUE) -#' key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0,,drop = FALSE] +#' key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0, , drop = FALSE] #' dir <- tempdir() #' write.csv(key, file.path(dir, "a.csv")) #' import_flobs("BlobColumn", "Table1", conn, dir) @@ -30,7 +30,7 @@ import_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", pattern = ".*", sub = FALSE, exists = FALSE, recursive = FALSE, - replace = FALSE){ + replace = FALSE) { check_sqlite_connection(conn) check_table_name(table_name, conn) check_column_name(column_name, table_name, exists = exists, conn) @@ -44,17 +44,19 @@ import_flobs <- function(column_name, table_name, conn, chk_string(pattern) - if(vld_false(sub)) { + if (vld_false(sub)) { files <- list_files(dir, recursive = recursive, pattern = pattern) names(files) <- basename(files) - if(anyDuplicated(names(files))) + if (anyDuplicated(names(files))) { stop("File names must be unique.", call. = FALSE) + } } else { files <- list_files(dir, recursive = NA, pattern = pattern) names(files) <- basename(dirname(files)) - if(anyDuplicated(names(files))) + if (anyDuplicated(names(files))) { stop("Directory names must be unique.", call. = FALSE) - if(is.na(sub)) { + } + if (is.na(sub)) { dirs <- list_dirs(dir, recursive = NA, pattern = pattern) names(dirs) <- basename(dirs) dirs[names(files)] <- files @@ -66,41 +68,44 @@ import_flobs <- function(column_name, table_name, conn, key <- table_pk_df(table_name, conn) column_exists <- column_exists(column_name, table_name, conn = conn) - if(!exists && !column_exists) + if (!exists && !column_exists) { add_blob_column(column_name, table_name, conn) + } ui_line(glue("Writing files to database")) success <- rep(FALSE, length = length(files)) names(success) <- names(files) - for(i in seq_along(files)){ + for (i in seq_along(files)) { values <- parse_filename(names(files)[i], sep) - if(is_length_unequal(values, key)){ + 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`.")) next } - for(j in seq_along(values)){ + for (j in seq_along(values)) { key[i, j] <- values[j] } - y <- try(read_flob(column_name, table_name, key[i,, drop = FALSE], conn), silent = TRUE) - if(!replace && !is_try_error(y)){ + 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")) next } - if(!is.na(files[i])) { + if (!is.na(files[i])) { flob <- flobr::flob(files[i]) - x <- try(write_flob(flob, key = key[i,,drop = FALSE], - column_name = column_name, - table_name = table_name, - conn = conn, - exists = TRUE), silent = TRUE) - - if(!is_try_error(x)){ + x <- try(write_flob(flob, + key = key[i, , drop = FALSE], + column_name = column_name, + table_name = table_name, + conn = conn, + exists = TRUE + ), silent = TRUE) + + if (!is_try_error(x)) { success[i] <- TRUE ui_done(glue("File {i}: {names(files)[i]} written to database")) } else { @@ -109,16 +114,18 @@ import_flobs <- function(column_name, table_name, conn, next } - if(is_try_error(y)) { + if (is_try_error(y)) { ui_done(glue("File {i}: {names(files)[i]} already absent from database.")) success[i] <- TRUE next } - x <- try(delete_flob(key = key[i,,drop = FALSE], - column_name = column_name, - table_name = table_name, - conn = conn)) - if(!is_try_error(x)){ + x <- try(delete_flob( + key = key[i, , drop = FALSE], + column_name = column_name, + table_name = table_name, + conn = conn + )) + if (!is_try_error(x)) { success[i] <- TRUE ui_done(glue("File {i}: {names(files)[i]} deleted in database.")) } else { @@ -130,7 +137,7 @@ import_flobs <- function(column_name, table_name, conn, #' Import all flobs. #' -#' Import \code{\link[flobr]{flob}}s to SQLite database from directory. +#' Import [flobr::flob()]s to SQLite database from directory. #' Table and column names are matched to directory names within main directory. #' Values in file names are matched to table primary key to determine where to write flob. #' @@ -154,7 +161,7 @@ import_flobs <- function(column_name, table_name, conn, #' DBI::dbDisconnect(conn) import_all_flobs <- function(conn, dir = ".", sep = "_-_", pattern = ".*", sub = FALSE, - exists = FALSE, replace = FALSE){ + exists = FALSE, replace = FALSE) { check_sqlite_connection(conn) chk_dir(dir) chk_string(sep) @@ -166,17 +173,19 @@ import_all_flobs <- function(conn, dir = ".", sep = "_-_", pattern = ".*", dirs <- dir_tree(dir, sub) success <- vector(mode = "list", length = length(dirs)) - for(i in seq_along(dirs)){ + for (i in seq_along(dirs)) { x <- dirs[[i]] 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)}")) - 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) + 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("") } diff --git a/R/save.R b/R/save.R index f8a3f78..7f8ea59 100644 --- a/R/save.R +++ b/R/save.R @@ -1,6 +1,6 @@ #' Save flobs. #' -#' Rename \code{\link[flobr]{flob}}s from a SQLite database BLOB column and save to directory. +#' Rename [flobr::flob()]s from a SQLite database BLOB column and save to directory. #' #' @inheritParams write_flob #' @param dir A string of the path to the directory to save the files in. @@ -26,7 +26,7 @@ #' dir <- tempdir() #' save_flobs("BlobColumn", "Table1", conn, dir) #' DBI::dbDisconnect(conn) -save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", sub = FALSE, replace = FALSE, slob_ext = NULL){ +save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", sub = FALSE, replace = FALSE, slob_ext = NULL) { check_sqlite_connection(conn) check_table_name(table_name, conn) check_column_name(column_name, table_name, exists = TRUE, conn) @@ -45,13 +45,13 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su success <- vector() success_names <- vector() - for(i in 1:nrow(values)){ + for (i in seq_len(nrow(values))) { key <- values[i, , drop = FALSE] new_file <- create_filename(key, sep = sep) new_file <- as.character(new_file) x <- try(read_flob(column_name, table_name, key, conn, slob = NA), silent = TRUE) - if(!is_try_error(x) && !(blob::is_blob(x) && is.null(slob_ext))){ - if(flobr::is_flob(x) || is.null(slob_ext)){ + if (!is_try_error(x) && !(blob::is_blob(x) && is.null(slob_ext))) { + if (flobr::is_flob(x) || is.null(slob_ext)) { filename <- flobr::flob_name(x) ext <- flobr::flob_ext(x) file <- glue("{filename}.{ext}") @@ -59,7 +59,7 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su success[i] <- new_file_ext success_names[i] <- file } else { - if(is.null(slob_ext)) err("`slob_ext` must be provided when slobs are present.") + if (is.null(slob_ext)) err("`slob_ext` must be provided when slobs are present.") filename <- "BLOB" ext <- slob_ext file <- glue("{filename}") @@ -68,14 +68,14 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su success_names[i] <- file } - if(vld_false(sub)) { - if(!replace && file.exists(file.path(dir, new_file_ext))) { + if (vld_false(sub)) { + if (!replace && file.exists(file.path(dir, new_file_ext))) { stop("File '", file.path(dir, new_file), "' already exists.", call. = FALSE) } flobr::unflob(x, dir = dir, name = new_file, ext = ext, slob = NA, check = FALSE) } else { - if(!replace && length(list.files(file.path(dir, new_file)))) { + if (!replace && length(list.files(file.path(dir, new_file)))) { stop("Directory '", file.path(dir, new_file), "' already contains a file.", call. = FALSE) } unlink(file.path(dir, new_file), recursive = TRUE) @@ -84,7 +84,7 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su } ui_done(glue("Row {i}: file {file} renamed to {new_file_ext}")) } else { - if(is.na(sub)) { + if (is.na(sub)) { dir.create(file.path(dir, new_file), recursive = TRUE) } ui_oops(glue("Row {i}: no file found")) @@ -97,7 +97,7 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su #' Save all flobs. #' -#' Rename \code{\link[flobr]{flob}}s from a SQLite database and save to directory. +#' Rename [flobr::flob()]s from a SQLite database and save to directory. #' #' @inheritParams save_flobs #' @inheritParams write_flob @@ -119,36 +119,39 @@ save_flobs <- function(column_name, table_name, conn, dir = ".", sep = "_-_", su #' DBI::dbDisconnect(conn) save_all_flobs <- function(table_name = NULL, conn, dir = ".", sep = "_-_", sub = FALSE, replace = FALSE, - geometry = FALSE){ + geometry = FALSE) { check_sqlite_connection(conn) - if(!is.null(table_name)) { + if (!is.null(table_name)) { check_table_name(table_name, conn) -} + } chk_flag(geometry) chk_string(dir) chk_string(sep) chk_lgl(sub) chk_flag(replace) - if(is.null(table_name)){ + if (is.null(table_name)) { table_name <- table_names(conn) } success <- vector(mode = "list") success_names <- vector() - for(i in table_name){ + for (i in table_name) { cols <- blob_columns(i, conn) - if(!geometry) cols <- cols[cols != "geometry"] - for(j in cols){ + if (!geometry) cols <- cols[cols != "geometry"] + for (j in cols) { name <- file.path(i, j) path <- file.path(dir, name) - if(!dir.exists(path)) + 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)}")) - success[[name]] <- save_flobs(j, i, conn, path, sep = sep, sub = sub, - replace = replace) + success[[name]] <- save_flobs(j, i, conn, path, + sep = sep, sub = sub, + replace = replace + ) ui_line("") } } diff --git a/R/ui.R b/R/ui.R index 2e1cdf1..92d31c1 100644 --- a/R/ui.R +++ b/R/ui.R @@ -23,5 +23,3 @@ ui_value <- function(x) { x <- crayon::blue(x) x } - - diff --git a/R/utils.R b/R/utils.R index 015ab23..3c9de36 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,8 +1,8 @@ -create_filename <- function(x, sep){ +create_filename <- function(x, sep) { glue_collapse(x, sep) } -parse_filename <- function(x, sep){ +parse_filename <- function(x, sep) { x <- tools::file_path_sans_ext(x) strsplit(x, sep)[[1]] } @@ -13,39 +13,41 @@ collapse_flob <- function(x) { glue("x'{y}'") } -list_dirs <- function(path, recursive = TRUE, pattern = ".*"){ +list_dirs <- function(path, recursive = TRUE, pattern = ".*") { dirs <- list.dirs(path, recursive = vld_true(recursive), full.names = TRUE) - if(pattern != ".*") { + if (pattern != ".*") { dirs <- dirs[grepl(pattern, dirs)] } dirs } -list_files <- function(path, recursive = TRUE, pattern = ".*"){ - if(pattern == ".*") +list_files <- function(path, recursive = TRUE, pattern = ".*") { + if (pattern == ".*") { pattern <- NULL + } files <- list.files(path, recursive = !vld_false(recursive), pattern = pattern, full.names = TRUE) dirs <- list.dirs(path, recursive = !vld_false(recursive), full.names = TRUE) # just those files nested in a subdirectory # the file.path(dirname(), basename()) is hack to get it to work on windows - if(is.na(recursive)) { - files <- files[dirname(dirname(files)) == file.path(dirname(path),basename(path))] + if (is.na(recursive)) { + files <- files[dirname(dirname(files)) == file.path(dirname(path), basename(path))] } setdiff(files, dirs) } -dir_tree <- function(path, sub){ +dir_tree <- function(path, sub) { dirs <- setdiff(list.dirs(path, recursive = TRUE, full.names = FALSE), "") x <- strsplit(dirs, "/") - if(!vld_false(sub)) { + if (!vld_false(sub)) { x <- x[which(sapply(x, length) == 2L)] - } else + } else { x <- x[which(sapply(x, length) > 1)] + } x } -is_length_unequal <- function(values, key){ +is_length_unequal <- function(values, key) { length(values) > length(key) } diff --git a/README.Rmd b/README.Rmd index 42112ff..67e9a57 100644 --- a/README.Rmd +++ b/README.Rmd @@ -49,7 +49,7 @@ library(dbflobr) flob <- flobr::flob(system.file("extdata", "flobr.pdf", package = "flobr")) str(flob) -# create a SQLite database connection +# create a SQLite database connection conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") # create a table 'Table1' of data @@ -57,7 +57,7 @@ DBI::dbWriteTable(conn, "Table1", data.frame(IntColumn = c(1L, 2L))) DBI::dbReadTable(conn, "Table1") -# specify which row to add the flob to by providing a key +# specify which row to add the flob to by providing a key key <- data.frame(IntColumn = 2L) # write the flob to the database in column 'BlobColumn' diff --git a/man/import_all_flobs.Rd b/man/import_all_flobs.Rd index b1e23ae..ee1b7f6 100644 --- a/man/import_all_flobs.Rd +++ b/man/import_all_flobs.Rd @@ -43,7 +43,7 @@ An invisible named list indicating directory path, file names and whether files were successfully written to database. } \description{ -Import \code{\link[flobr]{flob}}s to SQLite database from directory. +Import \code{\link[flobr:flob]{flobr::flob()}}s to SQLite database from directory. Table and column names are matched to directory names within main directory. Values in file names are matched to table primary key to determine where to write flob. } diff --git a/man/import_flobs.Rd b/man/import_flobs.Rd index 9354913..8b16e48 100644 --- a/man/import_flobs.Rd +++ b/man/import_flobs.Rd @@ -50,14 +50,14 @@ An invisible named vector indicating file name and whether the file was successfully written to database. } \description{ -Import \code{\link[flobr]{flob}}s to SQLite database column from directory. +Import \code{\link[flobr:flob]{flobr::flob()}}s to SQLite database column from directory. Values in file name are matched to table primary key to determine where to write flob. } \examples{ conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") DBI::dbGetQuery(conn, "CREATE TABLE Table1 (CharColumn TEXT PRIMARY KEY NOT NULL)") DBI::dbWriteTable(conn, "Table1", data.frame(CharColumn = c("a", "b")), append = TRUE) -key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0,,drop = FALSE] +key <- data.frame(CharColumn = "a", stringsAsFactors = FALSE)[0, , drop = FALSE] dir <- tempdir() write.csv(key, file.path(dir, "a.csv")) import_flobs("BlobColumn", "Table1", conn, dir) diff --git a/man/read_flob.Rd b/man/read_flob.Rd index f8fb1b1..519ef57 100644 --- a/man/read_flob.Rd +++ b/man/read_flob.Rd @@ -24,7 +24,7 @@ If NA, the function will adapt accordingly.} A flob or blob. } \description{ -Read a \code{\link[flobr]{flob}} from a SQLite database. +Read a \code{\link[flobr:flob]{flobr::flob()}} from a SQLite database. } \examples{ flob <- flobr::flob_obj diff --git a/man/save_all_flobs.Rd b/man/save_all_flobs.Rd index 26ad1dc..ecb9570 100644 --- a/man/save_all_flobs.Rd +++ b/man/save_all_flobs.Rd @@ -38,7 +38,7 @@ within a subdirectory are deleted.} An invisible named list of named vectors of the file names and new file names saved. } \description{ -Rename \code{\link[flobr]{flob}}s from a SQLite database and save to directory. +Rename \code{\link[flobr:flob]{flobr::flob()}}s from a SQLite database and save to directory. } \examples{ flob <- flobr::flob_obj diff --git a/man/save_flobs.Rd b/man/save_flobs.Rd index e65562f..7f56577 100644 --- a/man/save_flobs.Rd +++ b/man/save_flobs.Rd @@ -41,7 +41,7 @@ If slob_ext = NULL slobs will be ignored.} An invisible named vector of the file names and new file names saved. } \description{ -Rename \code{\link[flobr]{flob}}s from a SQLite database BLOB column and save to directory. +Rename \code{\link[flobr:flob]{flobr::flob()}}s from a SQLite database BLOB column and save to directory. } \examples{ flob <- flobr::flob_obj diff --git a/man/write_flob.Rd b/man/write_flob.Rd index 20c46da..dea07ca 100644 --- a/man/write_flob.Rd +++ b/man/write_flob.Rd @@ -27,7 +27,7 @@ IF FALSE, a new BLOB column is created.} An invisible copy of flob. } \description{ -Write a \code{\link[flobr]{flob}} to a SQLite database. +Write a \code{\link[flobr:flob]{flobr::flob()}} to a SQLite database. } \examples{ flob <- flobr::flob_obj diff --git a/tests/testthat/test-check.R b/tests/testthat/test-check.R index dc4452a..f995f57 100644 --- a/tests/testthat/test-check.R +++ b/tests/testthat/test-check.R @@ -1,17 +1,23 @@ test_that("check_sqlite_connection", { expect_error( - check_sqlite_connection(1), class = "chk_error") + check_sqlite_connection(1), + class = "chk_error" + ) conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") expect_identical(check_sqlite_connection(conn), NULL) expect_identical(check_sqlite_connection(conn, connected = TRUE), NULL) expect_error( - check_sqlite_connection(conn, connected = FALSE), class = "chk_error") + check_sqlite_connection(conn, connected = FALSE), + class = "chk_error" + ) DBI::dbDisconnect(conn) expect_identical(check_sqlite_connection(conn), NULL) expect_error( - check_sqlite_connection(conn, connected = TRUE), class = "chk_error") + check_sqlite_connection(conn, connected = TRUE), + class = "chk_error" + ) expect_identical(check_sqlite_connection(conn, connected = FALSE), NULL) }) @@ -23,9 +29,13 @@ test_that("check_table_name", { expect_true(DBI::dbCreateTable(conn, "local", local)) expect_error( - check_table_name(1, conn), class = "chk_error") + check_table_name(1, conn), + class = "chk_error" + ) expect_error( - check_table_name("e", conn), class = "chk_error") + check_table_name("e", conn), + class = "chk_error" + ) expect_identical(check_table_name("local", conn), "local") }) @@ -37,13 +47,21 @@ test_that("check_column_name", { expect_true(DBI::dbCreateTable(conn, "local", local)) expect_error( - check_column_name("test", table_name = 1, exists = TRUE, conn), class = "chk_error") + check_column_name("test", table_name = 1, exists = TRUE, conn), + class = "chk_error" + ) expect_error( - check_column_name("test", table_name = "e", exists = TRUE, conn), class = "chk_error") + check_column_name("test", table_name = "e", exists = TRUE, conn), + class = "chk_error" + ) expect_error( - check_column_name(1, table_name = "local", exists = TRUE, conn), class = "chk_error") + check_column_name(1, table_name = "local", exists = TRUE, conn), + class = "chk_error" + ) expect_error( - check_column_name("e", table_name = "local", exists = TRUE, conn), class = "chk_error") + check_column_name("e", table_name = "local", exists = TRUE, conn), + class = "chk_error" + ) expect_identical(check_column_name("test", table_name = "local", exists = TRUE, conn), "test") expect_identical(check_column_name("e", table_name = "local", exists = FALSE, conn), "e") }) @@ -57,9 +75,11 @@ test_that("check_column_blob", { expect_true(add_blob_column("blob", table_name = "local", conn = conn)) expect_error( - check_column_blob("test", table_name = "local", conn)) + check_column_blob("test", table_name = "local", conn) + ) expect_error( - check_column_blob("x", table_name = "local", conn)) + check_column_blob("x", table_name = "local", conn) + ) expect_identical(check_column_blob("blob", table_name = "local", conn), "blob") }) @@ -79,9 +99,11 @@ test_that("check_key", { key3 <- data.frame(num = 1.1, key = 2) expect_error( - check_key(table_name = "df", key = key2, conn)) + check_key(table_name = "df", key = key2, conn) + ) expect_error( - check_key(table_name = "df", key = key3, conn)) + check_key(table_name = "df", key = key3, conn) + ) expect_identical(check_key(table_name = "df", key = key, conn), key) }) @@ -110,5 +132,4 @@ test_that("check_flob_query", { expect_error(check_flob_query("non-blob", slob = TRUE), "`x` must be a blob of a serialized object.") # this is not ideal behavior expect_error(check_flob_query(slob_obj, slob = FALSE), "Serialized element of `x` must inherit from S3 class 'exint'.") - }) diff --git a/tests/testthat/test-import.R b/tests/testthat/test-import.R index d51a21e..8bfd42c 100644 --- a/tests/testthat/test-import.R +++ b/tests/testthat/test-import.R @@ -20,37 +20,49 @@ test_that("import_flobs works", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, int INTEGER NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, int))") + PRIMARY KEY (char, int))" + ) - DBI::dbExecute(conn, - "CREATE TABLE df2 ( + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( char TEXT NOT NULL, - num REAL NOT NULL)") + num REAL NOT NULL)" + ) # empty - DBI::dbExecute(conn, - "CREATE TABLE df3 ( + DBI::dbExecute( + conn, + "CREATE TABLE df3 ( char TEXT NOT NULL, int INTEGER NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char))") + PRIMARY KEY (char))" + ) DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "b", "b"), - int = c(1, 2, 3), - num = c(1, 1, 1), stringsAsFactors = FALSE), - append = TRUE) + data.frame( + char = c("a", "b", "b"), + int = c(1, 2, 3), + num = c(1, 1, 1), stringsAsFactors = FALSE + ), + append = TRUE + ) DBI::dbWriteTable(conn, "df3", - data.frame(char = c("a", "b", "c"), - int = c(1, 2, 3), - num = c(1, 1, 1), stringsAsFactors = FALSE), - append = TRUE) + data.frame( + char = c("a", "b", "c"), + int = c(1, 2, 3), + num = c(1, 1, 1), stringsAsFactors = FALSE + ), + append = TRUE + ) files <- list_files(path, recursive = FALSE) expect_length(list_files(path, recursive = FALSE), 3L) @@ -105,21 +117,26 @@ test_that("import_all_flobs works", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( - char TEXT PRIMARY KEY NOT NULL)") + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( + char TEXT PRIMARY KEY NOT NULL)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -135,18 +152,22 @@ test_that("import_all_flobs works", { x <- import_all_flobs(conn, path, exists = TRUE, replace = FALSE) expect_identical(sum(unlist(x)), 0L) expect_length(x, 3) - expect_identical(names(unlist(x)), c("df/New.a_-_1.pdf", - "df/New.b_-_1.pdf", - "df/New2.a_-_2.1.pdf", - "df2/New.b.pdf")) + expect_identical(names(unlist(x)), c( + "df/New.a_-_1.pdf", + "df/New.b_-_1.pdf", + "df/New2.a_-_2.1.pdf", + "df2/New.b.pdf" + )) x <- import_all_flobs(conn, path, exists = TRUE, replace = TRUE) expect_identical(sum(unlist(x)), 4L) expect_length(x, 3) - expect_identical(names(unlist(x)), c("df/New.a_-_1.pdf", - "df/New.b_-_1.pdf", - "df/New2.a_-_2.1.pdf", - "df2/New.b.pdf")) + expect_identical(names(unlist(x)), c( + "df/New.a_-_1.pdf", + "df/New.b_-_1.pdf", + "df/New2.a_-_2.1.pdf", + "df2/New.b.pdf" + )) x <- import_all_flobs(conn, path, sep = "-", exists = TRUE, replace = TRUE) expect_identical(sum(unlist(x)), 1L) @@ -155,8 +176,10 @@ test_that("import_all_flobs works", { expect_identical(sum(unlist(x)), 2L) expect_length(x, 3) - expect_identical(names(unlist(x)), c("df/New.b_-_1.pdf", - "df2/New.b.pdf")) + expect_identical(names(unlist(x)), c( + "df/New.b_-_1.pdf", + "df2/New.b.pdf" + )) }) test_that("import_all_flobs requires unique", { @@ -166,46 +189,63 @@ test_that("import_all_flobs requires unique", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) flob <- flobr::flob_obj write_flob(flob, "New", "df", key = data.frame(char = "a", num = 1), conn) expect_identical(save_all_flobs(conn = conn, dir = path), list(`df/New` = c(flobr.pdf = "a_-_1.pdf"))) - expect_error(import_flobs("New", "df", conn = conn, dir = path), - "`New` must not already exist in table `df`.") + expect_error( + import_flobs("New", "df", conn = conn, dir = path), + "`New` must not already exist in table `df`." + ) list.files(file.path(path), recursive = TRUE) - expect_identical(import_flobs("New", "df", conn = conn, dir = path, exists = TRUE), - structure(logical(0), .Names = character(0))) + expect_identical( + import_flobs("New", "df", conn = conn, dir = path, exists = TRUE), + structure(logical(0), .Names = character(0)) + ) - expect_identical(import_flobs("New", "df", conn = conn, dir = file.path(path, "df", "New"), exists = TRUE, replace = TRUE), - c("a_-_1.pdf" = TRUE)) + expect_identical( + import_flobs("New", "df", conn = conn, dir = file.path(path, "df", "New"), exists = TRUE, replace = TRUE), + c("a_-_1.pdf" = TRUE) + ) - expect_identical(import_flobs("New", "df", conn = conn, dir =path, exists = TRUE, replace = TRUE, recursive = TRUE), - c("a_-_1.pdf" = TRUE)) + expect_identical( + import_flobs("New", "df", conn = conn, dir = path, exists = TRUE, replace = TRUE, recursive = TRUE), + c("a_-_1.pdf" = TRUE) + ) save_all_flobs(conn = conn, dir = file.path(path, "sub")) - expect_identical(import_flobs("New", "df", conn = conn, dir = file.path(path, "df", "New"), exists = TRUE, replace = TRUE), - c("a_-_1.pdf" = TRUE)) + expect_identical( + import_flobs("New", "df", conn = conn, dir = file.path(path, "df", "New"), exists = TRUE, replace = TRUE), + c("a_-_1.pdf" = TRUE) + ) - expect_identical(import_flobs("New", "df", conn = conn, dir = file.path(path, "sub", "df", "New"), exists = TRUE, replace = TRUE), - c("a_-_1.pdf" = TRUE)) + expect_identical( + import_flobs("New", "df", conn = conn, dir = file.path(path, "sub", "df", "New"), exists = TRUE, replace = TRUE), + c("a_-_1.pdf" = TRUE) + ) - expect_error(import_flobs("New", "df", conn = conn, dir = path, exists = TRUE, recursive = TRUE), - "File names must be unique.") + expect_error( + import_flobs("New", "df", conn = conn, dir = path, exists = TRUE, recursive = TRUE), + "File names must be unique." + ) }) test_that("import_all_flobs is actually recursive", { @@ -215,24 +255,29 @@ test_that("import_all_flobs is actually recursive", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) flob <- flobr::flob_obj write_flob(flob, "New", "df", key = data.frame(char = "a", num = 1), conn) expect_identical(save_all_flobs(conn = conn, dir = file.path(path, "sub")), list(`df/New` = c(flobr.pdf = "a_-_1.pdf"))) - expect_identical(import_flobs("New", "df", conn = conn, dir = path, exists = TRUE, replace = TRUE, recursive = TRUE), - c("a_-_1.pdf" = TRUE)) + expect_identical( + import_flobs("New", "df", conn = conn, dir = path, exists = TRUE, replace = TRUE, recursive = TRUE), + c("a_-_1.pdf" = TRUE) + ) }) test_that("import_flobs works with subdirectory", { @@ -252,39 +297,56 @@ test_that("import_flobs works with subdirectory", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, int INTEGER NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, int))") + PRIMARY KEY (char, int))" + ) DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "b", "b"), - int = c(1, 2, 3), - num = c(1, 1, 1), stringsAsFactors = FALSE), - append = TRUE) - - expect_identical(list.files(path, recursive = TRUE), - c("a_-_1/data.csv", "b_-_2/data.csv", "b_-_3/data.csv")) - - expect_identical(import_flobs("New", "df", conn, path, sub = TRUE), - c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE)) - - expect_identical(import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), - c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE)) + data.frame( + char = c("a", "b", "b"), + int = c(1, 2, 3), + num = c(1, 1, 1), stringsAsFactors = FALSE + ), + append = TRUE + ) + + expect_identical( + list.files(path, recursive = TRUE), + c("a_-_1/data.csv", "b_-_2/data.csv", "b_-_3/data.csv") + ) + + expect_identical( + import_flobs("New", "df", conn, path, sub = TRUE), + c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE) + ) + + expect_identical( + import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), + c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE) + ) unlink(file.path(path, "a_-_1", "data.csv")) - expect_identical(import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), - c(`b_-_2` = TRUE, `b_-_3` = TRUE)) + expect_identical( + import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), + c(`b_-_2` = TRUE, `b_-_3` = TRUE) + ) write.csv(df, file.path(path, "b_-_2", "data2.csv")) - expect_error(import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), - "Directory names must be unique.") - - expect_identical(import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE, pattern = "data.csv"), - c(`b_-_2` = TRUE, `b_-_3` = TRUE)) + expect_error( + import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE), + "Directory names must be unique." + ) + + expect_identical( + import_flobs("New", "df", conn, path, sub = TRUE, exists = TRUE, replace = TRUE, pattern = "data.csv"), + c(`b_-_2` = TRUE, `b_-_3` = TRUE) + ) }) test_that("import_flobs does not recurse beyond 1", { @@ -304,24 +366,33 @@ test_that("import_flobs does not recurse beyond 1", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, int INTEGER NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, int))") + PRIMARY KEY (char, int))" + ) DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "b", "b"), - int = c(1, 2, 3), - num = c(1, 1, 1), stringsAsFactors = FALSE), - append = TRUE) - - expect_identical(import_flobs("New", "df", conn, path, sub = TRUE), - structure(logical(0), .Names = character(0))) - - expect_identical(import_flobs("New2", "df", conn, file.path(path, "extra"), sub = TRUE), - c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE)) + data.frame( + char = c("a", "b", "b"), + int = c(1, 2, 3), + num = c(1, 1, 1), stringsAsFactors = FALSE + ), + append = TRUE + ) + + expect_identical( + import_flobs("New", "df", conn, path, sub = TRUE), + structure(logical(0), .Names = character(0)) + ) + + expect_identical( + import_flobs("New2", "df", conn, file.path(path, "extra"), sub = TRUE), + c(`a_-_1` = TRUE, `b_-_2` = TRUE, `b_-_3` = TRUE) + ) }) @@ -332,22 +403,27 @@ test_that("import_flobs sub = TRUE", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( char TEXT PRIMARY KEY NOT NULL, - geometry2 BLOB)") + geometry2 BLOB)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -355,16 +431,23 @@ test_that("import_flobs sub = TRUE", { write_flob(flob, "geometry", "df", key = data.frame(char = "b"), conn) x <- save_all_flobs(conn = conn, dir = file.path(path, "dump"), sub = TRUE, geometry = TRUE) - expect_identical(x, list(`df/geometry` = c(flobr.pdf = "a_-_1.pdf", flobr.pdf = "b_-_1.pdf" - ), `df2/geometry2` = structure(logical(0), .Names = character(0)))) - - expect_identical(import_all_flobs(conn = conn, dir = file.path(path, "dump"), sub = TRUE, - exists = TRUE), - list(`df/geometry` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df2/geometry2` = structure(logical(0), .Names = character(0)))) - - expect_identical(import_all_flobs(conn = conn, dir = file.path(path, "dump"), sub = TRUE, - exists = TRUE, replace = TRUE), - list(`df/geometry` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df2/geometry2` = structure(logical(0), .Names = character(0)))) + expect_identical(x, list(`df/geometry` = c(flobr.pdf = "a_-_1.pdf", flobr.pdf = "b_-_1.pdf"), `df2/geometry2` = structure(logical(0), .Names = character(0)))) + + expect_identical( + import_all_flobs( + conn = conn, dir = file.path(path, "dump"), sub = TRUE, + exists = TRUE + ), + list(`df/geometry` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df2/geometry2` = structure(logical(0), .Names = character(0))) + ) + + expect_identical( + import_all_flobs( + conn = conn, dir = file.path(path, "dump"), sub = TRUE, + exists = TRUE, replace = TRUE + ), + list(`df/geometry` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df2/geometry2` = structure(logical(0), .Names = character(0))) + ) }) test_that("import_flobs sub = NA", { @@ -374,37 +457,45 @@ test_that("import_flobs sub = NA", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) flob <- flobr::flob_obj write_flob(flob, "geometry", "df", key = data.frame(char = "a", num = 1), conn) write_flob(flob, "geometry", "df", key = data.frame(char = "b"), conn) x <- save_all_flobs(conn = conn, dir = file.path(path, "dump"), sub = NA, geometry = TRUE) - expect_identical(x, list(`df/geometry` = c(flobr.pdf = "a_-_1.pdf", flobr.pdf = "b_-_1.pdf" - ))) + expect_identical(x, list(`df/geometry` = c(flobr.pdf = "a_-_1.pdf", flobr.pdf = "b_-_1.pdf"))) - expect_identical(import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE), - c(`a_-_1` = FALSE, `a_-_2.1` = TRUE, `b_-_1` = FALSE)) + expect_identical( + import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE), + c(`a_-_1` = FALSE, `a_-_2.1` = TRUE, `b_-_1` = FALSE) + ) - expect_identical(import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE, replace = TRUE), - c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE)) + expect_identical( + import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE, replace = TRUE), + c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE) + ) 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_identical(import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE, replace = TRUE), - c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE)) + expect_identical( + import_flobs("geometry", "df", conn = conn, dir = file.path(path, "dump", "df", "geometry"), sub = NA, exists = TRUE, replace = TRUE), + c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE) + ) expect_error(read_flob("geometry", "df", conn = conn, key = data.frame(char = "b", num = 1))) }) @@ -416,21 +507,26 @@ test_that("import_all_flobs works", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( - char TEXT PRIMARY KEY NOT NULL)") + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( + char TEXT PRIMARY KEY NOT NULL)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 21, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 21, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -441,27 +537,35 @@ test_that("import_all_flobs works", { save_all_flobs(conn = conn, dir = path, sub = NA) - expect_identical(import_all_flobs(conn, path, exists = TRUE, sub = TRUE), - list(`df/New` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df/New2` = c(`a_-_21` = FALSE), - `df2/New` = c(b = FALSE))) - - expect_identical(import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = TRUE), - list(`df/New` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_21` = TRUE), - `df2/New` = c(b = TRUE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, sub = TRUE), + list( + `df/New` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df/New2` = c(`a_-_21` = FALSE), + `df2/New` = c(b = FALSE) + ) + ) + + expect_identical( + import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = TRUE), + list( + `df/New` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_21` = TRUE), + `df2/New` = c(b = TRUE) + ) + ) list.files(path, recursive = TRUE, include.dirs = TRUE) import_flobs("New2", "df", conn = conn, dir = file.path(path, "df", "New2"), sub = TRUE, exists = TRUE, replace = TRUE) - expect_identical(import_all_flobs(conn, path, exists = TRUE, sub = NA), - list(`df/New` = c(`a_-_1` = FALSE, `a_-_21` = TRUE, `b_-_1` = FALSE - ), `df/New2` = c(`a_-_1` = TRUE, `a_-_21` = FALSE, `b_-_1` = TRUE - ), `df2/New` = c(a = TRUE, b = FALSE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, sub = NA), + list(`df/New` = c(`a_-_1` = FALSE, `a_-_21` = TRUE, `b_-_1` = FALSE), `df/New2` = c(`a_-_1` = TRUE, `a_-_21` = FALSE, `b_-_1` = TRUE), `df2/New` = c(a = TRUE, b = FALSE)) + ) - expect_identical(import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = NA), - list(`df/New` = c(`a_-_1` = TRUE, `a_-_21` = TRUE, `b_-_1` = TRUE - ), `df/New2` = c(`a_-_1` = TRUE, `a_-_21` = TRUE, `b_-_1` = TRUE - ), `df2/New` = c(a = TRUE, b = TRUE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = NA), + list(`df/New` = c(`a_-_1` = TRUE, `a_-_21` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_1` = TRUE, `a_-_21` = TRUE, `b_-_1` = TRUE), `df2/New` = c(a = TRUE, b = TRUE)) + ) }) test_that("import_all_flobs works with .", { @@ -471,21 +575,26 @@ test_that("import_all_flobs works with .", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( - char TEXT PRIMARY KEY NOT NULL)") + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( + char TEXT PRIMARY KEY NOT NULL)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -496,27 +605,35 @@ test_that("import_all_flobs works with .", { save_all_flobs(conn = conn, dir = path, sub = NA) - expect_identical(import_all_flobs(conn, path, exists = TRUE, sub = TRUE), - list(`df/New` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df/New2` = c(`a_-_2.1` = FALSE), - `df2/New` = c(b = FALSE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, sub = TRUE), + list( + `df/New` = c(`a_-_1` = FALSE, `b_-_1` = FALSE), `df/New2` = c(`a_-_2.1` = FALSE), + `df2/New` = c(b = FALSE) + ) + ) # why is it false for a_-_2.1!! (should be true) - expect_identical(import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = TRUE), - list(`df/New` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_2.1` = FALSE), - `df2/New` = c(b = TRUE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = TRUE), + list( + `df/New` = c(`a_-_1` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_2.1` = FALSE), + `df2/New` = c(b = TRUE) + ) + ) list.files(path, recursive = TRUE, include.dirs = TRUE) import_flobs("New2", "df", conn = conn, dir = file.path(path, "df", "New2"), sub = TRUE, exists = TRUE, replace = TRUE) - expect_identical(import_all_flobs(conn, path, exists = TRUE, sub = NA), - list(`df/New` = c(`a_-_1` = FALSE, `a_-_2.1` = TRUE, `b_-_1` = FALSE - ), `df/New2` = c(`a_-_1` = TRUE, `a_-_2.1` = FALSE, `b_-_1` = TRUE - ), `df2/New` = c(a = TRUE, b = FALSE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, sub = NA), + list(`df/New` = c(`a_-_1` = FALSE, `a_-_2.1` = TRUE, `b_-_1` = FALSE), `df/New2` = c(`a_-_1` = TRUE, `a_-_2.1` = FALSE, `b_-_1` = TRUE), `df2/New` = c(a = TRUE, b = FALSE)) + ) # why is it false for second a_-_2.1!! (should be true) - expect_identical(import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = NA), - list(`df/New` = c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE - ), `df/New2` = c(`a_-_1` = TRUE, `a_-_2.1` = FALSE, `b_-_1` = TRUE - ), `df2/New` = c(a = TRUE, b = TRUE))) + expect_identical( + import_all_flobs(conn, path, exists = TRUE, replace = TRUE, sub = NA), + list(`df/New` = c(`a_-_1` = TRUE, `a_-_2.1` = TRUE, `b_-_1` = TRUE), `df/New2` = c(`a_-_1` = TRUE, `a_-_2.1` = FALSE, `b_-_1` = TRUE), `df2/New` = c(a = TRUE, b = TRUE)) + ) }) diff --git a/tests/testthat/test-read-write.R b/tests/testthat/test-read-write.R index cc5a563..b1bb124 100644 --- a/tests/testthat/test-read-write.R +++ b/tests/testthat/test-read-write.R @@ -22,42 +22,54 @@ test_that("write_flob works", { flob <- flobr::flob_obj expect_error(write_flob(1, "flob", - table_name = "df", - exists = FALSE, key = key, conn = conn + table_name = "df", + exists = FALSE, key = key, conn = conn ), class = "chk_error") expect_error( write_flob(flob, "flob", - table_name = "test", - exists = FALSE, key = key, conn = conn - ), class = "chk_error") + table_name = "test", + exists = FALSE, key = key, conn = conn + ), + class = "chk_error" + ) expect_error( write_flob(flob, "flob", - table_name = "df", - exists = TRUE, key = key, conn = conn - ), class = "chk_error") + table_name = "df", + exists = TRUE, key = key, conn = conn + ), + class = "chk_error" + ) expect_error( write_flob(flob, "char", - table_name = "df", - exists = TRUE, key = key, conn = conn - ), class = "chk_error") + table_name = "df", + exists = TRUE, key = key, conn = conn + ), + class = "chk_error" + ) expect_error( write_flob(flob, "flob", - table_name = "df", - exists = FALSE, key = "a", conn = conn - ), class = "chk_error") + table_name = "df", + exists = FALSE, key = "a", conn = conn + ), + class = "chk_error" + ) expect_error( write_flob(flob, "flob", - table_name = "df", - exists = FALSE, key = key2, conn = conn - ), class = "chk_error") + table_name = "df", + exists = FALSE, key = key2, conn = conn + ), + class = "chk_error" + ) expect_error( write_flob(flob, "flob", - table_name = "df", - exists = TRUE, key = key2, conn = conn - ), class = "chk_error") + table_name = "df", + exists = TRUE, key = key2, conn = conn + ), + class = "chk_error" + ) expect_is(write_flob(flob, "flob", - table_name = "df", - exists = TRUE, key = key, conn = conn + table_name = "df", + exists = TRUE, key = key, conn = conn ), "flob") df2 <- DBI::dbReadTable(conn, "df") @@ -65,13 +77,21 @@ test_that("write_flob works", { ### read flob expect_error( - read_flob("flob", table_name = "test", key = key, conn = conn), class = "chk_error") + read_flob("flob", table_name = "test", key = key, conn = conn), + class = "chk_error" + ) expect_error( - read_flob("blob", table_name = "df", key = key, conn = conn), class = "chk_error") + read_flob("blob", table_name = "df", key = key, conn = conn), + class = "chk_error" + ) expect_error( - read_flob("flob", table_name = "df", key = key2, conn = conn), class = "chk_error") + read_flob("flob", table_name = "df", key = key2, conn = conn), + class = "chk_error" + ) expect_error( - read_flob("flob", table_name = "df", key = key3, conn = conn), class = "chk_error") + read_flob("flob", table_name = "df", key = key3, conn = conn), + class = "chk_error" + ) flob2 <- read_flob("flob", table_name = "df", key = key, conn = conn) expect_identical(flobr::flob_ext(flob2), flobr::flob_ext(flob)) @@ -79,21 +99,30 @@ test_that("write_flob works", { ### delete flobs expect_error( - delete_flob("flob", table_name = "test", key = key, conn = conn), class = "chk_error") + delete_flob("flob", table_name = "test", key = key, conn = conn), + class = "chk_error" + ) expect_error( - delete_flob("blob", table_name = "df", key = key, conn = conn), class = "chk_error") + delete_flob("blob", table_name = "df", key = key, conn = conn), + class = "chk_error" + ) expect_error( - delete_flob("flob", table_name = "df", key = key2, conn = conn), class = "chk_error") + delete_flob("flob", table_name = "df", key = key2, conn = conn), + class = "chk_error" + ) expect_is(delete_flob("flob", - table_name = "df", - key = key, conn = conn + table_name = "df", + key = key, conn = conn ), "flob") expect_error( - read_flob("flob", table_name = "df", key = key, conn = conn), class = "chk_error") + read_flob("flob", table_name = "df", key = key, conn = conn), + class = "chk_error" + ) expect_error( - delete_flob("flob", table_name = "df", key = key, conn = conn), class = "chk_error") - + delete_flob("flob", table_name = "df", key = key, conn = conn), + class = "chk_error" + ) }) test_that("write_flob column exists", { @@ -105,12 +134,16 @@ test_that("write_flob column exists", { flob <- flobr::flob_obj expect_error( - write_flob(flob, "New", "df", key, conn, exists = TRUE), class = "chk_error") + 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_error( - write_flob(flob, "New", "df", key, conn, exists = FALSE), class = "chk_error") + write_flob(flob, "New", "df", key, conn, exists = FALSE), + class = "chk_error" + ) }) test_that("add_blob_column works", { @@ -123,7 +156,9 @@ test_that("add_blob_column works", { ## add blob_column expect_error( - add_blob_column(table_name = "df", column_name = "x", conn = conn), class = "chk_error") + add_blob_column(table_name = "df", column_name = "x", conn = conn), + class = "chk_error" + ) expect_true(add_blob_column(table_name = "df", column_name = "flob", conn = conn)) df2 <- DBI::dbReadTable(conn, "df") @@ -140,4 +175,3 @@ test_that("add_blob_column works", { expect_is(x, "character") expect_length(x, 1L) }) - diff --git a/tests/testthat/test-save.R b/tests/testthat/test-save.R index d8cf0b6..aae2b16 100644 --- a/tests/testthat/test-save.R +++ b/tests/testthat/test-save.R @@ -5,21 +5,26 @@ test_that("save_flobs works", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( - char TEXT PRIMARY KEY NOT NULL)") + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( + char TEXT PRIMARY KEY NOT NULL)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -49,17 +54,21 @@ test_that("save_flobs works", { names(y) <- NULL expect_identical(y, "b.pdf") # regardless of order - expect_true(all(list.files(path, pattern = "pdf") %in% c("a_-_1.pdf", - "a_-_2.1.pdf", - "b.pdf", - "b_-_1.pdf"))) + expect_true(all(list.files(path, pattern = "pdf") %in% c( + "a_-_1.pdf", + "a_-_2.1.pdf", + "b.pdf", + "b_-_1.pdf" + ))) write_flob(flob, "geometry2", "df2", key = data.frame(char = "a"), conn) y <- save_all_flobs(conn = conn, dir = path) expect_identical(names(y), "df2/geometry2") - expect_error(save_all_flobs(conn = conn, dir = path, geometry = TRUE), - "already exists") + expect_error( + save_all_flobs(conn = conn, dir = path, geometry = TRUE), + "already exists" + ) y <- save_all_flobs(conn = conn, dir = path, geometry = TRUE, replace = TRUE) @@ -73,17 +82,22 @@ test_that("save_flobs works", { expect_error(save_all_flobs("df", conn, 2), class = "chk_error") expect_true(all(list.files(path, pattern = "pdf", recursive = TRUE) %in% - c("a_-_1.pdf", "a_-_2.1.pdf", - "b_-_1.pdf", "b.pdf", "df/geometry/a_-_1.pdf", - "df/geometry/a_-_2.1.pdf", "df/geometry/b_-_1.pdf", - "df2/geometry/b.pdf", "df2/geometry2/a.pdf"))) - - expect_error(save_all_flobs("df2", conn, path, geometry = TRUE), - "already exists") + c( + "a_-_1.pdf", "a_-_2.1.pdf", + "b_-_1.pdf", "b.pdf", "df/geometry/a_-_1.pdf", + "df/geometry/a_-_2.1.pdf", "df/geometry/b_-_1.pdf", + "df2/geometry/b.pdf", "df2/geometry2/a.pdf" + ))) + + expect_error( + save_all_flobs("df2", conn, path, geometry = TRUE), + "already exists" + ) expect_identical( save_all_flobs("df2", conn, path, geometry = TRUE, replace = TRUE), - list(`df2/geometry` = c(flobr.pdf = "b.pdf"), `df2/geometry2` = c(flobr.pdf = "a.pdf"))) + list(`df2/geometry` = c(flobr.pdf = "b.pdf"), `df2/geometry2` = c(flobr.pdf = "a.pdf")) + ) }) test_that("save_flobs works with sub", { @@ -93,22 +107,27 @@ test_that("save_flobs works with sub", { teardown(DBI::dbDisconnect(conn)) # 2 column pk - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( char TEXT NOT NULL, num REAL NOT NULL, - PRIMARY KEY (char, num))") + PRIMARY KEY (char, num))" + ) # one column pk with empty - DBI::dbExecute(conn, - "CREATE TABLE df2 ( + DBI::dbExecute( + conn, + "CREATE TABLE df2 ( char TEXT PRIMARY KEY NOT NULL, - geometry2 BLOB)") + geometry2 BLOB)" + ) # one column pk with two blob cols DBI::dbWriteTable(conn, "df", - data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), - append = TRUE) + data.frame(char = c("a", "a", "b"), num = c(1, 2.1, 1)), + append = TRUE + ) DBI::dbWriteTable(conn, "df2", data.frame(char = c("a", "b")), append = TRUE) flob <- flobr::flob_obj @@ -120,59 +139,76 @@ test_that("save_flobs works with sub", { names(x) <- NULL expect_identical(list.files(path, pattern = "pdf"), x) - expect_identical(list.files(path, recursive = TRUE), - c("a_-_1.pdf", "b_-_1.pdf")) + expect_identical( + list.files(path, recursive = TRUE), + c("a_-_1.pdf", "b_-_1.pdf") + ) unlink(path, recursive = TRUE) dir.create(path) x <- save_flobs("geometry", "df", conn, path, sub = TRUE) expect_identical(names(x), c("flobr.pdf", "flobr.pdf")) names(x) <- NULL - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c("a_-_1", "a_-_1/a_-_1.pdf", "b_-_1", "b_-_1/b_-_1.pdf"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c("a_-_1", "a_-_1/a_-_1.pdf", "b_-_1", "b_-_1/b_-_1.pdf")) + ) unlink(path, recursive = TRUE) dir.create(path) x <- save_flobs("geometry", "df", conn, path, sub = NA) expect_identical(names(x), c("flobr.pdf", "flobr.pdf")) names(x) <- NULL - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c("a_-_1", "a_-_1/a_-_1.pdf", "a_-_2.1", "b_-_1", "b_-_1/b_-_1.pdf"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c("a_-_1", "a_-_1/a_-_1.pdf", "a_-_2.1", "b_-_1", "b_-_1/b_-_1.pdf")) + ) unlink(path, recursive = TRUE) dir.create(path) y <- save_all_flobs(conn = conn, dir = path, geometry = TRUE) expect_identical(names(y), sort(c("df/geometry", "df2/geometry2"))) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c("df", "df/geometry", "df/geometry/a_-_1.pdf", "df/geometry/b_-_1.pdf", - "df2", "df2/geometry2"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c( + "df", "df/geometry", "df/geometry/a_-_1.pdf", "df/geometry/b_-_1.pdf", + "df2", "df2/geometry2" + )) + ) unlink(path, recursive = TRUE) dir.create(path) y <- save_all_flobs(conn = conn, dir = path, geometry = TRUE, sub = TRUE) expect_identical(names(y), sort(c("df/geometry", "df2/geometry2"))) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c("df", "df/geometry", "df/geometry/a_-_1", "df/geometry/a_-_1/a_-_1.pdf", "df/geometry/b_-_1", "df/geometry/b_-_1/b_-_1.pdf", "df2", "df2/geometry2"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c("df", "df/geometry", "df/geometry/a_-_1", "df/geometry/a_-_1/a_-_1.pdf", "df/geometry/b_-_1", "df/geometry/b_-_1/b_-_1.pdf", "df2", "df2/geometry2")) + ) unlink(path, recursive = TRUE) dir.create(path) y <- save_all_flobs(conn = conn, dir = path, geometry = TRUE, sub = NA) expect_identical(names(y), c("df/geometry", "df2/geometry2")) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c( - "df", "df/geometry", "df/geometry/a_-_1", - "df/geometry/a_-_1/a_-_1.pdf", - "df/geometry/a_-_2.1", "df/geometry/b_-_1", - "df/geometry/b_-_1/b_-_1.pdf", - "df2", "df2/geometry2", "df2/geometry2/a", "df2/geometry2/b"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c( + "df", "df/geometry", "df/geometry/a_-_1", + "df/geometry/a_-_1/a_-_1.pdf", + "df/geometry/a_-_2.1", "df/geometry/b_-_1", + "df/geometry/b_-_1/b_-_1.pdf", + "df2", "df2/geometry2", "df2/geometry2/a", "df2/geometry2/b" + )) + ) unlink(path, recursive = TRUE) dir.create(path) y <- save_all_flobs(conn = conn, dir = path, geometry = FALSE, sub = NA) expect_identical(names(y), c("df2/geometry2")) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - sort(c("df2", "df2/geometry2", "df2/geometry2/a", "df2/geometry2/b"))) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + sort(c("df2", "df2/geometry2", "df2/geometry2/a", "df2/geometry2/b")) + ) }) @@ -182,10 +218,12 @@ test_that("save_flob's slob compatibility", { conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") teardown(DBI::dbDisconnect(conn)) - DBI::dbExecute(conn, - "CREATE TABLE df ( + DBI::dbExecute( + conn, + "CREATE TABLE df ( PK TEXT NOT NULL, - PRIMARY KEY (PK))") + PRIMARY KEY (PK))" + ) flob_df <- data.frame(PK = "flob") DBI::dbWriteTable(conn, "df", flob_df, append = TRUE) @@ -198,29 +236,37 @@ test_that("save_flob's slob compatibility", { save_flobs("FlobBlob", "df", conn, dir = path, slob_ext = "pdf") - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - c("blob.pdf", "flob.pdf")) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + c("blob.pdf", "flob.pdf") + ) file.remove(file.path(path, "blob.pdf")) file.remove(file.path(path, "flob.pdf")) save_flobs("FlobBlob", "df", conn, dir = path, slob_ext = NULL) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - c("flob.pdf")) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + c("flob.pdf") + ) file.remove(file.path(path, "flob.pdf")) save_flobs("FlobBlob", "df", conn, dir = path, slob_ext = "pdf", sub = TRUE) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - c("blob", "blob/blob.pdf", "flob", "flob/flob.pdf")) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + c("blob", "blob/blob.pdf", "flob", "flob/flob.pdf") + ) unlink(file.path(path, "flob"), recursive = TRUE) unlink(file.path(path, "blob"), recursive = TRUE) save_flobs("FlobBlob", "df", conn, dir = path, slob_ext = NULL, sub = TRUE) - expect_identical(list.files(path, recursive = TRUE, include.dirs = TRUE), - c("flob", "flob/flob.pdf")) + expect_identical( + list.files(path, recursive = TRUE, include.dirs = TRUE), + c("flob", "flob/flob.pdf") + ) })