v3 RC1 #1

Merged
danieljsummers merged 25 commits from merge-projects into main 2024-01-06 20:51:49 +00:00
6 changed files with 144 additions and 137 deletions
Showing only changes of commit c6b3d275b0 - Show all commits

View File

@ -72,21 +72,21 @@ module Extensions =
member conn.findFirstByField<'TDoc> tableName fieldName op (value: obj) = member conn.findFirstByField<'TDoc> tableName fieldName op (value: obj) =
WithConn.Find.firstByField<'TDoc> tableName fieldName op value conn WithConn.Find.firstByField<'TDoc> tableName fieldName op value conn
/// Update an entire document /// Update an entire document by its ID
member conn.updateFull tableName (docId: 'TKey) (document: 'TDoc) = member conn.updateById tableName (docId: 'TKey) (document: 'TDoc) =
WithConn.Update.full tableName docId document conn WithConn.Update.byId tableName docId document conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
member conn.updateFullFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) = member conn.updateByFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) =
WithConn.Update.fullFunc tableName idFunc document conn WithConn.Update.byFunc tableName idFunc document conn
/// Update a partial document /// Patch a document by its ID
member conn.updatePartialById tableName (docId: 'TKey) (partial: 'TPatch) = member conn.patchById tableName (docId: 'TKey) (patch: 'TPatch) =
WithConn.Update.partialById tableName docId partial conn WithConn.Patch.byId tableName docId patch conn
/// Update partial documents using a comparison on a JSON field /// Patch documents using a comparison on a JSON field
member conn.updatePartialByField tableName fieldName op (value: obj) (partial: 'TPatch) = member conn.patchByField tableName fieldName op (value: obj) (patch: 'TPatch) =
WithConn.Update.partialByField tableName fieldName op value partial conn WithConn.Patch.byField tableName fieldName op value patch conn
/// Delete a document by its ID /// Delete a document by its ID
member conn.deleteById tableName (docId: 'TKey) = member conn.deleteById tableName (docId: 'TKey) =
@ -184,25 +184,25 @@ type SqliteConnectionCSharpExtensions =
static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, fieldName, op, value: obj) = static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, fieldName, op, value: obj) =
WithConn.Find.FirstByField<'TDoc>(tableName, fieldName, op, value, conn) WithConn.Find.FirstByField<'TDoc>(tableName, fieldName, op, value, conn)
/// Update an entire document /// Update an entire document by its ID
[<Extension>] [<Extension>]
static member inline UpdateFull<'TKey, 'TDoc>(conn, tableName, docId: 'TKey, document: 'TDoc) = static member inline UpdateById<'TKey, 'TDoc>(conn, tableName, docId: 'TKey, document: 'TDoc) =
WithConn.Update.full tableName docId document conn WithConn.Update.byId tableName docId document conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
[<Extension>] [<Extension>]
static member inline UpdateFullFunc<'TKey, 'TDoc>(conn, tableName, idFunc: System.Func<'TDoc, 'TKey>, doc: 'TDoc) = static member inline UpdateByFunc<'TKey, 'TDoc>(conn, tableName, idFunc: System.Func<'TDoc, 'TKey>, doc: 'TDoc) =
WithConn.Update.FullFunc(tableName, idFunc, doc, conn) WithConn.Update.ByFunc(tableName, idFunc, doc, conn)
/// Update a partial document /// Patch a document by its ID
[<Extension>] [<Extension>]
static member inline UpdatePartialById<'TKey, 'TPatch>(conn, tableName, docId: 'TKey, partial: 'TPatch) = static member inline PatchById<'TKey, 'TPatch>(conn, tableName, docId: 'TKey, patch: 'TPatch) =
WithConn.Update.partialById tableName docId partial conn WithConn.Patch.byId tableName docId patch conn
/// Update partial documents using a comparison on a JSON field /// Patch documents using a comparison on a JSON field
[<Extension>] [<Extension>]
static member inline UpdatePartialByField<'TPatch>(conn, tableName, fieldName, op, value: obj, partial: 'TPatch) = static member inline PatchByField<'TPatch>(conn, tableName, fieldName, op, value: obj, patch: 'TPatch) =
WithConn.Update.partialByField tableName fieldName op value partial conn WithConn.Patch.byField tableName fieldName op value patch conn
/// Delete a document by its ID /// Delete a document by its ID
[<Extension>] [<Extension>]

