RC4 changes (#7)

- Add `In` and `InArray` comparisons
- Replace `Op` with `Comparison` (internal API, but was public)
- Spell out comparisons in `Field` constructor functions

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2024-09-17 02:33:57 +00:00
parent 3bc662c984
commit 168bf0cd14
19 changed files with 883 additions and 569 deletions

View File

@@ -15,42 +15,54 @@ open Types
/// Unit tests for the Query module of the SQLite library
let queryTests = testList "Query" [
testList "whereByFields" [
test "succeeds for a single field when a logical operator is passed" {
test "succeeds for a single field when a logical comparison is passed" {
Expect.equal
(Query.whereByFields Any [ { Field.GT "theField" 0 with ParameterName = Some "@test" } ])
(Query.whereByFields Any [ { Field.Greater "theField" 0 with ParameterName = Some "@test" } ])
"data->>'theField' > @test"
"WHERE clause not correct"
}
test "succeeds for a single field when an existence operator is passed" {
test "succeeds for a single field when an existence comparison is passed" {
Expect.equal
(Query.whereByFields Any [ Field.NEX "thatField" ])
(Query.whereByFields Any [ Field.NotExists "thatField" ])
"data->>'thatField' IS NULL"
"WHERE clause not correct"
}
test "succeeds for a single field when a between operator is passed" {
test "succeeds for a single field when a between comparison is passed" {
Expect.equal
(Query.whereByFields All [ { Field.BT "aField" 50 99 with ParameterName = Some "@range" } ])
(Query.whereByFields All [ { Field.Between "aField" 50 99 with ParameterName = Some "@range" } ])
"data->>'aField' BETWEEN @rangemin AND @rangemax"
"WHERE clause not correct"
}
test "succeeds for all multiple fields with logical operators" {
test "succeeds for all multiple fields with logical comparisons" {
Expect.equal
(Query.whereByFields All [ Field.EQ "theFirst" "1"; Field.EQ "numberTwo" "2" ])
(Query.whereByFields All [ Field.Equal "theFirst" "1"; Field.Equal "numberTwo" "2" ])
"data->>'theFirst' = @field0 AND data->>'numberTwo' = @field1"
"WHERE clause not correct"
}
test "succeeds for any multiple fields with an existence operator" {
test "succeeds for any multiple fields with an existence comparison" {
Expect.equal
(Query.whereByFields Any [ Field.NEX "thatField"; Field.GE "thisField" 18 ])
(Query.whereByFields Any [ Field.NotExists "thatField"; Field.GreaterOrEqual "thisField" 18 ])
"data->>'thatField' IS NULL OR data->>'thisField' >= @field0"
"WHERE clause not correct"
}
test "succeeds for all multiple fields with between operators" {
test "succeeds for all multiple fields with between comparisons" {
Expect.equal
(Query.whereByFields All [ Field.BT "aField" 50 99; Field.BT "anotherField" "a" "b" ])
(Query.whereByFields All [ Field.Between "aField" 50 99; Field.Between "anotherField" "a" "b" ])
"data->>'aField' BETWEEN @field0min AND @field0max AND data->>'anotherField' BETWEEN @field1min AND @field1max"
"WHERE clause not correct"
}
test "succeeds for a field with an In comparison" {
Expect.equal
(Query.whereByFields All [ Field.In "this" [ "a"; "b"; "c" ] ])
"data->>'this' IN (@field0_0, @field0_1, @field0_2)"
"WHERE clause not correct"
}
test "succeeds for a field with an InArray comparison" {
Expect.equal
(Query.whereByFields All [ Field.InArray "this" "the_table" [ "a"; "b" ] ])
"EXISTS (SELECT 1 FROM json_each(the_table.data, '$.this') WHERE value IN (@field0_0, @field0_1))"
"WHERE clause not correct"
}
]
test "whereById succeeds" {
Expect.equal (Query.whereById "@id") "data->>'Id' = @id" "WHERE clause not correct"
@@ -72,7 +84,7 @@ let queryTests = testList "Query" [
}
test "byFields succeeds" {
Expect.equal
(Query.byFields "unit" Any [ Field.GT "That" 14 ])
(Query.byFields "unit" Any [ Field.Greater "That" 14 ])
"unit WHERE data->>'That' > @field0"
"By-Field query not correct"
}
@@ -98,14 +110,14 @@ let parametersTests = testList "Parameters" [
}
testList "addFieldParam" [
test "succeeds when adding a parameter" {
let paramList = addFieldParam "@field" (Field.EQ "it" 99) []
let paramList = addFieldParam "@field" (Field.Equal "it" 99) []
Expect.hasLength paramList 1 "There should have been a parameter added"
let theParam = Seq.head paramList
Expect.equal theParam.ParameterName "@field" "The parameter name is incorrect"
Expect.equal theParam.Value 99 "The parameter value is incorrect"
}
test "succeeds when not adding a parameter" {
let paramList = addFieldParam "@it" (Field.NEX "Coffee") []
let paramList = addFieldParam "@it" (Field.NotExists "Coffee") []
Expect.isEmpty paramList "There should not have been any parameters added"
}
]
@@ -380,14 +392,14 @@ let countTests = testList "Count" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.BT "NumValue" 10 20 ]
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.Between "NumValue" 10 20 ]
Expect.equal theCount 3L "There should have been 3 matching documents"
}
testTask "succeeds for a non-numeric range" {
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.BT "Value" "aardvark" "apple" ]
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.Between "Value" "aardvark" "apple" ]
Expect.equal theCount 1L "There should have been 1 matching document"
}
]
@@ -416,14 +428,14 @@ let existsTests = testList "Exists" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 10 ]
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.Equal "NumValue" 10 ]
Expect.isTrue exists "There should have been existing documents"
}
testTask "succeeds when no matching documents exist" {
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.LT "Nothing" "none" ]
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.Less "Nothing" "none" ]
Expect.isFalse exists "There should not have been any existing documents"
}
]
@@ -508,16 +520,43 @@ let findTests = testList "Find" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.GT "NumValue" 15 ]
Expect.equal (List.length docs) 2 "There should have been two documents returned"
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ]
Expect.hasLength docs 2 "There should have been two documents returned"
}
testTask "succeeds when documents are found using IN with numeric field" {
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
Expect.hasLength docs 1 "There should have been one document returned"
}
testTask "succeeds when documents are not found" {
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.GT "NumValue" 100 ]
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.Greater "NumValue" 100 ]
Expect.isTrue (List.isEmpty docs) "There should have been no documents returned"
}
testTask "succeeds for InArray when matching documents exist" {
use! db = SqliteDb.BuildDb()
do! Definition.ensureTable SqliteDb.TableName
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
let! docs =
Find.byFields<ArrayDocument>
SqliteDb.TableName All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ]
Expect.hasLength docs 2 "There should have been two documents returned"
}
testTask "succeeds for InArray when no matching documents exist" {
use! db = SqliteDb.BuildDb()
do! Definition.ensureTable SqliteDb.TableName
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
let! docs =
Find.byFields<ArrayDocument>
SqliteDb.TableName All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
Expect.isEmpty docs "There should have been no documents returned"
}
]
testList "byFieldsOrdered" [
testTask "succeeds when sorting ascending" {
@@ -526,7 +565,7 @@ let findTests = testList "Find" [
let! docs =
Find.byFieldsOrdered<JsonDocument>
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id" ]
SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
Expect.hasLength docs 2 "There should have been two documents returned"
Expect.equal
(docs |> List.map _.Id |> String.concat "|") "five|four" "The documents were not ordered correctly"
@@ -537,7 +576,7 @@ let findTests = testList "Find" [
let! docs =
Find.byFieldsOrdered<JsonDocument>
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id DESC" ]
SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
Expect.hasLength docs 2 "There should have been two documents returned"
Expect.equal
(docs |> List.map _.Id |> String.concat "|") "four|five" "The documents were not ordered correctly"
@@ -548,7 +587,7 @@ let findTests = testList "Find" [
let! docs =
Find.byFieldsOrdered<JsonDocument>
SqliteDb.TableName All [ Field.LE "NumValue" 10 ] [ Field.Named "Value" ]
SqliteDb.TableName All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ]
Expect.hasLength docs 3 "There should have been three documents returned"
Expect.equal
(docs |> List.map _.Id |> String.concat "|") "three|one|two" "Documents not ordered correctly"
@@ -559,7 +598,7 @@ let findTests = testList "Find" [
let! docs =
Find.byFieldsOrdered<JsonDocument>
SqliteDb.TableName All [ Field.LE "NumValue" 10 ] [ Field.Named "i:Value" ]
SqliteDb.TableName All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ]
Expect.hasLength docs 3 "There should have been three documents returned"
Expect.equal
(docs |> List.map _.Id |> String.concat "|") "three|two|one" "Documents not ordered correctly"
@@ -570,7 +609,7 @@ let findTests = testList "Find" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "another" ]
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Value" "another" ]
Expect.isSome doc "There should have been a document returned"
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
}
@@ -578,7 +617,7 @@ let findTests = testList "Find" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ]
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ]
Expect.isSome doc "There should have been a document returned"
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
}
@@ -586,7 +625,7 @@ let findTests = testList "Find" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "absent" ]
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Value" "absent" ]
Expect.isNone doc "There should not have been a document returned"
}
]
@@ -597,7 +636,7 @@ let findTests = testList "Find" [
let! doc =
Find.firstByFieldsOrdered<JsonDocument>
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
Expect.isSome doc "There should have been a document returned"
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
}
@@ -607,7 +646,7 @@ let findTests = testList "Find" [
let! doc =
Find.firstByFieldsOrdered<JsonDocument>
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
Expect.isSome doc "There should have been a document returned"
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
}
@@ -690,8 +729,8 @@ let patchTests = testList "Patch" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
do! Patch.byFields SqliteDb.TableName Any [ Field.EQ "Value" "purple" ] {| NumValue = 77 |}
let! after = Count.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 77 ]
do! Patch.byFields SqliteDb.TableName Any [ Field.Equal "Value" "purple" ] {| NumValue = 77 |}
let! after = Count.byFields SqliteDb.TableName Any [ Field.Equal "NumValue" 77 ]
Expect.equal after 2L "There should have been 2 documents returned"
}
testTask "succeeds when no document is updated" {
@@ -701,7 +740,7 @@ let patchTests = testList "Patch" [
Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test
do! Patch.byFields SqliteDb.TableName Any [ Field.EQ "Value" "burgundy" ] {| Foo = "green" |}
do! Patch.byFields SqliteDb.TableName Any [ Field.Equal "Value" "burgundy" ] {| Foo = "green" |}
}
]
]
@@ -740,7 +779,7 @@ let removeFieldsTests = testList "RemoveFields" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Sub" ]
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Sub" ]
try
let! _ = Find.byId<string, JsonDocument> SqliteDb.TableName "four"
Expect.isTrue false "The updated document should have failed to parse"
@@ -753,13 +792,13 @@ let removeFieldsTests = testList "RemoveFields" [
do! loadDocs ()
// This not raising an exception is the test
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Nothing" ]
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Nothing" ]
}
testTask "succeeds when no document is matched" {
use! db = SqliteDb.BuildDb()
// This not raising an exception is the test
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.NE "Abracadabra" "apple" ] [ "Value" ]
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.NotEqual "Abracadabra" "apple" ] [ "Value" ]
}
]
]
@@ -789,7 +828,7 @@ let deleteTests = testList "Delete" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
do! Delete.byFields SqliteDb.TableName Any [ Field.NE "Value" "purple" ]
do! Delete.byFields SqliteDb.TableName Any [ Field.NotEqual "Value" "purple" ]
let! remaining = Count.all SqliteDb.TableName
Expect.equal remaining 2L "There should have been 2 documents remaining"
}
@@ -797,7 +836,7 @@ let deleteTests = testList "Delete" [
use! db = SqliteDb.BuildDb()
do! loadDocs ()
do! Delete.byFields SqliteDb.TableName Any [ Field.EQ "Value" "crimson" ]
do! Delete.byFields SqliteDb.TableName Any [ Field.Equal "Value" "crimson" ]
let! remaining = Count.all SqliteDb.TableName
Expect.equal remaining 5L "There should have been 5 documents remaining"
}