using System.IO.Pipelines; using Expecto.CSharp; using Expecto; using BitBadger.Documents.Postgres; using Npgsql; namespace BitBadger.Documents.Tests.CSharp; using static CommonExtensionsAndTypesForNpgsqlFSharp; using static Runner; /// /// C# tests for the extensions on the NpgsqlConnection type /// public class PostgresCSharpExtensionTests { private static async Task LoadDocs(NpgsqlConnection conn) { foreach (var doc in JsonDocument.TestDocuments) await conn.Insert(SqliteDb.TableName, doc); } /// /// Create a connection to the throwaway database /// /// The throwaway database for which a connection should be made /// An open connection to the throwaway database private static NpgsqlConnection MkConn(ThrowawayPostgresDb db) { var conn = new NpgsqlConnection(db.ConnectionString); conn.Open(); return conn; } /// Set up a stream writer for a test private static PipeWriter WriteStream(Stream stream) => PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true)); /// Get the text of the given stream private static string StreamText(Stream stream) { stream.Position = 0L; using StreamReader reader = new(stream); return reader.ReadToEnd(); } /// Verify a JSON array begins with "[" and ends with "]" private static void VerifyBeginEnd(string json) { Expect.stringStarts(json, "[", "The array should have started with `[`"); Expect.stringEnds(json, "]", "The array should have ended with `]`"); } /// Verify the presence of a document by its ID private static void VerifyDocById(string json, string docId) => Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present"); /// Verify the presence of a document by its ID private static void VerifySingleById(string json, string docId) { VerifyBeginEnd(json); Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present"); } /// Verify the presence of any of the given document IDs in the given JSON private static void VerifyAnyById(string json, IEnumerable docIds) { var theIds = docIds.ToList(); if (theIds.Any(it => json.Contains($"{{\"Id\": \"{it}\""))) return; var ids = string.Join(", ", theIds); Expect.isTrue(false, $"Could not find any of IDs {ids} in {json}"); } /// Verify the JSON for `all` returning data private static void VerifyAllData(string json) { VerifyBeginEnd(json); IEnumerable ids = ["one", "two", "three", "four", "five"]; foreach (var docId in ids) VerifyDocById(json, docId); } /// Verify an empty JSON array private static void VerifyEmpty(string json) => Expect.equal(json, "[]", "There should be no documents returned"); /// Verify an empty JSON document private static void VerifyNoDoc(string json) => Expect.equal(json, "{}", "There should be no document returned"); /// Verify the JSON for an ordered query private static void VerifyExpectedOrder(string json, string idFirst, string idSecond, string? idThird = null, string? idFourth = null, string? idFifth = null) { var firstIdx = json.IndexOf($"{{\"Id\": \"{idFirst}\",", StringComparison.Ordinal); var secondIdx = json.IndexOf($"{{\"Id\": \"{idSecond}\",", StringComparison.Ordinal); VerifyBeginEnd(json); Expect.isGreaterThan(secondIdx, firstIdx, $"`{idSecond}` should have been after `{idFirst}`"); if (idThird is null) return; var thirdIdx = json.IndexOf($"{{\"Id\": \"{idThird}\",", StringComparison.Ordinal); Expect.isGreaterThan(thirdIdx, secondIdx, $"`{idThird}` should have been after `{idSecond}`"); if (idFourth is null) return; var fourthIdx = json.IndexOf($"{{\"Id\": \"{idFourth}\",", StringComparison.Ordinal); Expect.isGreaterThan(fourthIdx, thirdIdx, $"`{idFourth}` should have been after `{idThird}`"); if (idFifth is null) return; var fifthIdx = json.IndexOf($"{{\"Id\": \"{idFifth}\",", StringComparison.Ordinal); Expect.isGreaterThan(fifthIdx, fourthIdx, $"`{idFifth}` should have been after `{idFourth}`"); } /// /// Integration tests for the SQLite extension methods /// [Tests] public static readonly Test Integration = TestList("Postgres.C#.Extensions", [ TestList("CustomList", [ TestCase("succeeds when data is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.CustomList(Query.Find(PostgresDb.TableName), Parameters.None, Results.FromData); Expect.equal(docs.Count, 5, "There should have been 5 documents returned"); }), TestCase("succeeds when data is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.CustomList( $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.FromData); Expect.isEmpty(docs, "There should have been no documents returned"); }) ]), TestList("CustomJsonArray", [ TestCase("succeeds when data is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.CustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, Results.JsonFromData); VerifyBeginEnd(docs); Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned"); }), TestCase("succeeds when data is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.CustomJsonArray( $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData); VerifyEmpty(docs); }) ]), TestList("WriteJsonArray", [ TestCase("succeeds when data is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteCustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, Results.JsonFromData); var docs = StreamText(stream); VerifyBeginEnd(docs); Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when data is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteCustomJsonArray( $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("CustomSingle", [ TestCase("succeeds when a row is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id", [Tuple.Create("@id", Sql.@string("one"))], Results.FromData); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "one", "The incorrect document was returned"); }), TestCase("succeeds when a row is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id", [Tuple.Create("@id", Sql.@string("eighty"))], Results.FromData); Expect.isNull(doc, "There should not have been a document returned"); }) ]), TestList("CustomJsonSingle", [ TestCase("succeeds when a row is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.CustomJsonSingle( $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id", [Tuple.Create("@id", Sql.@string("one"))], Results.JsonFromData); Expect.stringStarts(doc, "{", "The document should have started with an open brace"); Expect.stringContains(doc, "\"Id\": \"one\"", "An incorrect document was returned"); Expect.stringEnds(doc, "}", "The document should have ended with a closing brace"); }), TestCase("succeeds when a row is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.CustomJsonSingle( $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id", [Tuple.Create("@id", Sql.@string("eighty"))], Results.JsonFromData); Expect.equal(doc, "{}", "There should not have been a document returned"); }) ]), TestList("CustomNonQuery", [ TestCase("succeeds when operating on data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.CustomNonQuery($"DELETE FROM {PostgresDb.TableName}", Parameters.None); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 0, "There should be no documents remaining in the table"); }), TestCase("succeeds when no data matches where clause", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.CustomNonQuery($"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))]); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should be 5 documents remaining in the table"); }) ]), TestCase("Scalar succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var nbr = await conn.CustomScalar("SELECT 5 AS test_value", Parameters.None, row => row.@int("test_value")); Expect.equal(nbr, 5, "The query should have returned the number 5"); }), TestCase("EnsureTable succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var exists = await TableExists(); var alsoExists = await KeyExists(); 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 TableExists(); alsoExists = await KeyExists(); Expect.isTrue(exists, "The table should now exist"); Expect.isTrue(alsoExists, "The key index should now exist"); return; Task KeyExists() => conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it", Parameters.None, Results.ToExists); Task TableExists() => conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'ensured') AS it", Parameters.None, Results.ToExists); }), TestCase("EnsureDocumentIndex succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var exists = await IndexExists(); Expect.isFalse(exists, "The index should not exist already"); await conn.EnsureTable("ensured"); await conn.EnsureDocumentIndex("ensured", DocumentIndex.Optimized); exists = await IndexExists(); Expect.isTrue(exists, "The index should now exist"); return; Task IndexExists() => conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it", Parameters.None, Results.ToExists); }), TestCase("EnsureFieldIndex succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var exists = await IndexExists(); Expect.isFalse(exists, "The index should not exist already"); await conn.EnsureTable("ensured"); await conn.EnsureFieldIndex("ensured", "test", ["Id", "Category"]); exists = await IndexExists(); Expect.isTrue(exists, "The index should now exist"); return; Task IndexExists() => conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it", Parameters.None, Results.ToExists); }), TestList("Insert", [ TestCase("succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should be no documents in the table"); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "turkey", Sub = new() { Foo = "gobble", Bar = "gobble" } }); var after = await conn.FindAll(PostgresDb.TableName); Expect.equal(after.Count, 1, "There should have been one document inserted"); }), TestCase("fails for duplicate key", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "test" }); try { await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "test" }); Expect.isTrue(false, "An exception should have been raised for duplicate document ID insert"); } catch (Exception) { // This is what should have happened } }) ]), TestList("Save", [ TestCase("succeeds when a document is inserted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should be no documents in the table"); await conn.Save(PostgresDb.TableName, new JsonDocument { Id = "test", Sub = new() { Foo = "a", Bar = "b" } }); var after = await conn.FindAll(PostgresDb.TableName); Expect.equal(after.Count, 1, "There should have been one document inserted"); }), TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "test", Sub = new() { Foo = "a", Bar = "b" } }); var before = await conn.FindById(PostgresDb.TableName, "test"); Expect.isNotNull(before, "There should have been a document returned"); Expect.equal(before.Id, "test", "The document is not correct"); await conn.Save(PostgresDb.TableName, new JsonDocument { Id = "test", Sub = new() { Foo = "c", Bar = "d" } }); var after = await conn.FindById(PostgresDb.TableName, "test"); Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.equal(after.Sub!.Foo, "c", "The updated document is not correct"); }) ]), TestCase("CountAll succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var theCount = await conn.CountAll(PostgresDb.TableName); Expect.equal(theCount, 5, "There should have been 5 matching documents"); }), TestCase("CountByFields succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var theCount = await conn.CountByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")]); Expect.equal(theCount, 2, "There should have been 2 matching documents"); }), TestCase("CountByContains succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var theCount = await conn.CountByContains(PostgresDb.TableName, new { Value = "purple" }); Expect.equal(theCount, 2, "There should have been 2 matching documents"); }), TestCase("CountByJsonPath succeeds", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var theCount = await conn.CountByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 5)"); Expect.equal(theCount, 3, "There should have been 3 matching documents"); }), TestList("ExistsById", [ TestCase("succeeds when a document exists", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsById(PostgresDb.TableName, "three"); Expect.isTrue(exists, "There should have been an existing document"); }), TestCase("succeeds when a document does not exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsById(PostgresDb.TableName, "seven"); Expect.isFalse(exists, "There should not have been an existing document"); }) ]), TestList("ExistsByField", [ TestCase("succeeds when documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Exists("Sub")]); Expect.isTrue(exists, "There should have been existing documents"); }), TestCase("succeeds when documents do not exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "six")]); Expect.isFalse(exists, "There should not have been existing documents"); }) ]), TestList("ExistsByContains", [ TestCase("succeeds when documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByContains(PostgresDb.TableName, new { NumValue = 10 }); Expect.isTrue(exists, "There should have been existing documents"); }), TestCase("succeeds when no matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByContains(PostgresDb.TableName, new { Nothing = "none" }); Expect.isFalse(exists, "There should not have been any existing documents"); }) ]), TestList("ExistsByJsonPath", [ TestCase("succeeds when documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByJsonPath(PostgresDb.TableName, "$.Sub.Foo ? (@ == \"green\")"); Expect.isTrue(exists, "There should have been existing documents"); }), TestCase("succeeds when no matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var exists = await conn.ExistsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 1000)"); Expect.isFalse(exists, "There should not have been any existing documents"); }) ]), TestList("FindAll", [ TestCase("succeeds when there is data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "one" }); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "three" }); await conn.Insert(PostgresDb.TableName, new JsonDocument { Id = "five" }); var results = await conn.FindAll(PostgresDb.TableName); Expect.equal(results.Count, 3, "There should have been 3 documents returned"); }), TestCase("succeeds when there is no data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var results = await conn.FindAll(PostgresDb.TableName); Expect.isEmpty(results, "There should have been no documents returned"); }) ]), TestList("FindAllOrdered", [ TestCase("succeeds when ordering numerically", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var results = await conn.FindAllOrdered(PostgresDb.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 = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var results = await conn.FindAllOrdered(PostgresDb.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 = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var results = await conn.FindAllOrdered(PostgresDb.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 = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindById(PostgresDb.TableName, "two"); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "two", "The incorrect document was returned"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindById(PostgresDb.TableName, "three hundred eighty-seven"); Expect.isNull(doc, "There should not have been a document returned"); }) ]), TestList("FindByFields", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]); Expect.equal(docs.Count, 1, "There should have been one document returned"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "mauve")]); Expect.isEmpty(docs, "There should have been no documents returned"); }) ]), TestList("FindByFieldsOrdered", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id")]); Expect.hasLength(docs, 2, "There should have been two document returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "five|four", "The documents were not ordered correctly"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); Expect.hasLength(docs, 2, "There should have been two document returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|five", "The documents were not ordered correctly"); }) ]), TestList("FindByContains", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } }); Expect.equal(docs.Count, 2, "There should have been two documents returned"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByContains(PostgresDb.TableName, new { Value = "mauve" }); Expect.isEmpty(docs, "There should have been no documents returned"); }) ]), TestList("FindByContainsOrdered", [ // Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar")]); Expect.hasLength(docs, 2, "There should have been two documents returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "two|four", "Documents not ordered correctly"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar DESC")]); Expect.hasLength(docs, 2, "There should have been two documents returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|two", "Documents not ordered correctly"); }) ]), TestList("FindByJsonPath", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 15)"); Expect.equal(docs.Count, 3, "There should have been 3 documents returned"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)"); Expect.isEmpty(docs, "There should have been no documents returned"); }) ]), TestList("FindByJsonPathOrdered", [ // Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4 TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue")]); Expect.hasLength(docs, 3, "There should have been 3 documents returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "one|three|two", "Documents not ordered correctly"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var docs = await conn.FindByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue DESC")]); Expect.hasLength(docs, 3, "There should have been 3 documents returned"); Expect.equal(string.Join('|', docs.Select(x => x.Id)), "two|three|one", "Documents not ordered correctly"); }) ]), TestList("FindFirstByFields", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "two", "The incorrect document was returned"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")]); Expect.isNotNull(doc, "There should have been a document returned"); Expect.contains(["five", "four"], doc.Id, "An incorrect document was returned"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "absent")]); Expect.isNull(doc, "There should not have been a document returned"); }) ]), TestList("FindFirstByFieldsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id")]); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal("five", doc.Id, "An incorrect document was returned"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal("four", doc.Id, "An incorrect document was returned"); }) ]), TestList("FindFirstByContains", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByContains(PostgresDb.TableName, new { Value = "another" }); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "two", "The incorrect document was returned"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } }); Expect.isNotNull(doc, "There should have been a document returned"); Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByContains(PostgresDb.TableName, new { Value = "absent" }); Expect.isNull(doc, "There should not have been a document returned"); }) ]), TestList("FindFirstByContainsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Value")]); 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 = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal("four", doc.Id, "An incorrect document was returned"); }) ]), TestList("FindFirstByJsonPath", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByJsonPath(PostgresDb.TableName, "$.Value ? (@ == \"FIRST!\")"); Expect.isNotNull(doc, "There should have been a document returned"); Expect.equal(doc.Id, "one", "The incorrect document was returned"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByJsonPath(PostgresDb.TableName, "$.Sub.Foo ? (@ == \"green\")"); Expect.isNotNull(doc, "There should have been a document returned"); Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByJsonPath(PostgresDb.TableName, "$.Id ? (@ == \"nope\")"); Expect.isNull(doc, "There should not have been a document returned"); }) ]), TestList("FindFirstByJsonPathOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByJsonPathOrdered(PostgresDb.TableName, "$.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 = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var doc = await conn.FindFirstByJsonPathOrdered(PostgresDb.TableName, "$.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("JsonAll", [ TestCase("succeeds when there is data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyAllData(await conn.JsonAll(PostgresDb.TableName)); }), TestCase("succeeds when there is no data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); VerifyEmpty(await conn.JsonAll(PostgresDb.TableName)); }) ]), TestList("JsonAllOrdered", [ TestCase("succeeds when ordering numerically", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder(await conn.JsonAllOrdered(PostgresDb.TableName, [Field.Named("n:NumValue")]), "one", "three", "two", "four", "five"); }), TestCase("succeeds when ordering numerically descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder(await conn.JsonAllOrdered(PostgresDb.TableName, [Field.Named("n:NumValue DESC")]), "five", "four", "two", "three", "one"); }), TestCase("succeeds when ordering alphabetically", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder(await conn.JsonAllOrdered(PostgresDb.TableName, [Field.Named("Id DESC")]), "two", "three", "one", "four", "five"); }) ]), TestList("JsonById", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var json = await conn.JsonById(PostgresDb.TableName, "two"); Expect.stringStarts(json, """{"Id": "two",""", "An incorrect document was returned"); Expect.stringEnds(json, "}", "JSON should have ended with this document"); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyNoDoc(await conn.JsonById(PostgresDb.TableName, "three hundred eighty-seven")); }) ]), TestList("JsonByFields", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifySingleById( await conn.JsonByFields(PostgresDb.TableName, FieldMatch.All, [Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]), "four"); }), TestCase("succeeds when documents are found using IN with numeric field", async() => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifySingleById( await conn.JsonByFields(PostgresDb.TableName, FieldMatch.All, [Field.In("NumValue", [2, 4, 6, 8])]), "three"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyEmpty(await conn.JsonByFields(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "mauve"), Field.NotEqual("NumValue", 40)])); }), TestCase("succeeds for InArray when matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.EnsureTable(PostgresDb.TableName); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); var json = await conn.JsonByFields(PostgresDb.TableName, FieldMatch.All, [Field.InArray("Values", PostgresDb.TableName, ["c"])]); VerifyBeginEnd(json); VerifyDocById(json, "first"); VerifyDocById(json, "second"); }), TestCase("succeeds for InArray when no matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.EnsureTable(PostgresDb.TableName); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); VerifyEmpty(await conn.JsonByFields(PostgresDb.TableName, FieldMatch.All, [Field.InArray("Values", PostgresDb.TableName, ["j"])])); }) ]), TestList("JsonByFieldsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")], [Field.Named("Id")]), "five", "four"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]), "four", "five"); }) ]), TestList("JsonByContains", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var json = await conn.JsonByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } }); VerifyBeginEnd(json); VerifyDocById(json, "two"); VerifyDocById(json, "four"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyEmpty(await conn.JsonByContains(PostgresDb.TableName, new { Value = "mauve" })); }) ]), TestList("JsonByContainsOrdered", [ // Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar")]), "two", "four"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar DESC")]), "four", "two"); }) ]), TestList("JsonByJsonPath", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); var json = await conn.JsonByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 15)"); VerifyBeginEnd(json); VerifyDocById(json, "one"); VerifyDocById(json, "two"); VerifyDocById(json, "three"); }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyEmpty(await conn.JsonByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)")); }) ]), TestList("JsonByJsonPathOrdered", [ // Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4 TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue")]), "one", "three", "two"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyExpectedOrder( await conn.JsonByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue DESC")]), "two", "three", "one"); }) ]), TestList("JsonFirstByFields", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]), "two"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyAnyById( await conn.JsonFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")]), ["five", "four"]); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyNoDoc(await conn.JsonFirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "absent")])); }) ]), TestList("JsonFirstByFieldsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id")]), "five"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]), "four"); }) ]), TestList("JsonFirstByContains", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById(await conn.JsonFirstByContains(PostgresDb.TableName, new { Value = "another" }), "two"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyAnyById(await conn.JsonFirstByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } }), ["two", "four"]); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyNoDoc(await conn.JsonFirstByContains(PostgresDb.TableName, new { Value = "absent" })); }) ]), TestList("JsonFirstByContainsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Value")]), "two"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]), "four"); }) ]), TestList("JsonFirstByJsonPath", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById(await conn.JsonFirstByJsonPath(PostgresDb.TableName, """$.Value ? (@ == "FIRST!")"""), "one"); }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyAnyById(await conn.JsonFirstByJsonPath(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")"""), ["two", "four"]); }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyNoDoc(await conn.JsonFirstByJsonPath(PostgresDb.TableName, """$.Id ? (@ == "nope")""")); }) ]), TestList("JsonFirstByJsonPathOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar")]), "two"); }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); VerifyDocById( await conn.JsonFirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar DESC")]), "four"); }) ]), TestList("WriteJsonAll", [ TestCase("succeeds when there is data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonAll(PostgresDb.TableName, writer); VerifyAllData(StreamText(stream)); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when there is no data", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonAll(PostgresDb.TableName, writer); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonAllOrdered", [ TestCase("succeeds when ordering numerically", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue")]); VerifyExpectedOrder(StreamText(stream), "one", "three", "two", "four", "five"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when ordering numerically descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue DESC")]); VerifyExpectedOrder(StreamText(stream), "five", "four", "two", "three", "one"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when ordering alphabetically", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]); VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonById", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonById(PostgresDb.TableName, writer, "two"); var json = StreamText(stream); Expect.stringStarts(json, """{"Id": "two",""", "An incorrect document was returned"); Expect.stringEnds(json, "}", "JSON should have ended with this document"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonById(PostgresDb.TableName, writer, "three hundred eighty-seven"); VerifyNoDoc(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByFields", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, [Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]); VerifySingleById(StreamText(stream), "four"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when documents are found using IN with numeric field", async() => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, [Field.In("NumValue", [2, 4, 6, 8])]); VerifySingleById(StreamText(stream), "three"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, [Field.Equal("Value", "mauve"), Field.NotEqual("NumValue", 40)]); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds for InArray when matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.EnsureTable(PostgresDb.TableName); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, [Field.InArray("Values", PostgresDb.TableName, ["c"])]); var json = StreamText(stream); VerifyBeginEnd(json); VerifyDocById(json, "first"); VerifyDocById(json, "second"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds for InArray when no matching documents exist", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await conn.EnsureTable(PostgresDb.TableName); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, [Field.InArray("Values", PostgresDb.TableName, ["j"])]); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByFieldsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, [Field.Equal("Value", "purple")], [Field.Named("Id")]); VerifyExpectedOrder(StreamText(stream), "five", "four"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); VerifyExpectedOrder(StreamText(stream), "four", "five"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByContains", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); var json = StreamText(stream); VerifyBeginEnd(json); VerifyDocById(json, "two"); VerifyDocById(json, "four"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Value = "mauve" }); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByContainsOrdered", [ // Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar")]); VerifyExpectedOrder(StreamText(stream), "two", "four"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar DESC")]); VerifyExpectedOrder(StreamText(stream), "four", "two"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByJsonPath", [ TestCase("succeeds when documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)"); var json = StreamText(stream); VerifyBeginEnd(json); VerifyDocById(json, "one"); VerifyDocById(json, "two"); VerifyDocById(json, "three"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when documents are not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)"); VerifyEmpty(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonByJsonPathOrdered", [ // Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4 TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue")]); VerifyExpectedOrder(StreamText(stream), "one", "three", "two"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", [Field.Named("n:NumValue DESC")]); VerifyExpectedOrder(StreamText(stream), "two", "three", "one"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByFields", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, [Field.Equal("Value", "another")]); VerifyDocById(StreamText(stream), "two"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, [Field.Equal("Value", "purple")]); VerifyAnyById(StreamText(stream), ["five", "four"]); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, [Field.Equal("Value", "absent")]); VerifyNoDoc(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByFieldsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id")]); VerifyDocById(StreamText(stream), "five"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, [Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); VerifyDocById(StreamText(stream), "four"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByContains", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "another" }); VerifyDocById(StreamText(stream), "two"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); VerifyAnyById(StreamText(stream), ["two", "four"]); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" }); VerifyNoDoc(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByContainsOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, [Field.Named("Value")]); VerifyDocById(StreamText(stream), "two"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]); VerifyDocById(StreamText(stream), "four"); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByJsonPath", [ TestCase("succeeds when a document is found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")"""); VerifyDocById(StreamText(stream), "one"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when multiple documents are found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")"""); VerifyAnyById(StreamText(stream), ["two", "four"]); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when a document is not found", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")"""); VerifyNoDoc(StreamText(stream)); } finally { await writer.CompleteAsync(); } }) ]), TestList("WriteJsonFirstByJsonPathOrdered", [ TestCase("succeeds when sorting ascending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar")]); VerifyDocById(StreamText(stream), "two"); } finally { await writer.CompleteAsync(); } }), TestCase("succeeds when sorting descending", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await using MemoryStream stream = new(); var writer = WriteStream(stream); try { await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar DESC")]); VerifyDocById(StreamText(stream), "four"); } finally { await writer.CompleteAsync(); } }) ]), TestList("UpdateById", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.UpdateById(PostgresDb.TableName, "one", new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } }); var after = await conn.FindById(PostgresDb.TableName, "one"); Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.equal(after.Id, "one", "The updated document is not correct (ID)"); Expect.equal(after.Value, "", "The updated document is not correct (Value)"); Expect.equal(after.NumValue, 0, "The updated document is not correct (NumValue)"); Expect.isNotNull(after.Sub, "The updated document should have had a sub-document"); Expect.equal(after.Sub!.Foo, "blue", "The updated document is not correct (Sub.Foo)"); Expect.equal(after.Sub.Bar, "red", "The updated document is not correct (Sub.Bar)"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.UpdateById(PostgresDb.TableName, "test", new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } }); }) ]), TestList("UpdateByFunc", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.UpdateByFunc(PostgresDb.TableName, doc => doc.Id, new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); var after = await conn.FindById(PostgresDb.TableName, "one"); Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.equal(after.Id, "one", "The updated document is not correct (ID)"); Expect.equal(after.Value, "le un", "The updated document is not correct (Value)"); Expect.equal(after.NumValue, 1, "The updated document is not correct (NumValue)"); Expect.isNull(after.Sub, "The updated document should not have had a sub-document"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.UpdateByFunc(PostgresDb.TableName, doc => doc.Id, new JsonDocument { Id = "one", Value = "le un", NumValue = 1 }); }) ]), TestList("PatchById", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.PatchById(PostgresDb.TableName, "one", new { NumValue = 44 }); var after = await conn.FindById(PostgresDb.TableName, "one"); Expect.isNotNull(after, "There should have been a document returned post-update"); Expect.equal(after.NumValue, 44, "The updated document is not correct"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.PatchById(PostgresDb.TableName, "test", new { Foo = "green" }); }) ]), TestList("PatchByFields", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.PatchByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")], new { NumValue = 77 }); var after = await conn.CountByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", 77)]); Expect.equal(after, 2, "There should have been 2 documents returned"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.PatchByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "burgundy")], new { Foo = "green" }); }) ]), TestList("PatchByContains", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.PatchByContains(PostgresDb.TableName, new { Value = "purple" }, new { NumValue = 77 }); var after = await conn.CountByContains(PostgresDb.TableName, new { NumValue = 77 }); Expect.equal(after, 2, "There should have been 2 documents returned"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.PatchByContains(PostgresDb.TableName, new { Value = "burgundy" }, new { Foo = "green" }); }) ]), TestList("PatchByJsonPath", [ TestCase("succeeds when a document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.PatchByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 10)", new { NumValue = 1000 }); var after = await conn.CountByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 999)"); Expect.equal(after, 2, "There should have been 2 documents returned"); }), TestCase("succeeds when no document is updated", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); var before = await conn.CountAll(PostgresDb.TableName); Expect.equal(before, 0, "There should have been no documents returned"); // This not raising an exception is the test await conn.PatchByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)", new { Foo = "green" }); }) ]), TestList("RemoveFieldsById", [ TestCase("succeeds when multiple fields are removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Sub", "Value"]); var updated = await Find.ById(PostgresDb.TableName, "two"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.equal(updated.Value, "", "The string value should have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a single field is removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Sub"]); var updated = await Find.ById(PostgresDb.TableName, "two"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.notEqual(updated.Value, "", "The string value should not have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a field is not removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); // This not raising an exception is the test await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["AFieldThatIsNotThere"]); }), TestCase("succeeds when no document is matched", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); // This not raising an exception is the test await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Value"]); }) ]), TestList("RemoveFieldsByFields", [ TestCase("succeeds when multiple fields are removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")], ["Sub", "Value"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.equal(updated.Value, "", "The string value should have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a single field is removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")], ["Sub"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.notEqual(updated.Value, "", "The string value should not have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a field is not removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); // This not raising an exception is the test await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")], ["Nothing"]); }), TestCase("succeeds when no document is matched", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); // This not raising an exception is the test await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.NotEqual("Abracadabra", "apple")], ["Value"]); }) ]), TestList("RemoveFieldsByContains", [ TestCase("succeeds when multiple fields are removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub", "Value"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.equal(updated.Value, "", "The string value should have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a single field is removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.notEqual(updated.Value, "", "The string value should not have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a field is not removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); // This not raising an exception is the test await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Nothing"]); }), TestCase("succeeds when no document is matched", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); // This not raising an exception is the test await conn.RemoveFieldsByContains(PostgresDb.TableName, new { Abracadabra = "apple" }, ["Value"]); }) ]), TestList("RemoveFieldsByJsonPath", [ TestCase("succeeds when multiple fields are removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub", "Value"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.equal(updated.Value, "", "The string value should have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a single field is removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub"]); var updated = await Find.ById(PostgresDb.TableName, "four"); Expect.isNotNull(updated, "The updated document should have been retrieved"); Expect.notEqual(updated.Value, "", "The string value should not have been removed"); Expect.isNull(updated.Sub, "The sub-document should have been removed"); }), TestCase("succeeds when a field is not removed", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); // This not raising an exception is the test await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Nothing"]); }), TestCase("succeeds when no document is matched", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); // This not raising an exception is the test await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")", ["Value"]); }) ]), TestList("DeleteById", [ TestCase("succeeds when a document is deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteById(PostgresDb.TableName, "four"); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 4, "There should have been 4 documents remaining"); }), TestCase("succeeds when a document is not deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteById(PostgresDb.TableName, "thirty"); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) ]), TestList("DeleteByFields", [ TestCase("succeeds when documents are deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByFields(PostgresDb.TableName, FieldMatch.Any, [Field.NotEqual("Value", "purple")]); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 2, "There should have been 2 documents remaining"); }), TestCase("succeeds when documents are not deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "crimson")]); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) ]), TestList("DeleteByContains", [ TestCase("succeeds when documents are deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByContains(PostgresDb.TableName, new { Value = "purple" }); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 3, "There should have been 3 documents remaining"); }), TestCase("succeeds when documents are not deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByContains(PostgresDb.TableName, new { Value = "crimson" }); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) ]), TestList("DeleteByJsonPath", [ TestCase("succeeds when documents are deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByJsonPath(PostgresDb.TableName, "$.Sub.Foo ? (@ == \"green\")"); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 3, "There should have been 3 documents remaining"); }), TestCase("succeeds when documents are not deleted", async () => { await using var db = PostgresDb.BuildDb(); await using var conn = MkConn(db); await LoadDocs(conn); await conn.DeleteByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 100)"); var remaining = await conn.CountAll(PostgresDb.TableName); Expect.equal(remaining, 5, "There should have been 5 documents remaining"); }) ]), ]); }