Make Json calls and stream writes async

This commit is contained in:
2025-04-05 19:33:47 -04:00
parent 74961c4ba7
commit 120a59ff7f
4 changed files with 203 additions and 155 deletions

View File

@@ -412,7 +412,8 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var docs = Custom.JsonArray(Query.Find(PostgresDb.TableName), Parameters.None, Results.JsonFromData);
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 `[`");
@@ -422,7 +423,8 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var docs = Custom.JsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
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");
})
@@ -436,7 +438,8 @@ public static class PostgresCSharpTests
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, Results.JsonFromData);
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 `[`");
@@ -450,7 +453,7 @@ public static class PostgresCSharpTests
await using MemoryStream stream = new();
await using var writer = WriteStream(stream);
Custom.WriteJsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
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");
@@ -485,7 +488,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
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");
@@ -496,7 +499,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
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");
})
@@ -1370,12 +1373,12 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyAllData(Json.All(PostgresDb.TableName));
VerifyAllData(await Json.All(PostgresDb.TableName));
}),
TestCase("succeeds when there is no data", async () =>
{
await using var db = PostgresDb.BuildDb();
VerifyEmpty(Json.All(PostgresDb.TableName));
VerifyEmpty(await Json.All(PostgresDb.TableName));
})
]),
TestList("AllOrdered",
@@ -1384,21 +1387,21 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(Json.AllOrdered(PostgresDb.TableName, [Field.Named("n:NumValue")]),
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(Json.AllOrdered(PostgresDb.TableName, [Field.Named("n:NumValue DESC")]),
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(Json.AllOrdered(PostgresDb.TableName, [Field.Named("Id DESC")]),
VerifyExpectedOrder(await Json.AllOrdered(PostgresDb.TableName, [Field.Named("Id DESC")]),
"two", "three", "one", "four", "five");
})
]),
@@ -1409,7 +1412,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var json = Json.ById(PostgresDb.TableName, "two");
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");
}),
@@ -1417,7 +1420,7 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyNoDoc(Json.ById(PostgresDb.TableName, "three hundred eighty-seven"));
VerifyNoDoc(await Json.ById(PostgresDb.TableName, "three hundred eighty-seven"));
})
]),
TestList("ByFields",
@@ -1427,7 +1430,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifySingleById(
Json.ByFields(PostgresDb.TableName, FieldMatch.All,
await Json.ByFields(PostgresDb.TableName, FieldMatch.All,
[Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]),
"four");
}),
@@ -1436,14 +1439,14 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifySingleById(
Json.ByFields(PostgresDb.TableName, FieldMatch.All, [Field.In("NumValue", [2, 4, 6, 8])]),
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(Json.ByFields(PostgresDb.TableName, FieldMatch.All,
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 () =>
@@ -1452,7 +1455,7 @@ public static class PostgresCSharpTests
await Definition.EnsureTable(PostgresDb.TableName);
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
var json = Json.ByFields(PostgresDb.TableName, FieldMatch.All,
var json = await Json.ByFields(PostgresDb.TableName, FieldMatch.All,
[Field.InArray("Values", PostgresDb.TableName, ["c"])]);
VerifyBeginEnd(json);
VerifyDocById(json, "first");
@@ -1463,7 +1466,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await Definition.EnsureTable(PostgresDb.TableName);
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
VerifyEmpty(Json.ByFields(PostgresDb.TableName, FieldMatch.All,
VerifyEmpty(await Json.ByFields(PostgresDb.TableName, FieldMatch.All,
[Field.InArray("Values", PostgresDb.TableName, ["j"])]));
})
]),
@@ -1474,7 +1477,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")],
await Json.ByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")],
[Field.Named("Id")]),
"five", "four");
}),
@@ -1483,7 +1486,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")],
await Json.ByFieldsOrdered(PostgresDb.TableName, FieldMatch.All, [Field.Equal("Value", "purple")],
[Field.Named("Id DESC")]),
"four", "five");
})
@@ -1495,7 +1498,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var json = Json.ByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } });
var json = await Json.ByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } });
VerifyBeginEnd(json);
VerifyDocById(json, "two");
VerifyDocById(json, "four");
@@ -1504,7 +1507,7 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyEmpty(Json.ByContains(PostgresDb.TableName, new { Value = "mauve" }));
VerifyEmpty(await Json.ByContains(PostgresDb.TableName, new { Value = "mauve" }));
})
]),
TestList("ByContainsOrdered",
@@ -1515,7 +1518,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
await Json.ByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
[Field.Named("Sub.Bar")]),
"two", "four");
}),
@@ -1524,7 +1527,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
await Json.ByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
[Field.Named("Sub.Bar DESC")]),
"four", "two");
})
@@ -1536,7 +1539,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var json = Json.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 15)");
var json = await Json.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 15)");
VerifyBeginEnd(json);
VerifyDocById(json, "one");
VerifyDocById(json, "two");
@@ -1546,7 +1549,7 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyEmpty(Json.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)"));
VerifyEmpty(await Json.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)"));
})
]),
TestList("ByJsonPathOrdered",
@@ -1557,7 +1560,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
await Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
[Field.Named("n:NumValue")]),
"one", "three", "two");
}),
@@ -1566,7 +1569,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyExpectedOrder(
Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
await Json.ByJsonPathOrdered(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
[Field.Named("n:NumValue DESC")]),
"two", "three", "one");
})
@@ -1578,7 +1581,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]),
await Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]),
"two");
}),
TestCase("succeeds when multiple documents are found", async () =>
@@ -1586,14 +1589,15 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyAnyById(
Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")]),
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(Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "absent")]));
VerifyNoDoc(await Json.FirstByFields(PostgresDb.TableName, FieldMatch.Any,
[Field.Equal("Value", "absent")]));
})
]),
TestList("FirstByFieldsOrdered",
@@ -1603,8 +1607,8 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")],
[Field.Named("Id")]),
await Json.FirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any,
[Field.Equal("Value", "purple")], [Field.Named("Id")]),
"five");
}),
TestCase("succeeds when sorting descending", async () =>
@@ -1612,8 +1616,8 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any, [Field.Equal("Value", "purple")],
[Field.Named("Id DESC")]),
await Json.FirstByFieldsOrdered(PostgresDb.TableName, FieldMatch.Any,
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]),
"four");
})
]),
@@ -1623,20 +1627,20 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(Json.FirstByContains(PostgresDb.TableName, new { Value = "another" }), "two");
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(Json.FirstByContains(PostgresDb.TableName, new { Sub = new { Foo = "green" } }),
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(Json.FirstByContains(PostgresDb.TableName, new { Value = "absent" }));
VerifyNoDoc(await Json.FirstByContains(PostgresDb.TableName, new { Value = "absent" }));
})
]),
TestList("FirstByContainsOrdered",
@@ -1646,7 +1650,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
await Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
[Field.Named("Value")]),
"two");
}),
@@ -1655,7 +1659,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
await Json.FirstByContainsOrdered(PostgresDb.TableName, new { Sub = new { Foo = "green" } },
[Field.Named("Value DESC")]),
"four");
})
@@ -1666,20 +1670,20 @@ public static class PostgresCSharpTests
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(Json.FirstByJsonPath(PostgresDb.TableName, """$.Value ? (@ == "FIRST!")"""), "one");
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(Json.FirstByJsonPath(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")"""),
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(Json.FirstByJsonPath(PostgresDb.TableName, """$.Id ? (@ == "nope")"""));
VerifyNoDoc(await Json.FirstByJsonPath(PostgresDb.TableName, """$.Id ? (@ == "nope")"""));
})
]),
TestList("FirstByJsonPathOrdered",
@@ -1689,7 +1693,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
await Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
[Field.Named("Sub.Bar")]),
"two");
}),
@@ -1698,7 +1702,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
VerifyDocById(
Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
await Json.FirstByJsonPathOrdered(PostgresDb.TableName, """$.Sub.Foo ? (@ == "green")""",
[Field.Named("Sub.Bar DESC")]),
"four");
})