v4.1 (#11)
- 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
This commit was merged in pull request #11.
This commit is contained in:
1 parent
5580284910
commit
43fed5789a
45 files changed
+13140
-1944
No files matched your search
@@ -4,6 +4,7 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<NoWarn>1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.IO.Pipelines;
|
||||
using Expecto.CSharp;
|
||||
using Expecto;
|
||||
using Microsoft.FSharp.Core;
|
||||
@@ -16,7 +17,7 @@ internal class TestSerializer : IDocumentSerializer
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// C# Tests for common functionality in <tt>BitBadger.Documents</tt>
|
||||
/// C# Tests for common functionality in <c>BitBadger.Documents</c>
|
||||
/// </summary>
|
||||
public static class CommonCSharpTests
|
||||
{
|
||||
@@ -417,7 +418,7 @@ public static class CommonCSharpTests
|
||||
})
|
||||
])
|
||||
]);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the Configuration static class
|
||||
/// </summary>
|
||||
@@ -647,7 +648,115 @@ public static class CommonCSharpTests
|
||||
})
|
||||
])
|
||||
]);
|
||||
|
||||
|
||||
private static string StreamText(Stream stream)
|
||||
{
|
||||
stream.Position = 0L;
|
||||
using StreamReader reader = new(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>Unit tests for the PipeWriter module</summary>
|
||||
private static readonly Test PipeWriterTests = TestList("PipeWriterModule",
|
||||
[
|
||||
TestList("WriteString",
|
||||
[
|
||||
TestCase("succeeds when writer is open", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
try
|
||||
{
|
||||
var result = await PipeWriterModule.WriteString(writer, "abc");
|
||||
Expect.isTrue(result, "The write operation should have been successful");
|
||||
Expect.equal(StreamText(stream), "abc", "The string was not written correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when writer is completed", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
await writer.CompleteAsync();
|
||||
|
||||
var result = await PipeWriterModule.WriteString(writer, "abc");
|
||||
Expect.isFalse(result, "The write operation should have returned false");
|
||||
Expect.equal(StreamText(stream), "", "No text should have been written");
|
||||
})
|
||||
]),
|
||||
TestList("WriteStrings",
|
||||
[
|
||||
TestCase("succeeds with no strings", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
try
|
||||
{
|
||||
await PipeWriterModule.WriteStrings(writer, []);
|
||||
Expect.equal(StreamText(stream), "[]", "An empty sequence of strings was not written correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds with one strings", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
try
|
||||
{
|
||||
await PipeWriterModule.WriteStrings(writer, ["le-test"]);
|
||||
Expect.equal(StreamText(stream), "[le-test]", "A sequence of one string was not written correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds with many strings", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
|
||||
try
|
||||
{
|
||||
await PipeWriterModule.WriteStrings(writer, ["z", "y", "x", "c", "b", "a"]);
|
||||
Expect.equal(StreamText(stream), "[z,y,x,c,b,a]",
|
||||
"A sequence of many strings was not written correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when the writer is completed early", async () =>
|
||||
{
|
||||
await using MemoryStream stream = new();
|
||||
var writer = PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
|
||||
|
||||
await PipeWriterModule.WriteStrings(writer, Items());
|
||||
Expect.equal(StreamText(stream), "[a,b,c", "The writing should have stopped when the writer completed");
|
||||
return;
|
||||
|
||||
IEnumerable<string> Items()
|
||||
{
|
||||
yield return "a";
|
||||
yield return "b";
|
||||
yield return "c";
|
||||
writer.Complete();
|
||||
yield return "d";
|
||||
yield return "e";
|
||||
yield return "f";
|
||||
}
|
||||
})
|
||||
])
|
||||
]);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests
|
||||
/// </summary>
|
||||
@@ -660,6 +769,7 @@ public static class CommonCSharpTests
|
||||
ParameterNameTests,
|
||||
AutoIdTests,
|
||||
QueryTests,
|
||||
PipeWriterTests,
|
||||
TestSequenced(ConfigurationTests)
|
||||
]);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,7 +1,9 @@
|
||||
using Expecto.CSharp;
|
||||
using System.IO.Pipelines;
|
||||
using Expecto.CSharp;
|
||||
using Expecto;
|
||||
using Microsoft.FSharp.Core;
|
||||
using BitBadger.Documents.Sqlite;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace BitBadger.Documents.Tests.CSharp;
|
||||
|
||||
@@ -147,7 +149,7 @@ public static class SqliteCSharpTests
|
||||
/// <summary>
|
||||
/// Add the test documents to the database
|
||||
/// </summary>
|
||||
internal static async Task LoadDocs()
|
||||
private static async Task LoadDocs()
|
||||
{
|
||||
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
}
|
||||
@@ -169,11 +171,139 @@ public static class SqliteCSharpTests
|
||||
}
|
||||
});
|
||||
|
||||
/// <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 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>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();
|
||||
}
|
||||
|
||||
/// Verify the presence of any of the given documents in the given JSON
|
||||
private static void VerifyAny(string json, IEnumerable<string> docs)
|
||||
{
|
||||
var theDocs = docs.ToList();
|
||||
if (theDocs.Any(json.Contains)) return;
|
||||
var anyDocs = string.Join(" | ", theDocs);
|
||||
Expect.isTrue(false, $"Could not find any of |{anyDocs}| in {json}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for the Custom module of the SQLite library
|
||||
/// </summary>
|
||||
private static readonly Test CustomTests = TestList("Custom",
|
||||
[
|
||||
TestList("List",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Custom.List(Query.Find(SqliteDb.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 = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Custom.List(
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", [new("@value", 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 = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var json = await Custom.JsonArray(Query.Find(SqliteDb.TableName), [], Results.JsonFromData);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Two,"Document ID `two` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Four, "Document ID `four` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Five, "Document ID `five` should have been found");
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
VerifyEmpty(await Custom.JsonArray(
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||
[new SqliteParameter("@value", 100)], Results.JsonFromData));
|
||||
})
|
||||
]),
|
||||
TestList("WriteJsonArray",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Custom.WriteJsonArray(Query.Find(SqliteDb.TableName), [], writer, Results.JsonFromData);
|
||||
var json = StreamText(stream);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Two, "Document ID `two` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Four, "Document ID `four` should have been found");
|
||||
Expect.stringContains(json, JsonDocument.Five, "Document ID `five` should have been found");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Custom.WriteJsonArray(
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||
[new SqliteParameter("@value", 100)], writer, Results.JsonFromData);
|
||||
VerifyEmpty(StreamText(stream));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
]),
|
||||
TestList("Single",
|
||||
[
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
@@ -196,26 +326,24 @@ public static class SqliteCSharpTests
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
]),
|
||||
TestList("List",
|
||||
TestList("JsonSingle",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Custom.List(Query.Find(SqliteDb.TableName), Parameters.None,
|
||||
Results.FromData<JsonDocument>);
|
||||
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
||||
var json = await Custom.JsonSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
[new SqliteParameter("@id", "one")], Results.JsonFromData);
|
||||
Expect.equal(json, JsonDocument.One, "The JSON document is incorrect");
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
TestCase("succeeds when a row is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Custom.List(
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", [new("@value", 100)],
|
||||
Results.FromData<JsonDocument>);
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
VerifyNoDoc(await Custom.JsonSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
[new SqliteParameter("@id", "eighty")], Results.JsonFromData));
|
||||
})
|
||||
]),
|
||||
TestList("NonQuery",
|
||||
@@ -757,6 +885,635 @@ public static class SqliteCSharpTests
|
||||
])
|
||||
]);
|
||||
|
||||
/// Integration tests for the Json module of the SQLite library
|
||||
private static readonly Test JsonTests = TestList("Json",
|
||||
[
|
||||
TestList("All",
|
||||
[
|
||||
TestCase("succeeds when there is data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "one", Bar = "two" });
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "three", Bar = "four" });
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
|
||||
|
||||
var json = await Json.All(SqliteDb.TableName);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, """{"Foo":"one","Bar":"two"}""", "The first document was not found");
|
||||
Expect.stringContains(json, """{"Foo":"three","Bar":"four"}""", "The second document was not found");
|
||||
Expect.stringContains(json, """{"Foo":"five","Bar":"six"}""", "The third document was not found");
|
||||
}),
|
||||
TestCase("succeeds when there is no data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
VerifyEmpty(await Json.All(SqliteDb.TableName));
|
||||
})
|
||||
]),
|
||||
TestList("AllOrdered",
|
||||
[
|
||||
TestCase("succeeds when ordering numerically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(await Json.AllOrdered(SqliteDb.TableName, [Field.Named("n:NumValue")]),
|
||||
$"[{JsonDocument.One},{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"The documents were not ordered correctly");
|
||||
}),
|
||||
TestCase("succeeds when ordering numerically descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(await Json.AllOrdered(SqliteDb.TableName, [Field.Named("n:NumValue DESC")]),
|
||||
$"[{JsonDocument.Five},{JsonDocument.Four},{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One}]",
|
||||
"The documents were not ordered correctly");
|
||||
}),
|
||||
TestCase("succeeds when ordering alphabetically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(await Json.AllOrdered(SqliteDb.TableName, [Field.Named("Id DESC")]),
|
||||
$"[{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"The documents were not ordered correctly");
|
||||
})
|
||||
]),
|
||||
TestList("ById",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(await Json.ById(SqliteDb.TableName, "two"), JsonDocument.Two,
|
||||
"The incorrect document was returned");
|
||||
}),
|
||||
TestCase("succeeds when a document is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
VerifyNoDoc(await Json.ById(SqliteDb.TableName, "three hundred eighty-seven"));
|
||||
})
|
||||
]),
|
||||
TestList("ByFields",
|
||||
[
|
||||
TestCase("succeeds when documents are found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var json = await Json.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.Greater("NumValue", 15)]);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, JsonDocument.Four, "Document `four` should have been returned");
|
||||
Expect.stringContains(json, JsonDocument.Five, "Document `five` should have been returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are found using IN with numeric field", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.ByFields(SqliteDb.TableName, FieldMatch.All, [Field.In("NumValue", [2, 4, 6, 8])]),
|
||||
$"[{JsonDocument.Three}]", "There should have been one document returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
VerifyEmpty(await Json.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.Greater("NumValue", 100)]));
|
||||
}),
|
||||
TestCase("succeeds for InArray when matching documents exist", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await Definition.EnsureTable(SqliteDb.TableName);
|
||||
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
|
||||
var json = await Json.ByFields(SqliteDb.TableName, FieldMatch.All,
|
||||
[Field.InArray("Values", SqliteDb.TableName, ["c"])]);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""",
|
||||
"Document `first` should have been returned");
|
||||
Expect.stringContains(json, """{"Id":"second","Values":["c","d","e"]}""",
|
||||
"Document `second` should have been returned");
|
||||
}),
|
||||
TestCase("succeeds for InArray when no matching documents exist", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await Definition.EnsureTable(SqliteDb.TableName);
|
||||
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
VerifyEmpty(await Json.ByFields(SqliteDb.TableName, FieldMatch.All,
|
||||
[Field.InArray("Values", SqliteDb.TableName, ["j"])]));
|
||||
})
|
||||
]),
|
||||
TestList("ByFieldsOrdered",
|
||||
[
|
||||
TestCase("succeeds when sorting ascending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.ByFieldsOrdered(SqliteDb.TableName, FieldMatch.Any, [Field.Greater("NumValue", 15)],
|
||||
[Field.Named("Id")]), $"[{JsonDocument.Five},{JsonDocument.Four}]",
|
||||
"Incorrect documents were returned");
|
||||
}),
|
||||
TestCase("succeeds when sorting descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.ByFieldsOrdered(SqliteDb.TableName, FieldMatch.Any, [Field.Greater("NumValue", 15)],
|
||||
[Field.Named("Id DESC")]), $"[{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"Incorrect documents were returned");
|
||||
}),
|
||||
TestCase("succeeds when sorting case-sensitively", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.ByFieldsOrdered(SqliteDb.TableName, FieldMatch.All, [Field.LessOrEqual("NumValue", 10)],
|
||||
[Field.Named("Value")]),
|
||||
$"[{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Two}]", "Documents not ordered correctly");
|
||||
}),
|
||||
TestCase("succeeds when sorting case-insensitively", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.ByFieldsOrdered(SqliteDb.TableName, FieldMatch.All, [Field.LessOrEqual("NumValue", 10)],
|
||||
[Field.Named("i:Value")]),
|
||||
$"[{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.One}]", "Documents not ordered correctly");
|
||||
})
|
||||
]),
|
||||
TestList("FirstByFields",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.FirstByFields(SqliteDb.TableName, FieldMatch.Any, [Field.Equal("Value", "another")]),
|
||||
JsonDocument.Two, "The incorrect document was returned");
|
||||
}),
|
||||
TestCase("succeeds when multiple documents are found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var json = await Json.FirstByFields(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")]);
|
||||
Expect.notEqual(json, "{}", "There should have been a document returned");
|
||||
VerifyAny(json, [JsonDocument.Two, JsonDocument.Four]);
|
||||
}),
|
||||
TestCase("succeeds when a document is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
VerifyNoDoc(await Json.FirstByFields(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.Equal("Value", "absent")]));
|
||||
})
|
||||
]),
|
||||
TestList("FirstByFieldsOrdered",
|
||||
[
|
||||
TestCase("succeeds when sorting ascending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.FirstByFieldsOrdered(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar")]), JsonDocument.Two,
|
||||
"An incorrect document was returned");
|
||||
}),
|
||||
TestCase("succeeds when sorting descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
Expect.equal(
|
||||
await Json.FirstByFieldsOrdered(SqliteDb.TableName, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]), JsonDocument.Four,
|
||||
"An incorrect document was returned");
|
||||
})
|
||||
]),
|
||||
TestList("WriteAll",
|
||||
[
|
||||
TestCase("succeeds when there is data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "one", Bar = "two" });
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "three", Bar = "four" });
|
||||
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteAll(SqliteDb.TableName, writer);
|
||||
var json = StreamText(stream);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, """{"Foo":"one","Bar":"two"}""", "The first document was not found");
|
||||
Expect.stringContains(json, """{"Foo":"three","Bar":"four"}""",
|
||||
"The second document was not found");
|
||||
Expect.stringContains(json, """{"Foo":"five","Bar":"six"}""", "The third document was not found");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when there is no data", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteAll(SqliteDb.TableName, writer);
|
||||
VerifyEmpty(StreamText(stream));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
]),
|
||||
TestList("WriteAllOrdered",
|
||||
[
|
||||
TestCase("succeeds when ordering numerically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue")]);
|
||||
Expect.equal(StreamText(stream),
|
||||
$"[{JsonDocument.One},{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"The documents were not ordered correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when ordering numerically descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue DESC")]);
|
||||
Expect.equal(StreamText(stream),
|
||||
$"[{JsonDocument.Five},{JsonDocument.Four},{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One}]",
|
||||
"The documents were not ordered correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when ordering alphabetically", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("Id DESC")]);
|
||||
Expect.equal(StreamText(stream),
|
||||
$"[{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"The documents were not ordered correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
]),
|
||||
TestList("WriteById",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteById(SqliteDb.TableName, writer, "two");
|
||||
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when a document is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteById(SqliteDb.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 = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Greater("NumValue", 15)]);
|
||||
var json = StreamText(stream);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, JsonDocument.Four, "Document `four` should have been returned");
|
||||
Expect.stringContains(json, JsonDocument.Five, "Document `five` should have been returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when documents are found using IN with numeric field", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
|
||||
[Field.In("NumValue", [2, 4, 6, 8])]);
|
||||
Expect.equal(StreamText(stream), $"[{JsonDocument.Three}]",
|
||||
"There should have been one document returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Greater("NumValue", 100)]);
|
||||
VerifyEmpty(StreamText(stream));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds for InArray when matching documents exist", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await Definition.EnsureTable(SqliteDb.TableName);
|
||||
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
|
||||
[Field.InArray("Values", SqliteDb.TableName, ["c"])]);
|
||||
var json = StreamText(stream);
|
||||
VerifyBeginEnd(json);
|
||||
Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""",
|
||||
"Document `first` should have been returned");
|
||||
Expect.stringContains(json, """{"Id":"second","Values":["c","d","e"]}""",
|
||||
"Document `second` should have been returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds for InArray when no matching documents exist", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await Definition.EnsureTable(SqliteDb.TableName);
|
||||
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
|
||||
[Field.InArray("Values", SqliteDb.TableName, ["j"])]);
|
||||
VerifyEmpty(StreamText(stream));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
]),
|
||||
TestList("WriteByFieldsOrdered",
|
||||
[
|
||||
TestCase("succeeds when sorting ascending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Greater("NumValue", 15)], [Field.Named("Id")]);
|
||||
Expect.equal(StreamText(stream), $"[{JsonDocument.Five},{JsonDocument.Four}]",
|
||||
"Incorrect documents were returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when sorting descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Greater("NumValue", 15)], [Field.Named("Id DESC")]);
|
||||
Expect.equal(StreamText(stream), $"[{JsonDocument.Four},{JsonDocument.Five}]",
|
||||
"Incorrect documents were returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when sorting case-sensitively", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
|
||||
[Field.LessOrEqual("NumValue", 10)], [Field.Named("Value")]);
|
||||
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Two}]",
|
||||
"Documents not ordered correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when sorting case-insensitively", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
|
||||
[Field.LessOrEqual("NumValue", 10)], [Field.Named("i:Value")]);
|
||||
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.One}]",
|
||||
"Documents not ordered correctly");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
]),
|
||||
TestList("WriteFirstByFields",
|
||||
[
|
||||
TestCase("succeeds when a document is found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Equal("Value", "another")]);
|
||||
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when multiple documents are found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")]);
|
||||
var json = StreamText(stream);
|
||||
Expect.notEqual(json, "{}", "There should have been a document returned");
|
||||
VerifyAny(json, [JsonDocument.Two, JsonDocument.Four]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when a document is not found", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteFirstByFields(SqliteDb.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 = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar")]);
|
||||
Expect.equal(StreamText(stream), JsonDocument.Two, "An incorrect document was returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
}),
|
||||
TestCase("succeeds when sorting descending", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
using MemoryStream stream = new();
|
||||
var writer = WriteStream(stream);
|
||||
try
|
||||
{
|
||||
await Json.WriteFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
|
||||
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]);
|
||||
Expect.equal(StreamText(stream), JsonDocument.Four, "An incorrect document was returned");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await writer.CompleteAsync();
|
||||
}
|
||||
})
|
||||
])
|
||||
]);
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for the Update module of the SQLite library
|
||||
/// </summary>
|
||||
@@ -1006,6 +1763,7 @@ public static class SqliteCSharpTests
|
||||
CountTests,
|
||||
ExistsTests,
|
||||
FindTests,
|
||||
JsonTests,
|
||||
UpdateTests,
|
||||
PatchTests,
|
||||
RemoveFieldsTests,
|
||||
|
||||
@@ -18,7 +18,7 @@ public class JsonDocument
|
||||
public string Value { get; set; } = "";
|
||||
public int NumValue { get; set; } = 0;
|
||||
public SubDocument? Sub { get; set; } = null;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A set of documents used for integration tests
|
||||
/// </summary>
|
||||
@@ -30,6 +30,22 @@ public class JsonDocument
|
||||
new() { Id = "four", Value = "purple", NumValue = 17, Sub = new() { Foo = "green", Bar = "red" } },
|
||||
new() { Id = "five", Value = "purple", NumValue = 18 }
|
||||
];
|
||||
|
||||
/// <summary>The JSON for document ID `one`</summary>
|
||||
public static string One = """{"Id":"one","Value":"FIRST!","NumValue":0,"Sub":null}""";
|
||||
|
||||
/// <summary>The JSON for document ID `two`</summary>
|
||||
public static string Two = """{"Id":"two","Value":"another","NumValue":10,"Sub":{"Foo":"green","Bar":"blue"}}""";
|
||||
|
||||
/// <summary>The JSON for document ID `three`</summary>
|
||||
public static string Three = """{"Id":"three","Value":"","NumValue":4,"Sub":null}""";
|
||||
|
||||
/// <summary>The JSON for document ID `four`</summary>
|
||||
public static string Four = """{"Id":"four","Value":"purple","NumValue":17,"Sub":{"Foo":"green","Bar":"red"}}""";
|
||||
|
||||
/// <summary>The JSON for document ID `five`</summary>
|
||||
public static string Five = """{"Id":"five","Value":"purple","NumValue":18,"Sub":null}""";
|
||||
|
||||
}
|
||||
|
||||
public class ArrayDocument
|
||||
|
||||
Reference in new issue
Block a user