View File

@ -39,17 +39,17 @@ module Query =
let ensureTable name = let ensureTable name =
Query.Definition.ensureTableFor name "TEXT" Query.Definition.ensureTableFor name "TEXT"
/// Document update queries /// Document patching (partial update) queries
module Update = module Patch =
/// Query to update a partial document by its ID /// Query to patch (partially update) a document by its ID
[<CompiledName "PartialById">] [<CompiledName "ById">]
let partialById tableName = let byId tableName =
$"""UPDATE %s{tableName} SET data = json_patch(data, json(@data)) WHERE {Query.whereById "@id"}""" $"""UPDATE %s{tableName} SET data = json_patch(data, json(@data)) WHERE {Query.whereById "@id"}"""
/// Query to update a partial document via a comparison on a JSON field /// Query to patch (partially update) a document via a comparison on a JSON field
[<CompiledName "PartialByField">] [<CompiledName "ByField">]
let partialByField tableName fieldName op = let byField tableName fieldName op =
sprintf sprintf
"UPDATE %s SET data = json_patch(data, json(@data)) WHERE %s" "UPDATE %s SET data = json_patch(data, json(@data)) WHERE %s"
tableName (Query.whereByField fieldName op "@field") tableName (Query.whereByField fieldName op "@field")
@ -295,32 +295,34 @@ module WithConn =
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Update = module Update =
/// Update an entire document /// Update an entire document by its ID
[<CompiledName "Full">] [<CompiledName "ById">]
let full tableName (docId: 'TKey) (document: 'TDoc) conn = let byId tableName (docId: 'TKey) (document: 'TDoc) conn =
Custom.nonQuery (Query.update tableName) [ idParam docId; jsonParam "@data" document ] conn Custom.nonQuery (Query.update tableName) [ idParam docId; jsonParam "@data" document ] conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
[<CompiledName "FSharpFullFunc">] [<CompiledName "FSharpByFunc">]
let fullFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) conn = let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) conn =
full tableName (idFunc document) document conn byId tableName (idFunc document) document conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
let FullFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, conn) = let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, conn) =
fullFunc tableName idFunc.Invoke document conn byFunc tableName idFunc.Invoke document conn
/// Commands to patch (partially update) documents
[<RequireQualifiedAccess>]
module Patch =
/// Update a partial document /// Patch a document by its ID
[<CompiledName "PartialById">] [<CompiledName "ById">]
let partialById tableName (docId: 'TKey) (partial: 'TPatch) conn = let byId tableName (docId: 'TKey) (patch: 'TPatch) conn =
Custom.nonQuery (Query.Update.partialById tableName) [ idParam docId; jsonParam "@data" partial ] conn Custom.nonQuery (Query.Patch.byId tableName) [ idParam docId; jsonParam "@data" patch ] conn
/// Update partial documents using a comparison on a JSON field /// Patch documents using a comparison on a JSON field
[<CompiledName "PartialByField">] [<CompiledName "ByField">]
let partialByField tableName fieldName op (value: obj) (partial: 'TPatch) (conn: SqliteConnection) = let byField tableName fieldName op (value: obj) (patch: 'TPatch) (conn: SqliteConnection) =
Custom.nonQuery Custom.nonQuery
(Query.Update.partialByField tableName fieldName op) (Query.Patch.byField tableName fieldName op) [ fieldParam value; jsonParam "@data" patch ] conn
[ fieldParam value; jsonParam "@data" partial ]
conn
/// Commands to delete documents /// Commands to delete documents
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
@ -490,34 +492,38 @@ module Find =
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Update = module Update =
/// Update an entire document /// Update an entire document by its ID
[<CompiledName "Full">] [<CompiledName "ById">]
let full tableName (docId: 'TKey) (document: 'TDoc) = let byId tableName (docId: 'TKey) (document: 'TDoc) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Update.full tableName docId document conn WithConn.Update.byId tableName docId document conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
[<CompiledName "FSharpFullFunc">] [<CompiledName "FSharpByFunc">]
let fullFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) = let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Update.fullFunc tableName idFunc document conn WithConn.Update.byFunc tableName idFunc document conn
/// Update an entire document /// Update an entire document by its ID, using the provided function to obtain the ID from the document
let FullFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc) = let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Update.FullFunc(tableName, idFunc, document, conn) WithConn.Update.ByFunc(tableName, idFunc, document, conn)
/// Commands to patch (partially update) documents
[<RequireQualifiedAccess>]
module Patch =
/// Update a partial document /// Patch a document by its ID
[<CompiledName "PartialById">] [<CompiledName "ById">]
let partialById tableName (docId: 'TKey) (partial: 'TPatch) = let byId tableName (docId: 'TKey) (patch: 'TPatch) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Update.partialById tableName docId partial conn WithConn.Patch.byId tableName docId patch conn
/// Update partial documents using a comparison on a JSON field in the WHERE clause /// Patch documents using a comparison on a JSON field in the WHERE clause
[<CompiledName "PartialByField">] [<CompiledName "ByField">]
let partialByField tableName fieldName op (value: obj) (partial: 'TPatch) = let byField tableName fieldName op (value: obj) (patch: 'TPatch) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Update.partialByField tableName fieldName op value partial conn WithConn.Patch.byField tableName fieldName op value patch conn
/// Commands to delete documents /// Commands to delete documents
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]

