Add Json write Postgres tests

This commit is contained in:
2025-04-05 21:55:40 -04:00
parent 120a59ff7f
commit 5e5dbd3b80
3 changed files with 814 additions and 2 deletions

View File

@@ -1706,6 +1706,423 @@ public static class PostgresCSharpTests
[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();
await using var writer = WriteStream(stream);
await Json.WriteAll(PostgresDb.TableName, writer);
VerifyAllData(StreamText(stream));
}),
TestCase("succeeds when there is no data", async () =>
{
await using var db = PostgresDb.BuildDb();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteAll(PostgresDb.TableName, writer);
VerifyEmpty(StreamText(stream));
})
]),
TestList("WriteAllOrdered",
[
TestCase("succeeds when ordering numerically", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue")]);
VerifyExpectedOrder(StreamText(stream), "one", "three", "two", "four", "five");
}),
TestCase("succeeds when ordering numerically descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue DESC")]);
VerifyExpectedOrder(StreamText(stream), "five", "four", "two", "three", "one");
}),
TestCase("succeeds when ordering alphabetically", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]);
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five");
})
]),
TestList("WriteById",
[
TestCase("succeeds when a document is found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
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");
}),
TestCase("succeeds when a document is not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteById(PostgresDb.TableName, writer, "three hundred eighty-seven");
VerifyNoDoc(StreamText(stream));
})
]),
TestList("WriteByFields",
[
TestCase("succeeds when documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]);
VerifySingleById(StreamText(stream), "four");
}),
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();
await using var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.In("NumValue", [2, 4, 6, 8])]);
VerifySingleById(StreamText(stream), "three");
}),
TestCase("succeeds when documents are not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.Equal("Value", "mauve"), Field.NotEqual("NumValue", 40)]);
VerifyEmpty(StreamText(stream));
}),
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();
await using var writer = WriteStream(stream);
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");
}),
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();
await using var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.InArray("Values", PostgresDb.TableName, ["j"])]);
VerifyEmpty(StreamText(stream));
})
]),
TestList("WriteByFieldsOrdered",
[
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All,
[Field.Equal("Value", "purple")], [Field.Named("Id")]);
VerifyExpectedOrder(StreamText(stream), "five", "four");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All,
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
VerifyExpectedOrder(StreamText(stream), "four", "five");
})
]),
TestList("WriteByContains",
[
TestCase("succeeds when documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } });
var json = StreamText(stream);
VerifyBeginEnd(json);
VerifyDocById(json, "two");
VerifyDocById(json, "four");
}),
TestCase("succeeds when documents are not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByContains(PostgresDb.TableName, writer, new { Value = "mauve" });
VerifyEmpty(StreamText(stream));
})
]),
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();
await using var writer = WriteStream(stream);
await Json.WriteByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } },
[Field.Named("Sub.Bar")]);
VerifyExpectedOrder(StreamText(stream), "two", "four");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } },
[Field.Named("Sub.Bar DESC")]);
VerifyExpectedOrder(StreamText(stream), "four", "two");
})
]),
TestList("WriteByJsonPath",
[
TestCase("succeeds when documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)");
var json = StreamText(stream);
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();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)");
VerifyEmpty(StreamText(stream));
})
]),
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();
await using var writer = WriteStream(stream);
await Json.WriteByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)",
[Field.Named("n:NumValue")]);
VerifyExpectedOrder(StreamText(stream), "one", "three", "two");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)",
[Field.Named("n:NumValue DESC")]);
VerifyExpectedOrder(StreamText(stream), "two", "three", "one");
})
]),
TestList("WriteFirstByFields",
[
TestCase("succeeds when a document is found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "another")]);
VerifyDocById(StreamText(stream), "two");
}),
TestCase("succeeds when multiple documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "purple")]);
VerifyAnyById(StreamText(stream), ["five", "four"]);
}),
TestCase("succeeds when a document is not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "absent")]);
VerifyNoDoc(StreamText(stream));
})
]),
TestList("WriteFirstByFieldsOrdered",
[
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "purple")], [Field.Named("Id")]);
VerifyDocById(StreamText(stream), "five");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
VerifyDocById(StreamText(stream), "four");
})
]),
TestList("WriteFirstByContains",
[
TestCase("succeeds when a document is found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "another" });
VerifyDocById(StreamText(stream), "two");
}),
TestCase("succeeds when multiple documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } });
VerifyAnyById(StreamText(stream), ["two", "four"]);
}),
TestCase("succeeds when a document is not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" });
VerifyNoDoc(StreamText(stream));
})
]),
TestList("WriteFirstByContainsOrdered",
[
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByContainsOrdered(PostgresDb.TableName, writer,
new { Sub = new { Foo = "green" } }, [Field.Named("Value")]);
VerifyDocById(StreamText(stream), "two");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByContainsOrdered(PostgresDb.TableName, writer,
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]);
VerifyDocById(StreamText(stream), "four");
})
]),
TestList("WriteFirstByJsonPath",
[
TestCase("succeeds when a document is found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")""");
VerifyDocById(StreamText(stream), "one");
}),
TestCase("succeeds when multiple documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""");
VerifyAnyById(StreamText(stream), ["two", "four"]);
}),
TestCase("succeeds when a document is not found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")""");
VerifyNoDoc(StreamText(stream));
})
]),
TestList("WriteFirstByJsonPathOrdered",
[
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""",
[Field.Named("Sub.Bar")]);
VerifyDocById(StreamText(stream), "two");
}),
TestCase("succeeds when sorting descending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""",
[Field.Named("Sub.Bar DESC")]);
VerifyDocById(StreamText(stream), "four");
})
])
]);