Skip to content
Open

Check #230

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
12 changes: 7 additions & 5 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ create_meta <- function(...) {
}

#' @export
create_meta.list <- function(metadata) {
create_meta.list <- function(metadata, ...) {
if(is.null(names(metadata)) || any(nchar(names(metadata)) == 0)) {
stop("All fields of metadata objects must be named!")
}
Expand Down Expand Up @@ -50,7 +50,7 @@ as.tsmeta <- function(meta, ...) {
}

#' @export
as.tsmeta.data.table <- function(meta) {
as.tsmeta.data.table <- function(meta, ...) {
if(nrow(meta) > 0) {
out <- apply(meta[, -"ts_key", with = FALSE], 1, as.list)
names(out) <- meta$ts_key
Expand All @@ -63,7 +63,7 @@ as.tsmeta.data.table <- function(meta) {
}

#' @export
as.tsmeta.list <- function(meta, check_depth = TRUE) {
as.tsmeta.list <- function(meta, check_depth = TRUE, ...) {
if(check_depth && !has_depth_2(meta) && length(meta) > 0) {
stop("A meta list must have exactly depth 2!")
}
Expand All @@ -76,13 +76,15 @@ as.tsmeta.list <- function(meta, check_depth = TRUE) {
}

#' @export
as.tsmeta.data.frame <- function(meta) {
as.tsmeta.data.frame <- function(meta, ...) {
as.tsmeta(as.data.table(meta))
}


#' @export
as.tsmeta.tsmeta <- identity
as.tsmeta.tsmeta <- function(meta, ...) {
meta
}


# printers ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param as.string logical If as.string is TRUE the string representation of the
#' Date is returned, otherwise a Date object.
#' @examples
#' index_to_date(2020.25)
#' \dontrun{index_to_date(2020.25)}
index_to_date <- function (x, as.string = FALSE)
{
if(inherits(x, "Date")) {
Expand Down Expand Up @@ -45,7 +45,7 @@ index_to_date <- function (x, as.string = FALSE)
#'
#' @return The numeric representation of the date that can be used with ts
#' @examples
#' date_to_index("2020-07-01")
#' \dontrun{date_to_index("2020-07-01")}
date_to_index <- function(x) {
x <- as.character(x)
components <- as.numeric(unlist(strsplit(x, "-")))
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test_db_create_connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ test_that("password from env, missing", {
})

test_that("asking for password", {
skip_if_not(require(rstudioapi) && rstudioapi::isAvailable())

fake_dbConnect = mock()

with_mock(
Expand Down Expand Up @@ -122,7 +124,7 @@ test_that("getting password from file", {
Sys.info = mock(list(user = "bobby")),
file.exists = mock(TRUE),
dbConnect = fake_dbConnect,
readPasswordFile = fake_readlines,
"timeseriesdb:::readPasswordFile" = fake_readlines,
{
db_connection_create("mydb",
passwd = "my/password/file",
Expand All @@ -146,7 +148,7 @@ test_that("password from file, line no too big", {

with_mock(
file.exists = mock(TRUE),
readPasswordFile = fake_readlines,
"timeseriesdb:::readPasswordFile" = fake_readlines,
{
expect_error(
db_connection_create("mydb",
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test_db_with_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("It creates a temp table and hands it over to admin", {

with_mock(
dbWriteTable = fake_dbWriteTable,
db_grant_to_admin = fake_db_grant_to_admin,
"timeseriesdb:::db_grant_to_admin" = fake_db_grant_to_admin,
dbRemoveTable = mock(),
{
db_with_temp_table("con", "temp", "content", "field.types", {1+1}, "schema")
Expand Down Expand Up @@ -37,7 +37,7 @@ test_that("it evaluates the code", {
fake_code = mock()
with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = mock(),
{
db_with_temp_table("con", "temp", "content", "field.types", fake_code(), "schema")
Expand All @@ -51,7 +51,7 @@ test_that("it removes the table even in error case", {

with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = fake_remove,
{
capture_error(db_with_temp_table("con", "temp", "content", "field.types", stop("oh noes"), "schema"))
Expand All @@ -67,7 +67,7 @@ context("db_with_tmp_read")
test_that("It warns when using regex with multiple ts_keys", {
with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = mock(),
dbExecute = mock(),
dbQuoteIdentifier = mock(),
Expand All @@ -85,7 +85,7 @@ test_that("It only uses the first element of ts_keys when regex == TRUE", {
db_quote_literal_mock = mock()
with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = mock(),
dbExecute = mock(),
dbQuoteIdentifier = mock(),
Expand All @@ -103,7 +103,7 @@ test_that("It evaluates the code", {

with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = mock(),
dbExecute = mock(),
dbQuoteIdentifier = mock(),
Expand All @@ -121,7 +121,7 @@ test_that("It removes the temp table in error case", {

with_mock(
dbWriteTable = mock(),
db_grant_to_admin = mock(),
"timeseriesdb:::db_grant_to_admin" = mock(),
dbRemoveTable = fake_dbRemoveTable,
dbExecute = mock(),
dbQuoteIdentifier = mock(),
Expand All @@ -144,7 +144,7 @@ test_with_fresh_db(con_admin, "temp_ts_read gets cleaned up in failure case", {
boom_bot <- mock(stop("Kablammy!"))

with_mock(
get_tsl_from_res = boom_bot,
"timeseriesdb:::get_tsl_from_res" = boom_bot,
{
e <- capture_error(db_ts_read(con_admin, "rts1", schema = "tsdb_test"))

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_json_to_ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mock_date_to_index <- mock(2019)
test_that("it converts a regular json to ts", {
# Is this too pedantic?
with_mock(
date_to_index = mock_date_to_index,
"timeseriesdb:::date_to_index" = mock_date_to_index,
{
x <- json_to_ts(regular_json)
expect_is(x, "ts")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ test_that("as.tsmeta.data.table skips depth check", {
fake_as.tsmeta.list <- mock()

with_mock(
as.tsmeta.list = fake_as.tsmeta.list,
"timeseriesdb:::as.tsmeta.list" = fake_as.tsmeta.list,
{
as.tsmeta(as.data.table(meta_df()))
}
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_read_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ test_that("is passes correct args to db_call_function unlocalized", {
fake_db_with_tmp_read <- function(con, keys, regex, code, schema){force(code)}

with_mock(
db_call_function = fake_db_call_function,
db_with_tmp_read = fake_db_with_tmp_read,
"timeseriesdb:::db_call_function" = fake_db_call_function,
"timeseriesdb:::db_with_tmp_read" = fake_db_with_tmp_read,
{
db_metadata_read("con", "vts1", valid_on = "2020-01-01", schema = "schema")

Expand All @@ -38,8 +38,8 @@ test_that("is passes correct args to db_call_function localized", {
fake_db_with_tmp_read <- function(con, keys, regex, code, schema){force(code)}

with_mock(
db_with_tmp_read = fake_db_with_tmp_read,
db_call_function = fake_db_call_function,
"timeseriesdb:::db_with_tmp_read" = fake_db_with_tmp_read,
"timeseriesdb:::db_call_function" = fake_db_call_function,
{
db_metadata_read("con", "vts1", valid_on = "2020-01-01", schema = "schema", locale = "de")

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_release_calendar.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ test_that("defaults", {
schema){force(code)}

with_mock(
db_call_function = fake_db_call_function,
"timeseriesdb:::db_call_function" = fake_db_call_function,
dbWriteTable = mock(),
db_with_temp_table = fake_db_with_tmp_table,
"timeseriesdb:::db_with_temp_table" = fake_db_with_tmp_table,
{
db_release_create("con",
"a_release",
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test_store_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ test_that("is passes correct args to db_call_function unlocalized", {
fake_db_call_function = mock()

with_mock(
db_with_temp_table = function(con, name, content, field.types, code, schema){eval(code)},
toJSON = mock("json"),
fromJSON = mock(list(status = "ok")),
"timeseriesdb:::db_with_temp_table" = function(con, name, content, field.types, code, schema){eval(code)},
"jsonlite::toJSON" = mock("json"),
"jsonlite::fromJSON" = mock(list(status = "ok")),
dbExecute = mock(),
db_call_function = fake_db_call_function,
"timeseriesdb:::db_call_function" = fake_db_call_function,
{
db_metadata_store("con",
as.tsmeta.list(
Expand Down Expand Up @@ -334,11 +334,11 @@ test_that("is passes correct args to db_call_function localized", {
fake_db_call_function = mock()

with_mock(
db_with_temp_table = function(con, name, content, field.types, code, schema){eval(code)},
toJSON = mock("json"),
fromJSON = mock(list(status = "ok")),
"timeseriesdb:::db_with_temp_table" = function(con, name, content, field.types, code, schema){eval(code)},
"jsonlite::toJSON" = mock("json"),
"jsonlite::fromJSON" = mock(list(status = "ok")),
dbExecute = mock(),
db_call_function = fake_db_call_function,
"timeseriesdb:::db_call_function" = fake_db_call_function,
{
db_metadata_store("con",
as.tsmeta.list(
Expand Down