From f2836bd759cb1e1c340ca4a5249b80cce666a041 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 23 Jan 2024 12:03:01 -0500 Subject: [PATCH] Implement Field throughout - Do not add a parameter for EX/NEX --- src/Common/Library.fs | 43 +++++----- src/Postgres/Extensions.fs | 48 +++++------ src/Postgres/Library.fs | 84 ++++++++++--------- src/Sqlite/Library.fs | 56 ++++++------- src/Tests.CSharp/CommonCSharpTests.cs | 12 +-- .../PostgresCSharpExtensionTests.cs | 26 +++--- src/Tests.CSharp/PostgresCSharpTests.cs | 46 ++++++---- src/Tests.CSharp/SqliteCSharpTests.cs | 15 +++- src/Tests/CommonTests.fs | 12 +-- src/Tests/PostgresExtensionTests.fs | 26 +++--- src/Tests/PostgresTests.fs | 54 +++++++----- src/Tests/SqliteTests.fs | 22 +++-- 12 files changed, 237 insertions(+), 207 deletions(-) diff --git a/src/Common/Library.fs b/src/Common/Library.fs index 47caade..56b6d1b 100644 --- a/src/Common/Library.fs +++ b/src/Common/Library.fs @@ -44,35 +44,35 @@ type Field = { Value: obj } with - /// Create an equals (=) field criteria + /// Create an equals (=) field criterion static member EQ name (value: obj) = { Name = name; Op = EQ; Value = value } - /// Create a greater than (>) field criteria + /// Create a greater than (>) field criterion static member GT name (value: obj) = { Name = name; Op = GT; Value = value } - /// Create a greater than or equal to (>=) field criteria + /// Create a greater than or equal to (>=) field criterion static member GE name (value: obj) = { Name = name; Op = GE; Value = value } - /// Create a less than (<) field criteria + /// Create a less than (<) field criterion static member LT name (value: obj) = { Name = name; Op = LT; Value = value } - /// Create a less than or equal to (<=) field criteria + /// Create a less than or equal to (<=) field criterion static member LE name (value: obj) = { Name = name; Op = LE; Value = value } - /// Create a not equals (<>) field criteria + /// Create a not equals (<>) field criterion static member NE name (value: obj) = { Name = name; Op = NE; Value = value } - /// Create an exists (IS NOT NULL) field criteria + /// Create an exists (IS NOT NULL) field criterion static member EX name = { Name = name; Op = EX; Value = obj () } - /// Create an not exists (IS NULL) field criteria + /// Create an not exists (IS NULL) field criterion static member NEX name = { Name = name; Op = NEX; Value = obj () } @@ -152,17 +152,14 @@ module Query = /// Create a WHERE clause fragment to implement a comparison on a field in a JSON document [] - let whereByField fieldName op paramName = - let theRest = - match op with - | EX | NEX -> string op - | _ -> $"{op} %s{paramName}" - $"data ->> '%s{fieldName}' {theRest}" + let whereByField field paramName = + let theRest = match field.Op with EX | NEX -> string field.Op | _ -> $"{field.Op} %s{paramName}" + $"data ->> '%s{field.Name}' {theRest}" /// Create a WHERE clause fragment to implement an ID-based query [] let whereById paramName = - whereByField (Configuration.idField ()) EQ paramName + whereByField (Field.EQ (Configuration.idField ()) 0) paramName /// Queries to define tables and indexes module Definition = @@ -223,8 +220,8 @@ module Query = /// Query to count matching documents using a text comparison on a JSON field [] - let byField tableName fieldName op = - $"""SELECT COUNT(*) AS it FROM %s{tableName} WHERE {whereByField fieldName op "@field"}""" + let byField tableName field = + $"""SELECT COUNT(*) AS it FROM %s{tableName} WHERE {whereByField field "@field"}""" /// Queries for determining document existence module Exists = @@ -236,8 +233,8 @@ module Query = /// Query to determine if documents exist using a comparison on a JSON field [] - let byField tableName fieldName op = - $"""SELECT EXISTS (SELECT 1 FROM %s{tableName} WHERE {whereByField fieldName op "@field"}) AS it""" + let byField tableName field = + $"""SELECT EXISTS (SELECT 1 FROM %s{tableName} WHERE {whereByField field "@field"}) AS it""" /// Queries for retrieving documents module Find = @@ -249,8 +246,8 @@ module Query = /// Query to retrieve documents using a comparison on a JSON field [] - let byField tableName fieldName op = - $"""{selectFromTable tableName} WHERE {whereByField fieldName op "@field"}""" + let byField tableName field = + $"""{selectFromTable tableName} WHERE {whereByField field "@field"}""" /// Queries to delete documents module Delete = @@ -262,5 +259,5 @@ module Query = /// Query to delete documents using a comparison on a JSON field [] - let byField tableName fieldName op = - $"""DELETE FROM %s{tableName} WHERE {whereByField fieldName op "@field"}""" + let byField tableName field = + $"""DELETE FROM %s{tableName} WHERE {whereByField field "@field"}""" diff --git a/src/Postgres/Extensions.fs b/src/Postgres/Extensions.fs index 4602341..9b89b84 100644 --- a/src/Postgres/Extensions.fs +++ b/src/Postgres/Extensions.fs @@ -50,8 +50,8 @@ module Extensions = WithProps.Count.all tableName (Sql.existingConnection conn) /// Count matching documents using a JSON field comparison query (->> =) - member conn.countByField tableName fieldName op (value: obj) = - WithProps.Count.byField tableName fieldName op value (Sql.existingConnection conn) + member conn.countByField tableName field = + WithProps.Count.byField tableName field (Sql.existingConnection conn) /// Count matching documents using a JSON containment query (@>) member conn.countByContains tableName criteria = @@ -66,8 +66,8 @@ module Extensions = WithProps.Exists.byId tableName docId (Sql.existingConnection conn) /// Determine if documents exist using a JSON field comparison query (->> =) - member conn.existsByField tableName fieldName op (value: obj) = - WithProps.Exists.byField tableName fieldName op value (Sql.existingConnection conn) + member conn.existsByField tableName field = + WithProps.Exists.byField tableName field (Sql.existingConnection conn) /// Determine if documents exist using a JSON containment query (@>) member conn.existsByContains tableName criteria = @@ -86,8 +86,8 @@ module Extensions = WithProps.Find.byId<'TKey, 'TDoc> tableName docId (Sql.existingConnection conn) /// Retrieve documents matching a JSON field comparison query (->> =) - member conn.findByField<'TDoc> tableName fieldName op (value: obj) = - WithProps.Find.byField<'TDoc> tableName fieldName op value (Sql.existingConnection conn) + member conn.findByField<'TDoc> tableName field = + WithProps.Find.byField<'TDoc> tableName field (Sql.existingConnection conn) /// Retrieve documents matching a JSON containment query (@>) member conn.findByContains<'TDoc> tableName (criteria: obj) = @@ -98,8 +98,8 @@ module Extensions = WithProps.Find.byJsonPath<'TDoc> tableName jsonPath (Sql.existingConnection conn) /// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found - member conn.findFirstByField<'TDoc> tableName fieldName op (value: obj) = - WithProps.Find.firstByField<'TDoc> tableName fieldName op value (Sql.existingConnection conn) + member conn.findFirstByField<'TDoc> tableName field = + WithProps.Find.firstByField<'TDoc> tableName field (Sql.existingConnection conn) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found member conn.findFirstByContains<'TDoc> tableName (criteria: obj) = @@ -122,8 +122,8 @@ module Extensions = WithProps.Patch.byId tableName docId patch (Sql.existingConnection conn) /// Patch documents using a JSON field comparison query in the WHERE clause (->> =) - member conn.patchByField tableName fieldName op (value: obj) (patch: 'TPatch) = - WithProps.Patch.byField tableName fieldName op value patch (Sql.existingConnection conn) + member conn.patchByField tableName field (patch: 'TPatch) = + WithProps.Patch.byField tableName field patch (Sql.existingConnection conn) /// Patch documents using a JSON containment query in the WHERE clause (@>) member conn.patchByContains tableName (criteria: 'TCriteria) (patch: 'TPatch) = @@ -138,8 +138,8 @@ module Extensions = WithProps.Delete.byId tableName docId (Sql.existingConnection conn) /// Delete documents by matching a JSON field comparison query (->> =) - member conn.deleteByField tableName fieldName op (value: obj) = - WithProps.Delete.byField tableName fieldName op value (Sql.existingConnection conn) + member conn.deleteByField tableName field = + WithProps.Delete.byField tableName field (Sql.existingConnection conn) /// Delete documents by matching a JSON containment query (@>) member conn.deleteByContains tableName (criteria: 'TContains) = @@ -209,8 +209,8 @@ type NpgsqlConnectionCSharpExtensions = /// Count matching documents using a JSON field comparison query (->> =) [] - static member inline CountByField(conn, tableName, fieldName, op, value: obj) = - WithProps.Count.byField tableName fieldName op value (Sql.existingConnection conn) + static member inline CountByField(conn, tableName, field) = + WithProps.Count.byField tableName field (Sql.existingConnection conn) /// Count matching documents using a JSON containment query (@>) [] @@ -229,8 +229,8 @@ type NpgsqlConnectionCSharpExtensions = /// Determine if documents exist using a JSON field comparison query (->> =) [] - static member inline ExistsByField(conn, tableName, fieldName, op, value: obj) = - WithProps.Exists.byField tableName fieldName op value (Sql.existingConnection conn) + static member inline ExistsByField(conn, tableName, field) = + WithProps.Exists.byField tableName field (Sql.existingConnection conn) /// Determine if documents exist using a JSON containment query (@>) [] @@ -254,8 +254,8 @@ type NpgsqlConnectionCSharpExtensions = /// Retrieve documents matching a JSON field comparison query (->> =) [] - static member inline FindByField<'TDoc>(conn, tableName, fieldName, op, value: obj) = - WithProps.Find.ByField<'TDoc>(tableName, fieldName, op, value, Sql.existingConnection conn) + static member inline FindByField<'TDoc>(conn, tableName, field) = + WithProps.Find.ByField<'TDoc>(tableName, field, Sql.existingConnection conn) /// Retrieve documents matching a JSON containment query (@>) [] @@ -269,8 +269,8 @@ type NpgsqlConnectionCSharpExtensions = /// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found [] - static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, fieldName, op, value: obj) = - WithProps.Find.FirstByField<'TDoc>(tableName, fieldName, op, value, Sql.existingConnection conn) + static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, field) = + WithProps.Find.FirstByField<'TDoc>(tableName, field, Sql.existingConnection conn) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found [] @@ -299,8 +299,8 @@ type NpgsqlConnectionCSharpExtensions = /// Patch documents using a JSON field comparison query in the WHERE clause (->> =) [] - static member inline PatchByField(conn, tableName, fieldName, op, value: obj, patch: 'TPatch) = - WithProps.Patch.byField tableName fieldName op value patch (Sql.existingConnection conn) + static member inline PatchByField(conn, tableName, field, patch: 'TPatch) = + WithProps.Patch.byField tableName field patch (Sql.existingConnection conn) /// Patch documents using a JSON containment query in the WHERE clause (@>) [] @@ -319,8 +319,8 @@ type NpgsqlConnectionCSharpExtensions = /// Delete documents by matching a JSON field comparison query (->> =) [] - static member inline DeleteByField(conn, tableName, fieldName, op, value: obj) = - WithProps.Delete.byField tableName fieldName op value (Sql.existingConnection conn) + static member inline DeleteByField(conn, tableName, field) = + WithProps.Delete.byField tableName field (Sql.existingConnection conn) /// Delete documents by matching a JSON containment query (@>) [] diff --git a/src/Postgres/Library.fs b/src/Postgres/Library.fs index 5fcb7cc..bb496ee 100644 --- a/src/Postgres/Library.fs +++ b/src/Postgres/Library.fs @@ -64,9 +64,18 @@ module Parameters = name, Sql.jsonb (Configuration.serializer().Serialize it) /// Create a JSON field parameter (name "@field") - [] - let fieldParam (value: obj) = - "@field", Sql.parameter (NpgsqlParameter("@field", value)) + [] + let addFieldParam field parameters = + match field.Op with + | EX | NEX -> parameters + | _ -> ("@field", Sql.parameter (NpgsqlParameter("@field", field.Value))) :: parameters + + /// Create a JSON field parameter (name "@field") + let AddField field parameters = + match field.Op with + | EX | NEX -> parameters + | _ -> + ("@field", Sql.parameter (NpgsqlParameter("@field", field.Value))) |> Seq.singleton |> Seq.append parameters /// An empty parameter sequence [] @@ -152,8 +161,8 @@ module Query = /// Query to patch documents match a JSON field comparison (->> =) [] - let byField tableName fieldName op = - $"""UPDATE %s{tableName} SET data = data || @data WHERE {Query.whereByField fieldName op "@field"}""" + let byField tableName field = + $"""UPDATE %s{tableName} SET data = data || @data WHERE {Query.whereByField field "@field"}""" /// Query to patch documents matching a JSON containment query (@>) [] @@ -302,8 +311,8 @@ module WithProps = /// Count matching documents using a JSON field comparison (->> =) [] - let byField tableName fieldName op (value: obj) sqlProps = - Custom.scalar (Query.Count.byField tableName fieldName op) [ fieldParam value ] toCount sqlProps + let byField tableName field sqlProps = + Custom.scalar (Query.Count.byField tableName field) (addFieldParam field []) toCount sqlProps /// Count matching documents using a JSON containment query (@>) [] @@ -326,8 +335,8 @@ module WithProps = /// Determine if a document exists using a JSON field comparison (->> =) [] - let byField tableName fieldName op (value: obj) sqlProps = - Custom.scalar (Query.Exists.byField tableName fieldName op) [ fieldParam value ] toExists sqlProps + let byField tableName field sqlProps = + Custom.scalar (Query.Exists.byField tableName field) (addFieldParam field []) toExists sqlProps /// Determine if a document exists using a JSON containment query (@>) [] @@ -363,13 +372,12 @@ module WithProps = /// Retrieve documents matching a JSON field comparison (->> =) [] - let byField<'TDoc> tableName fieldName op (value: obj) sqlProps = - Custom.list<'TDoc> (Query.Find.byField tableName fieldName op) [ fieldParam value ] fromData<'TDoc> sqlProps + let byField<'TDoc> tableName field sqlProps = + Custom.list<'TDoc> (Query.Find.byField tableName field) (addFieldParam field []) fromData<'TDoc> sqlProps /// Retrieve documents matching a JSON field comparison (->> =) - let ByField<'TDoc>(tableName, fieldName, op, value: obj, sqlProps) = - Custom.List<'TDoc>( - Query.Find.byField tableName fieldName op, [ fieldParam value ], fromData<'TDoc>, sqlProps) + let ByField<'TDoc>(tableName, field, sqlProps) = + Custom.List<'TDoc>(Query.Find.byField tableName field, addFieldParam field [], fromData<'TDoc>, sqlProps) /// Retrieve documents matching a JSON containment query (@>) [] @@ -395,14 +403,14 @@ module WithProps = /// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found [] - let firstByField<'TDoc> tableName fieldName op (value: obj) sqlProps = + let firstByField<'TDoc> tableName field sqlProps = Custom.single<'TDoc> - $"{Query.Find.byField tableName fieldName op} LIMIT 1" [ fieldParam value ] fromData<'TDoc> sqlProps + $"{Query.Find.byField tableName field} LIMIT 1" (addFieldParam field []) fromData<'TDoc> sqlProps /// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found - let FirstByField<'TDoc when 'TDoc: null>(tableName, fieldName, op, value: obj, sqlProps) = + let FirstByField<'TDoc when 'TDoc: null>(tableName, field, sqlProps) = Custom.Single<'TDoc>( - $"{Query.Find.byField tableName fieldName op} LIMIT 1", [ fieldParam value ], fromData<'TDoc>, sqlProps) + $"{Query.Find.byField tableName field} LIMIT 1", addFieldParam field [], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found [] @@ -461,9 +469,9 @@ module WithProps = /// Patch documents using a JSON field comparison query in the WHERE clause (->> =) [] - let byField tableName fieldName op (value: obj) (patch: 'TPatch) sqlProps = + let byField tableName field (patch: 'TPatch) sqlProps = Custom.nonQuery - (Query.Patch.byField tableName fieldName op) [ jsonParam "@data" patch; fieldParam value ] sqlProps + (Query.Patch.byField tableName field) (addFieldParam field [ jsonParam "@data" patch ]) sqlProps /// Patch documents using a JSON containment query in the WHERE clause (@>) [] @@ -488,8 +496,8 @@ module WithProps = /// Delete documents by matching a JSON field comparison query (->> =) [] - let byField tableName fieldName op (value: obj) sqlProps = - Custom.nonQuery (Query.Delete.byField tableName fieldName op) [ fieldParam value ] sqlProps + let byField tableName field sqlProps = + Custom.nonQuery (Query.Delete.byField tableName field) (addFieldParam field []) sqlProps /// Delete documents by matching a JSON contains query (@>) [] @@ -585,8 +593,8 @@ module Count = /// Count matching documents using a JSON field comparison query (->> =) [] - let byField tableName fieldName op (value: obj) = - WithProps.Count.byField tableName fieldName op value (fromDataSource ()) + let byField tableName field = + WithProps.Count.byField tableName field (fromDataSource ()) /// Count matching documents using a JSON containment query (@>) [] @@ -610,8 +618,8 @@ module Exists = /// Determine if documents exist using a JSON field comparison query (->> =) [] - let byField tableName fieldName op (value: obj) = - WithProps.Exists.byField tableName fieldName op value (fromDataSource ()) + let byField tableName field = + WithProps.Exists.byField tableName field (fromDataSource ()) /// Determine if documents exist using a JSON containment query (@>) [] @@ -648,12 +656,12 @@ module Find = /// Retrieve documents matching a JSON field comparison query (->> =) [] - let byField<'TDoc> tableName fieldName op (value: obj) = - WithProps.Find.byField<'TDoc> tableName fieldName op value (fromDataSource ()) + let byField<'TDoc> tableName field = + WithProps.Find.byField<'TDoc> tableName field (fromDataSource ()) /// Retrieve documents matching a JSON field comparison query (->> =) - let ByField<'TDoc>(tableName, fieldName, op, value: obj) = - WithProps.Find.ByField<'TDoc>(tableName, fieldName, op, value, fromDataSource ()) + let ByField<'TDoc>(tableName, field) = + WithProps.Find.ByField<'TDoc>(tableName, field, fromDataSource ()) /// Retrieve documents matching a JSON containment query (@>) [] @@ -675,12 +683,12 @@ module Find = /// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found [] - let firstByField<'TDoc> tableName fieldName op (value: obj) = - WithProps.Find.firstByField<'TDoc> tableName fieldName op value (fromDataSource ()) + let firstByField<'TDoc> tableName field = + WithProps.Find.firstByField<'TDoc> tableName field (fromDataSource ()) /// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found - let FirstByField<'TDoc when 'TDoc: null>(tableName, fieldName, op, value: obj) = - WithProps.Find.FirstByField<'TDoc>(tableName, fieldName, op, value, fromDataSource ()) + let FirstByField<'TDoc when 'TDoc: null>(tableName, field) = + WithProps.Find.FirstByField<'TDoc>(tableName, field, fromDataSource ()) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found [] @@ -731,8 +739,8 @@ module Patch = /// Patch documents using a JSON field comparison query in the WHERE clause (->> =) [] - let byField tableName fieldName op (value: obj) (patch: 'TPatch) = - WithProps.Patch.byField tableName fieldName op value patch (fromDataSource ()) + let byField tableName field (patch: 'TPatch) = + WithProps.Patch.byField tableName field patch (fromDataSource ()) /// Patch documents using a JSON containment query in the WHERE clause (@>) [] @@ -756,8 +764,8 @@ module Delete = /// Delete documents by matching a JSON field comparison query (->> =) [] - let byField tableName fieldName op (value: obj) = - WithProps.Delete.byField tableName fieldName op value (fromDataSource ()) + let byField tableName field = + WithProps.Delete.byField tableName field (fromDataSource ()) /// Delete documents by matching a JSON containment query (@>) [] diff --git a/src/Sqlite/Library.fs b/src/Sqlite/Library.fs index 86b4ee6..12a72e8 100644 --- a/src/Sqlite/Library.fs +++ b/src/Sqlite/Library.fs @@ -49,10 +49,10 @@ module Query = /// Query to patch (partially update) a document via a comparison on a JSON field [] - let byField tableName fieldName op = + let byField tableName field = sprintf "UPDATE %s SET data = json_patch(data, json(@data)) WHERE %s" - tableName (Query.whereByField fieldName op "@field") + tableName (Query.whereByField field "@field") /// Queries to remove a field from a document module RemoveField = @@ -64,10 +64,8 @@ module Query = /// Query to remove a field from a document via a comparison on a JSON field within the document [] - let byField tableName fieldName op = - sprintf - "UPDATE %s SET data = json_remove(data, @name) WHERE %s" - tableName (Query.whereByField fieldName op "@field") + let byField tableName field = + $"""UPDATE %s{tableName} SET data = json_remove(data, @name) WHERE {Query.whereByField field "@field"}""" /// Parameter handling helpers @@ -85,9 +83,17 @@ module Parameters = SqliteParameter(name, Configuration.serializer().Serialize it) /// Create a JSON field parameter (name "@field") - [] - let fieldParam (value: obj) = - SqliteParameter("@field", value) + [] + let addFieldParam field parameters = + match field.Op with + | EX | NEX -> parameters + | _ -> SqliteParameter("@field", field.Value) :: parameters + + /// Create a JSON field parameter (name "@field") + let AddField field parameters = + match field.Op with + | EX | NEX -> parameters + | _ -> SqliteParameter("@field", field.Value) |> Seq.singleton |> Seq.append parameters /// Create a JSON field name parameter (name "@name") [] @@ -244,7 +250,7 @@ module WithConn = /// Count matching documents using a comparison on a JSON field [] let byField tableName field conn = - Custom.scalar (Query.Count.byField tableName field.Name field.Op) [ fieldParam field.Value ] toCount conn + Custom.scalar (Query.Count.byField tableName field) (addFieldParam field []) toCount conn /// Commands to determine if documents exist [] @@ -258,7 +264,7 @@ module WithConn = /// Determine if a document exists using a comparison on a JSON field [] let byField tableName field conn = - Custom.scalar (Query.Exists.byField tableName field.Name field.Op) [ fieldParam field.Value ] toExists conn + Custom.scalar (Query.Exists.byField tableName field) (addFieldParam field []) toExists conn /// Commands to retrieve documents [] @@ -285,30 +291,21 @@ module WithConn = /// Retrieve documents via a comparison on a JSON field [] let byField<'TDoc> tableName field conn = - Custom.list<'TDoc> - (Query.Find.byField tableName field.Name field.Op) [ fieldParam field.Value ] fromData<'TDoc> conn + Custom.list<'TDoc> (Query.Find.byField tableName field) (addFieldParam field []) fromData<'TDoc> conn /// Retrieve documents via a comparison on a JSON field let ByField<'TDoc>(tableName, field, conn) = - Custom.List<'TDoc>( - Query.Find.byField tableName field.Name field.Op, [ fieldParam field.Value ], fromData<'TDoc>, conn) + Custom.List<'TDoc>(Query.Find.byField tableName field, addFieldParam field [], fromData<'TDoc>, conn) /// Retrieve documents via a comparison on a JSON field, returning only the first result [] let firstByField<'TDoc> tableName field conn = - Custom.single - $"{Query.Find.byField tableName field.Name field.Op} LIMIT 1" - [ fieldParam field.Value ] - fromData<'TDoc> - conn + Custom.single $"{Query.Find.byField tableName field} LIMIT 1" (addFieldParam field []) fromData<'TDoc> conn /// Retrieve documents via a comparison on a JSON field, returning only the first result let FirstByField<'TDoc when 'TDoc: null>(tableName, field, conn) = Custom.Single( - $"{Query.Find.byField tableName field.Name field.Op} LIMIT 1", - [ fieldParam field.Value ], - fromData<'TDoc>, - conn) + $"{Query.Find.byField tableName field} LIMIT 1", addFieldParam field [], fromData<'TDoc>, conn) /// Commands to update documents [] @@ -340,10 +337,7 @@ module WithConn = /// Patch documents using a comparison on a JSON field [] let byField tableName field (patch: 'TPatch) (conn: SqliteConnection) = - Custom.nonQuery - (Query.Patch.byField tableName field.Name field.Op) - [ fieldParam field.Value; jsonParam "@data" patch ] - conn + Custom.nonQuery (Query.Patch.byField tableName field) (addFieldParam field [ jsonParam "@data" patch ]) conn /// Commands to remove fields from documents [] @@ -358,9 +352,7 @@ module WithConn = [] let byField tableName field fieldName conn = Custom.nonQuery - (Query.RemoveField.byField tableName field.Name field.Op) - [ fieldParam field.Value; fieldNameParam fieldName ] - conn + (Query.RemoveField.byField tableName field) (addFieldParam field [ fieldNameParam fieldName ]) conn /// Commands to delete documents [] @@ -374,7 +366,7 @@ module WithConn = /// Delete documents by matching a comparison on a JSON field [] let byField tableName field conn = - Custom.nonQuery (Query.Delete.byField tableName field.Name field.Op) [ fieldParam field.Value ] conn + Custom.nonQuery (Query.Delete.byField tableName field) (addFieldParam field []) conn /// Commands to execute custom SQL queries diff --git a/src/Tests.CSharp/CommonCSharpTests.cs b/src/Tests.CSharp/CommonCSharpTests.cs index 7f989d9..1426c3c 100644 --- a/src/Tests.CSharp/CommonCSharpTests.cs +++ b/src/Tests.CSharp/CommonCSharpTests.cs @@ -177,12 +177,12 @@ public static class CommonCSharpTests { TestCase("succeeds when a logical operator is passed", () => { - Expect.equal(Query.WhereByField("theField", Op.GT, "@test"), "data ->> 'theField' > @test", + Expect.equal(Query.WhereByField(Field.GT("theField", 0), "@test"), "data ->> 'theField' > @test", "WHERE clause not correct"); }), TestCase("succeeds when an existence operator is passed", () => { - Expect.equal(Query.WhereByField("thatField", Op.NEX, ""), "data ->> 'thatField' IS NULL", + Expect.equal(Query.WhereByField(Field.NEX("thatField"), ""), "data ->> 'thatField' IS NULL", "WHERE clause not correct"); }) }), @@ -242,7 +242,7 @@ public static class CommonCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Query.Count.ByField("tbl", "thatField", Op.EQ), + Expect.equal(Query.Count.ByField("tbl", Field.EQ("thatField", 0)), "SELECT COUNT(*) AS it FROM tbl WHERE data ->> 'thatField' = @field", "JSON field text comparison count query not correct"); }) @@ -257,7 +257,7 @@ public static class CommonCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Query.Exists.ByField("tbl", "Test", Op.LT), + Expect.equal(Query.Exists.ByField("tbl", Field.LT("Test", 0)), "SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Test' < @field) AS it", "JSON field text comparison exists query not correct"); }) @@ -271,7 +271,7 @@ public static class CommonCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Query.Find.ByField("tbl", "Golf", Op.GE), + Expect.equal(Query.Find.ByField("tbl", Field.GE("Golf", 0)), "SELECT data FROM tbl WHERE data ->> 'Golf' >= @field", "SELECT by JSON comparison query not correct"); }) @@ -285,7 +285,7 @@ public static class CommonCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Query.Delete.ByField("tbl", "gone", Op.NEX), + Expect.equal(Query.Delete.ByField("tbl", Field.NEX("gone")), "DELETE FROM tbl WHERE data ->> 'gone' IS NULL", "DELETE by JSON comparison query not correct"); }) diff --git a/src/Tests.CSharp/PostgresCSharpExtensionTests.cs b/src/Tests.CSharp/PostgresCSharpExtensionTests.cs index 4f0ee63..7dede84 100644 --- a/src/Tests.CSharp/PostgresCSharpExtensionTests.cs +++ b/src/Tests.CSharp/PostgresCSharpExtensionTests.cs @@ -246,7 +246,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var theCount = await conn.CountByField(PostgresDb.TableName, "Value", Op.EQ, "purple"); + var theCount = await conn.CountByField(PostgresDb.TableName, Field.EQ("Value", "purple")); Expect.equal(theCount, 2, "There should have been 2 matching documents"); }), TestCase("CountByContains succeeds", async () => @@ -296,7 +296,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var exists = await conn.ExistsByField(PostgresDb.TableName, "Sub", Op.EX, ""); + var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EX("Sub")); Expect.isTrue(exists, "There should have been existing documents"); }), TestCase("succeeds when documents do not exist", async () => @@ -305,7 +305,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var exists = await conn.ExistsByField(PostgresDb.TableName, "NumValue", Op.EQ, "six"); + var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EQ("NumValue", "six")); Expect.isFalse(exists, "There should not have been existing documents"); }) }), @@ -403,7 +403,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var docs = await conn.FindByField(PostgresDb.TableName, "Value", Op.EQ, "another"); + var docs = await conn.FindByField(PostgresDb.TableName, Field.EQ("Value", "another")); Expect.equal(docs.Count, 1, "There should have been one document returned"); }), TestCase("succeeds when documents are not found", async () => @@ -412,7 +412,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var docs = await conn.FindByField(PostgresDb.TableName, "Value", Op.EQ, "mauve"); + var docs = await conn.FindByField(PostgresDb.TableName, Field.EQ("Value", "mauve")); Expect.isEmpty(docs, "There should have been no documents returned"); }) }), @@ -467,7 +467,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var doc = await conn.FindFirstByField(PostgresDb.TableName, "Value", Op.EQ, "another"); + var doc = await conn.FindFirstByField(PostgresDb.TableName, Field.EQ("Value", "another")); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "two", "The incorrect document was returned"); }), @@ -477,7 +477,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var doc = await conn.FindFirstByField(PostgresDb.TableName, "Value", Op.EQ, "purple"); + var doc = await conn.FindFirstByField(PostgresDb.TableName, Field.EQ("Value", "purple")); Expect.isNotNull(doc, "There should have been a document returned"); Expect.contains(new[] { "five", "four" }, doc.Id, "An incorrect document was returned"); }), @@ -487,7 +487,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - var doc = await conn.FindFirstByField(PostgresDb.TableName, "Value", Op.EQ, "absent"); + var doc = await conn.FindFirstByField(PostgresDb.TableName, Field.EQ("Value", "absent")); Expect.isNull(doc, "There should not have been a document returned"); }) }), @@ -650,8 +650,8 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - await conn.PatchByField(PostgresDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 }); - var after = await conn.CountByField(PostgresDb.TableName, "NumValue", Op.EQ, "77"); + await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 }); + var after = await conn.CountByField(PostgresDb.TableName, Field.EQ("NumValue", "77")); Expect.equal(after, 2, "There should have been 2 documents returned"); }), TestCase("succeeds when no document is updated", async () => @@ -662,7 +662,7 @@ public class PostgresCSharpExtensionTests Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test - await conn.PatchByField(PostgresDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" }); + await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" }); }) }), TestList("PatchByContains", new[] @@ -742,7 +742,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - await conn.DeleteByField(PostgresDb.TableName, "Value", Op.NE, "purple"); + await conn.DeleteByField(PostgresDb.TableName, Field.NE("Value", "purple")); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 2, "There should have been 2 documents remaining"); }), @@ -752,7 +752,7 @@ public class PostgresCSharpExtensionTests await using var conn = MkConn(db); await LoadDocs(); - await conn.DeleteByField(PostgresDb.TableName, "Value", Op.EQ, "crimson"); + await conn.DeleteByField(PostgresDb.TableName, Field.EQ("Value", "crimson")); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) diff --git a/src/Tests.CSharp/PostgresCSharpTests.cs b/src/Tests.CSharp/PostgresCSharpTests.cs index a4713c7..84a0080 100644 --- a/src/Tests.CSharp/PostgresCSharpTests.cs +++ b/src/Tests.CSharp/PostgresCSharpTests.cs @@ -32,11 +32,21 @@ public class PostgresCSharpTests Expect.equal(it.Item1, "@test", "JSON parameter not constructed correctly"); Expect.equal(it.Item2, Sql.jsonb("{\"Something\":\"good\"}"), "JSON parameter value incorrect"); }), - TestCase("Field succeeds", () => + TestList("AddField", new [] { - var it = Parameters.Field(242); - Expect.equal(it.Item1, "@field", "Field parameter not constructed correctly"); - Expect.isTrue(it.Item2.IsParameter, "Field parameter value incorrect"); + TestCase("succeeds when a parameter is added", () => + { + var it = Parameters.AddField(Field.EQ("it", "242"), Enumerable.Empty>()) + .ToList(); + Expect.hasLength(it, 1, "There should have been a parameter added"); + Expect.equal(it[0].Item1, "@field", "Field parameter not constructed correctly"); + Expect.isTrue(it[0].Item2.IsParameter, "Field parameter value incorrect"); + }), + TestCase("succeeds when a parameter is not added", () => + { + var it = Parameters.AddField(Field.EX("It"), Enumerable.Empty>()); + Expect.isEmpty(it, "There should not have been any parameters added"); + }) }), TestCase("None succeeds", () => { @@ -134,7 +144,7 @@ public class PostgresCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Postgres.Query.Patch.ByField(PostgresDb.TableName, "Snail", Op.LT), + Expect.equal(Postgres.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"); }), @@ -431,7 +441,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var theCount = await Count.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple"); + var theCount = await Count.ByField(PostgresDb.TableName, Field.EQ("Value", "purple")); Expect.equal(theCount, 2, "There should have been 2 matching documents"); }), TestCase("ByContains succeeds", async () => @@ -479,7 +489,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var exists = await Exists.ByField(PostgresDb.TableName, "Sub", Op.NEX, ""); + var exists = await Exists.ByField(PostgresDb.TableName, Field.NEX("Sub")); Expect.isTrue(exists, "There should have been existing documents"); }), TestCase("succeeds when documents do not exist", async () => @@ -487,7 +497,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var exists = await Exists.ByField(PostgresDb.TableName, "NumValue", Op.EQ, "six"); + var exists = await Exists.ByField(PostgresDb.TableName, Field.EQ("NumValue", "six")); Expect.isFalse(exists, "There should not have been existing documents"); }) }), @@ -578,7 +588,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var docs = await Find.ByField(PostgresDb.TableName, "Value", Op.EQ, "another"); + var docs = await Find.ByField(PostgresDb.TableName, Field.EQ("Value", "another")); Expect.equal(docs.Count, 1, "There should have been one document returned"); }), TestCase("succeeds when documents are not found", async () => @@ -586,7 +596,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var docs = await Find.ByField(PostgresDb.TableName, "Value", Op.EQ, "mauve"); + var docs = await Find.ByField(PostgresDb.TableName, Field.EQ("Value", "mauve")); Expect.isEmpty(docs, "There should have been no documents returned"); }) }), @@ -636,7 +646,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var doc = await Find.FirstByField(PostgresDb.TableName, "Value", Op.EQ, "another"); + var doc = await Find.FirstByField(PostgresDb.TableName, Field.EQ("Value", "another")); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "two", "The incorrect document was returned"); }), @@ -645,7 +655,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var doc = await Find.FirstByField(PostgresDb.TableName, "Value", Op.EQ, "purple"); + var doc = await Find.FirstByField(PostgresDb.TableName, Field.EQ("Value", "purple")); Expect.isNotNull(doc, "There should have been a document returned"); Expect.contains(new[] { "five", "four" }, doc.Id, "An incorrect document was returned"); }), @@ -654,7 +664,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - var doc = await Find.FirstByField(PostgresDb.TableName, "Value", Op.EQ, "absent"); + var doc = await Find.FirstByField(PostgresDb.TableName, Field.EQ("Value", "absent")); Expect.isNull(doc, "There should not have been a document returned"); }) }), @@ -813,8 +823,8 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - await Patch.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 }); - var after = await Count.ByField(PostgresDb.TableName, "NumValue", Op.EQ, "77"); + await Patch.ByField(PostgresDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 }); + var after = await Count.ByField(PostgresDb.TableName, Field.EQ("NumValue", "77")); Expect.equal(after, 2, "There should have been 2 documents returned"); }), TestCase("succeeds when no document is updated", async () => @@ -825,7 +835,7 @@ public class PostgresCSharpTests Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test - await Patch.ByField(PostgresDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" }); + await Patch.ByField(PostgresDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" }); }) }), TestList("ByContains", new[] @@ -903,7 +913,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - await Delete.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple"); + await Delete.ByField(PostgresDb.TableName, Field.EQ("Value", "purple")); var remaining = await Count.All(PostgresDb.TableName); Expect.equal(remaining, 3, "There should have been 3 documents remaining"); }), @@ -912,7 +922,7 @@ public class PostgresCSharpTests await using var db = PostgresDb.BuildDb(); await LoadDocs(); - await Delete.ByField(PostgresDb.TableName, "Value", Op.EQ, "crimson"); + await Delete.ByField(PostgresDb.TableName, Field.EQ("Value", "crimson")); var remaining = await Count.All(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) diff --git a/src/Tests.CSharp/SqliteCSharpTests.cs b/src/Tests.CSharp/SqliteCSharpTests.cs index c86cc07..3c6559b 100644 --- a/src/Tests.CSharp/SqliteCSharpTests.cs +++ b/src/Tests.CSharp/SqliteCSharpTests.cs @@ -36,7 +36,7 @@ public static class SqliteCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Sqlite.Query.Patch.ByField("tbl", "Part", Op.NE), + Expect.equal(Sqlite.Query.Patch.ByField("tbl", Field.NE("Part", 0)), "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field", "UPDATE partial by JSON comparison query not correct"); }) @@ -51,7 +51,7 @@ public static class SqliteCSharpTests }), TestCase("ByField succeeds", () => { - Expect.equal(Sqlite.Query.RemoveField.ByField("tbl", "Fly", Op.LT), + Expect.equal(Sqlite.Query.RemoveField.ByField("tbl", Field.LT("Fly", 0)), "UPDATE tbl SET data = json_remove(data, @name) WHERE data ->> 'Fly' < @field", "Remove field by field query not correct"); }) @@ -71,12 +71,19 @@ public static class SqliteCSharpTests Expect.equal(theParam.ParameterName, "@test", "The parameter name is incorrect"); Expect.equal(theParam.Value, "{\"Nice\":\"job\"}", "The parameter value is incorrect"); }), - TestCase("Field succeeds", () => + TestCase("AddField succeeds when adding a parameter", () => { - var theParam = Parameters.Field(99); + var paramList = Parameters.AddField(Field.EQ("it", 99), Enumerable.Empty()).ToList(); + Expect.hasLength(paramList, 1, "There should have been a parameter added"); + var theParam = paramList[0]; Expect.equal(theParam.ParameterName, "@field", "The parameter name is incorrect"); Expect.equal(theParam.Value, 99, "The parameter value is incorrect"); }), + TestCase("AddField succeeds when not adding a parameter", () => + { + var paramSeq = Parameters.AddField(Field.EX("Coffee"), Enumerable.Empty()); + Expect.isEmpty(paramSeq, "There should not have been any parameters added"); + }), TestCase("None succeeds", () => { Expect.isEmpty(Parameters.None, "The parameter list should have been empty"); diff --git a/src/Tests/CommonTests.fs b/src/Tests/CommonTests.fs index b7fcc4a..dd0ac97 100644 --- a/src/Tests/CommonTests.fs +++ b/src/Tests/CommonTests.fs @@ -45,13 +45,13 @@ let all = testList "whereByField" [ test "succeeds when a logical operator is passed" { Expect.equal - (Query.whereByField "theField" GT "@test") + (Query.whereByField (Field.GT "theField" 0) "@test") "data ->> 'theField' > @test" "WHERE clause not correct" } test "succeeds when an existence operator is passed" { Expect.equal - (Query.whereByField "thatField" NEX "") + (Query.whereByField (Field.NEX "thatField") "") "data ->> 'thatField' IS NULL" "WHERE clause not correct" } @@ -107,7 +107,7 @@ let all = } test "byField succeeds" { Expect.equal - (Query.Count.byField tbl "thatField" EQ) + (Query.Count.byField tbl (Field.EQ "thatField" 0)) $"SELECT COUNT(*) AS it FROM {tbl} WHERE data ->> 'thatField' = @field" "JSON field text comparison count query not correct" } @@ -121,7 +121,7 @@ let all = } test "byField succeeds" { Expect.equal - (Query.Exists.byField tbl "Test" LT) + (Query.Exists.byField tbl (Field.LT "Test" 0)) $"SELECT EXISTS (SELECT 1 FROM {tbl} WHERE data ->> 'Test' < @field) AS it" "JSON field text comparison exists query not correct" } @@ -135,7 +135,7 @@ let all = } test "byField succeeds" { Expect.equal - (Query.Find.byField tbl "Golf" GE) + (Query.Find.byField tbl (Field.GE "Golf" 0)) $"SELECT data FROM {tbl} WHERE data ->> 'Golf' >= @field" "SELECT by JSON comparison query not correct" } @@ -149,7 +149,7 @@ let all = } test "byField succeeds" { Expect.equal - (Query.Delete.byField tbl "gone" NEX) + (Query.Delete.byField tbl (Field.NEX "gone")) $"DELETE FROM {tbl} WHERE data ->> 'gone' IS NULL" "DELETE by JSON comparison query not correct" } diff --git a/src/Tests/PostgresExtensionTests.fs b/src/Tests/PostgresExtensionTests.fs index 4d215b2..cfc0135 100644 --- a/src/Tests/PostgresExtensionTests.fs +++ b/src/Tests/PostgresExtensionTests.fs @@ -214,7 +214,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! theCount = conn.countByField PostgresDb.TableName "Value" EQ "purple" + let! theCount = conn.countByField PostgresDb.TableName (Field.EQ "Value" "purple") Expect.equal theCount 2 "There should have been 2 matching documents" } testTask "countByContains succeeds" { @@ -257,7 +257,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! exists = conn.existsByField PostgresDb.TableName "Sub" EX "" + let! exists = conn.existsByField PostgresDb.TableName (Field.EX "Sub") Expect.isTrue exists "There should have been existing documents" } testTask "succeeds when documents do not exist" { @@ -265,7 +265,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! exists = conn.existsByField PostgresDb.TableName "NumValue" EQ "six" + let! exists = conn.existsByField PostgresDb.TableName (Field.EQ "NumValue" "six") Expect.isFalse exists "There should not have been existing documents" } ] @@ -354,7 +354,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! docs = conn.findByField PostgresDb.TableName "Value" EQ "another" + let! docs = conn.findByField 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" { @@ -362,7 +362,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! docs = conn.findByField PostgresDb.TableName "Value" EQ "mauve" + let! docs = conn.findByField PostgresDb.TableName (Field.EQ "Value" "mauve") Expect.isEmpty docs "There should have been no documents returned" } ] @@ -408,7 +408,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! doc = conn.findFirstByField PostgresDb.TableName "Value" EQ "another" + let! doc = conn.findFirstByField 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" } @@ -417,7 +417,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! doc = conn.findFirstByField PostgresDb.TableName "Value" EQ "purple" + let! doc = conn.findFirstByField 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" } @@ -426,7 +426,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - let! doc = conn.findFirstByField PostgresDb.TableName "Value" EQ "absent" + let! doc = conn.findFirstByField PostgresDb.TableName (Field.EQ "Value" "absent") Expect.isNone doc "There should not have been a document returned" } ] @@ -562,8 +562,8 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - do! conn.patchByField PostgresDb.TableName "Value" EQ "purple" {| NumValue = 77 |} - let! after = conn.countByField PostgresDb.TableName "NumValue" EQ "77" + do! conn.patchByField PostgresDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |} + let! after = conn.countByField PostgresDb.TableName (Field.EQ "NumValue" "77") Expect.equal after 2 "There should have been 2 documents returned" } testTask "succeeds when no document is updated" { @@ -573,7 +573,7 @@ let integrationTests = Expect.equal before 0 "There should have been no documents returned" // This not raising an exception is the test - do! conn.patchByField PostgresDb.TableName "Value" EQ "burgundy" {| Foo = "green" |} + do! conn.patchByField PostgresDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |} } ] testList "patchByContains" [ @@ -642,7 +642,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - do! conn.deleteByField PostgresDb.TableName "Value" EQ "purple" + do! conn.deleteByField PostgresDb.TableName (Field.EQ "Value" "purple") let! remaining = conn.countAll PostgresDb.TableName Expect.equal remaining 3 "There should have been 3 documents remaining" } @@ -651,7 +651,7 @@ let integrationTests = use conn = mkConn db do! loadDocs conn - do! conn.deleteByField PostgresDb.TableName "Value" EQ "crimson" + do! conn.deleteByField PostgresDb.TableName (Field.EQ "Value" "crimson") let! remaining = conn.countAll PostgresDb.TableName Expect.equal remaining 5 "There should have been 5 documents remaining" } diff --git a/src/Tests/PostgresTests.fs b/src/Tests/PostgresTests.fs index 66938f6..b0ef091 100644 --- a/src/Tests/PostgresTests.fs +++ b/src/Tests/PostgresTests.fs @@ -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.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.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" } @@ -357,7 +365,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 +405,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 +494,14 @@ let integrationTests = use db = PostgresDb.BuildDb() do! loadDocs () - let! docs = Find.byField PostgresDb.TableName "Value" EQ "another" + let! docs = Find.byField 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 PostgresDb.TableName "Value" EQ "mauve" + let! docs = Find.byField PostgresDb.TableName (Field.EQ "Value" "mauve") Expect.isEmpty docs "There should have been no documents returned" } ] @@ -534,7 +542,7 @@ let integrationTests = use db = PostgresDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField PostgresDb.TableName "Value" EQ "another" + let! doc = Find.firstByField 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 +550,7 @@ let integrationTests = use db = PostgresDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField PostgresDb.TableName "Value" EQ "purple" + let! doc = Find.firstByField 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 +558,7 @@ let integrationTests = use db = PostgresDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField PostgresDb.TableName "Value" EQ "absent" + let! doc = Find.firstByField PostgresDb.TableName (Field.EQ "Value" "absent") Expect.isNone doc "There should not have been a document returned" } ] @@ -682,8 +690,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 +701,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" [ @@ -759,7 +767,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 +775,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" } diff --git a/src/Tests/SqliteTests.fs b/src/Tests/SqliteTests.fs index 3f09467..a16b37f 100644 --- a/src/Tests/SqliteTests.fs +++ b/src/Tests/SqliteTests.fs @@ -27,7 +27,7 @@ let unitTests = } test "byField succeeds" { Expect.equal - (Query.Patch.byField "tbl" "Part" NE) + (Query.Patch.byField "tbl" (Field.NE "Part" 0)) "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field" "UPDATE partial by JSON comparison query not correct" } @@ -41,7 +41,7 @@ let unitTests = } test "byField succeeds" { Expect.equal - (Query.RemoveField.byField "tbl" "Fly" GT) + (Query.RemoveField.byField "tbl" (Field.GT "Fly" 0)) "UPDATE tbl SET data = json_remove(data, @name) WHERE data ->> 'Fly' > @field" "Remove field by field query not correct" } @@ -58,11 +58,19 @@ let unitTests = Expect.equal theParam.ParameterName "@test" "The parameter name is incorrect" Expect.equal theParam.Value """{"Nice":"job"}""" "The parameter value is incorrect" } - test "fieldParam succeeds" { - let theParam = fieldParam 99 - Expect.equal theParam.ParameterName "@field" "The parameter name is incorrect" - Expect.equal theParam.Value 99 "The parameter value is incorrect" - } + testList "addFieldParam" [ + test "succeeds when adding a parameter" { + let paramList = addFieldParam (Field.EQ "it" 99) [] + Expect.hasLength paramList 1 "There should have been a parameter added" + let theParam = paramList[0] + 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 (Field.NEX "Coffee") [] + Expect.isEmpty paramList "There should not have been any parameters added" + } + ] test "noParams succeeds" { Expect.isEmpty noParams "The parameter list should have been empty" }