View File

@ -348,7 +348,7 @@ public static class SqliteCSharpExtensionTests
Expect.isNull(doc, "There should not have been a document returned"); Expect.isNull(doc, "There should not have been a document returned");
}) })
}), }),
TestList("UpdateFull", new[] TestList("UpdateById", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
@ -357,7 +357,7 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(); await LoadDocs();
var testDoc = new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } }; var testDoc = new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } };
await conn.UpdateFull(SqliteDb.TableName, "one", testDoc); await conn.UpdateById(SqliteDb.TableName, "one", testDoc);
var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
Expect.equal(after.Id, "one", "The updated document is not correct"); Expect.equal(after.Id, "one", "The updated document is not correct");
@ -373,11 +373,11 @@ public static class SqliteCSharpExtensionTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await conn.UpdateFull(SqliteDb.TableName, "test", await conn.UpdateById(SqliteDb.TableName, "test",
new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } }); new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } });
}) })
}), }),
TestList("UpdateFullFunc", new[] TestList("UpdateByFunc", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
@ -385,7 +385,7 @@ public static class SqliteCSharpExtensionTests
await using var conn = Sqlite.Configuration.DbConn(); await using var conn = Sqlite.Configuration.DbConn();
await LoadDocs(); await LoadDocs();
await conn.UpdateFullFunc(SqliteDb.TableName, doc => doc.Id, await conn.UpdateByFunc(SqliteDb.TableName, doc => doc.Id,
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
@ -402,11 +402,11 @@ public static class SqliteCSharpExtensionTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await conn.UpdateFullFunc(SqliteDb.TableName, doc => doc.Id, await conn.UpdateByFunc(SqliteDb.TableName, doc => doc.Id,
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
}) })
}), }),
TestList("UpdatePartialById", new[] TestList("PatchById", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
@ -414,7 +414,7 @@ public static class SqliteCSharpExtensionTests
await using var conn = Sqlite.Configuration.DbConn(); await using var conn = Sqlite.Configuration.DbConn();
await LoadDocs(); await LoadDocs();
await conn.UpdatePartialById(SqliteDb.TableName, "one", new { NumValue = 44 }); await conn.PatchById(SqliteDb.TableName, "one", new { NumValue = 44 });
var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
Expect.equal(after.Id, "one", "The updated document is not correct"); Expect.equal(after.Id, "one", "The updated document is not correct");
@ -428,10 +428,10 @@ public static class SqliteCSharpExtensionTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await conn.UpdatePartialById(SqliteDb.TableName, "test", new { Foo = "green" }); await conn.PatchById(SqliteDb.TableName, "test", new { Foo = "green" });
}) })
}), }),
TestList("UpdatePartialByField", new[] TestList("PatchByField", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
@ -439,7 +439,7 @@ public static class SqliteCSharpExtensionTests
await using var conn = Sqlite.Configuration.DbConn(); await using var conn = Sqlite.Configuration.DbConn();
await LoadDocs(); await LoadDocs();
await conn.UpdatePartialByField(SqliteDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 }); await conn.PatchByField(SqliteDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
var after = await conn.CountByField(SqliteDb.TableName, "NumValue", Op.EQ, 77); var after = await conn.CountByField(SqliteDb.TableName, "NumValue", Op.EQ, 77);
Expect.equal(after, 2L, "There should have been 2 documents returned"); Expect.equal(after, 2L, "There should have been 2 documents returned");
}), }),
@ -451,7 +451,7 @@ public static class SqliteCSharpExtensionTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await conn.UpdatePartialByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" }); await conn.PatchByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
}) })
}), }),
TestList("DeleteById", new[] TestList("DeleteById", new[]

View File

@ -25,17 +25,17 @@ public static class SqliteCSharpTests
Expect.equal(Sqlite.Query.Definition.EnsureTable("tbl"), Expect.equal(Sqlite.Query.Definition.EnsureTable("tbl"),
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)", "CREATE TABLE statement not correct"); "CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)", "CREATE TABLE statement not correct");
}), }),
TestList("Update", new[] TestList("Patch", new[]
{ {
TestCase("PartialById succeeds", () => TestCase("ById succeeds", () =>
{ {
Expect.equal(Sqlite.Query.Update.PartialById("tbl"), Expect.equal(Sqlite.Query.Patch.ById("tbl"),
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id", "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id",
"UPDATE partial by ID statement not correct"); "UPDATE partial by ID statement not correct");
}), }),
TestCase("PartialByField succeeds", () => TestCase("ByField succeeds", () =>
{ {
Expect.equal(Sqlite.Query.Update.PartialByField("tbl", "Part", Op.NE), Expect.equal(Sqlite.Query.Patch.ByField("tbl", "Part", Op.NE),
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field", "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field",
"UPDATE partial by JSON comparison query not correct"); "UPDATE partial by JSON comparison query not correct");
}) })
@ -420,7 +420,7 @@ public static class SqliteCSharpTests
}), }),
TestList("Update", new[] TestList("Update", new[]
{ {
TestList("Full", new[] TestList("ById", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
@ -428,7 +428,7 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
var testDoc = new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } }; var testDoc = new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } };
await Update.Full(SqliteDb.TableName, "one", testDoc); await Update.ById(SqliteDb.TableName, "one", testDoc);
var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
Expect.equal(after!.Id, "one", "The updated document is not correct"); Expect.equal(after!.Id, "one", "The updated document is not correct");
@ -444,18 +444,18 @@ public static class SqliteCSharpTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await Update.Full(SqliteDb.TableName, "test", await Update.ById(SqliteDb.TableName, "test",
new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } }); new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } });
}) })
}), }),
TestList("FullFunc", new[] TestList("ByFunc", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
await using var db = await SqliteDb.BuildDb(); await using var db = await SqliteDb.BuildDb();
await LoadDocs(); await LoadDocs();
await Update.FullFunc(SqliteDb.TableName, doc => doc.Id, await Update.ByFunc(SqliteDb.TableName, doc => doc.Id,
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
@ -472,18 +472,21 @@ public static class SqliteCSharpTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await Update.FullFunc(SqliteDb.TableName, doc => doc.Id, await Update.ByFunc(SqliteDb.TableName, doc => doc.Id,
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
}) })
}), }),
TestList("PartialById", new[] }),
TestList("Patch", new[]
{
TestList("ById", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
await using var db = await SqliteDb.BuildDb(); await using var db = await SqliteDb.BuildDb();
await LoadDocs(); await LoadDocs();
await Update.PartialById(SqliteDb.TableName, "one", new { NumValue = 44 }); await Patch.ById(SqliteDb.TableName, "one", new { NumValue = 44 });
var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one"); var after = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "one");
Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.isNotNull(after, "There should have been a document returned post-update");
Expect.equal(after!.Id, "one", "The updated document is not correct"); Expect.equal(after!.Id, "one", "The updated document is not correct");
@ -497,18 +500,17 @@ public static class SqliteCSharpTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await Update.PartialById(SqliteDb.TableName, "test", new { Foo = "green" }); await Patch.ById(SqliteDb.TableName, "test", new { Foo = "green" });
}) })
}), }),
TestList("PartialByField", new[] TestList("ByField", new[]
{ {
TestCase("succeeds when a document is updated", async () => TestCase("succeeds when a document is updated", async () =>
{ {
await using var db = await SqliteDb.BuildDb(); await using var db = await SqliteDb.BuildDb();
await LoadDocs(); await LoadDocs();
await Update.PartialByField(SqliteDb.TableName, "Value", Op.EQ, "purple", await Patch.ByField(SqliteDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
new { NumValue = 77 });
var after = await Count.ByField(SqliteDb.TableName, "NumValue", Op.EQ, 77); var after = await Count.ByField(SqliteDb.TableName, "NumValue", Op.EQ, 77);
Expect.equal(after, 2L, "There should have been 2 documents returned"); Expect.equal(after, 2L, "There should have been 2 documents returned");
}), }),
@ -520,8 +522,7 @@ public static class SqliteCSharpTests
Expect.isEmpty(before, "There should have been no documents returned"); Expect.isEmpty(before, "There should have been no documents returned");
// This not raising an exception is the test // This not raising an exception is the test
await Update.PartialByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", await Patch.ByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
new { Foo = "green" });
}) })
}) })
}), }),

