Skip to content

Commit

Permalink
Clean Unit Tests
Browse files Browse the repository at this point in the history
Clean Unit Tests
  • Loading branch information
FlippieCoetser committed Oct 3, 2023
2 parents 3fac406 + 493c673 commit 8c1da5b
Show file tree
Hide file tree
Showing 25 changed files with 589 additions and 434 deletions.
4 changes: 2 additions & 2 deletions R/Memory.Storage.Broker.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Memory.Storage.Broker <- \(configuration = NULL) {
tables[[table]] <<- tables[[table]] |> rbind(entity)
return(data.frame())
}
operations[['Select']] <- \(table, fields = NULL) {
operations[['Select']] <- \(table, fields = '*') {
tables[[table]]
}
operations[['SelectWhereId']] <- \(id, table, fields = NULL) {
operations[['SelectWhereId']] <- \(id, table, fields = '*') {
tables[[table]][tables[[table]][['Id']] == id,]
}
operations[['Update']] <- \(entity, table) {
Expand Down
12 changes: 6 additions & 6 deletions R/Memory.Storage.Exceptions.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Memory.Storage.Exceptions <- \() {
exceptions <- list()
exceptions[['No.Execute.Query']] <- \(invoke) {
exceptions[['Not.Implemented']] <- \(invoke) {
if(invoke) {
stop('Memory Storage Provider Error: Execute.Query not implemented.', call. = FALSE)
stop('Memory.Storage: Not.Implemented.', call. = FALSE)
}
}
exceptions[['Key.Duplicate']] <- \(invoke) {
exceptions[['Key.Violation']] <- \(invoke) {
if(invoke) {
stop('Memory Storage Provider Error: Duplicate Id not allowed.', call. = FALSE)
stop('Memory.Storage: Key.Violation: Duplicate Primary Key not allowed.', call. = FALSE)
}
}
exceptions[['Entity.Not.Found']] <- \(invoke) {
if(invoke) {
stop('Memory Storage Provider Error: Entity not found.', call. = FALSE)
stop('Memory.Storage: Entity.Not.Found: Entity not found in storage.', call. = FALSE)
}
}
exceptions[['Table.Invalid']] <- \(invoke, table) {
if(invoke) {
stop('Memory Storage Provider Error: ', table, ' is not a valid table.', call. = FALSE)
stop('Memory.Storage: Table.Invalid: ', table, ' is not a valid table.', call. = FALSE)
}
}
return(exceptions)
Expand Down
15 changes: 8 additions & 7 deletions R/Memory.Storage.Service.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ Memory.Storage.Service <- \(broker) {
data |> broker[['Seed.Table']](table)
}
services[['Execute.Query']] <- \(...) {
TRUE |> validate[['NoImplementation']]()
TRUE |> validate[['Not.Implemented']]()
... |> broker[['Execute.Query']]()
}
services[['Add']] <- \(entity, table) {
entity |> validate[['Entity']]()
table |> validate[['Table']]()

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

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

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

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

table |> broker[['Select']](fields)
}
services[['RetrieveWhereId']] <- \(id, table, fields) {
Expand All @@ -39,15 +40,15 @@ Memory.Storage.Service <- \(broker) {

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

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

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

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

entity |> broker[['Update']](table)
}
services[['Remove']] <- \(id, table) {
Expand All @@ -56,7 +57,7 @@ Memory.Storage.Service <- \(broker) {

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

id |> broker[['Delete']](table)
id |> broker[['Delete']](table)
}
return(services)
}
16 changes: 10 additions & 6 deletions R/Memory.Storage.Validator.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ Memory.Storage.Validator <- \(broker = NULL) {
validators[['Is.Character']]() |>
validators[['Is.UUID']]('id')
}
validators[['NoImplementation']] <- \(input) {
input |> exception[['No.Execute.Query']]()
validators[['Not.Implemented']] <- \(input) {
input |> exception[['Not.Implemented']]()
}
validators[['Is.New.Entity']] <- \(entity, table) {
match.count <- entity[['Id']] |> broker[['SelectWhereId']](table) |> nrow()
(match.count != 0) |> exception[['Key.Duplicate']]()
tryCatch(
entity[['Id']] |> broker[['SelectWhereId']](table) |> validators[['Is.Empty']](),
error=\(...) TRUE |> exception[['Key.Violation']]()
)
return(entity)
}
validators[['Is.Existing.Entity']] <- \(entity, table) {
match.count <- entity[['Id']] |> broker[['SelectWhereId']](table) |> nrow()
(match.count == 0) |> exception[['Entity.Not.Found']]()
tryCatch(
entity[['Id']] |> broker[['SelectWhereId']](table) |> validators[['Has.One.Row']](),
error=\(...) TRUE |> exception[['Entity.Not.Found']]()
)
return(entity)
}
validators[['Is.Existing.Table']] <- \(table) {
Expand Down
4 changes: 2 additions & 2 deletions R/ODBC.Storage.Broker.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ ODBC.Storage.Broker <- \(configuration, sql = Query::SQL()) {
sql[['VALUES']](entity) |>
operations[['Execute.Query']]()
}
operations[['Select']] <- \(table, fields) {
operations[['Select']] <- \(table, fields = '*') {
fields |>
sql[['SELECT']]() |>
sql[['FROM']](table) |>
operations[['Execute.Query']]()
}
operations[['SelectWhereId']] <- \(id, table, fields) {
operations[['SelectWhereId']] <- \(id, table, fields = '*') {
fields |>
sql[['SELECT']]() |>
sql[['FROM']](table) |>
Expand Down
6 changes: 3 additions & 3 deletions R/ODBC.Storage.Exceptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ODBC.Storage.Exceptions <- \() {
}
exceptions[["Query"]] <- \(error) {
'Violation of PRIMARY KEY constraint' |>
grepl(error) |> exceptions[["Key.Duplicate"]]()
grepl(error) |> exceptions[["Key.Violation"]]()

'Cannot insert the value NULL into column' |>
grepl(error) |> exceptions[["Value.NULL"]]()
Expand All @@ -59,9 +59,9 @@ ODBC.Storage.Exceptions <- \() {

stop(error, call. = FALSE)
}
exceptions[["Key.Duplicate"]] <- \(invoke) {
exceptions[["Key.Violation"]] <- \(invoke) {
if (invoke) {
stop("SQL Error: Duplicate Id not allowed", call. = FALSE)
stop("ODBC.Storage: Key.Violation: Duplicate Primary Key not allowed.", call. = FALSE)
}
}
exceptions[['Value.NULL']] <- \(invoke) {
Expand Down
43 changes: 21 additions & 22 deletions tests/testthat/test-Memory.Storage.Broker.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
describe("Memory.Storage.Broker",{
it("Exist",{
describe('Memory.Storage.Broker',{
it('Exist',{
Memory.Storage.Broker |> expect.exist()
})
})

describe("When operations <- configuration |> Memory.Storage.Broker()",{
it("then operations is a list",{
describe('When operations <- configuration |> Memory.Storage.Broker()',{
it('then operations is a list',{
# When
operations <- Memory.Storage.Broker()

Expand All @@ -19,56 +19,56 @@ describe("When operations <- configuration |> Memory.Storage.Broker()",{
# Then
operations[['Create.Table']] |> expect.exist()
})
it("then operations contains Seed.Table operation",{
it('then operations contains Seed.Table operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Seed.Table']] |> expect.exist()
})
it("then operations contains Get.Tables operation",{
it('then operations contains Get.Tables operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Get.Tables']] |> expect.exist()
})
it("then operations contains Execute.Query operation",{
it('then operations contains Execute.Query operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Execute.Query']] |> expect.exist()
})
it("then operations contains Insert operation",{
it('then operations contains Insert operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Insert']] |> expect.exist()
})
it("then operations contains Select operation",{
it('then operations contains Select operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Select']] |> expect.exist()
})
it("then operations contains SelectWhereId operation",{
it('then operations contains SelectWhereId operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['SelectWhereId']] |> expect.exist()
})
it("then operations contains Update operation",{
it('then operations contains Update operation',{
# When
operations <- Memory.Storage.Broker()

# Then
operations[['Update']] |> expect.exist()
})
it("then operations contains Delete operation",{
it('then operations contains Delete operation',{
# When
operations <- Memory.Storage.Broker()

Expand All @@ -78,7 +78,7 @@ describe("When operations <- configuration |> Memory.Storage.Broker()",{
})

describe("When model |> operation[['Create.Table']](table)",{
it("then table is created in memory",{
it('then table is created in memory',{
# Given
operation <- Memory.Storage.Broker()

Expand All @@ -87,7 +87,6 @@ describe("When model |> operation[['Create.Table']](table)",{
Task = character(0),
Status = character(0)
)

table <- 'Todo'

# When
Expand All @@ -99,7 +98,7 @@ describe("When model |> operation[['Create.Table']](table)",{
})

describe("when entities |> operation[['Seed.Table']](table)",{
it("then entities are inserted into table in memory",{
it('then entities are inserted into table in memory',{
# Given
operation <- configuration |> Memory.Storage.Broker()

Expand All @@ -118,7 +117,7 @@ describe("when entities |> operation[['Seed.Table']](table)",{
})

describe("when operation[['Get.Tables']]()",{
it("then returns data.frame with table names if data in memory has one or more table",{
it('then returns data.frame with table names if data in memory has one or more table',{
# Given
operation <- Memory.Storage.Broker()
Todo.Mock.Data |> operation[['Seed.Table']]('Todo')
Expand All @@ -134,7 +133,7 @@ describe("when operation[['Get.Tables']]()",{
})

describe("when query |> operation[['Execute.Query']]()",{
it("then an empty data frame is returned",{
it('then an empty data frame is returned',{
# Given
operation <- Memory.Storage.Broker()

Expand All @@ -148,7 +147,7 @@ describe("when query |> operation[['Execute.Query']]()",{
})

describe("when entity |> operation[['Insert']](table)",{
it("then entity is inserted into memory data",{
it('then entity is inserted into memory data',{
# Given
configuration <- data.frame()

Expand Down Expand Up @@ -176,7 +175,7 @@ describe("when entity |> operation[['Insert']](table)",{
})

describe("when table |> operation[['Select']]()",{
it("then all entities are returned from memory data",{
it('then all entities are returned from memory data',{
# Given
configuration <- data.frame()

Expand All @@ -194,7 +193,7 @@ describe("when table |> operation[['Select']]()",{
})

describe("when id |> operation[['SelectWhereId']](table)",{
it("then entity is returned from memory data",{
it('then entity is returned from memory data',{
# Given
configuration <- data.frame()

Expand All @@ -215,7 +214,7 @@ describe("when id |> operation[['SelectWhereId']](table)",{
})

describe("when entity |> operation[['Update']](table)",{
it("then entity is updated in table in memory data",{
it('then entity is updated in table in memory data',{
# Given
configuration <- data.frame()

Expand Down Expand Up @@ -247,7 +246,7 @@ describe("when entity |> operation[['Update']](table)",{
})

describe("when id |> operation[['Delete']](table)",{
it("then entity with matching id is deleted from table in memory data if exist",{
it('then entity with matching id is deleted from table in memory data if exist',{
# Given
configuration <- data.frame()

Expand Down
Loading

0 comments on commit 8c1da5b

Please sign in to comment.