Skip to content
Draft
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
106 changes: 38 additions & 68 deletions tests/testthat/helper-data_storage.R
Original file line number Diff line number Diff line change
@@ -1,92 +1,62 @@

#' Set of common tests for different data storage providers
#' @param init_fun function to initialize data storage provider
#' @param provider_name string with name of data storage provider
#'
#' @keywords internal
test_that_common_data_storage <- function(init_fun, provider_name) {
testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Can write to database via DataStorage"
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))
it_common_data_storage <- function(init_fun) {
require(testthat)
dashboard_name <- function() sprintf("dashboard-%s", rlang::hash(Sys.time()))

test_common_data_storage(data_storage, dashboard_name)
}
#
do.call(
it,
list(
"Can write to database via DataStorage",
test_common_data_storage(init_fun(), dashboard_name())
),
envir = parent.frame()
)

testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Insert and read events without details"
do.call(
it,
list(
"Insert and read events without details",
test_common_empty_details(init_fun(), dashboard_name())
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))

test_common_empty_details(data_storage, dashboard_name)
}
envir = parent.frame()
)

testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Insert and read custom fields with length > 1"
do.call(
it,
list("Insert and read custom fields with length > 1",
test_common_len_gt_1(init_fun(), dashboard_name())
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))

test_common_len_gt_1(data_storage, dashboard_name)
}
envir = parent.frame()
)

testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Insert and read custom fields with length > 1 on a pre-populated file"
do.call(
it,
list(
"Insert and read custom fields with length > 1 on a pre-populated file",
test_common_len_gt_1_alt(init_fun(), dashboard_name())
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))

test_common_len_gt_1_alt(data_storage, dashboard_name)
}
envir = parent.frame()
)

testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Time column is writen / read correctly"
do.call(
it,
list("Time column is writen / read correctly",
test_common_read_date(init_fun(), dashboard_name())
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))

test_common_read_date(data_storage, dashboard_name)
}
envir = parent.frame()
)

