Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Is.Existing.Table Validation in OBDC.Storage.Service #27

Merged
merged 34 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
71c9583
EXCEPTIONS: ODBC.Storage.Exceptions() ReturnsListContainingTable.Inva…
FlippieCoetser Oct 3, 2023
d7ff31b
EXCEPTIONS: ODBC.Storage.Exceptions() ReturnsListContainingTable.Inva…
FlippieCoetser Oct 3, 2023
7764604
EXCEPTIONS: input |> exception[['Table.Invalid']]() ThrowsExceptionIf…
FlippieCoetser Oct 3, 2023
262b8ac
EXCEPTIONS: input |> exception[['Table.Invalid']]() ThrowsExceptionIf…
FlippieCoetser Oct 3, 2023
9eee104
EXCEPTIONS: input |> exception[['Query']]() ThrowsExceptionIfInputCon…
FlippieCoetser Oct 3, 2023
4d38e6a
EXCEPTIONS: input |> exception[['Query']]() ThrowsExceptionIfInputCon…
FlippieCoetser Oct 3, 2023
ab7a1a7
BROKER: ODBC.Storage.Broker() ReturnsListContainingGet.TablesOperatio…
FlippieCoetser Oct 3, 2023
e325e39
BROKER: ODBC.Storage.Broker() ReturnsListContainingGet.TablesOperatio…
FlippieCoetser Oct 3, 2023
975e6c2
BROKER: operate[['Get.Tables']]() ReturnsVectorWithTablesNames -> FAIL
FlippieCoetser Oct 3, 2023
4c80e85
BROKER: operate[['Get.Tables']]() ReturnsVectorWithTablesNames -> PASS
FlippieCoetser Oct 3, 2023
225cc8f
VALIDATION: ODBC.Storage.Validator() ReturnsListContainingIs.Existing…
FlippieCoetser Oct 3, 2023
0a72a31
VALIDATION: ODBC.Storage.Validator() ReturnsListContainingIs.Existing…
FlippieCoetser Oct 3, 2023
e7f62d5
BROKER: operate[['Get.Tables']]() ReturnsDataFrame -> FAIL
FlippieCoetser Oct 3, 2023
326a06c
BROKER: operate[['Get.Tables']]() ReturnsDataFrame -> PASS
FlippieCoetser Oct 3, 2023
ba14893
CODE RUB: Update SQL Query
FlippieCoetser Oct 3, 2023
ed073a7
CODE RUB: Update SQL Query
FlippieCoetser Oct 3, 2023
0ffaf06
VALIDATION: table |> validate[['Is.Existing.Table']]() ThrowsExceptio…
FlippieCoetser Oct 3, 2023
0e25eed
VALIDATION: table |> validate[['Is.Existing.Table']]() ThrowsExceptio…
FlippieCoetser Oct 3, 2023
1c90bff
VALIDATION: table |> validate[['Is.Existing.Table']]() ThrowsExceptio…
FlippieCoetser Oct 3, 2023
468c168
SERVICE: entity |> service[['Add']](table) ThrowsExceptionIfTableIsIn…
FlippieCoetser Oct 3, 2023
8196c3e
SERVICE: entity |> service[['Add']](table) ThrowsExceptionIfTableIsIn…
FlippieCoetser Oct 3, 2023
4b812e0
SERVICE: table |> service[['Retrieve']]() ThrowsExceptionIfTableIsInv…
FlippieCoetser Oct 3, 2023
ca6631a
SERVICE: table |> service[['Retrieve']]() ThrowsExceptionIfTableIsInv…
FlippieCoetser Oct 3, 2023
251af77
SERVICE: id |> service[['RetrieveWhereId']](table) ThrowsExceptionIfT…
FlippieCoetser Oct 3, 2023
dba9239
SERVICE: id |> service[['RetrieveWhereId']](table) ThrowsExceptionIfT…
FlippieCoetser Oct 3, 2023
9a79c30
SERVICE: entity |> service[['Modify']](table) ThrowsExceptionIfTableI…
FlippieCoetser Oct 3, 2023
af1594d
SERVICE: entity |> service[['Modify']](table) ThrowsExceptionIfTableI…
FlippieCoetser Oct 3, 2023
bb2c1bb
SERVICE: id |> service[['Remove']](table) ThrowsExceptionIfTableIsInv…
FlippieCoetser Oct 3, 2023
857f768
SERVICE: id |> service[['Remove']](table) ThrowsExceptionIfTableIsInv…
FlippieCoetser Oct 3, 2023
14527e4
CODE RUB: Add Todo Notes
FlippieCoetser Oct 3, 2023
106e741
TESTING: Update Unit Test
FlippieCoetser Oct 5, 2023
0ba6496
TESTING: Update Unit Tests
FlippieCoetser Oct 5, 2023
877c37d
TESTING: Update Unit Tests
FlippieCoetser Oct 5, 2023
3462839
TESTING : Update Unit Tests
FlippieCoetser Oct 5, 2023
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
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)
})
})
Loading