Skip to content

Commit

Permalink
Add Is.Existing.Table Validation in OBDC.Storage.Service
Browse files Browse the repository at this point in the history
Add Is.Existing.Table Validation in OBDC.Storage.Service
  • Loading branch information
FlippieCoetser committed Oct 5, 2023
2 parents 8c1da5b + 3462839 commit 0e448e2
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 135 deletions.
1 change: 1 addition & 0 deletions R/Memory.Storage.Broker.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Memory.Storage.Broker <- \(configuration = NULL) {
tables[[table]] <<- tables[[table]] |> rbind(data)
return(NULL)
}
# TODO: Return as data.frame with name column
operations[['Get.Tables']] <- \() {
tables |> names()
}
Expand Down
1 change: 1 addition & 0 deletions R/Memory.Storage.Validator.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Memory.Storage.Validator <- \(broker = NULL) {
)
return(entity)
}
# TODO: align with ODBC.Storage.Validator
validators[['Is.Existing.Table']] <- \(table) {
broker[['Get.Tables']]() |>
is.element(table) |>
Expand Down
4 changes: 4 additions & 0 deletions R/ODBC.Storage.Broker.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ ODBC.Storage.Broker <- \(configuration, sql = Query::SQL()) {

return(output)
}
operations[['Get.Tables']] <- \() {
query <- "SELECT TABLE_NAME as name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"
query |> operations[['Execute.Query']]()
}
operations[['Insert']] <- \(entity, table) {
table |>
sql[['INSERT']](entity) |>
Expand Down
9 changes: 9 additions & 0 deletions R/ODBC.Storage.Exceptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ ODBC.Storage.Exceptions <- \() {

'Conversion failed when converting from a character string to uniqueidentifier' |>
grepl(error) |> exceptions[["Conversion.Failed"]]()

table <- sub(".*Invalid object name '[^\\.]+\\.([^']+)'.*", "\\1", error)
"Invalid object name 'dbo.Invalid'." |>
grepl(error) |> exceptions[["Table.Invalid"]](table)

stop(error, call. = FALSE)
}
Expand All @@ -79,5 +83,10 @@ ODBC.Storage.Exceptions <- \() {
stop('Query is null. Provide a Query.', call. = FALSE)
}
}
exceptions[['Table.Invalid']] <- \(invoke, table) {
if(invoke) {
stop('ODBC.Storage: Table.Invalid: ', table, ' is not a valid table.', call. = FALSE)
}
}
return(exceptions)
}
18 changes: 15 additions & 3 deletions R/ODBC.Storage.Service.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ODBC.Storage.Service <- \(broker) {
validate <- ODBC.Storage.Validator()
validate <- ODBC.Storage.Validator(broker)

services <- list()
services[['Execute.Query']] <- \(query) {
Expand All @@ -12,29 +12,41 @@ ODBC.Storage.Service <- \(broker) {
entity |> validate[['Entity']]()
table |> validate[['Table']]()

table |> validate[['Is.Existing.Table']]()
# TODO: entity |> validate[['Is.New.Entity']](table)

entity |> broker[['Insert']](table)
}
services[['Retrieve']] <- \(table, fields) {
services[['Retrieve']] <- \(table, fields = '*') {
table |> validate[['Table']]()

table |> validate[['Is.Existing.Table']]()

table |> broker[['Select']](fields)
}
services[['RetrieveWhereId']] <- \(id, table, fields) {
services[['RetrieveWhereId']] <- \(id, table, fields = '*') {
id |> validate[['Id']]()
table |> validate[['Table']]()

table |> validate[['Is.Existing.Table']]()

id |> broker[['SelectWhereId']](table, fields)
}
services[['Modify']] <- \(entity, table) {
entity |> validate[['Entity']]()
table |> validate[['Table']]()

table |> validate[['Is.Existing.Table']]()
# TODO: entity |> validate[['Is.Existing.Entity']](table)

entity |> broker[['Update']](table)
}
services[['Remove']] <- \(id, table) {
id |> validate[['Id']]()
table |> validate[['Table']]()

table |> validate[['Is.Existing.Table']]()

id |> broker[['Delete']](table)
}
return(services)
Expand Down
7 changes: 6 additions & 1 deletion R/ODBC.Storage.Validator.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ODBC.Storage.Validator <- \() {
ODBC.Storage.Validator <- \(broker = NULL) {
exception <- ODBC.Storage.Exceptions()

validators <- Validate::Validator()
Expand All @@ -24,5 +24,10 @@ ODBC.Storage.Validator <- \() {
validators[['Is.Character']]() |>
validators[['Is.UUID']]('id')
}
validators[['Is.Existing.Table']] <- \(table) {
tables <- broker[['Get.Tables']]()
any(tables[['name']] == table) |> isFALSE() |> exception[['Table.Invalid']](table)
return(table)
}
return(validators)
}
2 changes: 0 additions & 2 deletions tests/testthat/helper.sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ configurator <-
ODBC.Configuration.Service() |>
ODBC.Configuration.Processor()

configuration <- configurator[["Get.Config"]]()

sql <- Query::SQL()
sql.utilities <- Query::SQL.Utilities()
sql.functions <- Query::SQL.Functions()
Expand Down
52 changes: 44 additions & 8 deletions tests/testthat/test-ODBC.Configuration.Broker.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('When operations <- ODBC.Configuration.Broker()',{

describe("When configuration <- operation[['Get.Preset.Config']]()",{
it('then configuration is a list',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -47,6 +48,7 @@ describe("When configuration <- operation[['Get.Preset.Config']]()",{
configuration |> expect.list()
})
it('then configuration contains drv parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -57,6 +59,7 @@ describe("When configuration <- operation[['Get.Preset.Config']]()",{
configuration[['drv']] |> expect.exist()
})
it('then configuration contains dsn parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -67,6 +70,7 @@ describe("When configuration <- operation[['Get.Preset.Config']]()",{
configuration[['dsn']] |> expect.exist()
})
it('then configuration contains uid parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -77,6 +81,7 @@ describe("When configuration <- operation[['Get.Preset.Config']]()",{
configuration[['uid']] |> expect.exist()
})
it('then configuration contains pwd parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -87,57 +92,67 @@ describe("When configuration <- operation[['Get.Preset.Config']]()",{
configuration[['pwd']] |> expect.exist()
})
it('then an exception is thrown is no DSN found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No DSN environment variable not found in .Renviron Configuration file."

cache <- 'DSN' |> environment[['Get.Env.Variable']]()

# When
'DSN' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Preset.Config']]() |> expect.error(expect.error)

# Then
'DSN' |> environment[['Cache.Env.Variable']]('DSN')
'DSN' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no UID found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No UID environment variable not found in .Renviron Configuration file."

cache <- 'UID' |> environment[['Get.Env.Variable']]()

# When
'UID' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Preset.Config']]() |> expect.error(expect.error)

# Then
'UID' |> environment[['Cache.Env.Variable']]('UID')
'UID' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no PWD found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No PWD environment variable not found in .Renviron Configuration file."

cache <- 'PWD' |> environment[['Get.Env.Variable']]()

# When
'PWD' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Preset.Config']]() |> expect.error(expect.error)

# Then
'PWD' |> environment[['Cache.Env.Variable']]('PWD')
'PWD' |> environment[['Cache.Env.Variable']](cache)
})
})

describe("When configuration <- operation[['Get.Manual.Config']]()",{
it('then configuration is a list',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -148,6 +163,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration |> expect.list()
})
it('then configuration contains drv parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -158,6 +174,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['drv']] |> expect.exist()
})
it('then configuration contains driver parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -168,6 +185,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['driver']] |> expect.exist()
})
it('then configuration contains server parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -178,6 +196,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['server']] |> expect.exist()
})
it('then configuration contains database parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -188,6 +207,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['database']] |> expect.exist()
})
it('then configuration contains uid parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -198,6 +218,7 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['uid']] |> expect.exist()
})
it('then configuration contains pwd parameter',{
skip_if_not(environment == 'local')
# Given
operations <- ODBC.Configuration.Broker()

Expand All @@ -208,83 +229,98 @@ describe("When configuration <- operation[['Get.Manual.Config']]()",{
configuration[['pwd']] |> expect.exist()
})
it('then an exception is thrown is no DRIVER found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No DRIVER environment variable not found in .Renviron Configuration file."

cache <- 'DRIVER' |> environment[['Get.Env.Variable']]()

# When
'DRIVER' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Manual.Config']]() |> expect.error(expect.error)

# Then
'DRIVER' |> environment[['Cache.Env.Variable']]('DRIVER')
'DRIVER' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no SERVER found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No SERVER environment variable not found in .Renviron Configuration file."

cache <- 'SERVER' |> environment[['Get.Env.Variable']]()

# When
'SERVER' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Manual.Config']]() |> expect.error(expect.error)

# Then
'SERVER' |> environment[['Cache.Env.Variable']]('SERVER')
'SERVER' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no DATABASE found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No DATABASE environment variable not found in .Renviron Configuration file."

cache <- 'DATABASE' |> environment[['Get.Env.Variable']]()

# When
'DATABASE' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Manual.Config']]() |> expect.error(expect.error)

# Then
'DATABASE' |> environment[['Cache.Env.Variable']]('DATABASE')
'DATABASE' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no UID found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No UID environment variable not found in .Renviron Configuration file."

cache <- 'UID' |> environment[['Get.Env.Variable']]()

# When
'UID' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Manual.Config']]() |> expect.error(expect.error)

# Then
'UID' |> environment[['Cache.Env.Variable']]('UID')
'UID' |> environment[['Cache.Env.Variable']](cache)
})
it('then an exception is thrown is no PWD found in .Renviron',{
skip_if_not(environment == 'local')
# Given
environment <- Environment::Environment()
operation <- ODBC.Configuration.Broker()

expect.error <- "No PWD environment variable not found in .Renviron Configuration file."

cache <- 'PWD' |> environment[['Get.Env.Variable']]()

# When
'PWD' |> environment[['Clear.Env.Variable']]()

# Then
operation[['Get.Manual.Config']]() |> expect.error(expect.error)

# Then
'PWD' |> environment[['Cache.Env.Variable']]('PWD')
'PWD' |> environment[['Cache.Env.Variable']](cache)
})
})

0 comments on commit 0e448e2

Please sign in to comment.