- Implements `Field` type for by-field actions (**BREAKING**)
- Adds `RemoveFields*` functions/methods for removing fields from documents
This commit was merged in pull request #2.
This commit is contained in:
2024-01-23 21:23:24 -05:00
committed by GitHub
parent 68ad874256
commit 06daa4ea5c
19 changed files with 1701 additions and 291 deletions

View File

@@ -18,15 +18,23 @@ let unitTests =
("@test", Sql.jsonb """{"Something":"good"}""")
"JSON parameter not constructed correctly"
}
test "fieldParam succeeds" {
let it = fieldParam 242
Expect.equal (fst it) "@field" "Field parameter name not correct"
match snd it with
| SqlValue.Parameter value ->
Expect.equal value.ParameterName "@field" "Parameter name not correct"
Expect.equal value.Value 242 "Parameter value not correct"
| _ -> Expect.isTrue false "The parameter was not a Parameter type"
}
testList "addFieldParam" [
test "succeeds when a parameter is added" {
let paramList = addFieldParam "@field" (Field.EQ "it" "242") []
Expect.hasLength paramList 1 "There should have been a parameter added"
let it = paramList[0]
Expect.equal (fst it) "@field" "Field parameter name not correct"
match snd it with
| SqlValue.Parameter value ->
Expect.equal value.ParameterName "@field" "Parameter name not correct"
Expect.equal value.Value "242" "Parameter value not correct"
| _ -> Expect.isTrue false "The parameter was not a Parameter type"
}
test "succeeds when a parameter is not added" {
let paramList = addFieldParam "@field" (Field.EX "tacos") []
Expect.isEmpty paramList "There should not have been any parameters added"
}
]
test "noParams succeeds" {
Expect.isEmpty noParams "The no-params sequence should be empty"
}
@@ -110,7 +118,7 @@ let unitTests =
}
test "byField succeeds" {
Expect.equal
(Query.Patch.byField PostgresDb.TableName "Snail" LT)
(Query.Patch.byField PostgresDb.TableName (Field.LT "Snail" 0))
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Snail' < @field"
"UPDATE partial by ID statement not correct"
}
@@ -127,6 +135,32 @@ let unitTests =
"UPDATE partial by JSON Path statement not correct"
}
]
testList "RemoveFields" [
test "byId succeeds" {
Expect.equal
(Query.RemoveFields.byId "tbl")
"UPDATE tbl SET data = data - @name WHERE data ->> 'Id' = @id"
"Remove field by ID query not correct"
}
test "byField succeeds" {
Expect.equal
(Query.RemoveFields.byField "tbl" (Field.LT "Fly" 0))
"UPDATE tbl SET data = data - @name WHERE data ->> 'Fly' < @field"
"Remove field by field query not correct"
}
test "byContains succeeds" {
Expect.equal
(Query.RemoveFields.byContains "tbl")
"UPDATE tbl SET data = data - @name WHERE data @> @criteria"
"Remove field by contains query not correct"
}
test "byJsonPath succeeds" {
Expect.equal
(Query.RemoveFields.byJsonPath "tbl")
"UPDATE tbl SET data = data - @name WHERE data @? @path::jsonpath"
"Remove field by JSON path query not correct"
}
]
testList "Delete" [
test "byContains succeeds" {
Expect.equal (Query.Delete.byContains PostgresDb.TableName)
@@ -357,7 +391,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! theCount = Count.byField PostgresDb.TableName "Value" EQ "purple"
let! theCount = Count.byField PostgresDb.TableName (Field.EQ "Value" "purple")
Expect.equal theCount 2 "There should have been 2 matching documents"
}
testTask "byContains succeeds" {
@@ -397,14 +431,14 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! exists = Exists.byField PostgresDb.TableName "Sub" EX ""
let! exists = Exists.byField PostgresDb.TableName (Field.EX "Sub")
Expect.isTrue exists "There should have been existing documents"
}
testTask "succeeds when documents do not exist" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! exists = Exists.byField PostgresDb.TableName "NumValue" EQ "six"
let! exists = Exists.byField PostgresDb.TableName (Field.EQ "NumValue" "six")
Expect.isFalse exists "There should not have been existing documents"
}
]
@@ -486,14 +520,14 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! docs = Find.byField<JsonDocument> PostgresDb.TableName "Value" EQ "another"
let! docs = Find.byField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
Expect.equal (List.length docs) 1 "There should have been one document returned"
}
testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! docs = Find.byField<JsonDocument> PostgresDb.TableName "Value" EQ "mauve"
let! docs = Find.byField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "mauve")
Expect.isEmpty docs "There should have been no documents returned"
}
]
@@ -534,7 +568,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName "Value" EQ "another"
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
Expect.isSome doc "There should have been a document returned"
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
}
@@ -542,7 +576,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName "Value" EQ "purple"
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "purple")
Expect.isSome doc "There should have been a document returned"
Expect.contains [ "five"; "four" ] doc.Value.Id "An incorrect document was returned"
}
@@ -550,7 +584,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName "Value" EQ "absent"
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "absent")
Expect.isNone doc "There should not have been a document returned"
}
]
@@ -682,8 +716,8 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! Patch.byField PostgresDb.TableName "Value" EQ "purple" {| NumValue = 77 |}
let! after = Count.byField PostgresDb.TableName "NumValue" EQ "77"
do! Patch.byField PostgresDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |}
let! after = Count.byField PostgresDb.TableName (Field.EQ "NumValue" "77")
Expect.equal after 2 "There should have been 2 documents returned"
}
testTask "succeeds when no document is updated" {
@@ -693,7 +727,7 @@ let integrationTests =
Expect.equal before 0 "There should have been no documents returned"
// This not raising an exception is the test
do! Patch.byField PostgresDb.TableName "Value" EQ "burgundy" {| Foo = "green" |}
do! Patch.byField PostgresDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |}
}
]
testList "byContains" [
@@ -735,6 +769,148 @@ let integrationTests =
}
]
]
testList "RemoveFields" [
testList "byId" [
testTask "succeeds when multiple fields are removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub"; "Value" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 1 "There should be 1 document without Value fields"
}
testTask "succeeds when a single field is removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 0 "There should be no documents without Value fields"
}
testTask "succeeds when a field is not removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
// This not raising an exception is the test
do! RemoveFields.byId PostgresDb.TableName "two" [ "AFieldThatIsNotThere" ]
}
testTask "succeeds when no document is matched" {
use db = PostgresDb.BuildDb()
// This not raising an exception is the test
do! RemoveFields.byId PostgresDb.TableName "two" [ "Value" ]
}
]
testList "byField" [
testTask "succeeds when multiple fields are removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub"; "Value" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 1 "There should be 1 document without Value fields"
}
testTask "succeeds when a single field is removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 0 "There should be no documents without Value fields"
}
testTask "succeeds when a field is not removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
// This not raising an exception is the test
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Nothing" ]
}
testTask "succeeds when no document is matched" {
use db = PostgresDb.BuildDb()
// This not raising an exception is the test
do! RemoveFields.byField PostgresDb.TableName (Field.NE "Abracadabra" "apple") [ "Value" ]
}
]
testList "byContains" [
testTask "succeeds when multiple fields are removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub"; "Value" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 1 "There should be 1 document without Value fields"
}
testTask "succeeds when a single field is removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 0 "There should be no documents without Value fields"
}
testTask "succeeds when a field is not removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
// This not raising an exception is the test
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Nothing" ]
}
testTask "succeeds when no document is matched" {
use db = PostgresDb.BuildDb()
// This not raising an exception is the test
do! RemoveFields.byContains PostgresDb.TableName {| Abracadabra = "apple" |} [ "Value" ]
}
]
testList "byJsonPath" [
testTask "succeeds when multiple fields are removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub"; "Value" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 1 "There should be 1 document without Value fields"
}
testTask "succeeds when a single field is removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub" ]
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
Expect.equal noValue 0 "There should be no documents without Value fields"
}
testTask "succeeds when a field is not removed" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
// This not raising an exception is the test
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Nothing" ]
}
testTask "succeeds when no document is matched" {
use db = PostgresDb.BuildDb()
// This not raising an exception is the test
do! RemoveFields.byJsonPath PostgresDb.TableName "$.Abracadabra ? (@ == \"apple\")" [ "Value" ]
}
]
]
testList "Delete" [
testList "byId" [
testTask "succeeds when a document is deleted" {
@@ -759,7 +935,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! Delete.byField PostgresDb.TableName "Value" EQ "purple"
do! Delete.byField PostgresDb.TableName (Field.EQ "Value" "purple")
let! remaining = Count.all PostgresDb.TableName
Expect.equal remaining 3 "There should have been 3 documents remaining"
}
@@ -767,7 +943,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! Delete.byField PostgresDb.TableName "Value" EQ "crimson"
do! Delete.byField PostgresDb.TableName (Field.EQ "Value" "crimson")
let! remaining = Count.all PostgresDb.TableName
Expect.equal remaining 5 "There should have been 5 documents remaining"
}