Version 4 rc1 (#6)
Changes in this version: - **BREAKING CHANGE**: All `*byField`/`*ByField` functions are now `*byFields`/`*ByFields`, and take a `FieldMatch` case before the list of fields. The `Compat` namespace in both libraries will assist in this transition. In support of this change, the `Field` parameter name is optional; the library will generate parameter names for it if they are not specified. - **BREAKING CHANGE**: The `Query` namespaces have had some significant work, particularly from the full-query perspective. Most have been broken up into the base query and modifiers `by*` that will combine the base query with the `WHERE` clause needed to satisfy the criteria. - **FEATURE / BREAKING CHANGE**: PostgreSQL document fields will now be cast to numeric if the parameter value passed to the query is numeric. This drove the `Query` breaking changes, as the fields need to have their intended value for the library to generate the appropriate SQL. Additionally, if code assumes the library can be given something like `8` and transform it to `"8"`, this is no longer the case. - **FEATURE**: All `Find` queries (except `byId`/`ById`) now have a version with the `Ordered` suffix. These take a list of fields by which the query should be ordered. A new `Field` method called `Named` can assist with creating these fields. Prefixing the field name with `n:` will cast the field to numeric in PostgreSQL (and will be ignored by SQLite); adding " DESC" to the field name will sort it descending (Z-A, high to low) instead of ascending (A-Z, low to high). - **BREAKING CHANGE** (PostgreSQL only): `fieldNameParam`/`Parameters.FieldName` are now plural. The function still only generates one parameter, but the name is now the same between PostgreSQL and SQLite. The goal of this library is to abstract the differences away as much as practical, and this furthers that end. There are functions with these names in the `Compat` namespace. - **FEATURE**: In the F# v3 library, lists of parameters were expected to be F#'s `List` type, and the C# version took either `List<T>` or `IEnumerable<T>`. In this version, these all expect `seq`/`IEnumerable<T>`. F#'s `List` satisfies the `seq` constraints, so this should not be a breaking change. - **FEATURE**: `Field`s now may have qualifiers; this allows tables to be aliased when joining multiple tables (as all have the same `data` column). F# users can use `with` to specify this at creation, and both F# and C# can use the `WithQualifier` method to create a field with the qualifier specified. Parameter names for fields may be specified in a similar way, substituting `ParameterName` for `Qualifier`. Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using Expecto.CSharp;
|
||||
using Expecto;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using BitBadger.Documents.Sqlite;
|
||||
|
||||
namespace BitBadger.Documents.Tests.CSharp;
|
||||
@@ -18,10 +17,10 @@ public static class SqliteCSharpExtensionTests
|
||||
/// Integration tests for the SQLite extension methods
|
||||
/// </summary>
|
||||
[Tests]
|
||||
public static readonly Test Integration = TestList("Sqlite.C#.Extensions", new[]
|
||||
{
|
||||
TestList("CustomSingle", new[]
|
||||
{
|
||||
public static readonly Test Integration = TestList("Sqlite.C#.Extensions",
|
||||
[
|
||||
TestList("CustomSingle",
|
||||
[
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -29,7 +28,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
new[] { Parameters.Id("one") }, Results.FromData<JsonDocument>);
|
||||
[Parameters.Id("one")], Results.FromData<JsonDocument>);
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc!.Id, "one", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -40,19 +39,19 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
new[] { Parameters.Id("eighty") }, Results.FromData<JsonDocument>);
|
||||
[Parameters.Id("eighty")], Results.FromData<JsonDocument>);
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
TestList("CustomList", new[]
|
||||
{
|
||||
]),
|
||||
TestList("CustomList",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.CustomList(Query.SelectFromTable(SqliteDb.TableName), Parameters.None,
|
||||
var docs = await conn.CustomList(Query.Find(SqliteDb.TableName), Parameters.None,
|
||||
Results.FromData<JsonDocument>);
|
||||
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
||||
}),
|
||||
@@ -63,13 +62,13 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.CustomList(
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||
new[] { new SqliteParameter("@value", 100) }, Results.FromData<JsonDocument>);
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", [new("@value", 100)],
|
||||
Results.FromData<JsonDocument>);
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
TestList("CustomNonQuery", new[]
|
||||
{
|
||||
]),
|
||||
TestList("CustomNonQuery",
|
||||
[
|
||||
TestCase("succeeds when operating on data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -88,12 +87,12 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
await conn.CustomNonQuery($"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||
new[] { new SqliteParameter("@value", 100) });
|
||||
[new("@value", 100)]);
|
||||
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 5L, "There should be 5 documents remaining in the table");
|
||||
})
|
||||
}),
|
||||
]),
|
||||
TestCase("CustomScalar succeeds", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -107,41 +106,44 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
|
||||
Func<string, ValueTask<bool>> itExists = async name =>
|
||||
await conn.CustomScalar(
|
||||
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
||||
new SqliteParameter[] { new("@name", name) }, Results.ToExists);
|
||||
|
||||
var exists = await itExists("ensured");
|
||||
var alsoExists = await itExists("idx_ensured_key");
|
||||
var exists = await ItExists("ensured");
|
||||
var alsoExists = await ItExists("idx_ensured_key");
|
||||
Expect.isFalse(exists, "The table should not exist already");
|
||||
Expect.isFalse(alsoExists, "The key index should not exist already");
|
||||
|
||||
await conn.EnsureTable("ensured");
|
||||
|
||||
exists = await itExists("ensured");
|
||||
alsoExists = await itExists("idx_ensured_key");
|
||||
exists = await ItExists("ensured");
|
||||
alsoExists = await ItExists("idx_ensured_key");
|
||||
Expect.isTrue(exists, "The table should now exist");
|
||||
Expect.isTrue(alsoExists, "The key index should now exist");
|
||||
return;
|
||||
|
||||
Task<bool> ItExists(string name) =>
|
||||
conn.CustomScalar($"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
||||
[new("@name", name)], Results.ToExists);
|
||||
}),
|
||||
TestCase("EnsureFieldIndex succeeds", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
var indexExists = () => conn.CustomScalar(
|
||||
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
||||
Parameters.None, Results.ToExists);
|
||||
|
||||
var exists = await indexExists();
|
||||
var exists = await IndexExists();
|
||||
Expect.isFalse(exists, "The index should not exist already");
|
||||
|
||||
await conn.EnsureTable("ensured");
|
||||
await conn.EnsureFieldIndex("ensured", "test", new[] { "Id", "Category" });
|
||||
exists = await indexExists();
|
||||
await conn.EnsureFieldIndex("ensured", "test", ["Id", "Category"]);
|
||||
exists = await IndexExists();
|
||||
Expect.isTrue(exists, "The index should now exist");
|
||||
return;
|
||||
|
||||
Task<bool> IndexExists() =>
|
||||
conn.CustomScalar(
|
||||
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
||||
Parameters.None, Results.ToExists);
|
||||
}),
|
||||
TestList("Insert", new[]
|
||||
{
|
||||
TestList("Insert",
|
||||
[
|
||||
TestCase("succeeds", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -168,9 +170,9 @@ public static class SqliteCSharpExtensionTests
|
||||
// This is what is supposed to happen
|
||||
}
|
||||
})
|
||||
}),
|
||||
TestList("Save", new[]
|
||||
{
|
||||
]),
|
||||
TestList("Save",
|
||||
[
|
||||
TestCase("succeeds when a document is inserted", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -203,7 +205,7 @@ public static class SqliteCSharpExtensionTests
|
||||
Expect.equal(after!.Id, "test", "The updated document is not correct");
|
||||
Expect.isNull(after.Sub, "There should not have been a sub-document in the updated document");
|
||||
})
|
||||
}),
|
||||
]),
|
||||
TestCase("CountAll succeeds", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -219,11 +221,11 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await conn.CountByField(SqliteDb.TableName, Field.EQ("Value", "purple"));
|
||||
var theCount = await conn.CountByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")]);
|
||||
Expect.equal(theCount, 2L, "There should have been 2 matching documents");
|
||||
}),
|
||||
TestList("ExistsById", new[]
|
||||
{
|
||||
TestList("ExistsById",
|
||||
[
|
||||
TestCase("succeeds when a document exists", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -242,16 +244,16 @@ public static class SqliteCSharpExtensionTests
|
||||
var exists = await conn.ExistsById(SqliteDb.TableName, "seven");
|
||||
Expect.isFalse(exists, "There should not have been an existing document");
|
||||
})
|
||||
}),
|
||||
TestList("ExistsByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("ExistsByFields",
|
||||
[
|
||||
TestCase("succeeds when documents exist", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.GE("NumValue", 10));
|
||||
var exists = await conn.ExistsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.GE("NumValue", 10)]);
|
||||
Expect.isTrue(exists, "There should have been existing documents");
|
||||
}),
|
||||
TestCase("succeeds when no matching documents exist", async () =>
|
||||
@@ -260,12 +262,13 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.EQ("Nothing", "none"));
|
||||
var exists =
|
||||
await conn.ExistsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Nothing", "none")]);
|
||||
Expect.isFalse(exists, "There should not have been any existing documents");
|
||||
})
|
||||
}),
|
||||
TestList("FindAll", new[]
|
||||
{
|
||||
]),
|
||||
TestList("FindAll",
|
||||
[
|
||||
TestCase("succeeds when there is data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -285,9 +288,46 @@ public static class SqliteCSharpExtensionTests
|
||||
var results = await conn.FindAll<JsonDocument>(SqliteDb.TableName);
|
||||
Expect.isEmpty(results, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
TestList("FindById", new[]
|
||||
{
|
||||
]),
|
||||
TestList("FindAllOrdered",
|
||||
[
|
||||
TestCase("succeeds when ordering numerically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var results = await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue")]);
|
||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "one|three|two|four|five",
|
||||
"The documents were not ordered correctly");
|
||||
}),
|
||||
TestCase("succeeds when ordering numerically descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var results =
|
||||
await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue DESC")]);
|
||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "five|four|two|three|one",
|
||||
"The documents were not ordered correctly");
|
||||
}),
|
||||
TestCase("succeeds when ordering alphabetically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var results = await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("Id DESC")]);
|
||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "two|three|one|four|five",
|
||||
"The documents were not ordered correctly");
|
||||
})
|
||||
]),
|
||||
TestList("FindById",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -307,16 +347,17 @@ public static class SqliteCSharpExtensionTests
|
||||
var doc = await conn.FindById<string, JsonDocument>(SqliteDb.TableName, "eighty-seven");
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
TestList("FindByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("FindByFields",
|
||||
[
|
||||
TestCase("succeeds when documents are found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.GT("NumValue", 15));
|
||||
var docs = await conn.FindByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.GT("NumValue", 15)]);
|
||||
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
@@ -325,19 +366,46 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "mauve"));
|
||||
var docs = await conn.FindByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Value", "mauve")]);
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
TestList("FindFirstByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("ByFieldsOrdered",
|
||||
[
|
||||
TestCase("succeeds when documents are found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.GT("NumValue", 15)], [Field.Named("Id")]);
|
||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "five|four",
|
||||
"There should have been two documents returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.GT("NumValue", 15)], [Field.Named("Id DESC")]);
|
||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|five",
|
||||
"There should have been two documents returned");
|
||||
})
|
||||
]),
|
||||
TestList("FindFirstByFields",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "another"));
|
||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Value", "another")]);
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -347,9 +415,10 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Sub.Foo", "green"));
|
||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Sub.Foo", "green")]);
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.contains(new[] { "two", "four" }, doc!.Id, "An incorrect document was returned");
|
||||
Expect.contains(["two", "four"], doc!.Id, "An incorrect document was returned");
|
||||
}),
|
||||
TestCase("succeeds when a document is not found", async () =>
|
||||
{
|
||||
@@ -357,12 +426,38 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "absent"));
|
||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Value", "absent")]);
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
TestList("UpdateById", new[]
|
||||
{
|
||||
]),
|
||||
TestList("FindFirstByFieldsOrdered",
|
||||
[
|
||||
TestCase("succeeds when sorting ascending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar")]);
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal("two", doc!.Id, "An incorrect document was returned");
|
||||
}),
|
||||
TestCase("succeeds when sorting descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]);
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal("four", doc!.Id, "An incorrect document was returned");
|
||||
})
|
||||
]),
|
||||
TestList("UpdateById",
|
||||
[
|
||||
TestCase("succeeds when a document is updated", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -389,9 +484,9 @@ public static class SqliteCSharpExtensionTests
|
||||
await conn.UpdateById(SqliteDb.TableName, "test",
|
||||
new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } });
|
||||
})
|
||||
}),
|
||||
TestList("UpdateByFunc", new[]
|
||||
{
|
||||
]),
|
||||
TestList("UpdateByFunc",
|
||||
[
|
||||
TestCase("succeeds when a document is updated", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -418,9 +513,9 @@ public static class SqliteCSharpExtensionTests
|
||||
await conn.UpdateByFunc(SqliteDb.TableName, doc => doc.Id,
|
||||
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
|
||||
})
|
||||
}),
|
||||
TestList("PatchById", new[]
|
||||
{
|
||||
]),
|
||||
TestList("PatchById",
|
||||
[
|
||||
TestCase("succeeds when a document is updated", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -443,17 +538,18 @@ public static class SqliteCSharpExtensionTests
|
||||
// This not raising an exception is the test
|
||||
await conn.PatchById(SqliteDb.TableName, "test", new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("PatchByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("PatchByFields",
|
||||
[
|
||||
TestCase("succeeds when a document is updated", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||
var after = await conn.CountByField(SqliteDb.TableName, Field.EQ("NumValue", 77));
|
||||
await conn.PatchByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")],
|
||||
new { NumValue = 77 });
|
||||
var after = await conn.CountByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 77)]);
|
||||
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
||||
}),
|
||||
TestCase("succeeds when no document is updated", async () =>
|
||||
@@ -464,18 +560,19 @@ public static class SqliteCSharpExtensionTests
|
||||
Expect.isEmpty(before, "There should have been no documents returned");
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||
await conn.PatchByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "burgundy")],
|
||||
new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsById", new[]
|
||||
{
|
||||
]),
|
||||
TestList("RemoveFieldsById",
|
||||
[
|
||||
TestCase("succeeds when fields are removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Sub", "Value" });
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["Sub", "Value"]);
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||
@@ -488,7 +585,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["AFieldThatIsNotThere"]);
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
@@ -496,18 +593,19 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Value" });
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["Value"]);
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("RemoveFieldsByFields",
|
||||
[
|
||||
TestCase("succeeds when a field is removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Sub" });
|
||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)],
|
||||
["Sub"]);
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
@@ -519,7 +617,8 @@ public static class SqliteCSharpExtensionTests
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Nothing" });
|
||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)],
|
||||
["Nothing"]);
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
@@ -527,11 +626,12 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.NE("Abracadabra", "apple"), new[] { "Value" });
|
||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Abracadabra", "apple")],
|
||||
["Value"]);
|
||||
})
|
||||
}),
|
||||
TestList("DeleteById", new[]
|
||||
{
|
||||
]),
|
||||
TestList("DeleteById",
|
||||
[
|
||||
TestCase("succeeds when a document is deleted", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
@@ -552,16 +652,16 @@ public static class SqliteCSharpExtensionTests
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||
})
|
||||
}),
|
||||
TestList("DeleteByField", new[]
|
||||
{
|
||||
]),
|
||||
TestList("DeleteByFields",
|
||||
[
|
||||
TestCase("succeeds when documents are deleted", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(SqliteDb.TableName, Field.NE("Value", "purple"));
|
||||
await conn.DeleteByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Value", "purple")]);
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
||||
}),
|
||||
@@ -571,11 +671,11 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(SqliteDb.TableName, Field.EQ("Value", "crimson"));
|
||||
await conn.DeleteByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "crimson")]);
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||
})
|
||||
}),
|
||||
]),
|
||||
TestCase("Clean up database", () => Sqlite.Configuration.UseConnectionString("data source=:memory:"))
|
||||
});
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user