Add Custom Json tests
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<NoWarn>1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -319,7 +319,7 @@ public static class PostgresCSharpTests
|
||||
"By-JSON Path query not correct");
|
||||
})
|
||||
]);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add the test documents to the database
|
||||
/// </summary>
|
||||
@@ -328,6 +328,22 @@ public static class PostgresCSharpTests
|
||||
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
}
|
||||
|
||||
/// <summary>Set up a stream writer for a test</summary>
|
||||
internal static StreamWriter WriteStream(Stream stream)
|
||||
{
|
||||
StreamWriter writer = new(stream);
|
||||
writer.AutoFlush = true;
|
||||
return writer;
|
||||
}
|
||||
|
||||
/// Get the text of the given stream
|
||||
internal static string StreamText(Stream stream)
|
||||
{
|
||||
stream.Position = 0L;
|
||||
using StreamReader reader = new(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for the Configuration module of the PostgreSQL library
|
||||
/// </summary>
|
||||
@@ -389,6 +405,57 @@ public static class PostgresCSharpTests
|
||||
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 = 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 = 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();
|
||||
await using var writer = WriteStream(stream);
|
||||
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 `[`");
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await using MemoryStream stream = new();
|
||||
await using var writer = WriteStream(stream);
|
||||
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");
|
||||
})
|
||||
]),
|
||||
TestList("Single",
|
||||
[
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
@@ -411,6 +478,29 @@ public static class PostgresCSharpTests
|
||||
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 = 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 = 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 () =>
|
||||
|
||||
Reference in New Issue
Block a user