Add Postgres Query byFields tests

This commit is contained in:
Daniel J. Summers 2024-08-08 22:36:48 -04:00
parent d8f64417e5
commit 85750e19f2
3 changed files with 134 additions and 8 deletions

View File

@ -118,6 +118,29 @@ public static class PostgresCSharpTests
Expect.isTrue(it[1].Item2.IsParameter, "Maximum field parameter value incorrect");
})
]),
#pragma warning restore CS0618
TestList("FieldNames",
[
TestCase("succeeds for one name", () =>
{
var (name, value) = Parameters.FieldNames(["bob"]);
Expect.equal(name, "@name", "The parameter name was incorrect");
if (!value.IsString)
{
Expect.isTrue(false, "The parameter was not a String type");
}
}),
TestCase("succeeds for multiple names", () =>
{
var (name, value) = Parameters.FieldNames(["bob", "tom", "mike"]);
Expect.equal(name, "@name", "The parameter name was incorrect");
if (!value.IsStringArray)
{
Expect.isTrue(false, "The parameter was not a StringArray type");
}
})
]),
#pragma warning disable CS0618
TestList("FieldName",
[
TestCase("succeeds for one name", () =>
@ -269,6 +292,14 @@ public static class PostgresCSharpTests
Expect.equal(Postgres.Query.Count.All(PostgresDb.TableName),
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName}", "Count query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.Count.ByFields("x", FieldMatch.All,
[Field.EQ("thatField", 0), Field.EQ("anotherField", 8)]),
$"SELECT COUNT(*) AS it FROM x WHERE data->>'thatField' = @field0 AND data->>'anotherField' = @field1",
"JSON field text comparison count query not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{
@ -298,6 +329,14 @@ public static class PostgresCSharpTests
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data->>'Id' = @id) AS it",
"ID existence query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.Exists.ByFields("q", FieldMatch.Any,
[Field.LT("Test", 0).WithParameterName("@a"), Field.LT("Unit", "x").WithParameterName("@b")]),
$"SELECT EXISTS (SELECT 1 FROM q WHERE data->>'Test' < @a OR data->>'Unit' < @b) AS it",
"JSON field text comparison exists query not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{
@ -327,6 +366,13 @@ public static class PostgresCSharpTests
$"SELECT data FROM {PostgresDb.TableName} WHERE data->>'Id' = @id",
"SELECT by ID query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.Find.ByFields("x", FieldMatch.Any, [Field.GE("Golf", 0), Field.LE("Flog", 1)]),
$"SELECT data FROM x WHERE data->>'Golf' >= @field0 OR data->>'Flog' <= @field1",
"SELECT by JSON comparison query not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{
@ -356,6 +402,14 @@ public static class PostgresCSharpTests
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data->>'Id' = @id",
"UPDATE partial by ID statement not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.Patch.ByFields("x", FieldMatch.All,
[Field.LT("Snail", 0), Field.BT("Slug", 8, 14)]),
$"UPDATE x SET data = data || @data WHERE data->>'Snail' < @field0 AND (data->>'Slug')::numeric BETWEEN @field1min AND @field1max",
"UPDATE partial by ID statement not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{
@ -385,6 +439,14 @@ public static class PostgresCSharpTests
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data->>'Id' = @id",
"Remove field by ID query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.RemoveFields.ByFields("x", FieldMatch.Any,
[Field.LT("Fly", 0), Field.LT("Ant", 2)]),
$"UPDATE x SET data = data - @name WHERE data->>'Fly' < @field0 OR data->>'Ant' < @field1",
"Remove field by field query not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{
@ -414,6 +476,13 @@ public static class PostgresCSharpTests
$"DELETE FROM {PostgresDb.TableName} WHERE data->>'Id' = @id",
"DELETE by ID query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(
Postgres.Query.Delete.ByFields("tbl", FieldMatch.All, [Field.NEX("gone"), Field.EX("here")]),
$"DELETE FROM tbl WHERE data->>'gone' IS NULL AND data->>'here' IS NOT NULL",
"DELETE by JSON comparison query not correct");
}),
#pragma warning disable CS0618
TestCase("ByField succeeds", () =>
{

View File

@ -113,6 +113,23 @@ let unitTests =
| _ -> Expect.isTrue false "Maximum parameter was not a Parameter type"
}
]
testList "fieldNameParams" [
test "succeeds for one name" {
let name, value = fieldNameParams [ "bob" ]
Expect.equal name "@name" "The parameter name was incorrect"
match value with
| SqlValue.String it -> Expect.equal it "bob" "The parameter value was incorrect"
| _ -> Expect.isTrue false "The parameter was not a String type"
}
test "succeeds for multiple names" {
let name, value = fieldNameParams [ "bob"; "tom"; "mike" ]
Expect.equal name "@name" "The parameter name was incorrect"
match value with
| SqlValue.StringArray it ->
Expect.equal it [| "bob"; "tom"; "mike" |] "The parameter value was incorrect"
| _ -> Expect.isTrue false "The parameter was not a StringArray type"
}
]
testList "fieldNameParam" [
test "succeeds for one name" {
let name, value = fieldNameParam [ "bob" ]
@ -248,6 +265,12 @@ let unitTests =
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName}"
"Count query not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.Count.byFields "tbl" All [ Field.EQ "thatField" 0; Field.EQ "anotherField" 8])
$"SELECT COUNT(*) AS it FROM tbl WHERE data->>'thatField' = @field0 AND data->>'anotherField' = @field1"
"JSON field text comparison count query not correct"
}
test "byField succeeds" {
Expect.equal
(Query.Count.byField PostgresDb.TableName (Field.EQ "thatField" 0))
@ -274,6 +297,14 @@ let unitTests =
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data->>'Id' = @id) AS it"
"ID existence query not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.Exists.byFields "tbl" Any
[ { Field.LT "Test" 0 with ParameterName = Some "@a" }
{ Field.GT "Unit" "x" with ParameterName = Some "@b" } ])
$"SELECT EXISTS (SELECT 1 FROM tbl WHERE data->>'Test' < @a OR data->>'Unit' > @b) AS it"
"JSON field text comparison exists query not correct"
}
test "byField succeeds" {
Expect.equal
(Query.Exists.byField PostgresDb.TableName (Field.LT "Test" 0))
@ -300,6 +331,12 @@ let unitTests =
$"SELECT data FROM {PostgresDb.TableName} WHERE data->>'Id' = @id"
"SELECT by ID query not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.Find.byFields "tbl" Any [ Field.GE "Golf" 0; Field.LE "Flog" 1 ])
$"SELECT data FROM tbl WHERE data->>'Golf' >= @field0 OR data->>'Flog' <= @field1"
"SELECT by JSON comparison query not correct"
}
test "byField succeeds" {
Expect.equal
(Query.Find.byField PostgresDb.TableName (Field.GE "Golf" 0))
@ -326,6 +363,12 @@ let unitTests =
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data->>'Id' = @id"
"UPDATE partial by ID statement not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.Patch.byFields "x" All [ Field.LT "Snail" 0; Field.BT "Slug" 8 12 ])
$"UPDATE x SET data = data || @data WHERE data->>'Snail' < @field0 AND (data->>'Slug')::numeric BETWEEN @field1min AND @field1max"
"UPDATE partial by ID statement not correct"
}
test "byField succeeds" {
Expect.equal
(Query.Patch.byField PostgresDb.TableName (Field.LT "Snail" 0))
@ -348,26 +391,32 @@ let unitTests =
testList "RemoveFields" [
test "byId succeeds" {
Expect.equal
(Query.RemoveFields.byId "tbl")
"UPDATE tbl SET data = data - @name WHERE data->>'Id' = @id"
(Query.RemoveFields.byId PostgresDb.TableName)
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data->>'Id' = @id"
"Remove field by ID query not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.RemoveFields.byFields "tbl" Any [ Field.LT "Fly" 0; Field.LT "Ant" 2 ])
"UPDATE tbl SET data = data - @name WHERE data->>'Fly' < @field0 OR data->>'Ant' < @field1"
"Remove field by field 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' < @field0"
(Query.RemoveFields.byField PostgresDb.TableName (Field.LT "Fly" 0))
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data->>'Fly' < @field0"
"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"
(Query.RemoveFields.byContains PostgresDb.TableName)
$"UPDATE {PostgresDb.TableName} 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"
(Query.RemoveFields.byJsonPath PostgresDb.TableName)
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data @? @path::jsonpath"
"Remove field by JSON path query not correct"
}
]
@ -378,6 +427,12 @@ let unitTests =
$"DELETE FROM {PostgresDb.TableName} WHERE data->>'Id' = @id"
"DELETE by ID query not correct"
}
test "byFields succeeds" {
Expect.equal
(Query.Delete.byFields PostgresDb.TableName All [ Field.NEX "gone"; Field.EX "here" ])
$"DELETE FROM {PostgresDb.TableName} WHERE data->>'gone' IS NULL AND data->>'here' IS NOT NULL"
"DELETE by JSON comparison query not correct"
}
test "byField succeeds" {
Expect.equal
(Query.Delete.byField PostgresDb.TableName (Field.NEX "gone"))

View File

@ -8,6 +8,8 @@ open Expecto
open Microsoft.Data.Sqlite
open Types
#nowarn "0044"
/// Integration tests for the F# extensions on the SqliteConnection data type
let integrationTests =
let loadDocs () = backgroundTask {