testthat::test_that(
glue::glue(
.sep = " ",
provider_name,
"Date colimn is writen / read correctly"
do.call(
it,
list("Date column is writen / read correctly",
test_common_read_date(init_fun(), dashboard_name())
),
{
data_storage <- init_fun()
dashboard_name <- paste0("dashboard-", rlang::hash(Sys.time()))

test_common_read_date(data_storage, dashboard_name)
}
envir = parent.frame()
)
}

Expand Down
194 changes: 94 additions & 100 deletions tests/testthat/test-auxiliary_functions.R
Original file line number Diff line number Diff line change
@@ -1,119 +1,113 @@
test_that("Build valid SQL query", {
describe("build_query_sql: builds valid SQL query", {
con <- odbc::dbConnect(RSQLite::SQLite(), ":memory:")

build_query_sql("table_name", .con = con) %>%
as.character() %>%
expect_equal("SELECT * FROM `table_name`")

days_ago <- lubridate::today() - 10
days_ago_double <- days_ago %>% lubridate::as_datetime() %>% as.double()

days_future <- lubridate::today() + 15
days_future_double <- (days_future + 1) %>% lubridate::as_datetime() %>% as.double()

build_query_sql("table_name", days_ago, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name` WHERE time >= {days_ago_double}"
))
it("with no dates", {
build_query_sql("table_name", .con = con) %>%
as.character() %>%
expect_equal("SELECT * FROM `table_name`")
})

build_query_sql("table_name", date_to = days_future, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name` WHERE time < {days_future_double}"
))
it("with date 'from'", {
build_query_sql("table_name", days_ago, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name` WHERE time >= {days_ago_double}"
))
})

build_query_sql("table_name", days_ago, days_future, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name`",
" WHERE time >= {days_ago_double}",
" AND time < {days_future_double}"
))
it("with date 'to'", {
build_query_sql("table_name", date_to = days_future, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name` WHERE time < {days_future_double}"
))
})

build_query_sql(
"table_name",
as.Date("2023-04-13"),
as.Date("2000-01-01"),
.con = con
) %>%
as.character() %>%
expect_equal(
glue::glue(
it("with date 'from' and 'to'", {
build_query_sql("table_name", days_ago, days_future, .con = con) %>%
as.character() %>%
expect_equal(glue::glue(
"SELECT * FROM `table_name`",
" WHERE time >= {lubridate::as_datetime('2023-04-13') %>% as.double()}",
" AND time < {lubridate::as_datetime('2000-01-02') %>% as.double()}"
)
)

" WHERE time >= {days_ago_double}",
" AND time < {days_future_double}"
))
})
})

test_that("build_mongo_connection_string: Build valid string with NULL", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = NULL,
options = NULL
),
"mongodb://localhost:27017/"
)
})
describe("build_mongo_connection_string: builds valid string", {
it("with NULL", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = NULL,
options = NULL
),
"mongodb://localhost:27017/"
)
})

test_that("build_mongo_connection_string: Build valid string with user and pass", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = "a_user",
password = "a_pass",
authdb = NULL,
options = NULL
),
"mongodb://a_user:a_pass@localhost:27017/"
)
})
it("with user and pass", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = "a_user",
password = "a_pass",
authdb = NULL,
options = NULL
),
"mongodb://a_user:a_pass@localhost:27017/"
)
})

test_that("build_mongo_connection_string: Build valid string with `authdb`", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = "path_to_authdb",
options = NULL
),
"mongodb://localhost:27017/path_to_authdb"
)
})
it("with `authdb`", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = "path_to_authdb",
options = NULL
),
"mongodb://localhost:27017/path_to_authdb"
)
})

test_that("build_mongo_connection_string: Build valid string with `options`", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = NULL,
options = list("option1" = "value1", "option2" = "value2")
),
"mongodb://localhost:27017/?option1=value1&option2=value2"
)
})
it("with `options`", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = NULL,
password = NULL,
authdb = NULL,
options = list("option1" = "value1", "option2" = "value2")
),
"mongodb://localhost:27017/?option1=value1&option2=value2"
)
})

test_that("build_mongo_connection_string: Build valid string with all parameters", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = "a_user",
password = "a_pass",
authdb = "path_to_authdb",
options = list("option1" = "value1", "option2" = "value2")
),
"mongodb://a_user:a_pass@localhost:27017/path_to_authdb?option1=value1&option2=value2"
)
it("with all parameters", {
expect_equal(
build_mongo_connection_string(
host = "localhost",
port = 27017,
username = "a_user",
password = "a_pass",
authdb = "path_to_authdb",
options = list("option1" = "value1", "option2" = "value2")
),
"mongodb://a_user:a_pass@localhost:27017/path_to_authdb?option1=value1&option2=value2"
)
})
})
2 changes: 1 addition & 1 deletion tests/testthat/test-data-storage-mariadb.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_mariadb, "MariaDB")
describe("MariaDB", it_common_data_storage(init_test_mariadb))
2 changes: 1 addition & 1 deletion tests/testthat/test-data-storage-mongodb.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_mongodb, "MongoDB")
describe("MongoDB", it_common_data_storage(init_test_mongodb))
2 changes: 1 addition & 1 deletion tests/testthat/test-data-storage-mssqlserver.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_mssql, "MSSQLServer")
describe("MSSQLServer", it_common_data_storage(init_test_mssql))
4 changes: 2 additions & 2 deletions tests/testthat/test-data-storage-postgresql.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Test suite common to data storages (see `helper-data_storage.R`)
withr::with_envvar(
c("TEST_POSTGRESQL_DRIVER" = "RPostgres"),
code = test_that_common_data_storage(init_test_postgres, "PostgreSQL")
code = describe("PostgreSQL (RPostgres)", it_common_data_storage(init_test_postgres))
)

withr::with_envvar(
c("TEST_POSTGRESQL_DRIVER" = "RPostgreSQL"),
code = test_that_common_data_storage(init_test_postgres, "PostgreSQL")
code = describe("PostgreSQL (RPostgreSQL)", it_common_data_storage(init_test_postgres))
)
2 changes: 1 addition & 1 deletion tests/testthat/test-data_storage-logfile.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_logfile, "LogFile")
describe("LogFile", it_common_data_storage(init_test_logfile))
2 changes: 1 addition & 1 deletion tests/testthat/test-data_storage-sqlite.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_sqlite, "SQLite")
describe("SQLite", it_common_data_storage(init_test_sqlite))
Loading