View File

@ -231,14 +231,14 @@ let integrationTests =
Expect.isFalse (Option.isSome doc) "There should not have been a document returned" Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
} }
] ]
testList "updateFull" [ testList "updateById" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
do! loadDocs () do! loadDocs ()
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } } let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
do! conn.updateFull SqliteDb.TableName "one" testDoc do! conn.updateById SqliteDb.TableName "one" testDoc
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one" let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
if Option.isNone after then if Option.isNone after then
Expect.isTrue false "There should have been a document returned post-update" Expect.isTrue false "There should have been a document returned post-update"
@ -252,22 +252,20 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! conn.updateFull do! conn.updateById
SqliteDb.TableName SqliteDb.TableName
"test" "test"
{ emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } } { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
} }
] ]
testList "updateFullFunc" [ testList "updateByFunc" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
do! loadDocs () do! loadDocs ()
do! conn.updateFullFunc do! conn.updateByFunc
SqliteDb.TableName SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
(_.Id)
{ Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one" let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
if Option.isNone after then if Option.isNone after then
Expect.isTrue false "There should have been a document returned post-update" Expect.isTrue false "There should have been a document returned post-update"
@ -284,19 +282,17 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! conn.updateFullFunc do! conn.updateByFunc
SqliteDb.TableName SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
(_.Id)
{ Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
} }
] ]
testList "updatePartialById" [ testList "patchById" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
do! loadDocs () do! loadDocs ()
do! conn.updatePartialById SqliteDb.TableName "one" {| NumValue = 44 |} do! conn.patchById SqliteDb.TableName "one" {| NumValue = 44 |}
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one" let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
if Option.isNone after then if Option.isNone after then
Expect.isTrue false "There should have been a document returned post-update" Expect.isTrue false "There should have been a document returned post-update"
@ -310,16 +306,16 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! conn.updatePartialById SqliteDb.TableName "test" {| Foo = "green" |} do! conn.patchById SqliteDb.TableName "test" {| Foo = "green" |}
} }
] ]
testList "updatePartialByField" [ testList "patchByField" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
do! loadDocs () do! loadDocs ()
do! conn.updatePartialByField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |} do! conn.patchByField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |}
let! after = conn.countByField SqliteDb.TableName "NumValue" EQ 77 let! after = conn.countByField SqliteDb.TableName "NumValue" EQ 77
Expect.equal after 2L "There should have been 2 documents returned" Expect.equal after 2L "There should have been 2 documents returned"
} }
@ -331,7 +327,7 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! conn.updatePartialByField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |} do! conn.patchByField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |}
} }
] ]
testList "deleteById" [ testList "deleteById" [

View File

@ -17,16 +17,16 @@ let unitTests =
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)" "CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)"
"CREATE TABLE statement not correct" "CREATE TABLE statement not correct"
} }
testList "Update" [ testList "Patch" [
test "partialById succeeds" { test "byId succeeds" {
Expect.equal Expect.equal
(Query.Update.partialById "tbl") (Query.Patch.byId "tbl")
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id" "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id"
"UPDATE partial by ID statement not correct" "UPDATE partial by ID statement not correct"
} }
test "partialByField succeeds" { test "byField succeeds" {
Expect.equal Expect.equal
(Query.Update.partialByField "tbl" "Part" NE) (Query.Patch.byField "tbl" "Part" NE)
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field" "UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field"
"UPDATE partial by JSON comparison query not correct" "UPDATE partial by JSON comparison query not correct"
} }
@ -385,13 +385,13 @@ let integrationTests =
] ]
] ]
testList "Update" [ testList "Update" [
testList "full" [ testList "byId" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } } let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
do! Update.full SqliteDb.TableName "one" testDoc do! Update.byId SqliteDb.TableName "one" testDoc
let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one" let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one"
Expect.isSome after "There should have been a document returned post-update" Expect.isSome after "There should have been a document returned post-update"
Expect.equal after.Value testDoc "The updated document is not correct" Expect.equal after.Value testDoc "The updated document is not correct"
@ -403,18 +403,19 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! Update.full do! Update.byId
SqliteDb.TableName SqliteDb.TableName
"test" "test"
{ emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } } { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
} }
] ]
testList "fullFunc" [ testList "byFunc" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
do! Update.fullFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } do! Update.byFunc
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one" let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one"
Expect.isSome after "There should have been a document returned post-update" Expect.isSome after "There should have been a document returned post-update"
Expect.equal Expect.equal
@ -429,15 +430,18 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! Update.fullFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } do! Update.byFunc
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
} }
] ]
testList "partialById" [ ]
testList "Patch" [
testList "byId" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
do! Update.partialById SqliteDb.TableName "one" {| NumValue = 44 |} do! Patch.byId SqliteDb.TableName "one" {| NumValue = 44 |}
let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one" let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one"
Expect.isSome after "There should have been a document returned post-update" Expect.isSome after "There should have been a document returned post-update"
Expect.equal after.Value.NumValue 44 "The updated document is not correct" Expect.equal after.Value.NumValue 44 "The updated document is not correct"
@ -449,15 +453,15 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! Update.partialById SqliteDb.TableName "test" {| Foo = "green" |} do! Patch.byId SqliteDb.TableName "test" {| Foo = "green" |}
} }
] ]
testList "partialByField" [ testList "byField" [
testTask "succeeds when a document is updated" { testTask "succeeds when a document is updated" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
do! Update.partialByField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |} do! Patch.byField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |}
let! after = Count.byField SqliteDb.TableName "NumValue" EQ 77 let! after = Count.byField SqliteDb.TableName "NumValue" EQ 77
Expect.equal after 2L "There should have been 2 documents returned" Expect.equal after 2L "There should have been 2 documents returned"
} }
@ -468,7 +472,7 @@ let integrationTests =
Expect.isEmpty before "There should have been no documents returned" Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test // This not raising an exception is the test
do! Update.partialByField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |} do! Patch.byField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |}
} }
] ]
] ]