- Add `Json` module to return JSON strings and write JSON as it's read to a `PipeWriter` - Add `docfx`-based documentation to allow how-to docs and API docs to be generated on the same site Reviewed-on: #11
2843 lines
127 KiB
C#
2843 lines
127 KiB
C#
using System.IO.Pipelines;
|
|
using Expecto.CSharp;
|
|
using Expecto;
|
|
using BitBadger.Documents.Postgres;
|
|
using ThrowawayDb.Postgres;
|
|
|
|
namespace BitBadger.Documents.Tests.CSharp;
|
|
|
|
using static CommonExtensionsAndTypesForNpgsqlFSharp;
|
|
using static Runner;
|
|
|
|
/// <summary>
|
|
/// C# tests for the PostgreSQL implementation of <c>BitBadger.Documents</c>
|
|
/// </summary>
|
|
public static class PostgresCSharpTests
|
|
{
|
|
/// <summary>
|
|
/// Unit tests for the Parameters module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test ParametersTests = TestList("Parameters",
|
|
[
|
|
TestList("Id",
|
|
[
|
|
// NOTE: these tests also exercise all branches of the internal parameterFor function
|
|
TestCase("succeeds for byte ID", () =>
|
|
{
|
|
var it = Parameters.Id((sbyte)7);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int8(7), "Byte ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for unsigned byte ID", () =>
|
|
{
|
|
var it = Parameters.Id((byte)7);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int8(7), "Unsigned byte ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for short ID", () =>
|
|
{
|
|
var it = Parameters.Id((short)44);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int16(44), "Short ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for unsigned short ID", () =>
|
|
{
|
|
var it = Parameters.Id((ushort)64);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int16(64), "Unsigned short ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for integer ID", () =>
|
|
{
|
|
var it = Parameters.Id(88);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@int(88), "ID parameter value incorrect");
|
|
}),
|
|
TestCase("succeeds for unsigned integer ID", () =>
|
|
{
|
|
var it = Parameters.Id((uint)889);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@int(889), "Unsigned int ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for long ID", () =>
|
|
{
|
|
var it = Parameters.Id((long)123);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int64(123), "Long ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for unsigned long ID", () =>
|
|
{
|
|
var it = Parameters.Id((ulong)6464);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.int64(6464), "Unsigned long ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for decimal ID", () =>
|
|
{
|
|
var it = Parameters.Id((decimal)4.56);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@decimal((decimal)4.56), "Decimal ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for single ID", () =>
|
|
{
|
|
var it = Parameters.Id((float)5.67);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@double((float)5.67), "Single ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for double ID", () =>
|
|
{
|
|
var it = Parameters.Id(6.78);
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@double(6.78), "Double ID parameter not constructed correctly");
|
|
}),
|
|
TestCase("succeeds for string ID", () =>
|
|
{
|
|
var it = Parameters.Id("99");
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@string("99"), "ID parameter value incorrect");
|
|
}),
|
|
TestCase("succeeds for non-numeric non-string ID", () =>
|
|
{
|
|
var it = Parameters.Id(new Uri("https://example.com"));
|
|
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.@string("https://example.com/"),
|
|
"Non-numeric, non-string parameter value incorrect");
|
|
})
|
|
]),
|
|
TestCase("Json succeeds", () =>
|
|
{
|
|
var it = Parameters.Json("@test", new { Something = "good" });
|
|
Expect.equal(it.Item1, "@test", "JSON parameter not constructed correctly");
|
|
Expect.equal(it.Item2, Sql.jsonb("{\"Something\":\"good\"}"), "JSON parameter value incorrect");
|
|
}),
|
|
TestList("AddFields",
|
|
[
|
|
TestCase("succeeds when a parameter is added", () =>
|
|
{
|
|
var paramList = Parameters.AddFields([Field.Equal("it", "242")], []).ToList();
|
|
Expect.hasLength(paramList, 1, "There should have been a parameter added");
|
|
var (name, value) = paramList[0];
|
|
Expect.equal(name, "@field0", "Field parameter name not correct");
|
|
Expect.equal(value, Sql.@string("242"), "Field parameter value not correct");
|
|
}),
|
|
TestCase("succeeds when multiple independent parameters are added", () =>
|
|
{
|
|
var paramList = Parameters.AddFields([Field.Equal("me", "you"), Field.Greater("us", "them")],
|
|
[Parameters.Id(14)]).ToList();
|
|
Expect.hasLength(paramList, 3, "There should have been 2 parameters added");
|
|
var (name, value) = paramList[0];
|
|
Expect.equal(name, "@id", "First field parameter name not correct");
|
|
Expect.equal(value, Sql.@int(14), "First field parameter value not correct");
|
|
(name, value) = paramList[1];
|
|
Expect.equal(name, "@field0", "Second field parameter name not correct");
|
|
Expect.equal(value, Sql.@string("you"), "Second field parameter value not correct");
|
|
(name, value) = paramList[2];
|
|
Expect.equal(name, "@field1", "Third parameter name not correct");
|
|
Expect.equal(value, Sql.@string("them"), "Third parameter value not correct");
|
|
}),
|
|
TestCase("succeeds when a parameter is not added", () =>
|
|
{
|
|
var paramList = Parameters.AddFields([Field.Exists("tacos")], []).ToList();
|
|
Expect.isEmpty(paramList, "There should not have been any parameters added");
|
|
}),
|
|
TestCase("succeeds when two parameters are added for one field", () =>
|
|
{
|
|
var paramList =
|
|
Parameters.AddFields([Field.Between("that", "eh", "zed").WithParameterName("@test")], []).ToList();
|
|
Expect.hasLength(paramList, 2, "There should have been 2 parameters added");
|
|
var (name, value) = paramList[0];
|
|
Expect.equal(name, "@testmin", "Minimum field name not correct");
|
|
Expect.equal(value, Sql.@string("eh"), "Minimum field value not correct");
|
|
(name, value) = paramList[1];
|
|
Expect.equal(name, "@testmax", "Maximum field name not correct");
|
|
Expect.equal(value, Sql.@string("zed"), "Maximum field value not correct");
|
|
})
|
|
]),
|
|
TestList("FieldNames",
|
|
[
|
|
TestCase("succeeds for one name", () =>
|
|
{
|
|
var (name, value) = Parameters.FieldNames(["bob"]);
|
|
Expect.equal(name, "@name", "The parameter name was incorrect");
|
|
if (!value.IsString)
|
|
{
|
|
Expect.isTrue(false, "The parameter was not a String type");
|
|
}
|
|
}),
|
|
TestCase("succeeds for multiple names", () =>
|
|
{
|
|
var (name, value) = Parameters.FieldNames(["bob", "tom", "mike"]);
|
|
Expect.equal(name, "@name", "The parameter name was incorrect");
|
|
if (!value.IsStringArray)
|
|
{
|
|
Expect.isTrue(false, "The parameter was not a StringArray type");
|
|
}
|
|
})
|
|
]),
|
|
TestCase("None succeeds", () =>
|
|
{
|
|
Expect.isEmpty(Parameters.None, "The no-params sequence should be empty");
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Unit tests for the Query module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test QueryTests = TestList("Query",
|
|
[
|
|
TestList("WhereByFields",
|
|
[
|
|
TestCase("succeeds for a single field when a logical comparison is passed", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.Any,
|
|
[Field.Greater("theField", "0").WithParameterName("@test")]),
|
|
"data->>'theField' > @test", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for a single field when an existence comparison is passed", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereByFields(FieldMatch.Any, [Field.NotExists("thatField")]),
|
|
"data->>'thatField' IS NULL", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for a single field when a between comparison is passed with numeric values", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.All,
|
|
[Field.Between("aField", 50, 99).WithParameterName("@range")]),
|
|
"(data->>'aField')::numeric BETWEEN @rangemin AND @rangemax", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for a single field when a between comparison is passed with non-numeric values", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.Any,
|
|
[Field.Between("field0", "a", "b").WithParameterName("@alpha")]),
|
|
"data->>'field0' BETWEEN @alphamin AND @alphamax", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for all multiple fields with logical comparisons", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.All,
|
|
[Field.Equal("theFirst", "1"), Field.Equal("numberTwo", "2")]),
|
|
"data->>'theFirst' = @field0 AND data->>'numberTwo' = @field1", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for any multiple fields with an existence comparison", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.Any,
|
|
[Field.NotExists("thatField"), Field.GreaterOrEqual("thisField", 18)]),
|
|
"data->>'thatField' IS NULL OR (data->>'thisField')::numeric >= @field0",
|
|
"WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for all multiple fields with between comparisons", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.All,
|
|
[Field.Between("aField", 50, 99), Field.Between("anotherField", "a", "b")]),
|
|
"(data->>'aField')::numeric BETWEEN @field0min AND @field0max AND data->>'anotherField' BETWEEN @field1min AND @field1max",
|
|
"WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for a field with an InArray comparison", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.WhereByFields(FieldMatch.All, [Field.InArray("theField", "the_table", ["q", "r"])]),
|
|
"data->'theField' ?| @field0", "WHERE clause not correct");
|
|
})
|
|
]),
|
|
TestList("WhereById",
|
|
[
|
|
TestCase("succeeds for numeric ID", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereById(18), "(data->>'Id')::numeric = @id", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for string ID", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereById("18"), "data->>'Id' = @id", "WHERE clause not correct");
|
|
}),
|
|
TestCase("succeeds for non-numeric non-string ID", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereById(new Uri("https://example.com")), "data->>'Id' = @id",
|
|
"WHERE clause not correct");
|
|
}),
|
|
]),
|
|
TestList("Definition",
|
|
[
|
|
TestCase("EnsureTable succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.Definition.EnsureTable(PostgresDb.TableName),
|
|
$"CREATE TABLE IF NOT EXISTS {PostgresDb.TableName} (data JSONB NOT NULL)",
|
|
"CREATE TABLE statement not constructed correctly");
|
|
}),
|
|
TestCase("EnsureDocumentIndex succeeds for full index", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.Definition.EnsureDocumentIndex("schema.tbl", DocumentIndex.Full),
|
|
"CREATE INDEX IF NOT EXISTS idx_tbl_document ON schema.tbl USING GIN (data)",
|
|
"CREATE INDEX statement not constructed correctly");
|
|
}),
|
|
TestCase("EnsureDocumentIndex succeeds for JSONB Path Ops index", () =>
|
|
{
|
|
Expect.equal(
|
|
Postgres.Query.Definition.EnsureDocumentIndex(PostgresDb.TableName, DocumentIndex.Optimized),
|
|
string.Format(
|
|
"CREATE INDEX IF NOT EXISTS idx_{0}_document ON {0} USING GIN (data jsonb_path_ops)",
|
|
PostgresDb.TableName),
|
|
"CREATE INDEX statement not constructed correctly");
|
|
})
|
|
]),
|
|
TestCase("WhereDataContains succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereDataContains("@test"), "data @> @test", "WHERE clause not correct");
|
|
}),
|
|
TestCase("WhereJsonPathMatches succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.WhereJsonPathMatches("@path"), "data @? @path::jsonpath",
|
|
"WHERE clause not correct");
|
|
}),
|
|
TestCase("Patch succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.Patch(PostgresDb.TableName),
|
|
$"UPDATE {PostgresDb.TableName} SET data = data || @data", "Patch query not correct");
|
|
}),
|
|
TestCase("RemoveFields succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.RemoveFields(PostgresDb.TableName),
|
|
$"UPDATE {PostgresDb.TableName} SET data = data - @name", "Field removal query not correct");
|
|
}),
|
|
TestCase("ById succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.ById("test", "14"), "test WHERE data->>'Id' = @id", "By-ID query not correct");
|
|
}),
|
|
TestCase("ByFields succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.ByFields("unit", FieldMatch.Any, [Field.Greater("That", 14)]),
|
|
"unit WHERE (data->>'That')::numeric > @field0", "By-Field query not correct");
|
|
}),
|
|
TestCase("ByContains succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.ByContains("exam"), "exam WHERE data @> @criteria",
|
|
"By-Contains query not correct");
|
|
}),
|
|
TestCase("ByPathMach succeeds", () =>
|
|
{
|
|
Expect.equal(Postgres.Query.ByPathMatch("verify"), "verify WHERE data @? @path::jsonpath",
|
|
"By-JSON Path query not correct");
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Add the test documents to the database
|
|
/// </summary>
|
|
private static async Task LoadDocs()
|
|
{
|
|
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
|
}
|
|
|
|
/// <summary>Set up a stream writer for a test</summary>
|
|
private static PipeWriter WriteStream(Stream stream) =>
|
|
PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
|
|
|
/// <summary>Get the text of the given stream</summary>
|
|
private static string StreamText(Stream stream)
|
|
{
|
|
stream.Position = 0L;
|
|
using StreamReader reader = new(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
/// <summary>Verify a JSON array begins with "[" and ends with "]"</summary>
|
|
private static void VerifyBeginEnd(string json)
|
|
{
|
|
Expect.stringStarts(json, "[", "The array should have started with `[`");
|
|
Expect.stringEnds(json, "]", "The array should have ended with `]`");
|
|
}
|
|
|
|
/// <summary>Verify the presence of a document by its ID</summary>
|
|
private static void VerifyDocById(string json, string docId) =>
|
|
Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present");
|
|
|
|
/// <summary>Verify the presence of a document by its ID</summary>
|
|
private static void VerifySingleById(string json, string docId)
|
|
{
|
|
VerifyBeginEnd(json);
|
|
Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present");
|
|
}
|
|
|
|
/// <summary>Verify the presence of any of the given document IDs in the given JSON</summary>
|
|
private static void VerifyAnyById(string json, IEnumerable<string> 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}");
|
|
}
|
|
|
|
/// <summary>Verify the JSON for `all` returning data</summary>
|
|
private static void VerifyAllData(string json)
|
|
{
|
|
VerifyBeginEnd(json);
|
|
IEnumerable<string> ids = ["one", "two", "three", "four", "five"];
|
|
foreach (var docId in ids) VerifyDocById(json, docId);
|
|
}
|
|
|
|
/// <summary>Verify an empty JSON array</summary>
|
|
private static void VerifyEmpty(string json) =>
|
|
Expect.equal(json, "[]", "There should be no documents returned");
|
|
|
|
/// <summary>Verify an empty JSON document</summary>
|
|
private static void VerifyNoDoc(string json) =>
|
|
Expect.equal(json, "{}", "There should be no document returned");
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Configuration module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test ConfigurationTests = TestList("Configuration",
|
|
[
|
|
TestCase("UseDataSource disposes existing source", () =>
|
|
{
|
|
using var db1 = ThrowawayDatabase.Create(PostgresDb.ConnStr.Value);
|
|
var source = PostgresDb.MkDataSource(db1.ConnectionString);
|
|
Postgres.Configuration.UseDataSource(source);
|
|
|
|
using var db2 = ThrowawayDatabase.Create(PostgresDb.ConnStr.Value);
|
|
Postgres.Configuration.UseDataSource(PostgresDb.MkDataSource(db2.ConnectionString));
|
|
try
|
|
{
|
|
_ = source.OpenConnection();
|
|
Expect.isTrue(false, "Data source should have been disposed");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// This is what should have happened
|
|
}
|
|
}),
|
|
TestCase("DataSource returns configured data source", () =>
|
|
{
|
|
using var db = ThrowawayDatabase.Create(PostgresDb.ConnStr.Value);
|
|
var source = PostgresDb.MkDataSource(db.ConnectionString);
|
|
Postgres.Configuration.UseDataSource(source);
|
|
|
|
Expect.isTrue(ReferenceEquals(source, Postgres.Configuration.DataSource()),
|
|
"Data source should have been the same");
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Custom module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test CustomTests = TestList("Custom",
|
|
[
|
|
TestList("List",
|
|
[
|
|
TestCase("succeeds when data is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Custom.List(Query.Find(PostgresDb.TableName), Parameters.None,
|
|
Results.FromData<JsonDocument>);
|
|
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 LoadDocs();
|
|
|
|
var docs = await Custom.List(
|
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.FromData<JsonDocument>);
|
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("JsonArray",
|
|
[
|
|
TestCase("succeeds when data is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Custom.JsonArray(Query.Find(PostgresDb.TableName), Parameters.None,
|
|
Results.JsonFromData);
|
|
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
|
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
|
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
|
}),
|
|
TestCase("succeeds when data is not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Custom.JsonArray(
|
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData);
|
|
Expect.equal(docs, "[]", "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("WriteJsonArray",
|
|
[
|
|
TestCase("succeeds when data is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer,
|
|
Results.JsonFromData);
|
|
var docs = StreamText(stream);
|
|
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
|
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
|
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
}),
|
|
TestCase("succeeds when data is not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Custom.WriteJsonArray(
|
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData);
|
|
Expect.equal(StreamText(stream), "[]", "There should have been no documents returned");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("Single",
|
|
[
|
|
TestCase("succeeds when a row is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Custom.Single($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
|
[Tuple.Create("@id", Sql.@string("one"))], Results.FromData<JsonDocument>);
|
|
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 LoadDocs();
|
|
|
|
var doc = await Custom.Single($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
|
[Tuple.Create("@id", Sql.@string("eighty"))], Results.FromData<JsonDocument>);
|
|
Expect.isNull(doc, "There should not have been a document returned");
|
|
})
|
|
]),
|
|
TestList("JsonSingle",
|
|
[
|
|
TestCase("succeeds when a row is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Custom.JsonSingle($"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 LoadDocs();
|
|
|
|
var doc = await Custom.JsonSingle($"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("NonQuery",
|
|
[
|
|
TestCase("succeeds when operating on data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Custom.NonQuery($"DELETE FROM {PostgresDb.TableName}", Parameters.None);
|
|
|
|
var remaining = await Count.All(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 LoadDocs();
|
|
|
|
await Custom.NonQuery($"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))]);
|
|
|
|
var remaining = await Count.All(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();
|
|
|
|
var nbr = await Custom.Scalar("SELECT 5 AS test_value", Parameters.None, row => row.@int("test_value"));
|
|
Expect.equal(nbr, 5, "The query should have returned the number 5");
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Definition module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test DefinitionTests = TestList("Definition",
|
|
[
|
|
TestCase("EnsureTable succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
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 Definition.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<bool> TableExists() => Custom.Scalar(
|
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'ensured') AS it", Parameters.None,
|
|
Results.ToExists);
|
|
|
|
Task<bool> KeyExists() => Custom.Scalar(
|
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it", Parameters.None,
|
|
Results.ToExists);
|
|
}),
|
|
TestCase("EnsureDocumentIndex succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
var exists = await IndexExists();
|
|
Expect.isFalse(exists, "The index should not exist already");
|
|
|
|
await Definition.EnsureTable("ensured");
|
|
await Definition.EnsureDocumentIndex("ensured", DocumentIndex.Optimized);
|
|
exists = await IndexExists();
|
|
Expect.isTrue(exists, "The index should now exist");
|
|
return;
|
|
|
|
Task<bool> IndexExists() => Custom.Scalar(
|
|
"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();
|
|
|
|
var exists = await IndexExists();
|
|
Expect.isFalse(exists, "The index should not exist already");
|
|
|
|
await Definition.EnsureTable("ensured");
|
|
await Definition.EnsureFieldIndex("ensured", "test", ["Id", "Category"]);
|
|
exists = await IndexExists();
|
|
Expect.isTrue(exists, "The index should now exist");
|
|
return;
|
|
|
|
Task<bool> IndexExists() => Custom.Scalar(
|
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it", Parameters.None,
|
|
Results.ToExists);
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Document module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test DocumentTests = TestList("Document",
|
|
[
|
|
TestList("Insert",
|
|
[
|
|
TestCase("succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should be no documents in the table");
|
|
|
|
await Document.Insert(PostgresDb.TableName,
|
|
new JsonDocument { Id = "turkey", Sub = new() { Foo = "gobble", Bar = "gobble" } });
|
|
var after = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(after, 1, "There should have been one document inserted");
|
|
}),
|
|
TestCase("fails for duplicate key", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Id = "test" });
|
|
try
|
|
{
|
|
await Document.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
|
|
}
|
|
}),
|
|
TestCase("succeeds when adding a numeric auto ID", async () =>
|
|
{
|
|
try
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.Number);
|
|
Configuration.UseIdField("Key");
|
|
await using var db = PostgresDb.BuildDb();
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should be no documents in the table");
|
|
|
|
await Document.Insert(PostgresDb.TableName, new NumIdDocument { Text = "one" });
|
|
await Document.Insert(PostgresDb.TableName, new NumIdDocument { Text = "two" });
|
|
await Document.Insert(PostgresDb.TableName, new NumIdDocument { Key = 77, Text = "three" });
|
|
await Document.Insert(PostgresDb.TableName, new NumIdDocument { Text = "four" });
|
|
|
|
var after = await Find.AllOrdered<NumIdDocument>(PostgresDb.TableName, [Field.Named("n:Key")]);
|
|
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
Expect.sequenceEqual(after.Select(x => x.Key), [1, 2, 77, 78],
|
|
"The IDs were not generated correctly");
|
|
}
|
|
finally
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
Configuration.UseIdField("Id");
|
|
}
|
|
}),
|
|
TestCase("succeeds when adding a GUID auto ID", async () =>
|
|
{
|
|
try
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.Guid);
|
|
await using var db = PostgresDb.BuildDb();
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should be no documents in the table");
|
|
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "one" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "two" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Id = "abc123", Value = "three" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "four" });
|
|
|
|
var after = await Find.All<JsonDocument>(PostgresDb.TableName);
|
|
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
Expect.equal(after.Count(x => x.Id.Length == 32), 3, "Three of the IDs should have been GUIDs");
|
|
Expect.equal(after.Count(x => x.Id == "abc123"), 1, "The provided ID should have been used as-is");
|
|
}
|
|
finally
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
}
|
|
}),
|
|
TestCase("succeeds when adding a RandomString auto ID", async () =>
|
|
{
|
|
try
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.RandomString);
|
|
Configuration.UseIdStringLength(44);
|
|
await using var db = PostgresDb.BuildDb();
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should be no documents in the table");
|
|
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "one" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "two" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Id = "abc123", Value = "three" });
|
|
await Document.Insert(PostgresDb.TableName, new JsonDocument { Value = "four" });
|
|
|
|
var after = await Find.All<JsonDocument>(PostgresDb.TableName);
|
|
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
Expect.equal(after.Count(x => x.Id.Length == 44), 3,
|
|
"Three of the IDs should have been 44-character random strings");
|
|
Expect.equal(after.Count(x => x.Id == "abc123"), 1, "The provided ID should have been used as-is");
|
|
}
|
|
finally
|
|
{
|
|
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
Configuration.UseIdStringLength(16);
|
|
}
|
|
})
|
|
]),
|
|
TestList("Save",
|
|
[
|
|
TestCase("succeeds when a document is inserted", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should be no documents in the table");
|
|
|
|
await Document.Save(PostgresDb.TableName,
|
|
new JsonDocument { Id = "test", Sub = new() { Foo = "a", Bar = "b" } });
|
|
var after = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(after, 1, "There should have been one document inserted");
|
|
}),
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await Document.Insert(PostgresDb.TableName,
|
|
new JsonDocument { Id = "test", Sub = new() { Foo = "a", Bar = "b" } });
|
|
|
|
var before = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "test");
|
|
Expect.isNotNull(before, "There should have been a document returned");
|
|
Expect.equal(before.Id, "test", "The document is not correct");
|
|
|
|
before.Sub = new() { Foo = "c", Bar = "d" };
|
|
await Document.Save(PostgresDb.TableName, before);
|
|
var after = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "test");
|
|
Expect.isNotNull(after, "There should have been a document returned post-update");
|
|
Expect.equal(after.Id, "test", "The document is not correct");
|
|
Expect.equal(after.Sub!.Foo, "c", "The updated document is not correct");
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Count module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test CountTests = TestList("Count",
|
|
[
|
|
TestCase("All succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var theCount = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(theCount, 5, "There should have been 5 matching documents");
|
|
}),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds for numeric range", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var theCount = await Count.ByFields(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Between("NumValue", 10, 20)]);
|
|
Expect.equal(theCount, 3, "There should have been 3 matching documents");
|
|
}),
|
|
TestCase("succeeds for non-numeric range", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var theCount = await Count.ByFields(PostgresDb.TableName, FieldMatch.All,
|
|
[Field.Between("Value", "aardvark", "apple")]);
|
|
Expect.equal(theCount, 1, "There should have been 1 matching document");
|
|
})
|
|
]),
|
|
TestCase("ByContains succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var theCount = await Count.ByContains(PostgresDb.TableName, new { Value = "purple" });
|
|
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
|
}),
|
|
TestCase("ByJsonPath succeeds", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var theCount = await Count.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 5)");
|
|
Expect.equal(theCount, 3, "There should have been 3 matching documents");
|
|
})
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Exists module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test ExistsTests = TestList("Exists",
|
|
[
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when a document exists", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var exists = await Exists.ById(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 LoadDocs();
|
|
|
|
var exists = await Exists.ById(PostgresDb.TableName, "seven");
|
|
Expect.isFalse(exists, "There should not have been an existing document");
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when documents exist", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var exists = await Exists.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.NotExists("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 LoadDocs();
|
|
|
|
var exists = await Exists.ByFields(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("NumValue", "six")]);
|
|
Expect.isFalse(exists, "There should not have been existing documents");
|
|
})
|
|
]),
|
|
TestList("ByContains",
|
|
[
|
|
TestCase("succeeds when documents exist", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var exists = await Exists.ByContains(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 LoadDocs();
|
|
|
|
var exists = await Exists.ByContains(PostgresDb.TableName, new { Nothing = "none" });
|
|
Expect.isFalse(exists, "There should not have been any existing documents");
|
|
})
|
|
]),
|
|
TestList("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when documents exist", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var exists = await Exists.ByJsonPath(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 LoadDocs();
|
|
|
|
var exists = await Exists.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 1000)");
|
|
Expect.isFalse(exists, "There should not have been any existing documents");
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Find module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test FindTests = TestList("Find",
|
|
[
|
|
TestList("All",
|
|
[
|
|
TestCase("succeeds when there is data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
await Document.Insert(PostgresDb.TableName, new SubDocument { Foo = "one", Bar = "two" });
|
|
await Document.Insert(PostgresDb.TableName, new SubDocument { Foo = "three", Bar = "four" });
|
|
await Document.Insert(PostgresDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
|
|
|
|
var results = await Find.All<SubDocument>(PostgresDb.TableName);
|
|
Expect.hasLength(results, 3, "There should have been 3 documents returned");
|
|
}),
|
|
TestCase("succeeds when there is no data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
var results = await Find.All<SubDocument>(PostgresDb.TableName);
|
|
Expect.isEmpty(results, "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("AllOrdered",
|
|
[
|
|
TestCase("succeeds when ordering numerically", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var results =
|
|
await Find.AllOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var results =
|
|
await Find.AllOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var results = await Find.AllOrdered<JsonDocument>(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("ById",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "three hundred eighty-seven");
|
|
Expect.isNull(doc, "There should not have been a document returned");
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("Value", "another")]);
|
|
Expect.hasLength(docs, 1, "There should have been one document returned");
|
|
}),
|
|
TestCase("succeeds when documents are found using IN with numeric field", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.All,
|
|
[Field.In("NumValue", [2, 4, 6, 8])]);
|
|
Expect.hasLength(docs, 1, "There should have been one document returned");
|
|
}),
|
|
TestCase("succeeds when documents are not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("Value", "mauve")]);
|
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
|
}),
|
|
TestCase("succeeds for InArray when matching documents exist", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
|
|
var docs = await Find.ByFields<ArrayDocument>(PostgresDb.TableName, FieldMatch.All,
|
|
[Field.InArray("Values", PostgresDb.TableName, ["c"])]);
|
|
Expect.hasLength(docs, 2, "There should have been two document returned");
|
|
}),
|
|
TestCase("succeeds for InArray when no matching documents exist", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
|
|
var docs = await Find.ByFields<ArrayDocument>(PostgresDb.TableName, FieldMatch.All,
|
|
[Field.InArray("Values", PostgresDb.TableName, ["j"])]);
|
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("ByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByFieldsOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var docs = await Find.ByFieldsOrdered<JsonDocument>(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("ByContains",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByContains<JsonDocument>(PostgresDb.TableName,
|
|
new { Sub = new { Foo = "green" } });
|
|
Expect.hasLength(docs, 2, "There should have been two documents returned");
|
|
}),
|
|
TestCase("succeeds when documents are not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByContains<JsonDocument>(PostgresDb.TableName, new { Value = "mauve" });
|
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("ByContainsOrdered",
|
|
[
|
|
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByContainsOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var docs = await Find.ByContainsOrdered<JsonDocument>(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("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByJsonPath<JsonDocument>(PostgresDb.TableName, "$.NumValue ? (@ < 15)");
|
|
Expect.hasLength(docs, 3, "There should have been 3 documents returned");
|
|
}),
|
|
TestCase("succeeds when documents are not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var docs = await Find.ByJsonPath<JsonDocument>(PostgresDb.TableName, "$.NumValue ? (@ < 0)");
|
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
|
})
|
|
]),
|
|
TestList("ByJsonPathOrdered",
|
|
[
|
|
// 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 LoadDocs();
|
|
|
|
var docs = await Find.ByJsonPathOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var docs = await Find.ByJsonPathOrdered<JsonDocument>(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("FirstByFields",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByFields<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByFields<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("Value", "absent")]);
|
|
Expect.isNull(doc, "There should not have been a document returned");
|
|
})
|
|
]),
|
|
TestList("FirstByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByFieldsOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByFieldsOrdered<JsonDocument>(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("FirstByContains",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByContains<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByContains<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByContains<JsonDocument>(PostgresDb.TableName, new { Value = "absent" });
|
|
Expect.isNull(doc, "There should not have been a document returned");
|
|
})
|
|
]),
|
|
TestList("FirstByContainsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByContainsOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByContainsOrdered<JsonDocument>(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("FirstByJsonPath",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByJsonPath<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByJsonPath<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByJsonPath<JsonDocument>(PostgresDb.TableName, "$.Id ? (@ == \"nope\")");
|
|
Expect.isNull(doc, "There should not have been a document returned");
|
|
})
|
|
]),
|
|
TestList("FirstByJsonPathOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var doc = await Find.FirstByJsonPathOrdered<JsonDocument>(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 LoadDocs();
|
|
|
|
var doc = await Find.FirstByJsonPathOrdered<JsonDocument>(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");
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>Verify the JSON for an ordered query</summary>
|
|
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}`");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Json module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test JsonTests = TestList("Json",
|
|
[
|
|
TestList("All",
|
|
[
|
|
TestCase("succeeds when there is data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyAllData(await Json.All(PostgresDb.TableName));
|
|
}),
|
|
TestCase("succeeds when there is no data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
VerifyEmpty(await Json.All(PostgresDb.TableName));
|
|
})
|
|
]),
|
|
TestList("AllOrdered",
|
|
[
|
|
TestCase("succeeds when ordering numerically", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyExpectedOrder(await Json.AllOrdered(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 LoadDocs();
|
|
VerifyExpectedOrder(await Json.AllOrdered(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 LoadDocs();
|
|
VerifyExpectedOrder(await Json.AllOrdered(PostgresDb.TableName, [Field.Named("Id DESC")]),
|
|
"two", "three", "one", "four", "five");
|
|
})
|
|
]),
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var json = await Json.ById(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 LoadDocs();
|
|
VerifyNoDoc(await Json.ById(PostgresDb.TableName, "three hundred eighty-seven"));
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifySingleById(
|
|
await Json.ByFields(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 LoadDocs();
|
|
VerifySingleById(
|
|
await Json.ByFields(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 LoadDocs();
|
|
VerifyEmpty(await Json.ByFields(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 Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
|
|
var json = await Json.ByFields(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 Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
VerifyEmpty(await Json.ByFields(PostgresDb.TableName, FieldMatch.All,
|
|
[Field.InArray("Values", PostgresDb.TableName, ["j"])]));
|
|
})
|
|
]),
|
|
TestList("ByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByFieldsOrdered(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 LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")],
|
|
[Field.Named("Id DESC")]),
|
|
"four", "five");
|
|
})
|
|
]),
|
|
TestList("ByContains",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var json = await Json.ByContains(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 LoadDocs();
|
|
VerifyEmpty(await Json.ByContains(PostgresDb.TableName, new { Value = "mauve" }));
|
|
})
|
|
]),
|
|
TestList("ByContainsOrdered",
|
|
[
|
|
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByContainsOrdered(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 LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
|
|
[Field.Named("Sub.Bar DESC")]),
|
|
"four", "two");
|
|
})
|
|
]),
|
|
TestList("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
var json = await Json.ByJsonPath(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 LoadDocs();
|
|
VerifyEmpty(await Json.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)"));
|
|
})
|
|
]),
|
|
TestList("ByJsonPathOrdered",
|
|
[
|
|
// 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 LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
|
|
[Field.Named("n:NumValue")]),
|
|
"one", "three", "two");
|
|
}),
|
|
TestCase("succeeds when sorting descending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyExpectedOrder(
|
|
await Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
|
|
[Field.Named("n:NumValue DESC")]),
|
|
"two", "three", "one");
|
|
})
|
|
]),
|
|
TestList("FirstByFields",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]),
|
|
"two");
|
|
}),
|
|
TestCase("succeeds when multiple documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyAnyById(
|
|
await Json.FirstByFields(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 LoadDocs();
|
|
VerifyNoDoc(await Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("Value", "absent")]));
|
|
})
|
|
]),
|
|
TestList("FirstByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByFieldsOrdered(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 LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]),
|
|
"four");
|
|
})
|
|
]),
|
|
TestList("FirstByContains",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(await Json.FirstByContains(PostgresDb.TableName, new { Value = "another" }), "two");
|
|
}),
|
|
TestCase("succeeds when multiple documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyAnyById(await Json.FirstByContains(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 LoadDocs();
|
|
VerifyNoDoc(await Json.FirstByContains(PostgresDb.TableName, new { Value = "absent" }));
|
|
})
|
|
]),
|
|
TestList("FirstByContainsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
|
|
[Field.Named("Value")]),
|
|
"two");
|
|
}),
|
|
TestCase("succeeds when sorting descending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
|
|
[Field.Named("Value DESC")]),
|
|
"four");
|
|
})
|
|
]),
|
|
TestList("FirstByJsonPath",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(await Json.FirstByJsonPath(PostgresDb.TableName, """$.Value ? (@ == "FIRST!")"""), "one");
|
|
}),
|
|
TestCase("succeeds when multiple documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyAnyById(await Json.FirstByJsonPath(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")"""),
|
|
["two", "four"]);
|
|
}),
|
|
TestCase("succeeds when a document is not found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyNoDoc(await Json.FirstByJsonPath(PostgresDb.TableName, """$.Id ? (@ == "nope")"""));
|
|
})
|
|
]),
|
|
TestList("FirstByJsonPathOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
|
|
[Field.Named("Sub.Bar")]),
|
|
"two");
|
|
}),
|
|
TestCase("succeeds when sorting descending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
VerifyDocById(
|
|
await Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
|
|
[Field.Named("Sub.Bar DESC")]),
|
|
"four");
|
|
})
|
|
]),
|
|
TestList("WriteAll",
|
|
[
|
|
TestCase("succeeds when there is data", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteAll(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 MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteAll(PostgresDb.TableName, writer);
|
|
VerifyEmpty(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteAllOrdered",
|
|
[
|
|
TestCase("succeeds when ordering numerically", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteAllOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteAllOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]);
|
|
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteById",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteById(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteById(PostgresDb.TableName, writer, "three hundred eighty-seven");
|
|
VerifyNoDoc(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByFields",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFields(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFields(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFields(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 Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFields(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 Definition.EnsureTable(PostgresDb.TableName);
|
|
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
|
|
[Field.InArray("Values", PostgresDb.TableName, ["j"])]);
|
|
VerifyEmpty(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFieldsOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All,
|
|
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
|
|
VerifyExpectedOrder(StreamText(stream), "four", "five");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByContains",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByContains(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByContains(PostgresDb.TableName, writer, new { Value = "mauve" });
|
|
VerifyEmpty(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByContainsOrdered",
|
|
[
|
|
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByContainsOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } },
|
|
[Field.Named("Sub.Bar DESC")]);
|
|
VerifyExpectedOrder(StreamText(stream), "four", "two");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByJsonPath",
|
|
[
|
|
TestCase("succeeds when documents are found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByJsonPath(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)");
|
|
VerifyEmpty(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteByJsonPathOrdered",
|
|
[
|
|
// 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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByJsonPathOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)",
|
|
[Field.Named("n:NumValue DESC")]);
|
|
VerifyExpectedOrder(StreamText(stream), "two", "three", "one");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByFields",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByFields(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByFields(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
|
|
[Field.Equal("Value", "absent")]);
|
|
VerifyNoDoc(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByFieldsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByFieldsOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any,
|
|
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
|
|
VerifyDocById(StreamText(stream), "four");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByContains",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByContains(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByContains(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" });
|
|
VerifyNoDoc(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByContainsOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByContainsOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByContainsOrdered(PostgresDb.TableName, writer,
|
|
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]);
|
|
VerifyDocById(StreamText(stream), "four");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByJsonPath",
|
|
[
|
|
TestCase("succeeds when a document is found", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByJsonPath(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByJsonPath(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")""");
|
|
VerifyNoDoc(StreamText(stream));
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
]),
|
|
TestList("WriteFirstByJsonPathOrdered",
|
|
[
|
|
TestCase("succeeds when sorting ascending", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByJsonPathOrdered(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 LoadDocs();
|
|
await using MemoryStream stream = new();
|
|
var writer = WriteStream(stream);
|
|
try
|
|
{
|
|
await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer,
|
|
"""$.Sub.Foo ? (@ == "green")""",
|
|
[Field.Named("Sub.Bar DESC")]);
|
|
VerifyDocById(StreamText(stream), "four");
|
|
}
|
|
finally
|
|
{
|
|
await writer.CompleteAsync();
|
|
}
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Update module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test UpdateTests = TestList("Update",
|
|
[
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Update.ById(PostgresDb.TableName, "one",
|
|
new JsonDocument { Id = "one", Sub = new() { Foo = "blue", Bar = "red" } });
|
|
var after = await Find.ById<string, JsonDocument>(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Update.ById(PostgresDb.TableName, "test",
|
|
new JsonDocument { Id = "x", Sub = new() { Foo = "blue", Bar = "red" } });
|
|
})
|
|
]),
|
|
TestList("ByFunc",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Update.ByFunc(PostgresDb.TableName, doc => doc.Id,
|
|
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
|
|
var after = await Find.ById<string, JsonDocument>(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Update.ByFunc(PostgresDb.TableName, doc => doc.Id,
|
|
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Patch module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test PatchTests = TestList("Patch",
|
|
[
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Patch.ById(PostgresDb.TableName, "one", new { NumValue = 44 });
|
|
var after = await Find.ById<string, JsonDocument>(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Patch.ById(PostgresDb.TableName, "test", new { Foo = "green" });
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Patch.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")],
|
|
new { NumValue = 77 });
|
|
var after = await Count.ByFields(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Patch.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "burgundy")],
|
|
new { Foo = "green" });
|
|
})
|
|
]),
|
|
TestList("ByContains",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Patch.ByContains(PostgresDb.TableName, new { Value = "purple" }, new { NumValue = 77 });
|
|
var after = await Count.ByContains(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Patch.ByContains(PostgresDb.TableName, new { Value = "burgundy" }, new { Foo = "green" });
|
|
})
|
|
]),
|
|
TestList("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when a document is updated", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Patch.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 10)", new { NumValue = 1000 });
|
|
var after = await Count.ByJsonPath(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();
|
|
|
|
var before = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(before, 0, "There should have been no documents returned");
|
|
|
|
// This not raising an exception is the test
|
|
await Patch.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)", new { Foo = "green" });
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the RemoveFields module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test RemoveFieldsTests = TestList("RemoveFields",
|
|
[
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when multiple fields are removed", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await RemoveFields.ById(PostgresDb.TableName, "two", ["Sub", "Value"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
await RemoveFields.ById(PostgresDb.TableName, "two", ["Sub"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ById(PostgresDb.TableName, "two", ["AFieldThatIsNotThere"]);
|
|
}),
|
|
TestCase("succeeds when no document is matched", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ById(PostgresDb.TableName, "two", ["Value"]);
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when multiple fields are removed", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")],
|
|
["Sub", "Value"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")],
|
|
["Sub"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("NumValue", "17")],
|
|
["Nothing"]);
|
|
}),
|
|
TestCase("succeeds when no document is matched", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any,
|
|
[Field.NotEqual("Abracadabra", "apple")], ["Value"]);
|
|
})
|
|
]),
|
|
TestList("ByContains",
|
|
[
|
|
TestCase("succeeds when multiple fields are removed", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub", "Value"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Nothing"]);
|
|
}),
|
|
TestCase("succeeds when no document is matched", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByContains(PostgresDb.TableName, new { Abracadabra = "apple" }, ["Value"]);
|
|
})
|
|
]),
|
|
TestList("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when multiple fields are removed", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub", "Value"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub"]);
|
|
var updated = await Find.ById<string, JsonDocument>(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 LoadDocs();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Nothing"]);
|
|
}),
|
|
TestCase("succeeds when no document is matched", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
|
|
// This not raising an exception is the test
|
|
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")", ["Value"]);
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// Integration tests for the Delete module of the PostgreSQL library
|
|
/// </summary>
|
|
private static readonly Test DeleteTests = TestList("Delete",
|
|
[
|
|
TestList("ById",
|
|
[
|
|
TestCase("succeeds when a document is deleted", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Delete.ById(PostgresDb.TableName, "four");
|
|
var remaining = await Count.All(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 LoadDocs();
|
|
|
|
await Delete.ById(PostgresDb.TableName, "thirty");
|
|
var remaining = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
|
})
|
|
]),
|
|
TestList("ByFields",
|
|
[
|
|
TestCase("succeeds when documents are deleted", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Delete.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")]);
|
|
var remaining = await Count.All(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 LoadDocs();
|
|
|
|
await Delete.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "crimson")]);
|
|
var remaining = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
|
})
|
|
]),
|
|
TestList("ByContains",
|
|
[
|
|
TestCase("succeeds when documents are deleted", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Delete.ByContains(PostgresDb.TableName, new { Value = "purple" });
|
|
var remaining = await Count.All(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 LoadDocs();
|
|
|
|
await Delete.ByContains(PostgresDb.TableName, new { Value = "crimson" });
|
|
var remaining = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
|
})
|
|
]),
|
|
TestList("ByJsonPath",
|
|
[
|
|
TestCase("succeeds when documents are deleted", async () =>
|
|
{
|
|
await using var db = PostgresDb.BuildDb();
|
|
await LoadDocs();
|
|
|
|
await Delete.ByJsonPath(PostgresDb.TableName, "$.Sub.Foo ? (@ == \"green\")");
|
|
var remaining = await Count.All(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 LoadDocs();
|
|
|
|
await Delete.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 100)");
|
|
var remaining = await Count.All(PostgresDb.TableName);
|
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
|
})
|
|
])
|
|
]);
|
|
|
|
/// <summary>
|
|
/// All Postgres C# tests
|
|
/// </summary>
|
|
[Tests]
|
|
public static readonly Test All = TestList("Postgres.C#",
|
|
[
|
|
TestList("Unit", [ParametersTests, QueryTests]),
|
|
TestSequenced(TestList("Integration",
|
|
[
|
|
ConfigurationTests,
|
|
CustomTests,
|
|
DefinitionTests,
|
|
DocumentTests,
|
|
CountTests,
|
|
ExistsTests,
|
|
FindTests,
|
|
JsonTests,
|
|
UpdateTests,
|
|
PatchTests,
|
|
RemoveFieldsTests,
|
|
DeleteTests
|
|
]))
|
|
]);
|
|
}
|