v4.1 #11

Merged
danieljsummers merged 22 commits from v4point1 into main 2025-04-19 19:50:17 +00:00
19 changed files with 2884 additions and 1444 deletions
Showing only changes of commit 5eee3ce92a - Show all commits

View File

@ -1,6 +1,7 @@
namespace BitBadger.Documents namespace BitBadger.Documents
open System.Security.Cryptography open System.Security.Cryptography
open System.Text
/// <summary>The types of comparisons available for JSON fields</summary> /// <summary>The types of comparisons available for JSON fields</summary>
/// <exclude /> /// <exclude />
@ -631,3 +632,47 @@ module Query =
|> function path -> path + defaultArg direction "") |> function path -> path + defaultArg direction "")
|> String.concat ", " |> String.concat ", "
|> function it -> $" ORDER BY {it}" |> function it -> $" ORDER BY {it}"
open System.IO.Pipelines
/// <summary>Functions that manipulate <c>PipeWriter</c>s</summary>
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module PipeWriter =
/// <summary>Write a UTF-8 string to this pipe</summary>
/// <param name="writer">The PipeWriter to which the string should be written</param>
/// <param name="text">The string to be written to the pipe</param>
/// <returns><c>true</c> if the pipe is still open, <c>false</c> if not</returns>
[<CompiledName "WriteString">]
let writeString (writer: PipeWriter) (text: string) = backgroundTask {
try
let! writeResult = writer.WriteAsync(Encoding.UTF8.GetBytes text)
let! flushResult = writer.FlushAsync()
return not (writeResult.IsCompleted || flushResult.IsCompleted)
with :? System.ObjectDisposedException -> return false
}
/// <summary>Write an array of strings, abandoning the sequence if the pipe is closed</summary>
/// <param name="writer">The PipeWriter to which the strings should be written</param>
/// <param name="items">The strings to be written</param>
/// <returns><c>true</c> if the pipe is still open, <c>false</c> if not</returns>
[<CompiledName "WriteStrings">]
let writeStrings writer items = backgroundTask {
let rec writeNext docs idx = backgroundTask {
match items |> Seq.tryItem idx with
| Some item ->
if idx > 0 then
let! _ = writeString writer ","
()
match! writeString writer item with
| true -> return! writeNext docs (idx + 1)
| false -> return false
| None -> return true
}
let! _ = writeString writer "["
let! isCleanFinish = writeNext items 0
if isCleanFinish then
let! _ = writeString writer "]"
()
}

View File

@ -26,10 +26,10 @@ module Extensions =
member conn.customJsonArray query parameters mapFunc = member conn.customJsonArray query parameters mapFunc =
Custom.jsonArray query parameters mapFunc (Sql.existingConnection conn) Custom.jsonArray query parameters mapFunc (Sql.existingConnection conn)
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
member conn.writeCustomJsonArray query parameters writer mapFunc = member conn.writeCustomJsonArray query parameters writer mapFunc =
Custom.writeJsonArray query parameters writer mapFunc (Sql.existingConnection conn) Custom.writeJsonArray query parameters writer mapFunc (Sql.existingConnection conn)
@ -298,9 +298,9 @@ module Extensions =
member conn.jsonAll tableName = member conn.jsonAll tableName =
Json.all tableName (Sql.existingConnection conn) Json.all tableName (Sql.existingConnection conn)
/// <summary>Write all documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
member conn.writeJsonAll tableName writer = member conn.writeJsonAll tableName writer =
Json.writeAll tableName writer (Sql.existingConnection conn) Json.writeAll tableName writer (Sql.existingConnection conn)
@ -314,11 +314,11 @@ module Extensions =
Json.allOrdered tableName orderFields (Sql.existingConnection conn) Json.allOrdered tableName orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in the /// Write all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in the
/// document /// document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonAllOrdered tableName writer orderFields = member conn.writeJsonAllOrdered tableName writer orderFields =
Json.writeAllOrdered tableName writer orderFields (Sql.existingConnection conn) Json.writeAllOrdered tableName writer orderFields (Sql.existingConnection conn)
@ -330,9 +330,9 @@ module Extensions =
member conn.jsonById<'TKey> tableName (docId: 'TKey) = member conn.jsonById<'TKey> tableName (docId: 'TKey) =
Json.byId tableName docId (Sql.existingConnection conn) Json.byId tableName docId (Sql.existingConnection conn)
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
member conn.writeJsonById<'TKey> tableName writer (docId: 'TKey) = member conn.writeJsonById<'TKey> tableName writer (docId: 'TKey) =
Json.writeById tableName writer docId (Sql.existingConnection conn) Json.writeById tableName writer docId (Sql.existingConnection conn)
@ -346,11 +346,11 @@ module Extensions =
Json.byFields tableName howMatched fields (Sql.existingConnection conn) Json.byFields tableName howMatched fields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>,
/// etc.) /// etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
member conn.writeJsonByFields tableName writer howMatched fields = member conn.writeJsonByFields tableName writer howMatched fields =
@ -369,11 +369,11 @@ module Extensions =
Json.byFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn) Json.byFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>,
/// etc.) ordered by the given fields in the document /// etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -388,10 +388,10 @@ module Extensions =
Json.byContains tableName criteria (Sql.existingConnection conn) Json.byContains tableName criteria (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
member conn.writeJsonByContains tableName writer (criteria: obj) = member conn.writeJsonByContains tableName writer (criteria: obj) =
Json.writeByContains tableName writer criteria (Sql.existingConnection conn) Json.writeByContains tableName writer criteria (Sql.existingConnection conn)
@ -408,11 +408,11 @@ module Extensions =
Json.byContainsOrdered tableName criteria orderFields (Sql.existingConnection conn) Json.byContainsOrdered tableName criteria orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered
/// ordered by the given fields in the document /// by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonByContainsOrdered tableName writer (criteria: obj) orderFields = member conn.writeJsonByContainsOrdered tableName writer (criteria: obj) orderFields =
@ -426,10 +426,10 @@ module Extensions =
Json.byJsonPath tableName jsonPath (Sql.existingConnection conn) Json.byJsonPath tableName jsonPath (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
member conn.writeJsonByJsonPath tableName writer jsonPath = member conn.writeJsonByJsonPath tableName writer jsonPath =
Json.writeByJsonPath tableName writer jsonPath (Sql.existingConnection conn) Json.writeByJsonPath tableName writer jsonPath (Sql.existingConnection conn)
@ -446,11 +446,11 @@ module Extensions =
Json.byJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn) Json.byJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) ordered /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by
/// by the given fields in the document /// the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonByJsonPathOrdered tableName writer jsonPath orderFields = member conn.writeJsonByJsonPathOrdered tableName writer jsonPath orderFields =
@ -467,11 +467,11 @@ module Extensions =
Json.firstByFields tableName howMatched fields (Sql.existingConnection conn) Json.firstByFields tableName howMatched fields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
member conn.writeJsonFirstByFields tableName writer howMatched fields = member conn.writeJsonFirstByFields tableName writer howMatched fields =
@ -490,11 +490,11 @@ module Extensions =
Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn) Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -510,11 +510,11 @@ module Extensions =
Json.firstByContains tableName criteria (Sql.existingConnection conn) Json.firstByContains tableName criteria (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query
/// (<c>@&gt;</c>) /// (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
member conn.writeJsonFirstByContains tableName writer (criteria: obj) = member conn.writeJsonFirstByContains tableName writer (criteria: obj) =
Json.writeFirstByContains tableName writer criteria (Sql.existingConnection conn) Json.writeFirstByContains tableName writer criteria (Sql.existingConnection conn)
@ -531,11 +531,11 @@ module Extensions =
Json.firstByContainsOrdered tableName criteria orderFields (Sql.existingConnection conn) Json.firstByContainsOrdered tableName criteria orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query
/// (<c>@&gt;</c>) ordered by the given fields in the document /// (<c>@&gt;</c>) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonFirstByContainsOrdered tableName writer (criteria: obj) orderFields = member conn.writeJsonFirstByContainsOrdered tableName writer (criteria: obj) orderFields =
@ -549,10 +549,10 @@ module Extensions =
Json.firstByJsonPath tableName jsonPath (Sql.existingConnection conn) Json.firstByJsonPath tableName jsonPath (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
member conn.writeJsonFirstByJsonPath tableName writer jsonPath = member conn.writeJsonFirstByJsonPath tableName writer jsonPath =
Json.writeFirstByJsonPath tableName writer jsonPath (Sql.existingConnection conn) Json.writeFirstByJsonPath tableName writer jsonPath (Sql.existingConnection conn)
@ -569,11 +569,11 @@ module Extensions =
Json.firstByJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn) Json.firstByJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonFirstByJsonPathOrdered tableName writer jsonPath orderFields = member conn.writeJsonFirstByJsonPathOrdered tableName writer jsonPath orderFields =
@ -710,11 +710,11 @@ type NpgsqlConnectionCSharpExtensions =
static member inline CustomJsonArray(conn, query, parameters, mapFunc) = static member inline CustomJsonArray(conn, query, parameters, mapFunc) =
Custom.JsonArray(query, parameters, mapFunc, Sql.existingConnection conn) Custom.JsonArray(query, parameters, mapFunc, Sql.existingConnection conn)
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
[<Extension>] [<Extension>]
static member inline WriteCustomJsonArray(conn, query, parameters, writer, mapFunc) = static member inline WriteCustomJsonArray(conn, query, parameters, writer, mapFunc) =
@ -1048,10 +1048,10 @@ type NpgsqlConnectionCSharpExtensions =
static member inline JsonAll(conn, tableName) = static member inline JsonAll(conn, tableName) =
Json.all tableName (Sql.existingConnection conn) Json.all tableName (Sql.existingConnection conn)
/// <summary>Write all documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
[<Extension>] [<Extension>]
static member inline WriteJsonAll(conn, tableName, writer) = static member inline WriteJsonAll(conn, tableName, writer) =
Json.writeAll tableName writer (Sql.existingConnection conn) Json.writeAll tableName writer (Sql.existingConnection conn)
@ -1068,12 +1068,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.allOrdered tableName orderFields (Sql.existingConnection conn) Json.allOrdered tableName orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in the /// Write all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in the
/// document /// document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]
static member inline WriteJsonAllOrdered(conn, tableName, writer, orderFields) = static member inline WriteJsonAllOrdered(conn, tableName, writer, orderFields) =
@ -1088,10 +1088,10 @@ type NpgsqlConnectionCSharpExtensions =
static member inline JsonById<'TKey>(conn, tableName, docId: 'TKey) = static member inline JsonById<'TKey>(conn, tableName, docId: 'TKey) =
Json.byId tableName docId (Sql.existingConnection conn) Json.byId tableName docId (Sql.existingConnection conn)
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
[<Extension>] [<Extension>]
static member inline WriteJsonById<'TKey>(conn, tableName, writer, docId) = static member inline WriteJsonById<'TKey>(conn, tableName, writer, docId) =
@ -1108,11 +1108,11 @@ type NpgsqlConnectionCSharpExtensions =
Json.byFields tableName howMatched fields (Sql.existingConnection conn) Json.byFields tableName howMatched fields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<Extension>] [<Extension>]
@ -1134,12 +1134,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.byFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn) Json.byFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -1157,11 +1157,11 @@ type NpgsqlConnectionCSharpExtensions =
Json.byContains tableName criteria (Sql.existingConnection conn) Json.byContains tableName criteria (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
[<Extension>] [<Extension>]
static member inline WriteJsonByContains(conn, tableName, writer, criteria: obj) = static member inline WriteJsonByContains(conn, tableName, writer, criteria: obj) =
@ -1181,12 +1181,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.byContainsOrdered tableName criteria orderFields (Sql.existingConnection conn) Json.byContainsOrdered tableName criteria orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered by
/// by the given fields in the document /// the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]
@ -1203,11 +1203,11 @@ type NpgsqlConnectionCSharpExtensions =
Json.byJsonPath tableName jsonPath (Sql.existingConnection conn) Json.byJsonPath tableName jsonPath (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
[<Extension>] [<Extension>]
static member inline WriteJsonByJsonPath(conn, tableName, writer, jsonPath) = static member inline WriteJsonByJsonPath(conn, tableName, writer, jsonPath) =
@ -1226,12 +1226,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.byJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn) Json.byJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by the
/// the given fields in the document /// given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]
@ -1249,12 +1249,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByFields tableName howMatched fields (Sql.existingConnection conn) Json.firstByFields tableName howMatched fields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<Extension>] [<Extension>]
@ -1276,12 +1276,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn) Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -1300,11 +1300,11 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByContains tableName criteria (Sql.existingConnection conn) Json.firstByContains tableName criteria (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
[<Extension>] [<Extension>]
static member inline WriteJsonFirstByContains(conn, tableName, writer, criteria: obj) = static member inline WriteJsonFirstByContains(conn, tableName, writer, criteria: obj) =
@ -1324,12 +1324,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByContainsOrdered tableName criteria orderFields (Sql.existingConnection conn) Json.firstByContainsOrdered tableName criteria orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]
@ -1346,11 +1346,11 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByJsonPath tableName jsonPath (Sql.existingConnection conn) Json.firstByJsonPath tableName jsonPath (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
[<Extension>] [<Extension>]
static member inline WriteJsonFirstByJsonPath(conn, tableName, writer, jsonPath) = static member inline WriteJsonFirstByJsonPath(conn, tableName, writer, jsonPath) =
@ -1370,12 +1370,12 @@ type NpgsqlConnectionCSharpExtensions =
Json.firstByJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn) Json.firstByJsonPathOrdered tableName jsonPath orderFields (Sql.existingConnection conn)
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param> /// <param name="conn">The <c>NpgsqlConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]

View File

@ -38,19 +38,19 @@ module Custom =
let JsonArray(query, parameters, mapFunc) = let JsonArray(query, parameters, mapFunc) =
WithProps.Custom.JsonArray(query, parameters, mapFunc, fromDataSource ()) WithProps.Custom.JsonArray(query, parameters, mapFunc, fromDataSource ())
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
let writeJsonArray query parameters writer mapFunc = let writeJsonArray query parameters writer mapFunc =
WithProps.Custom.writeJsonArray query parameters writer mapFunc (fromDataSource ()) WithProps.Custom.writeJsonArray query parameters writer mapFunc (fromDataSource ())
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
let WriteJsonArray(query, parameters, writer, mapFunc) = let WriteJsonArray(query, parameters, writer, mapFunc) =
WithProps.Custom.WriteJsonArray(query, parameters, writer, mapFunc, fromDataSource ()) WithProps.Custom.WriteJsonArray(query, parameters, writer, mapFunc, fromDataSource ())
@ -527,9 +527,9 @@ module Json =
let all tableName = let all tableName =
WithProps.Json.all tableName (fromDataSource ()) WithProps.Json.all tableName (fromDataSource ())
/// <summary>Write all documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
[<CompiledName "WriteAll">] [<CompiledName "WriteAll">]
let writeAll tableName writer = let writeAll tableName writer =
WithProps.Json.writeAll tableName writer (fromDataSource ()) WithProps.Json.writeAll tableName writer (fromDataSource ())
@ -545,11 +545,11 @@ module Json =
WithProps.Json.allOrdered tableName orderFields (fromDataSource ()) WithProps.Json.allOrdered tableName orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in the /// Write all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in the
/// document /// document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteAllOrdered">] [<CompiledName "WriteAllOrdered">]
let writeAllOrdered tableName writer orderFields = let writeAllOrdered tableName writer orderFields =
@ -563,9 +563,9 @@ module Json =
let byId<'TKey> tableName (docId: 'TKey) = let byId<'TKey> tableName (docId: 'TKey) =
WithProps.Json.byId tableName docId (fromDataSource ()) WithProps.Json.byId tableName docId (fromDataSource ())
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
[<CompiledName "WriteById">] [<CompiledName "WriteById">]
let writeById<'TKey> tableName writer (docId: 'TKey) = let writeById<'TKey> tableName writer (docId: 'TKey) =
@ -581,10 +581,10 @@ module Json =
WithProps.Json.byFields tableName howMatched fields (fromDataSource ()) WithProps.Json.byFields tableName howMatched fields (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<CompiledName "WriteByFields">] [<CompiledName "WriteByFields">]
@ -605,11 +605,11 @@ module Json =
WithProps.Json.byFieldsOrdered tableName howMatched queryFields orderFields (fromDataSource ()) WithProps.Json.byFieldsOrdered tableName howMatched queryFields orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -626,10 +626,10 @@ module Json =
WithProps.Json.byContains tableName criteria (fromDataSource ()) WithProps.Json.byContains tableName criteria (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
[<CompiledName "WriteByContains">] [<CompiledName "WriteByContains">]
let writeByContains tableName writer (criteria: obj) = let writeByContains tableName writer (criteria: obj) =
@ -648,11 +648,11 @@ module Json =
WithProps.Json.byContainsOrdered tableName criteria orderFields (fromDataSource ()) WithProps.Json.byContainsOrdered tableName criteria orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered by
/// by the given fields in the document /// the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteByContainsOrdered">] [<CompiledName "WriteByContainsOrdered">]
@ -668,10 +668,10 @@ module Json =
WithProps.Json.byJsonPath tableName jsonPath (fromDataSource ()) WithProps.Json.byJsonPath tableName jsonPath (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
[<CompiledName "WriteByJsonPath">] [<CompiledName "WriteByJsonPath">]
let writeByJsonPath tableName writer jsonPath = let writeByJsonPath tableName writer jsonPath =
@ -689,11 +689,11 @@ module Json =
WithProps.Json.byJsonPathOrdered tableName jsonPath orderFields (fromDataSource ()) WithProps.Json.byJsonPathOrdered tableName jsonPath orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by the
/// the given fields in the document /// given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteByJsonPathOrdered">] [<CompiledName "WriteByJsonPathOrdered">]
@ -710,11 +710,11 @@ module Json =
WithProps.Json.firstByFields tableName howMatched fields (fromDataSource ()) WithProps.Json.firstByFields tableName howMatched fields (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<CompiledName "WriteFirstByFields">] [<CompiledName "WriteFirstByFields">]
@ -735,11 +735,11 @@ module Json =
WithProps.Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (fromDataSource ()) WithProps.Json.firstByFieldsOrdered tableName howMatched queryFields orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -756,10 +756,10 @@ module Json =
WithProps.Json.firstByContains tableName criteria (fromDataSource ()) WithProps.Json.firstByContains tableName criteria (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
[<CompiledName "WriteFirstByContains">] [<CompiledName "WriteFirstByContains">]
let writeFirstByContains tableName writer (criteria: obj) = let writeFirstByContains tableName writer (criteria: obj) =
@ -778,11 +778,11 @@ module Json =
WithProps.Json.firstByContainsOrdered tableName criteria orderFields (fromDataSource ()) WithProps.Json.firstByContainsOrdered tableName criteria orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteFirstByContainsOrdered">] [<CompiledName "WriteFirstByContainsOrdered">]
@ -798,10 +798,10 @@ module Json =
WithProps.Json.firstByJsonPath tableName jsonPath (fromDataSource ()) WithProps.Json.firstByJsonPath tableName jsonPath (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
[<CompiledName "WriteFirstByJsonPath">] [<CompiledName "WriteFirstByJsonPath">]
let writeFirstByJsonPath tableName writer jsonPath = let writeFirstByJsonPath tableName writer jsonPath =
@ -820,11 +820,11 @@ module Json =
WithProps.Json.firstByJsonPathOrdered tableName jsonPath orderFields (fromDataSource ()) WithProps.Json.firstByJsonPathOrdered tableName jsonPath orderFields (fromDataSource ())
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteFirstByJsonPathOrdered">] [<CompiledName "WriteFirstByJsonPathOrdered">]

View File

@ -1,9 +1,5 @@
namespace BitBadger.Documents.Postgres namespace BitBadger.Documents.Postgres
open System.IO
open System.Text
open System.Threading.Tasks
/// <summary>The type of index to generate for the document</summary> /// <summary>The type of index to generate for the document</summary>
[<Struct>] [<Struct>]
type DocumentIndex = type DocumentIndex =
@ -268,6 +264,8 @@ module Query =
Query.statementWhere statement (whereJsonPathMatches "@path") Query.statementWhere statement (whereJsonPathMatches "@path")
open System.Text
/// <summary>Functions for dealing with results</summary> /// <summary>Functions for dealing with results</summary>
[<AutoOpen>] [<AutoOpen>]
module Results = module Results =
@ -337,24 +335,16 @@ module Results =
let ToJsonArray(mapFunc: System.Func<RowReader, string>, sqlProps) = let ToJsonArray(mapFunc: System.Func<RowReader, string>, sqlProps) =
toJsonArray mapFunc.Invoke sqlProps toJsonArray mapFunc.Invoke sqlProps
/// <summary>Write a JSON array of items for the results of a query to the given <c>StreamWriter</c></summary> /// <summary>Write a JSON array of items for the results of a query to the given <c>PipeWriter</c></summary>
/// <param name="writer">The StreamWriter to which results should be written</param> /// <param name="writer">The PipeWriter to which results should be written</param>
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param> /// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
/// <param name="sqlProps">The query from which JSON should be extracted</param> /// <param name="sqlProps">The query from which JSON should be extracted</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
let writeJsonArray (writer: StreamWriter) (mapFunc: RowReader -> string) sqlProps = backgroundTask { let writeJsonArray writer (mapFunc: RowReader -> string) sqlProps =
let await (it: Task) = it.ConfigureAwait(false).GetAwaiter().GetResult() sqlProps |> Sql.toSeq mapFunc |> PipeWriter.writeStrings writer
do! writer.WriteAsync "["
let mutable isFirst = true
do! sqlProps
|> Sql.iterAsync (fun it ->
if isFirst then isFirst <- false else await (writer.WriteAsync ",")
(mapFunc >> writer.WriteAsync >> await) it)
do! writer.WriteAsync "]"
}
/// <summary>Write a JSON array of items for the results of a query to the given <c>StreamWriter</c></summary> /// <summary>Write a JSON array of items for the results of a query to the given <c>PipeWriter</c></summary>
/// <param name="writer">The StreamWriter to which results should be written</param> /// <param name="writer">The PipeWriter to which results should be written</param>
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param> /// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
/// <param name="sqlProps">The query from which JSON should be extracted</param> /// <param name="sqlProps">The query from which JSON should be extracted</param>
let WriteJsonArray(writer, mapFunc: System.Func<RowReader, string>, sqlProps) = let WriteJsonArray(writer, mapFunc: System.Func<RowReader, string>, sqlProps) =

View File

@ -1,7 +1,6 @@
/// <summary>Versions of queries that accept <c>SqlProps</c> as the last parameter</summary> /// <summary>Versions of queries that accept <c>SqlProps</c> as the last parameter</summary>
module BitBadger.Documents.Postgres.WithProps module BitBadger.Documents.Postgres.WithProps
open System.IO
open BitBadger.Documents open BitBadger.Documents
open Npgsql.FSharp open Npgsql.FSharp
@ -55,10 +54,10 @@ module Custom =
let JsonArray(query, parameters, mapFunc: System.Func<RowReader, string>, sqlProps) = let JsonArray(query, parameters, mapFunc: System.Func<RowReader, string>, sqlProps) =
jsonArray query parameters mapFunc.Invoke sqlProps jsonArray query parameters mapFunc.Invoke sqlProps
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
@ -67,10 +66,10 @@ module Custom =
|> Sql.parameters (FSharpList.ofSeq parameters) |> Sql.parameters (FSharpList.ofSeq parameters)
|> writeJsonArray writer mapFunc |> writeJsonArray writer mapFunc
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
let WriteJsonArray(query, parameters, writer, mapFunc: System.Func<RowReader, string>, sqlProps) = let WriteJsonArray(query, parameters, writer, mapFunc: System.Func<RowReader, string>, sqlProps) =
@ -724,9 +723,9 @@ module Json =
let all tableName sqlProps = let all tableName sqlProps =
Custom.jsonArray (Query.find tableName) [] jsonFromData sqlProps Custom.jsonArray (Query.find tableName) [] jsonFromData sqlProps
/// <summary>Write all documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteAll">] [<CompiledName "WriteAll">]
let writeAll tableName writer sqlProps = let writeAll tableName writer sqlProps =
@ -744,11 +743,11 @@ module Json =
Custom.jsonArray (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] jsonFromData sqlProps Custom.jsonArray (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] jsonFromData sqlProps
/// <summary> /// <summary>
/// Write all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in the /// Write all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in the
/// document /// document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteAllOrdered">] [<CompiledName "WriteAllOrdered">]
@ -765,15 +764,16 @@ module Json =
let byId<'TKey> tableName (docId: 'TKey) sqlProps = let byId<'TKey> tableName (docId: 'TKey) sqlProps =
Custom.jsonSingle (Query.byId (Query.find tableName) docId) [ idParam docId ] jsonFromData sqlProps Custom.jsonSingle (Query.byId (Query.find tableName) docId) [ idParam docId ] jsonFromData sqlProps
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteById">] [<CompiledName "WriteById">]
let writeById<'TKey> tableName (writer: StreamWriter) (docId: 'TKey) sqlProps = backgroundTask { let writeById<'TKey> tableName writer (docId: 'TKey) sqlProps = backgroundTask {
let! json = byId tableName docId sqlProps let! json = byId tableName docId sqlProps
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary>Retrieve JSON documents matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)</summary> /// <summary>Retrieve JSON documents matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)</summary>
@ -788,10 +788,10 @@ module Json =
(Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
@ -823,11 +823,11 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -852,10 +852,10 @@ module Json =
(Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps (Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteByContains">] [<CompiledName "WriteByContains">]
@ -881,11 +881,11 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>) ordered by
/// by the given fields in the document /// the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
@ -909,10 +909,10 @@ module Json =
(Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps (Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteByJsonPath">] [<CompiledName "WriteByJsonPath">]
@ -937,11 +937,11 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by /// Write JSON documents to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>) ordered by the
/// the given fields in the document /// given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
@ -966,18 +966,19 @@ module Json =
(Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons(<c>-&gt;&gt; =</c>,
/// (<c>-&gt;&gt; =</c>, etc.) /// etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFields">] [<CompiledName "WriteFirstByFields">]
let writeFirstByFields tableName (writer: StreamWriter) howMatched fields sqlProps = backgroundTask { let writeFirstByFields tableName writer howMatched fields sqlProps = backgroundTask {
let! json = firstByFields tableName howMatched fields sqlProps let! json = firstByFields tableName howMatched fields sqlProps
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary> /// <summary>
@ -999,21 +1000,21 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFieldsOrdered">] [<CompiledName "WriteFirstByFieldsOrdered">]
let writeFirstByFieldsOrdered tableName (writer: StreamWriter) howMatched queryFields orderFields sqlProps = let writeFirstByFieldsOrdered tableName writer howMatched queryFields orderFields sqlProps = backgroundTask {
backgroundTask { let! json = firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps
let! json = firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps let! _ = PipeWriter.writeString writer json
do! writer.WriteAsync json ()
} }
/// <summary>Retrieve the first JSON document matching a JSON containment query (<c>@&gt;</c>)</summary> /// <summary>Retrieve the first JSON document matching a JSON containment query (<c>@&gt;</c>)</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
@ -1026,16 +1027,17 @@ module Json =
(Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps (Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByContains">] [<CompiledName "WriteFirstByContains">]
let writeFirstByContains tableName (writer: StreamWriter) (criteria: obj) sqlProps = backgroundTask { let writeFirstByContains tableName writer (criteria: obj) sqlProps = backgroundTask {
let! json = firstByContains tableName criteria sqlProps let! json = firstByContains tableName criteria sqlProps
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary> /// <summary>
@ -1056,20 +1058,20 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON containment query (<c>@&gt;</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON containment query (<c>@&gt;</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="criteria">The document to match with the containment query</param> /// <param name="criteria">The document to match with the containment query</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByContainsOrdered">] [<CompiledName "WriteFirstByContainsOrdered">]
let writeFirstByContainsOrdered tableName (writer: StreamWriter) (criteria: obj) orderFields sqlProps = let writeFirstByContainsOrdered tableName writer (criteria: obj) orderFields sqlProps = backgroundTask {
backgroundTask { let! json = firstByContainsOrdered tableName criteria orderFields sqlProps
let! json = firstByContainsOrdered tableName criteria orderFields sqlProps let! _ = PipeWriter.writeString writer json
do! writer.WriteAsync json ()
} }
/// <summary>Retrieve the first JSON document matching a JSON Path match query (<c>@?</c>)</summary> /// <summary>Retrieve the first JSON document matching a JSON Path match query (<c>@?</c>)</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
@ -1082,16 +1084,17 @@ module Json =
(Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps (Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByJsonPath">] [<CompiledName "WriteFirstByJsonPath">]
let writeFirstByJsonPath tableName (writer: StreamWriter) jsonPath sqlProps = backgroundTask { let writeFirstByJsonPath tableName writer jsonPath sqlProps = backgroundTask {
let! json = firstByJsonPath tableName jsonPath sqlProps let! json = firstByJsonPath tableName jsonPath sqlProps
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary> /// <summary>
@ -1112,18 +1115,19 @@ module Json =
sqlProps sqlProps
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching a JSON Path match query (<c>@?</c>) /// Write the first JSON document to the given <c>PipeWriter</c> matching a JSON Path match query (<c>@?</c>)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="jsonPath">The JSON Path expression to match</param> /// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param> /// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByJsonPathOrdered">] [<CompiledName "WriteFirstByJsonPathOrdered">]
let writeFirstByJsonPathOrdered tableName (writer: StreamWriter) jsonPath orderFields sqlProps = backgroundTask { let writeFirstByJsonPathOrdered tableName writer jsonPath orderFields sqlProps = backgroundTask {
let! json = firstByJsonPathOrdered tableName jsonPath orderFields sqlProps let! json = firstByJsonPathOrdered tableName jsonPath orderFields sqlProps
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary>Commands to update documents</summary> /// <summary>Commands to update documents</summary>

View File

@ -25,10 +25,10 @@ module Extensions =
member conn.customJsonArray query parameters mapFunc = member conn.customJsonArray query parameters mapFunc =
Custom.jsonArray query parameters mapFunc conn Custom.jsonArray query parameters mapFunc conn
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
member conn.writeCustomJsonArray query parameters writer mapFunc = member conn.writeCustomJsonArray query parameters writer mapFunc =
Custom.writeJsonArray query parameters writer mapFunc conn Custom.writeJsonArray query parameters writer mapFunc conn
@ -242,46 +242,46 @@ module Extensions =
member conn.jsonFirstByFieldsOrdered tableName howMatched queryFields orderFields = member conn.jsonFirstByFieldsOrdered tableName howMatched queryFields orderFields =
Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn
/// <summary>Write all JSON documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all JSON documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
member conn.writeJsonAll tableName writer = member conn.writeJsonAll tableName writer =
Json.writeAll tableName writer conn Json.writeAll tableName writer conn
/// <summary> /// <summary>
/// Write all JSON all documents in the given table to the given <c>StreamWriter</c>, ordered by the given /// Write all JSON all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields
/// fields in the document /// in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
member conn.writeJsonAllOrdered tableName writer orderFields = member conn.writeJsonAllOrdered tableName writer orderFields =
Json.writeAllOrdered tableName writer orderFields conn Json.writeAllOrdered tableName writer orderFields conn
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
member conn.writeJsonById<'TKey> tableName writer (docId: 'TKey) = member conn.writeJsonById<'TKey> tableName writer (docId: 'TKey) =
Json.writeById tableName writer docId conn Json.writeById tableName writer docId conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>,
/// etc.) /// etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
member conn.writeJsonByFields tableName writer howMatched fields = member conn.writeJsonByFields tableName writer howMatched fields =
Json.writeByFields tableName writer howMatched fields conn Json.writeByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>,
/// etc.) ordered by the given fields in the document /// etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -289,22 +289,22 @@ module Extensions =
Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
member conn.writeJsonFirstByFields tableName writer howMatched fields = member conn.writeJsonFirstByFields tableName writer howMatched fields =
Json.writeFirstByFields tableName writer howMatched fields conn Json.writeFirstByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -399,11 +399,11 @@ type SqliteConnectionCSharpExtensions =
static member inline CustomJsonArray(conn, query, parameters, mapFunc) = static member inline CustomJsonArray(conn, query, parameters, mapFunc) =
Custom.JsonArray(query, parameters, mapFunc, conn) Custom.JsonArray(query, parameters, mapFunc, conn)
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
[<Extension>] [<Extension>]
static member inline WriteCustomJsonArray(conn, query, parameters, writer, mapFunc) = static member inline WriteCustomJsonArray(conn, query, parameters, writer, mapFunc) =
@ -668,41 +668,41 @@ type SqliteConnectionCSharpExtensions =
static member inline JsonFirstByFieldsOrdered(conn, tableName, howMatched, queryFields, orderFields) = static member inline JsonFirstByFieldsOrdered(conn, tableName, howMatched, queryFields, orderFields) =
Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn
/// <summary>Write all JSON documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all JSON documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
[<Extension>] [<Extension>]
static member inline WriteJsonAll(conn, tableName, writer) = static member inline WriteJsonAll(conn, tableName, writer) =
Json.writeAll tableName writer conn Json.writeAll tableName writer conn
/// <summary> /// <summary>
/// Write all JSON all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in /// Write all JSON all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in
/// the document /// the document
/// </summary> /// </summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<Extension>] [<Extension>]
static member inline WriteJsonAllOrdered(conn, tableName, writer, orderFields) = static member inline WriteJsonAllOrdered(conn, tableName, writer, orderFields) =
Json.writeAllOrdered tableName writer orderFields conn Json.writeAllOrdered tableName writer orderFields conn
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
[<Extension>] [<Extension>]
static member inline WriteJsonById<'TKey>(conn, tableName, writer, docId: 'TKey) = static member inline WriteJsonById<'TKey>(conn, tableName, writer, docId: 'TKey) =
Json.writeById tableName writer docId conn Json.writeById tableName writer docId conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<Extension>] [<Extension>]
@ -710,12 +710,12 @@ type SqliteConnectionCSharpExtensions =
Json.writeByFields tableName writer howMatched fields conn Json.writeByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -724,12 +724,12 @@ type SqliteConnectionCSharpExtensions =
Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<Extension>] [<Extension>]
@ -737,12 +737,12 @@ type SqliteConnectionCSharpExtensions =
Json.writeFirstByFields tableName writer howMatched fields conn Json.writeFirstByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param> /// <param name="conn">The <c>SqliteConnection</c> on which to run the query</param>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>

View File

@ -44,20 +44,20 @@ module Custom =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Custom.JsonArray(query, parameters, mapFunc, conn) WithConn.Custom.JsonArray(query, parameters, mapFunc, conn)
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
let writeJsonArray query parameters writer mapFunc = let writeJsonArray query parameters writer mapFunc =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Custom.writeJsonArray query parameters writer mapFunc conn WithConn.Custom.writeJsonArray query parameters writer mapFunc conn
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
let WriteJsonArray(query, parameters, writer, mapFunc) = let WriteJsonArray(query, parameters, writer, mapFunc) =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
@ -446,29 +446,29 @@ module Json =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn WithConn.Json.firstByFieldsOrdered tableName howMatched queryFields orderFields conn
/// <summary>Write all JSON documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all JSON documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
[<CompiledName "WriteAll">] [<CompiledName "WriteAll">]
let writeAll tableName writer = let writeAll tableName writer =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Json.writeAll tableName writer conn WithConn.Json.writeAll tableName writer conn
/// <summary> /// <summary>
/// Write all JSON all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in /// Write all JSON all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in
/// the document /// the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
[<CompiledName "WriteAllOrdered">] [<CompiledName "WriteAllOrdered">]
let writeAllOrdered tableName writer orderFields = let writeAllOrdered tableName writer orderFields =
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
WithConn.Json.writeAllOrdered tableName writer orderFields conn WithConn.Json.writeAllOrdered tableName writer orderFields conn
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
[<CompiledName "WriteById">] [<CompiledName "WriteById">]
let writeById<'TKey> tableName writer (docId: 'TKey) = let writeById<'TKey> tableName writer (docId: 'TKey) =
@ -476,10 +476,10 @@ module Json =
WithConn.Json.writeById tableName writer docId conn WithConn.Json.writeById tableName writer docId conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<CompiledName "WriteByFields">] [<CompiledName "WriteByFields">]
@ -488,11 +488,11 @@ module Json =
WithConn.Json.writeByFields tableName writer howMatched fields conn WithConn.Json.writeByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -502,11 +502,11 @@ module Json =
WithConn.Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn WithConn.Json.writeByFieldsOrdered tableName writer howMatched queryFields orderFields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
[<CompiledName "WriteFirstByFields">] [<CompiledName "WriteFirstByFields">]
@ -515,11 +515,11 @@ module Json =
WithConn.Json.writeFirstByFields tableName writer howMatched fields conn WithConn.Json.writeFirstByFields tableName writer howMatched fields conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>

View File

@ -1,8 +1,5 @@
namespace BitBadger.Documents.Sqlite namespace BitBadger.Documents.Sqlite
open System.IO
open System.Text
open BitBadger.Documents
open Microsoft.Data.Sqlite open Microsoft.Data.Sqlite
/// <summary>Configuration for document handling</summary> /// <summary>Configuration for document handling</summary>
@ -34,6 +31,8 @@ module Configuration =
| None -> invalidOp "Please provide a connection string before attempting data access" | None -> invalidOp "Please provide a connection string before attempting data access"
open BitBadger.Documents
/// <summary>Query definitions</summary> /// <summary>Query definitions</summary>
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Query = module Query =
@ -183,6 +182,8 @@ module Parameters =
Seq.empty<SqliteParameter> Seq.empty<SqliteParameter>
open System.Text
/// <summary>Helper functions for handling results</summary> /// <summary>Helper functions for handling results</summary>
[<AutoOpen>] [<AutoOpen>]
module Results = module Results =
@ -294,22 +295,18 @@ module Results =
/// <param name="writer">The StreamWriter to which results should be written</param> /// <param name="writer">The StreamWriter to which results should be written</param>
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param> /// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
let writeJsonArray (cmd: SqliteCommand) (writer: StreamWriter) (mapFunc: SqliteDataReader -> string) = let writeJsonArray (cmd: SqliteCommand) writer (mapFunc: SqliteDataReader -> string) = backgroundTask {
backgroundTask { use! rdr = cmd.ExecuteReaderAsync()
use! rdr = cmd.ExecuteReaderAsync() return
do! writer.WriteAsync "[" seq { while rdr.Read() do yield mapFunc rdr }
let mutable isFirst = true |> PipeWriter.writeStrings writer
while! rdr.ReadAsync() do }
if isFirst then isFirst <- false else do! writer.WriteAsync ","
do! writer.WriteAsync(mapFunc rdr)
do! writer.WriteAsync "]"
}
/// <summary>Write a JSON array of items for the results of a query to the given <c>StreamWriter</c></summary> /// <summary>Write a JSON array of items for the results of a query to the given <c>StreamWriter</c></summary>
/// <param name="cmd">The command to execute</param> /// <param name="cmd">The command to execute</param>
/// <param name="writer">The StreamWriter to which results should be written</param> /// <param name="writer">The StreamWriter to which results should be written</param>
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param> /// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
let WriteJsonArray (cmd: SqliteCommand) (writer: StreamWriter) (mapFunc: System.Func<SqliteDataReader, string>) = let WriteJsonArray (cmd: SqliteCommand) writer (mapFunc: System.Func<SqliteDataReader, string>) =
writeJsonArray cmd writer mapFunc.Invoke writeJsonArray cmd writer mapFunc.Invoke

View File

@ -1,7 +1,6 @@
/// <summary>Versions of queries that accept a <c>SqliteConnection</c> as the last parameter</summary> /// <summary>Versions of queries that accept a <c>SqliteConnection</c> as the last parameter</summary>
module BitBadger.Documents.Sqlite.WithConn module BitBadger.Documents.Sqlite.WithConn
open System.IO
open BitBadger.Documents open BitBadger.Documents
open Microsoft.Data.Sqlite open Microsoft.Data.Sqlite
@ -64,10 +63,10 @@ module Custom =
let JsonArray(query, parameters, mapFunc: System.Func<SqliteDataReader, string>, conn) = let JsonArray(query, parameters, mapFunc: System.Func<SqliteDataReader, string>, conn) =
jsonArray query parameters mapFunc.Invoke conn jsonArray query parameters mapFunc.Invoke conn
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "FSharpWriteJsonArray">] [<CompiledName "FSharpWriteJsonArray">]
@ -82,10 +81,10 @@ module Custom =
cmd.Parameters.AddRange parameters cmd.Parameters.AddRange parameters
writeJsonArray cmd writer mapFunc writeJsonArray cmd writer mapFunc
/// <summary>Execute a query, writing its results to the given <c>StreamWriter</c></summary> /// <summary>Execute a query, writing its results to the given <c>PipeWriter</c></summary>
/// <param name="query">The query to retrieve the results</param> /// <param name="query">The query to retrieve the results</param>
/// <param name="parameters">Parameters to use for the query</param> /// <param name="parameters">Parameters to use for the query</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="mapFunc">The mapping function to extract the document</param> /// <param name="mapFunc">The mapping function to extract the document</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
let WriteJsonArray(query, parameters, writer, mapFunc: System.Func<SqliteDataReader, string>, conn) = let WriteJsonArray(query, parameters, writer, mapFunc: System.Func<SqliteDataReader, string>, conn) =
@ -554,42 +553,43 @@ module Json =
jsonFromData jsonFromData
conn conn
/// <summary>Write all JSON documents in the given table to the given <c>StreamWriter</c></summary> /// <summary>Write all JSON documents in the given table to the given <c>PipeWriter</c></summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "WriteAll">] [<CompiledName "WriteAll">]
let writeAll tableName writer conn = let writeAll tableName writer conn =
Custom.writeJsonArray (Query.find tableName) [] writer jsonFromData conn Custom.writeJsonArray (Query.find tableName) [] writer jsonFromData conn
/// <summary> /// <summary>
/// Write all JSON all documents in the given table to the given <c>StreamWriter</c>, ordered by the given fields in /// Write all JSON all documents in the given table to the given <c>PipeWriter</c>, ordered by the given fields in
/// the document /// the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "WriteAllOrdered">] [<CompiledName "WriteAllOrdered">]
let writeAllOrdered tableName writer orderFields conn = let writeAllOrdered tableName writer orderFields conn =
Custom.writeJsonArray (Query.find tableName + Query.orderBy orderFields SQLite) [] writer jsonFromData conn Custom.writeJsonArray (Query.find tableName + Query.orderBy orderFields SQLite) [] writer jsonFromData conn
/// <summary>Write a JSON document to the given <c>StreamWriter</c> by its ID</summary> /// <summary>Write a JSON document to the given <c>PipeWriter</c> by its ID</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="docId">The ID of the document to retrieve</param> /// <param name="docId">The ID of the document to retrieve</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "WriteById">] [<CompiledName "WriteById">]
let writeById<'TKey> tableName (writer: StreamWriter) (docId: 'TKey) conn = backgroundTask { let writeById<'TKey> tableName writer (docId: 'TKey) conn = backgroundTask {
let! json = Custom.jsonSingle (Query.byId (Query.find tableName) docId) [ idParam docId ] jsonFromData conn let! json = Custom.jsonSingle (Query.byId (Query.find tableName) docId) [ idParam docId ] jsonFromData conn
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
@ -603,11 +603,11 @@ module Json =
conn conn
/// <summary> /// <summary>
/// Write JSON documents to the given <c>StreamWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) /// Write JSON documents to the given <c>PipeWriter</c> matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.)
/// ordered by the given fields in the document /// ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param> /// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
@ -622,43 +622,44 @@ module Json =
conn conn
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) /// (<c>-&gt;&gt; =</c>, etc.)
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="fields">The field conditions to match</param> /// <param name="fields">The field conditions to match</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFields">] [<CompiledName "WriteFirstByFields">]
let writeFirstByFields tableName (writer: StreamWriter) howMatched fields conn = backgroundTask { let writeFirstByFields tableName writer howMatched fields conn = backgroundTask {
let! json = let! json =
Custom.jsonSingle Custom.jsonSingle
(Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData conn (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData conn
do! writer.WriteAsync json let! _ = PipeWriter.writeString writer json
()
} }
/// <summary> /// <summary>
/// Write the first JSON document to the given <c>StreamWriter</c> matching JSON field comparisons /// Write the first JSON document to the given <c>PipeWriter</c> matching JSON field comparisons
/// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document /// (<c>-&gt;&gt; =</c>, etc.) ordered by the given fields in the document
/// </summary> /// </summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param> /// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
/// <param name="writer">The StreamWriter to which the results should be written</param> /// <param name="writer">The PipeWriter to which the results should be written</param>
/// <param name="howMatched">Whether to match any or all of the field conditions</param> /// <param name="howMatched">Whether to match any or all of the field conditions</param>
/// <param name="queryFields">The field conditions to match</param> /// <param name="queryFields">The field conditions to match</param>
/// <param name="orderFields">Fields by which the results should be ordered</param> /// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param> /// <param name="conn">The <c>SqliteConnection</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFieldsOrdered">] [<CompiledName "WriteFirstByFieldsOrdered">]
let writeFirstByFieldsOrdered tableName (writer: StreamWriter) howMatched queryFields orderFields conn = let writeFirstByFieldsOrdered tableName writer howMatched queryFields orderFields conn = backgroundTask {
backgroundTask { let! json =
let! json = Custom.jsonSingle
Custom.jsonSingle (Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite)
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite) (addFieldParams queryFields [])
(addFieldParams queryFields []) jsonFromData
jsonFromData conn
conn let! _ = PipeWriter.writeString writer json
do! writer.WriteAsync json ()
} }
/// <summary>Commands to update documents</summary> /// <summary>Commands to update documents</summary>

View File

@ -1,3 +1,4 @@
using System.IO.Pipelines;
using Expecto.CSharp; using Expecto.CSharp;
using Expecto; using Expecto;
using Microsoft.FSharp.Core; using Microsoft.FSharp.Core;
@ -648,6 +649,114 @@ 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> /// <summary>
/// Unit tests /// Unit tests
/// </summary> /// </summary>
@ -660,6 +769,7 @@ public static class CommonCSharpTests
ParameterNameTests, ParameterNameTests,
AutoIdTests, AutoIdTests,
QueryTests, QueryTests,
PipeWriterTests,
TestSequenced(ConfigurationTests) TestSequenced(ConfigurationTests)
]); ]);
} }

View File

@ -1,3 +1,4 @@
using System.IO.Pipelines;
using Expecto.CSharp; using Expecto.CSharp;
using Expecto; using Expecto;
using BitBadger.Documents.Postgres; using BitBadger.Documents.Postgres;
@ -31,12 +32,8 @@ public class PostgresCSharpExtensionTests
} }
/// <summary>Set up a stream writer for a test</summary> /// <summary>Set up a stream writer for a test</summary>
private static StreamWriter WriteStream(Stream stream) private static PipeWriter WriteStream(Stream stream) =>
{ PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
StreamWriter writer = new(stream);
writer.AutoFlush = true;
return writer;
}
/// Get the text of the given stream /// Get the text of the given stream
private static string StreamText(Stream stream) private static string StreamText(Stream stream)
@ -54,10 +51,8 @@ public class PostgresCSharpExtensionTests
} }
/// <summary>Verify the presence of a document by its ID</summary> /// <summary>Verify the presence of a document by its ID</summary>
private static void VerifyDocById(string json, string docId) private static void VerifyDocById(string json, string docId) =>
{
Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present"); Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present");
}
/// <summary>Verify the presence of a document by its ID</summary> /// <summary>Verify the presence of a document by its ID</summary>
private static void VerifySingleById(string json, string docId) private static void VerifySingleById(string json, string docId)
@ -84,16 +79,12 @@ public class PostgresCSharpExtensionTests
} }
/// <summary>Verify an empty JSON array</summary> /// <summary>Verify an empty JSON array</summary>
private static void VerifyEmpty(string json) private static void VerifyEmpty(string json) =>
{
Expect.equal(json, "[]", "There should be no documents returned"); Expect.equal(json, "[]", "There should be no documents returned");
}
/// <summary>Verify an empty JSON document</summary> /// <summary>Verify an empty JSON document</summary>
private static void VerifyNoDoc(string json) private static void VerifyNoDoc(string json) =>
{
Expect.equal(json, "{}", "There should be no document returned"); Expect.equal(json, "{}", "There should be no document returned");
}
/// <summary>Verify the JSON for an ordered query</summary> /// <summary>Verify the JSON for an ordered query</summary>
private static void VerifyExpectedOrder(string json, string idFirst, string idSecond, string? idThird = null, private static void VerifyExpectedOrder(string json, string idFirst, string idSecond, string? idThird = null,
@ -158,9 +149,8 @@ public class PostgresCSharpExtensionTests
var docs = await conn.CustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, var docs = await conn.CustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None,
Results.JsonFromData); Results.JsonFromData);
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`"); VerifyBeginEnd(docs);
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned"); 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 () => TestCase("succeeds when data is not found", async () =>
{ {
@ -171,7 +161,7 @@ public class PostgresCSharpExtensionTests
var docs = await conn.CustomJsonArray( var docs = await conn.CustomJsonArray(
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData); [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData);
Expect.equal(docs, "[]", "There should have been no documents returned"); VerifyEmpty(docs);
}) })
]), ]),
TestList("WriteJsonArray", TestList("WriteJsonArray",
@ -183,14 +173,19 @@ public class PostgresCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteCustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, try
Results.JsonFromData); {
await conn.WriteCustomJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer,
var docs = StreamText(stream); Results.JsonFromData);
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`"); var docs = StreamText(stream);
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned"); VerifyBeginEnd(docs);
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`"); Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when data is not found", async () => TestCase("succeeds when data is not found", async () =>
{ {
@ -199,12 +194,18 @@ public class PostgresCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteCustomJsonArray( try
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", {
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData); await conn.WriteCustomJsonArray(
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
Expect.equal(StreamText(stream), "[]", "There should have been no documents returned"); [Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("CustomSingle", TestList("CustomSingle",
@ -1331,18 +1332,32 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAll(PostgresDb.TableName, writer); try
VerifyAllData(StreamText(stream)); {
await conn.WriteJsonAll(PostgresDb.TableName, writer);
VerifyAllData(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when there is no data", async () => TestCase("succeeds when there is no data", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await using var conn = MkConn(db); await using var conn = MkConn(db);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAll(PostgresDb.TableName, writer); try
VerifyEmpty(StreamText(stream)); {
await conn.WriteJsonAll(PostgresDb.TableName, writer);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonAllOrdered", TestList("WriteJsonAllOrdered",
@ -1353,9 +1368,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue")]); try
VerifyExpectedOrder(StreamText(stream), "one", "three", "two", "four", "five"); {
await conn.WriteJsonAllOrdered(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 () => TestCase("succeeds when ordering numerically descending", async () =>
{ {
@ -1363,9 +1385,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue DESC")]); try
VerifyExpectedOrder(StreamText(stream), "five", "four", "two", "three", "one"); {
await conn.WriteJsonAllOrdered(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 () => TestCase("succeeds when ordering alphabetically", async () =>
{ {
@ -1373,9 +1402,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]); try
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five"); {
await conn.WriteJsonAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]);
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonById", TestList("WriteJsonById",
@ -1387,11 +1423,18 @@ public class PostgresCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonById<string>(PostgresDb.TableName, writer, "two"); try
var json = StreamText(stream); {
Expect.stringStarts(json, """{"Id": "two",""", "An incorrect document was returned"); await conn.WriteJsonById<string>(PostgresDb.TableName, writer, "two");
Expect.stringEnds(json, "}", "JSON should have ended with this document"); 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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1399,9 +1442,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonById<string>(PostgresDb.TableName, writer, "three hundred eighty-seven"); try
VerifyNoDoc(StreamText(stream)); {
await conn.WriteJsonById<string>(PostgresDb.TableName, writer, "three hundred eighty-seven");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByFields", TestList("WriteJsonByFields",
@ -1412,10 +1462,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]); {
VerifySingleById(StreamText(stream), "four"); await conn.WriteJsonByFields(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() => TestCase("succeeds when documents are found using IN with numeric field", async() =>
{ {
@ -1423,10 +1480,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.In("NumValue", [2, 4, 6, 8])]); {
VerifySingleById(StreamText(stream), "three"); await conn.WriteJsonByFields(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 () => TestCase("succeeds when documents are not found", async () =>
{ {
@ -1434,10 +1498,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "mauve"), Field.NotEqual("NumValue", 40)]); {
VerifyEmpty(StreamText(stream)); await conn.WriteJsonByFields(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 () => TestCase("succeeds for InArray when matching documents exist", async () =>
{ {
@ -1447,13 +1518,20 @@ public class PostgresCSharpExtensionTests
foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", PostgresDb.TableName, ["c"])]); {
var json = StreamText(stream); await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All,
VerifyBeginEnd(json); [Field.InArray("Values", PostgresDb.TableName, ["c"])]);
VerifyDocById(json, "first"); var json = StreamText(stream);
VerifyDocById(json, "second"); VerifyBeginEnd(json);
VerifyDocById(json, "first");
VerifyDocById(json, "second");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds for InArray when no matching documents exist", async () => TestCase("succeeds for InArray when no matching documents exist", async () =>
{ {
@ -1463,10 +1541,17 @@ public class PostgresCSharpExtensionTests
foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(PostgresDb.TableName, doc);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", PostgresDb.TableName, ["j"])]); {
VerifyEmpty(StreamText(stream)); await conn.WriteJsonByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.InArray("Values", PostgresDb.TableName, ["j"])]);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByFieldsOrdered", TestList("WriteJsonByFieldsOrdered",
@ -1477,10 +1562,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "purple")], [Field.Named("Id")]); {
VerifyExpectedOrder(StreamText(stream), "five", "four"); await conn.WriteJsonByFieldsOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1488,10 +1580,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); {
VerifyExpectedOrder(StreamText(stream), "four", "five"); await conn.WriteJsonByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All,
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
VerifyExpectedOrder(StreamText(stream), "four", "five");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByContains", TestList("WriteJsonByContains",
@ -1503,12 +1602,19 @@ public class PostgresCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } });
VerifyDocById(json, "two"); var json = StreamText(stream);
VerifyDocById(json, "four"); VerifyBeginEnd(json);
VerifyDocById(json, "two");
VerifyDocById(json, "four");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when documents are not found", async () => TestCase("succeeds when documents are not found", async () =>
{ {
@ -1516,9 +1622,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Value = "mauve" }); try
VerifyEmpty(StreamText(stream)); {
await conn.WriteJsonByContains(PostgresDb.TableName, writer, new { Value = "mauve" });
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByContainsOrdered", TestList("WriteJsonByContainsOrdered",
@ -1530,10 +1643,18 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, try
[Field.Named("Sub.Bar")]); {
VerifyExpectedOrder(StreamText(stream), "two", "four"); await conn.WriteJsonByContainsOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1541,10 +1662,18 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, try
[Field.Named("Sub.Bar DESC")]); {
VerifyExpectedOrder(StreamText(stream), "four", "two"); await conn.WriteJsonByContainsOrdered(PostgresDb.TableName, writer,
new { Sub = new { Foo = "green" } },
[Field.Named("Sub.Bar DESC")]);
VerifyExpectedOrder(StreamText(stream), "four", "two");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByJsonPath", TestList("WriteJsonByJsonPath",
@ -1556,13 +1685,20 @@ public class PostgresCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)"); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)");
VerifyDocById(json, "one"); var json = StreamText(stream);
VerifyDocById(json, "two"); VerifyBeginEnd(json);
VerifyDocById(json, "three"); VerifyDocById(json, "one");
VerifyDocById(json, "two");
VerifyDocById(json, "three");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when documents are not found", async () => TestCase("succeeds when documents are not found", async () =>
{ {
@ -1570,9 +1706,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)"); try
VerifyEmpty(StreamText(stream)); {
await conn.WriteJsonByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)");
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByJsonPathOrdered", TestList("WriteJsonByJsonPathOrdered",
@ -1584,10 +1727,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", try
[Field.Named("n:NumValue")]); {
VerifyExpectedOrder(StreamText(stream), "one", "three", "two"); await conn.WriteJsonByJsonPathOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1595,10 +1745,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", try
[Field.Named("n:NumValue DESC")]); {
VerifyExpectedOrder(StreamText(stream), "two", "three", "one"); await conn.WriteJsonByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)",
[Field.Named("n:NumValue DESC")]);
VerifyExpectedOrder(StreamText(stream), "two", "three", "one");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByFields", TestList("WriteJsonFirstByFields",
@ -1609,10 +1766,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "another")]); {
VerifyDocById(StreamText(stream), "two"); await conn.WriteJsonFirstByFields(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 () => TestCase("succeeds when multiple documents are found", async () =>
{ {
@ -1620,10 +1784,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")]); {
VerifyAnyById(StreamText(stream), ["five", "four"]); await conn.WriteJsonFirstByFields(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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1631,10 +1802,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "absent")]); {
VerifyNoDoc(StreamText(stream)); await conn.WriteJsonFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "absent")]);
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByFieldsOrdered", TestList("WriteJsonFirstByFieldsOrdered",
@ -1645,10 +1823,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")], [Field.Named("Id")]); {
VerifyDocById(StreamText(stream), "five"); await conn.WriteJsonFirstByFieldsOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1656,10 +1841,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); {
VerifyDocById(StreamText(stream), "four"); await conn.WriteJsonFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]);
VerifyDocById(StreamText(stream), "four");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByContains", TestList("WriteJsonFirstByContains",
@ -1670,9 +1862,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "another" }); try
VerifyDocById(StreamText(stream), "two"); {
await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "another" });
VerifyDocById(StreamText(stream), "two");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when multiple documents are found", async () => TestCase("succeeds when multiple documents are found", async () =>
{ {
@ -1680,9 +1879,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); try
VerifyAnyById(StreamText(stream), ["two", "four"]); {
await conn.WriteJsonFirstByContains(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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1690,9 +1897,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" }); try
VerifyNoDoc(StreamText(stream)); {
await conn.WriteJsonFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" });
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByContainsOrdered", TestList("WriteJsonFirstByContainsOrdered",
@ -1703,10 +1917,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByContainsOrdered(PostgresDb.TableName, writer, try
new { Sub = new { Foo = "green" } }, [Field.Named("Value")]); {
VerifyDocById(StreamText(stream), "two"); await conn.WriteJsonFirstByContainsOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1714,10 +1935,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByContainsOrdered(PostgresDb.TableName, writer, try
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]); {
VerifyDocById(StreamText(stream), "four"); await conn.WriteJsonFirstByContainsOrdered(PostgresDb.TableName, writer,
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]);
VerifyDocById(StreamText(stream), "four");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByJsonPath", TestList("WriteJsonFirstByJsonPath",
@ -1728,9 +1956,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")"""); try
VerifyDocById(StreamText(stream), "one"); {
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")""");
VerifyDocById(StreamText(stream), "one");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when multiple documents are found", async () => TestCase("succeeds when multiple documents are found", async () =>
{ {
@ -1738,9 +1973,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")"""); try
VerifyAnyById(StreamText(stream), ["two", "four"]); {
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""");
VerifyAnyById(StreamText(stream), ["two", "four"]);
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when a document is not found", async () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1748,9 +1990,16 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")"""); try
VerifyNoDoc(StreamText(stream)); {
await conn.WriteJsonFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")""");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByJsonPathOrdered", TestList("WriteJsonFirstByJsonPathOrdered",
@ -1761,10 +2010,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer, try
"""$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar")]); {
VerifyDocById(StreamText(stream), "two"); await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer,
"""$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar")]);
VerifyDocById(StreamText(stream), "two");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when sorting descending", async () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1772,10 +2028,17 @@ public class PostgresCSharpExtensionTests
await using var conn = MkConn(db); await using var conn = MkConn(db);
await LoadDocs(conn); await LoadDocs(conn);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer, try
"""$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar DESC")]); {
VerifyDocById(StreamText(stream), "four"); await conn.WriteJsonFirstByJsonPathOrdered(PostgresDb.TableName, writer,
"""$.Sub.Foo ? (@ == "green")""", [Field.Named("Sub.Bar DESC")]);
VerifyDocById(StreamText(stream), "four");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("UpdateById", TestList("UpdateById",

View File

@ -1,3 +1,4 @@
using System.IO.Pipelines;
using Expecto.CSharp; using Expecto.CSharp;
using Expecto; using Expecto;
using BitBadger.Documents.Postgres; using BitBadger.Documents.Postgres;
@ -329,12 +330,8 @@ public static class PostgresCSharpTests
} }
/// <summary>Set up a stream writer for a test</summary> /// <summary>Set up a stream writer for a test</summary>
private static StreamWriter WriteStream(Stream stream) private static PipeWriter WriteStream(Stream stream) =>
{ PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
StreamWriter writer = new(stream);
writer.AutoFlush = true;
return writer;
}
/// <summary>Get the text of the given stream</summary> /// <summary>Get the text of the given stream</summary>
private static string StreamText(Stream stream) private static string StreamText(Stream stream)
@ -352,10 +349,8 @@ public static class PostgresCSharpTests
} }
/// <summary>Verify the presence of a document by its ID</summary> /// <summary>Verify the presence of a document by its ID</summary>
private static void VerifyDocById(string json, string docId) private static void VerifyDocById(string json, string docId) =>
{
Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present"); Expect.stringContains(json, $"{{\"Id\": \"{docId}\",", $"Document `{docId}` not present");
}
/// <summary>Verify the presence of a document by its ID</summary> /// <summary>Verify the presence of a document by its ID</summary>
private static void VerifySingleById(string json, string docId) private static void VerifySingleById(string json, string docId)
@ -382,16 +377,12 @@ public static class PostgresCSharpTests
} }
/// <summary>Verify an empty JSON array</summary> /// <summary>Verify an empty JSON array</summary>
private static void VerifyEmpty(string json) private static void VerifyEmpty(string json) =>
{
Expect.equal(json, "[]", "There should be no documents returned"); Expect.equal(json, "[]", "There should be no documents returned");
}
/// <summary>Verify an empty JSON document</summary> /// <summary>Verify an empty JSON document</summary>
private static void VerifyNoDoc(string json) private static void VerifyNoDoc(string json) =>
{
Expect.equal(json, "{}", "There should be no document returned"); Expect.equal(json, "{}", "There should be no document returned");
}
/// <summary> /// <summary>
/// Integration tests for the Configuration module of the PostgreSQL library /// Integration tests for the Configuration module of the PostgreSQL library
@ -486,14 +477,20 @@ public static class PostgresCSharpTests
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, try
Results.JsonFromData); {
await Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer,
var docs = StreamText(stream); Results.JsonFromData);
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`"); var docs = StreamText(stream);
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned"); Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
Expect.stringEnds(docs, "]", "The JSON array should have ended 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 () => TestCase("succeeds when data is not found", async () =>
{ {
@ -501,11 +498,18 @@ public static class PostgresCSharpTests
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Custom.WriteJsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath", try
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData); {
await Custom.WriteJsonArray(
Expect.equal(StreamText(stream), "[]", "There should have been no documents returned"); $"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", TestList("Single",
@ -1714,17 +1718,31 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAll(PostgresDb.TableName, writer); try
VerifyAllData(StreamText(stream)); {
await Json.WriteAll(PostgresDb.TableName, writer);
VerifyAllData(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when there is no data", async () => TestCase("succeeds when there is no data", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAll(PostgresDb.TableName, writer); try
VerifyEmpty(StreamText(stream)); {
await Json.WriteAll(PostgresDb.TableName, writer);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteAllOrdered", TestList("WriteAllOrdered",
@ -1734,27 +1752,48 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue")]); try
VerifyExpectedOrder(StreamText(stream), "one", "three", "two", "four", "five"); {
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 () => TestCase("succeeds when ordering numerically descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("n:NumValue DESC")]); try
VerifyExpectedOrder(StreamText(stream), "five", "four", "two", "three", "one"); {
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 () => TestCase("succeeds when ordering alphabetically", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]); try
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five"); {
await Json.WriteAllOrdered(PostgresDb.TableName, writer, [Field.Named("Id DESC")]);
VerifyExpectedOrder(StreamText(stream), "two", "three", "one", "four", "five");
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteById", TestList("WriteById",
@ -1765,20 +1804,34 @@ public static class PostgresCSharpTests
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteById(PostgresDb.TableName, writer, "two"); try
var json = StreamText(stream); {
Expect.stringStarts(json, """{"Id": "two",""", "An incorrect document was returned"); await Json.WriteById(PostgresDb.TableName, writer, "two");
Expect.stringEnds(json, "}", "JSON should have ended with this document"); 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 () => TestCase("succeeds when a document is not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteById(PostgresDb.TableName, writer, "three hundred eighty-seven"); try
VerifyNoDoc(StreamText(stream)); {
await Json.WriteById(PostgresDb.TableName, writer, "three hundred eighty-seven");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByFields", TestList("WriteByFields",
@ -1788,30 +1841,51 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.In("Value", ["purple", "blue"]), Field.Exists("Sub")]); {
VerifySingleById(StreamText(stream), "four"); 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() => TestCase("succeeds when documents are found using IN with numeric field", async() =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.In("NumValue", [2, 4, 6, 8])]); {
VerifySingleById(StreamText(stream), "three"); 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 () => TestCase("succeeds when documents are not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "mauve"), Field.NotEqual("NumValue", 40)]); {
VerifyEmpty(StreamText(stream)); 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 () => TestCase("succeeds for InArray when matching documents exist", async () =>
{ {
@ -1820,13 +1894,20 @@ public static class PostgresCSharpTests
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", PostgresDb.TableName, ["c"])]); {
var json = StreamText(stream); await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
VerifyBeginEnd(json); [Field.InArray("Values", PostgresDb.TableName, ["c"])]);
VerifyDocById(json, "first"); var json = StreamText(stream);
VerifyDocById(json, "second"); VerifyBeginEnd(json);
VerifyDocById(json, "first");
VerifyDocById(json, "second");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds for InArray when no matching documents exist", async () => TestCase("succeeds for InArray when no matching documents exist", async () =>
{ {
@ -1834,10 +1915,17 @@ public static class PostgresCSharpTests
await Definition.EnsureTable(PostgresDb.TableName); await Definition.EnsureTable(PostgresDb.TableName);
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(PostgresDb.TableName, doc);
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", PostgresDb.TableName, ["j"])]); {
VerifyEmpty(StreamText(stream)); await Json.WriteByFields(PostgresDb.TableName, writer, FieldMatch.All,
[Field.InArray("Values", PostgresDb.TableName, ["j"])]);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByFieldsOrdered", TestList("WriteByFieldsOrdered",
@ -1847,20 +1935,34 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "purple")], [Field.Named("Id")]); {
VerifyExpectedOrder(StreamText(stream), "five", "four"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.All, try
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); {
VerifyExpectedOrder(StreamText(stream), "four", "five"); 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", TestList("WriteByContains",
@ -1871,21 +1973,35 @@ public static class PostgresCSharpTests
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await Json.WriteByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } });
VerifyDocById(json, "two"); var json = StreamText(stream);
VerifyDocById(json, "four"); VerifyBeginEnd(json);
VerifyDocById(json, "two");
VerifyDocById(json, "four");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when documents are not found", async () => TestCase("succeeds when documents are not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByContains(PostgresDb.TableName, writer, new { Value = "mauve" }); try
VerifyEmpty(StreamText(stream)); {
await Json.WriteByContains(PostgresDb.TableName, writer, new { Value = "mauve" });
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByContainsOrdered", TestList("WriteByContainsOrdered",
@ -1896,20 +2012,34 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, try
[Field.Named("Sub.Bar")]); {
VerifyExpectedOrder(StreamText(stream), "two", "four"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByContainsOrdered(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }, try
[Field.Named("Sub.Bar DESC")]); {
VerifyExpectedOrder(StreamText(stream), "four", "two"); 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", TestList("WriteByJsonPath",
@ -1920,22 +2050,36 @@ public static class PostgresCSharpTests
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)"); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)");
VerifyDocById(json, "one"); var json = StreamText(stream);
VerifyDocById(json, "two"); VerifyBeginEnd(json);
VerifyDocById(json, "three"); VerifyDocById(json, "one");
VerifyDocById(json, "two");
VerifyDocById(json, "three");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when documents are not found", async () => TestCase("succeeds when documents are not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)"); try
VerifyEmpty(StreamText(stream)); {
await Json.WriteByJsonPath(PostgresDb.TableName, writer, "$.NumValue ? (@ < 0)");
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByJsonPathOrdered", TestList("WriteByJsonPathOrdered",
@ -1946,20 +2090,34 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", try
[Field.Named("n:NumValue")]); {
VerifyExpectedOrder(StreamText(stream), "one", "three", "two"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByJsonPathOrdered(PostgresDb.TableName, writer, "$.NumValue ? (@ < 15)", try
[Field.Named("n:NumValue DESC")]); {
VerifyExpectedOrder(StreamText(stream), "two", "three", "one"); 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", TestList("WriteFirstByFields",
@ -1969,30 +2127,51 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "another")]); {
VerifyDocById(StreamText(stream), "two"); 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 () => TestCase("succeeds when multiple documents are found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")]); {
VerifyAnyById(StreamText(stream), ["five", "four"]); 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 () => TestCase("succeeds when a document is not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "absent")]); {
VerifyNoDoc(StreamText(stream)); await Json.WriteFirstByFields(PostgresDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "absent")]);
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteFirstByFieldsOrdered", TestList("WriteFirstByFieldsOrdered",
@ -2002,20 +2181,34 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")], [Field.Named("Id")]); {
VerifyDocById(StreamText(stream), "five"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(PostgresDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "purple")], [Field.Named("Id DESC")]); {
VerifyDocById(StreamText(stream), "four"); 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", TestList("WriteFirstByContains",
@ -2025,27 +2218,48 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "another" }); try
VerifyDocById(StreamText(stream), "two"); {
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 () => TestCase("succeeds when multiple documents are found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Sub = new { Foo = "green" } }); try
VerifyAnyById(StreamText(stream), ["two", "four"]); {
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 () => TestCase("succeeds when a document is not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" }); try
VerifyNoDoc(StreamText(stream)); {
await Json.WriteFirstByContains(PostgresDb.TableName, writer, new { Value = "absent" });
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteFirstByContainsOrdered", TestList("WriteFirstByContainsOrdered",
@ -2055,20 +2269,34 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByContainsOrdered(PostgresDb.TableName, writer, try
new { Sub = new { Foo = "green" } }, [Field.Named("Value")]); {
VerifyDocById(StreamText(stream), "two"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByContainsOrdered(PostgresDb.TableName, writer, try
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]); {
VerifyDocById(StreamText(stream), "four"); 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", TestList("WriteFirstByJsonPath",
@ -2078,27 +2306,48 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")"""); try
VerifyDocById(StreamText(stream), "one"); {
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Value ? (@ == "FIRST!")""");
VerifyDocById(StreamText(stream), "one");
}
finally
{
await writer.CompleteAsync();
}
}), }),
TestCase("succeeds when multiple documents are found", async () => TestCase("succeeds when multiple documents are found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")"""); try
VerifyAnyById(StreamText(stream), ["two", "four"]); {
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 () => TestCase("succeeds when a document is not found", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")"""); try
VerifyNoDoc(StreamText(stream)); {
await Json.WriteFirstByJsonPath(PostgresDb.TableName, writer, """$.Id ? (@ == "nope")""");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteFirstByJsonPathOrdered", TestList("WriteFirstByJsonPathOrdered",
@ -2108,20 +2357,36 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""", try
[Field.Named("Sub.Bar")]); {
VerifyDocById(StreamText(stream), "two"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
await using var db = PostgresDb.BuildDb(); await using var db = PostgresDb.BuildDb();
await LoadDocs(); await LoadDocs();
await using MemoryStream stream = new(); await using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer, """$.Sub.Foo ? (@ == "green")""", try
[Field.Named("Sub.Bar DESC")]); {
VerifyDocById(StreamText(stream), "four"); await Json.WriteFirstByJsonPathOrdered(PostgresDb.TableName, writer,
"""$.Sub.Foo ? (@ == "green")""",
[Field.Named("Sub.Bar DESC")]);
VerifyDocById(StreamText(stream), "four");
}
finally
{
await writer.CompleteAsync();
}
}) })
]) ])
]); ]);

View File

@ -1,3 +1,4 @@
using System.IO.Pipelines;
using Expecto.CSharp; using Expecto.CSharp;
using Expecto; using Expecto;
using BitBadger.Documents.Sqlite; using BitBadger.Documents.Sqlite;
@ -25,24 +26,16 @@ public static class SqliteCSharpExtensionTests
} }
/// <summary>Verify an empty JSON array</summary> /// <summary>Verify an empty JSON array</summary>
private static void VerifyEmpty(string json) private static void VerifyEmpty(string json) =>
{
Expect.equal(json, "[]", "There should be no documents returned"); Expect.equal(json, "[]", "There should be no documents returned");
}
/// <summary>Verify an empty JSON document</summary> /// <summary>Verify an empty JSON document</summary>
private static void VerifyNoDoc(string json) private static void VerifyNoDoc(string json) =>
{
Expect.equal(json, "{}", "There should be no document returned"); Expect.equal(json, "{}", "There should be no document returned");
}
/// <summary>Set up a stream writer for a test</summary> /// <summary>Set up a stream writer for a test</summary>
private static StreamWriter WriteStream(Stream stream) private static PipeWriter WriteStream(Stream stream) =>
{ PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
StreamWriter writer = new(stream);
writer.AutoFlush = true;
return writer;
}
/// <summary>Get the text of the given stream</summary> /// <summary>Get the text of the given stream</summary>
private static string StreamText(Stream stream) private static string StreamText(Stream stream)
@ -127,16 +120,22 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteCustomJsonArray(Query.Find(SqliteDb.TableName), [], writer, Results.JsonFromData); try
{
var json = StreamText(stream); await conn.WriteCustomJsonArray(Query.Find(SqliteDb.TableName), [], writer, Results.JsonFromData);
VerifyBeginEnd(json); var json = StreamText(stream);
Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found"); VerifyBeginEnd(json);
Expect.stringContains(json, JsonDocument.Two,"Document ID `two` should have been found"); Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found");
Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found"); Expect.stringContains(json, JsonDocument.Two, "Document ID `two` should have been found");
Expect.stringContains(json, JsonDocument.Four, "Document ID `four` should have been found"); Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found");
Expect.stringContains(json, JsonDocument.Five, "Document ID `five` 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 () => TestCase("succeeds when data is not found", async () =>
{ {
@ -145,12 +144,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteCustomJsonArray( try
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", {
[new SqliteParameter("@value", 100)], writer, Results.JsonFromData); await conn.WriteCustomJsonArray(
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
VerifyEmpty(StreamText(stream)); [new SqliteParameter("@value", 100)], writer, Results.JsonFromData);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("CustomSingle", TestList("CustomSingle",
@ -828,13 +833,21 @@ public static class SqliteCSharpExtensionTests
await conn.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" }); await conn.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAll(SqliteDb.TableName, writer); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await conn.WriteJsonAll(SqliteDb.TableName, writer);
Expect.stringContains(json, """{"Foo":"one","Bar":"two"}""", "The first document was not found"); var json = StreamText(stream);
Expect.stringContains(json, """{"Foo":"three","Bar":"four"}""", "The second document was not found"); VerifyBeginEnd(json);
Expect.stringContains(json, """{"Foo":"five","Bar":"six"}""", "The third document was not found"); 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 () => TestCase("succeeds when there is no data", async () =>
{ {
@ -842,9 +855,16 @@ public static class SqliteCSharpExtensionTests
await using var conn = Sqlite.Configuration.DbConn(); await using var conn = Sqlite.Configuration.DbConn();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAll(SqliteDb.TableName, writer); try
VerifyEmpty(StreamText(stream)); {
await conn.WriteJsonAll(SqliteDb.TableName, writer);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonAllOrdered", TestList("WriteJsonAllOrdered",
@ -856,11 +876,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.One},{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.Four},{JsonDocument.Five}]", await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue")]);
"The documents were not ordered correctly"); 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 () => TestCase("succeeds when ordering numerically descending", async () =>
{ {
@ -869,11 +896,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue DESC")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.Five},{JsonDocument.Four},{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One}]", await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue DESC")]);
"The documents were not ordered correctly"); 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 () => TestCase("succeeds when ordering alphabetically", async () =>
{ {
@ -882,11 +916,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("Id DESC")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Four},{JsonDocument.Five}]", await conn.WriteJsonAllOrdered(SqliteDb.TableName, writer, [Field.Named("Id DESC")]);
"The documents were not ordered correctly"); 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("WriteJsonById", TestList("WriteJsonById",
@ -898,9 +939,16 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonById(SqliteDb.TableName, writer, "two"); try
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned"); {
await conn.WriteJsonById(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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -909,9 +957,16 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonById(SqliteDb.TableName, writer, "three hundred eighty-seven"); try
VerifyNoDoc(StreamText(stream)); {
await conn.WriteJsonById(SqliteDb.TableName, writer, "three hundred eighty-seven");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByFields", TestList("WriteJsonByFields",
@ -923,13 +978,20 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 15)]); {
var json = StreamText(stream); await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.Any,
VerifyBeginEnd(json); [Field.Greater("NumValue", 15)]);
Expect.stringContains(json, JsonDocument.Four, "Document `four` should have been returned"); var json = StreamText(stream);
Expect.stringContains(json, JsonDocument.Five, "Document `five` should have been returned"); 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 () => TestCase("succeeds when documents are found using IN with numeric field", async () =>
{ {
@ -938,11 +1000,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.In("NumValue", [2, 4, 6, 8])]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three}]", await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All,
"There should have been one document returned"); [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 () => TestCase("succeeds when documents are not found", async () =>
{ {
@ -951,10 +1020,17 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 100)]); {
VerifyEmpty(StreamText(stream)); await conn.WriteJsonByFields(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 () => TestCase("succeeds for InArray when matching documents exist", async () =>
{ {
@ -964,15 +1040,22 @@ public static class SqliteCSharpExtensionTests
foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(SqliteDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(SqliteDb.TableName, doc);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", SqliteDb.TableName, ["c"])]); {
var json = StreamText(stream); await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All,
VerifyBeginEnd(json); [Field.InArray("Values", SqliteDb.TableName, ["c"])]);
Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""", var json = StreamText(stream);
"Document `first` should have been returned"); VerifyBeginEnd(json);
Expect.stringContains(json, """{"Id":"second","Values":["c","d","e"]}""", Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""",
"Document `second` should have been returned"); "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 () => TestCase("succeeds for InArray when no matching documents exist", async () =>
{ {
@ -982,10 +1065,17 @@ public static class SqliteCSharpExtensionTests
foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(SqliteDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await conn.Insert(SqliteDb.TableName, doc);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", SqliteDb.TableName, ["j"])]); {
VerifyEmpty(StreamText(stream)); await conn.WriteJsonByFields(SqliteDb.TableName, writer, FieldMatch.All,
[Field.InArray("Values", SqliteDb.TableName, ["j"])]);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonByFieldsOrdered", TestList("WriteJsonByFieldsOrdered",
@ -997,11 +1087,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 15)], [Field.Named("Id")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Five},{JsonDocument.Four}]", await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
"Incorrect documents were returned"); [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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1010,11 +1107,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 15)], [Field.Named("Id DESC")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Four},{JsonDocument.Five}]", await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
"Incorrect documents were returned"); [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 () => TestCase("succeeds when sorting case-sensitively", async () =>
{ {
@ -1023,11 +1127,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.LessOrEqual("NumValue", 10)], [Field.Named("Value")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Two}]", await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
"Documents not ordered correctly"); [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 () => TestCase("succeeds when sorting case-insensitively", async () =>
{ {
@ -1036,11 +1147,18 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.LessOrEqual("NumValue", 10)], [Field.Named("i:Value")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.One}]", await conn.WriteJsonByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
"Documents not ordered correctly"); [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("WriteJsonFirstByFields", TestList("WriteJsonFirstByFields",
@ -1052,10 +1170,17 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "another")]); {
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned"); await conn.WriteJsonFirstByFields(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 () => TestCase("succeeds when multiple documents are found", async () =>
{ {
@ -1064,12 +1189,19 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")]); {
var json = StreamText(stream); await conn.WriteJsonFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
Expect.notEqual(json, "{}", "There should have been a document returned"); [Field.Equal("Sub.Foo", "green")]);
VerifyAny(json, [JsonDocument.Two, JsonDocument.Four]); 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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1078,10 +1210,17 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "absent")]); {
VerifyNoDoc(StreamText(stream)); await conn.WriteJsonFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "absent")]);
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteJsonFirstByFieldsOrdered", TestList("WriteJsonFirstByFieldsOrdered",
@ -1093,10 +1232,17 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar")]); {
Expect.equal(StreamText(stream), JsonDocument.Two, "An incorrect document was returned"); await conn.WriteJsonFirstByFieldsOrdered(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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1105,10 +1251,17 @@ public static class SqliteCSharpExtensionTests
await LoadDocs(conn); await LoadDocs(conn);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await conn.WriteJsonFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]); {
Expect.equal(StreamText(stream), JsonDocument.Four, "An incorrect document was returned"); await conn.WriteJsonFirstByFieldsOrdered(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();
}
}) })
]), ]),
TestList("UpdateById", TestList("UpdateById",

View File

@ -1,4 +1,5 @@
using Expecto.CSharp; using System.IO.Pipelines;
using Expecto.CSharp;
using Expecto; using Expecto;
using Microsoft.FSharp.Core; using Microsoft.FSharp.Core;
using BitBadger.Documents.Sqlite; using BitBadger.Documents.Sqlite;
@ -178,24 +179,16 @@ public static class SqliteCSharpTests
} }
/// <summary>Verify an empty JSON array</summary> /// <summary>Verify an empty JSON array</summary>
private static void VerifyEmpty(string json) private static void VerifyEmpty(string json) =>
{
Expect.equal(json, "[]", "There should be no documents returned"); Expect.equal(json, "[]", "There should be no documents returned");
}
/// <summary>Verify an empty JSON document</summary> /// <summary>Verify an empty JSON document</summary>
private static void VerifyNoDoc(string json) private static void VerifyNoDoc(string json) =>
{
Expect.equal(json, "{}", "There should be no document returned"); Expect.equal(json, "{}", "There should be no document returned");
}
/// <summary>Set up a stream writer for a test</summary> /// <summary>Set up a stream writer for a test</summary>
private static StreamWriter WriteStream(Stream stream) private static PipeWriter WriteStream(Stream stream) =>
{ PipeWriter.Create(stream, new StreamPipeWriterOptions(leaveOpen: true));
StreamWriter writer = new(stream);
writer.AutoFlush = true;
return writer;
}
/// <summary>Get the text of the given stream</summary> /// <summary>Get the text of the given stream</summary>
private static string StreamText(Stream stream) private static string StreamText(Stream stream)
@ -274,16 +267,22 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Custom.WriteJsonArray(Query.Find(SqliteDb.TableName), [], writer, Results.JsonFromData); try
{
var json = StreamText(stream); await Custom.WriteJsonArray(Query.Find(SqliteDb.TableName), [], writer, Results.JsonFromData);
VerifyBeginEnd(json); var json = StreamText(stream);
Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found"); VerifyBeginEnd(json);
Expect.stringContains(json, JsonDocument.Two,"Document ID `two` should have been found"); Expect.stringContains(json, JsonDocument.One, "Document ID `one` should have been found");
Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found"); Expect.stringContains(json, JsonDocument.Two, "Document ID `two` should have been found");
Expect.stringContains(json, JsonDocument.Four, "Document ID `four` should have been found"); Expect.stringContains(json, JsonDocument.Three, "Document ID `three` should have been found");
Expect.stringContains(json, JsonDocument.Five, "Document ID `five` 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 () => TestCase("succeeds when data is not found", async () =>
{ {
@ -291,12 +290,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Custom.WriteJsonArray( try
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", {
[new SqliteParameter("@value", 100)], writer, Results.JsonFromData); await Custom.WriteJsonArray(
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
VerifyEmpty(StreamText(stream)); [new SqliteParameter("@value", 100)], writer, Results.JsonFromData);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("Single", TestList("Single",
@ -1096,22 +1101,37 @@ public static class SqliteCSharpTests
await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" }); await Document.Insert(SqliteDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAll(SqliteDb.TableName, writer); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await Json.WriteAll(SqliteDb.TableName, writer);
Expect.stringContains(json, """{"Foo":"one","Bar":"two"}""", "The first document was not found"); var json = StreamText(stream);
Expect.stringContains(json, """{"Foo":"three","Bar":"four"}""", "The second document was not found"); VerifyBeginEnd(json);
Expect.stringContains(json, """{"Foo":"five","Bar":"six"}""", "The third document was not found"); 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 () => TestCase("succeeds when there is no data", async () =>
{ {
await using var db = await SqliteDb.BuildDb(); await using var db = await SqliteDb.BuildDb();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAll(SqliteDb.TableName, writer); try
VerifyEmpty(StreamText(stream)); {
await Json.WriteAll(SqliteDb.TableName, writer);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteAllOrdered", TestList("WriteAllOrdered",
@ -1122,11 +1142,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.One},{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.Four},{JsonDocument.Five}]", await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue")]);
"The documents were not ordered correctly"); 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 () => TestCase("succeeds when ordering numerically descending", async () =>
{ {
@ -1134,11 +1161,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue DESC")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.Five},{JsonDocument.Four},{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One}]", await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("n:NumValue DESC")]);
"The documents were not ordered correctly"); 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 () => TestCase("succeeds when ordering alphabetically", async () =>
{ {
@ -1146,11 +1180,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("Id DESC")]); try
Expect.equal(StreamText(stream), {
$"[{JsonDocument.Two},{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Four},{JsonDocument.Five}]", await Json.WriteAllOrdered(SqliteDb.TableName, writer, [Field.Named("Id DESC")]);
"The documents were not ordered correctly"); 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", TestList("WriteById",
@ -1161,9 +1202,16 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteById(SqliteDb.TableName, writer, "two"); try
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned"); {
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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1171,9 +1219,16 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteById(SqliteDb.TableName, writer, "three hundred eighty-seven"); try
VerifyNoDoc(StreamText(stream)); {
await Json.WriteById(SqliteDb.TableName, writer, "three hundred eighty-seven");
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByFields", TestList("WriteByFields",
@ -1184,12 +1239,20 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.Any, [Field.Greater("NumValue", 15)]); try
var json = StreamText(stream); {
VerifyBeginEnd(json); await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.Any,
Expect.stringContains(json, JsonDocument.Four, "Document `four` should have been returned"); [Field.Greater("NumValue", 15)]);
Expect.stringContains(json, JsonDocument.Five, "Document `five` should have been returned"); 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 () => TestCase("succeeds when documents are found using IN with numeric field", async () =>
{ {
@ -1197,11 +1260,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.In("NumValue", [2, 4, 6, 8])]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three}]", await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
"There should have been one document returned"); [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 () => TestCase("succeeds when documents are not found", async () =>
{ {
@ -1209,9 +1279,17 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.Any, [Field.Greater("NumValue", 100)]); try
VerifyEmpty(StreamText(stream)); {
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 () => TestCase("succeeds for InArray when matching documents exist", async () =>
{ {
@ -1220,15 +1298,22 @@ public static class SqliteCSharpTests
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", SqliteDb.TableName, ["c"])]); {
var json = StreamText(stream); await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
VerifyBeginEnd(json); [Field.InArray("Values", SqliteDb.TableName, ["c"])]);
Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""", var json = StreamText(stream);
"Document `first` should have been returned"); VerifyBeginEnd(json);
Expect.stringContains(json, """{"Id":"second","Values":["c","d","e"]}""", Expect.stringContains(json, """{"Id":"first","Values":["a","b","c"]}""",
"Document `second` should have been returned"); "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 () => TestCase("succeeds for InArray when no matching documents exist", async () =>
{ {
@ -1237,10 +1322,17 @@ public static class SqliteCSharpTests
foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc); foreach (var doc in ArrayDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.InArray("Values", SqliteDb.TableName, ["j"])]); {
VerifyEmpty(StreamText(stream)); await Json.WriteByFields(SqliteDb.TableName, writer, FieldMatch.All,
[Field.InArray("Values", SqliteDb.TableName, ["j"])]);
VerifyEmpty(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteByFieldsOrdered", TestList("WriteByFieldsOrdered",
@ -1251,11 +1343,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 15)], [Field.Named("Id")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Five},{JsonDocument.Four}]", await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
"Incorrect documents were returned"); [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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1263,11 +1362,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Greater("NumValue", 15)], [Field.Named("Id DESC")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Four},{JsonDocument.Five}]", await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any,
"Incorrect documents were returned"); [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 () => TestCase("succeeds when sorting case-sensitively", async () =>
{ {
@ -1275,11 +1381,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.LessOrEqual("NumValue", 10)], [Field.Named("Value")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.One},{JsonDocument.Two}]", await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
"Documents not ordered correctly"); [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 () => TestCase("succeeds when sorting case-insensitively", async () =>
{ {
@ -1287,11 +1400,18 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All, try
[Field.LessOrEqual("NumValue", 10)], [Field.Named("i:Value")]); {
Expect.equal(StreamText(stream), $"[{JsonDocument.Three},{JsonDocument.Two},{JsonDocument.One}]", await Json.WriteByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.All,
"Documents not ordered correctly"); [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", TestList("WriteFirstByFields",
@ -1302,10 +1422,17 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "another")]); {
Expect.equal(StreamText(stream), JsonDocument.Two, "The incorrect document was returned"); 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 () => TestCase("succeeds when multiple documents are found", async () =>
{ {
@ -1313,12 +1440,19 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")]); {
var json = StreamText(stream); await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
Expect.notEqual(json, "{}", "There should have been a document returned"); [Field.Equal("Sub.Foo", "green")]);
VerifyAny(json, [JsonDocument.Two, JsonDocument.Four]); 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 () => TestCase("succeeds when a document is not found", async () =>
{ {
@ -1326,10 +1460,17 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Value", "absent")]); {
VerifyNoDoc(StreamText(stream)); await Json.WriteFirstByFields(SqliteDb.TableName, writer, FieldMatch.Any,
[Field.Equal("Value", "absent")]);
VerifyNoDoc(StreamText(stream));
}
finally
{
await writer.CompleteAsync();
}
}) })
]), ]),
TestList("WriteFirstByFieldsOrdered", TestList("WriteFirstByFieldsOrdered",
@ -1340,10 +1481,17 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar")]); {
Expect.equal(StreamText(stream), JsonDocument.Two, "An incorrect document was returned"); 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 () => TestCase("succeeds when sorting descending", async () =>
{ {
@ -1351,10 +1499,17 @@ public static class SqliteCSharpTests
await LoadDocs(); await LoadDocs();
using MemoryStream stream = new(); using MemoryStream stream = new();
await using var writer = WriteStream(stream); var writer = WriteStream(stream);
await Json.WriteFirstByFieldsOrdered(SqliteDb.TableName, writer, FieldMatch.Any, try
[Field.Equal("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]); {
Expect.equal(StreamText(stream), JsonDocument.Four, "An incorrect document was returned"); 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();
}
}) })
]) ])
]); ]);

View File

@ -1,5 +1,7 @@
module CommonTests module CommonTests
open System.IO
open System.IO.Pipelines
open BitBadger.Documents open BitBadger.Documents
open Expecto open Expecto
@ -484,6 +486,81 @@ let queryTests = testList "Query" [
] ]
] ]
let private streamText (stream: Stream) =
stream.Position <- 0L
use reader = new StreamReader(stream)
reader.ReadToEnd()
/// Unit tests for the PipeWriter module
let pipeWriterTests = testList "Extensions.PipeWriter" [
testList "writeString" [
testTask "succeeds when writer is open" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
try
let! result = PipeWriter.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
writer.Complete()
}
testTask "succeeds when writer is completed" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
do! writer.CompleteAsync()
let! result = PipeWriter.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" [
testTask "succeeds with no strings" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
try
do! PipeWriter.writeStrings writer []
Expect.equal (streamText stream) "[]" "An empty sequence of strings was not written correctly"
finally
writer.Complete()
}
testTask "succeeds with one strings" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
try
do! PipeWriter.writeStrings writer [ "le-test" ]
Expect.equal (streamText stream) "[le-test]" "A sequence of one string was not written correctly"
finally
writer.Complete()
}
testTask "succeeds with many strings" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
try
do! PipeWriter.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
writer.Complete()
}
testTask "succeeds when the writer is completed early" {
use stream = new MemoryStream()
let writer = PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
let items = seq {
"a"
"b"
"c"
writer.Complete()
"d"
"e"
"f"
}
do! PipeWriter.writeStrings writer items
Expect.equal (streamText stream) "[a,b,c" "The writing should have stopped when the writer completed"
}
]
]
/// Tests which do not hit the database /// Tests which do not hit the database
let all = testList "Common" [ let all = testList "Common" [
comparisonTests comparisonTests
@ -492,5 +569,6 @@ let all = testList "Common" [
parameterNameTests parameterNameTests
autoIdTests autoIdTests
queryTests queryTests
pipeWriterTests
testSequenced configurationTests testSequenced configurationTests
] ]

View File

@ -1,6 +1,7 @@
module PostgresExtensionTests module PostgresExtensionTests
open System.IO open System.IO
open System.IO.Pipelines
open BitBadger.Documents open BitBadger.Documents
open BitBadger.Documents.Postgres open BitBadger.Documents.Postgres
open BitBadger.Documents.Tests open BitBadger.Documents.Tests
@ -16,9 +17,7 @@ let private mkConn (db: ThrowawayPostgresDb) =
/// Set up a stream writer for a test /// Set up a stream writer for a test
let private writeStream (stream: Stream) = let private writeStream (stream: Stream) =
let writer = new StreamWriter(stream) PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
writer.AutoFlush <- true
writer
/// Get the text of the given stream /// Get the text of the given stream
let private streamText (stream: Stream) = let private streamText (stream: Stream) =
@ -142,13 +141,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeCustomJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData try
do! conn.writeCustomJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
let docs = streamText stream let docs = streamText stream
Expect.stringStarts docs "[" "The JSON array should have started with `[`" Expect.stringStarts docs "[" "The JSON array should have started with `[`"
Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned" Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned"
Expect.stringEnds docs "]" "The JSON array should have ended with `[`" Expect.stringEnds docs "]" "The JSON array should have ended with `[`"
finally
writer.Complete()
} }
testTask "succeeds when data is not found" { testTask "succeeds when data is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -156,14 +157,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeCustomJsonArray try
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath" do! conn.writeCustomJsonArray
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ] $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
writer [ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
jsonFromData writer
jsonFromData
Expect.equal (streamText stream) "[]" "There should have been no documents returned" Expect.equal (streamText stream) "[]" "There should have been no documents returned"
finally
writer.Complete()
} }
] ]
testList "customSingle" [ testList "customSingle" [
@ -1170,17 +1173,23 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAll PostgresDb.TableName writer try
verifyAllData (streamText stream) do! conn.writeJsonAll PostgresDb.TableName writer
verifyAllData (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds when there is no data" { testTask "succeeds when there is no data" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
use conn = mkConn db use conn = mkConn db
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAll PostgresDb.TableName writer try
verifyEmpty (streamText stream) do! conn.writeJsonAll PostgresDb.TableName writer
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonAllOrdered" [ testList "writeJsonAllOrdered" [
@ -1190,9 +1199,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue" ] try
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") (Some "four") (Some "five") do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue" ]
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") (Some "four") (Some "five")
finally
writer.Complete()
} }
testTask "succeeds when ordering numerically descending" { testTask "succeeds when ordering numerically descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1200,9 +1212,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue DESC" ] try
verifyExpectedOrder (streamText stream) "five" "four" (Some "two") (Some "three") (Some "one") do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder (streamText stream) "five" "four" (Some "two") (Some "three") (Some "one")
finally
writer.Complete()
} }
testTask "succeeds when ordering alphabetically" { testTask "succeeds when ordering alphabetically" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1210,9 +1225,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "Id DESC" ] try
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") (Some "four") (Some "five") do! conn.writeJsonAllOrdered PostgresDb.TableName writer [ Field.Named "Id DESC" ]
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") (Some "four") (Some "five")
finally
writer.Complete()
} }
] ]
testList "writeJsonById" [ testList "writeJsonById" [
@ -1222,11 +1240,14 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonById PostgresDb.TableName writer "two" try
let json = streamText stream do! conn.writeJsonById PostgresDb.TableName writer "two"
Expect.stringStarts json """{"Id": "two",""" "An incorrect document was returned" let json = streamText stream
Expect.stringEnds json "}" "JSON should have ended with this document" Expect.stringStarts json """{"Id": "two",""" "An incorrect document was returned"
Expect.stringEnds json "}" "JSON should have ended with this document"
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1234,9 +1255,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonById PostgresDb.TableName writer "three hundred eighty-seven" try
verifyNoDoc (streamText stream) do! conn.writeJsonById PostgresDb.TableName writer "three hundred eighty-seven"
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByFields" [ testList "writeJsonByFields" [
@ -1246,10 +1270,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
PostgresDb.TableName writer All [ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ] do! conn.writeJsonByFields
verifySingleById (streamText stream) "four" PostgresDb.TableName
writer
All
[ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ]
verifySingleById (streamText stream) "four"
finally
writer.Complete()
} }
testTask "succeeds when documents are found using IN with numeric field" { testTask "succeeds when documents are found using IN with numeric field" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1257,9 +1287,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields PostgresDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ] try
verifySingleById (streamText stream) "three" do! conn.writeJsonByFields PostgresDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
verifySingleById (streamText stream) "three"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1267,10 +1300,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
PostgresDb.TableName writer All [ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ] do! conn.writeJsonByFields
verifyEmpty (streamText stream) PostgresDb.TableName
writer
All
[ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds for InArray when matching documents exist" { testTask "succeeds for InArray when matching documents exist" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1279,13 +1318,16 @@ let integrationTests =
for doc in ArrayDocument.TestDocuments do do! conn.insert PostgresDb.TableName doc for doc in ArrayDocument.TestDocuments do do! conn.insert PostgresDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ] do! conn.writeJsonByFields
let json = streamText stream PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ]
verifyBeginEnd json let json = streamText stream
verifyDocById json "first" verifyBeginEnd json
verifyDocById json "second" verifyDocById json "first"
verifyDocById json "second"
finally
writer.Complete()
} }
testTask "succeeds for InArray when no matching documents exist" { testTask "succeeds for InArray when no matching documents exist" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1294,10 +1336,13 @@ let integrationTests =
for doc in ArrayDocument.TestDocuments do do! conn.insert PostgresDb.TableName doc for doc in ArrayDocument.TestDocuments do do! conn.insert PostgresDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ] do! conn.writeJsonByFields
verifyEmpty (streamText stream) PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByFieldsOrdered" [ testList "writeJsonByFieldsOrdered" [
@ -1307,10 +1352,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ] do! conn.writeJsonByFieldsOrdered
verifyExpectedOrder (streamText stream) "five" "four" None None None PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyExpectedOrder (streamText stream) "five" "four" None None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1318,10 +1366,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ] do! conn.writeJsonByFieldsOrdered
verifyExpectedOrder (streamText stream) "four" "five" None None None PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyExpectedOrder (streamText stream) "four" "five" None None None
finally
writer.Complete()
} }
] ]
testList "writeJsonByContains" [ testList "writeJsonByContains" [
@ -1331,12 +1382,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} try
let json = streamText stream do! conn.writeJsonByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |}
verifyBeginEnd json let json = streamText stream
verifyDocById json "two" verifyBeginEnd json
verifyDocById json "four" verifyDocById json "two"
verifyDocById json "four"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1344,9 +1398,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByContains PostgresDb.TableName writer {| Value = "mauve" |} try
verifyEmpty (streamText stream) do! conn.writeJsonByContains PostgresDb.TableName writer {| Value = "mauve" |}
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByContainsOrdered" [ testList "writeJsonByContainsOrdered" [
@ -1357,10 +1414,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ] do! conn.writeJsonByContainsOrdered
verifyExpectedOrder (streamText stream) "two" "four" None None None PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
verifyExpectedOrder (streamText stream) "two" "four" None None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1368,10 +1428,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ] do! conn.writeJsonByContainsOrdered
verifyExpectedOrder (streamText stream) "four" "two" None None None PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
verifyExpectedOrder (streamText stream) "four" "two" None None None
finally
writer.Complete()
} }
] ]
testList "writeJsonByJsonPath" [ testList "writeJsonByJsonPath" [
@ -1381,13 +1444,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 15)" try
let json = streamText stream do! conn.writeJsonByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 15)"
verifyBeginEnd json let json = streamText stream
verifyDocById json "one" verifyBeginEnd json
verifyDocById json "two" verifyDocById json "one"
verifyDocById json "three" verifyDocById json "two"
verifyDocById json "three"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1395,9 +1461,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 0)" try
verifyEmpty (streamText stream) do! conn.writeJsonByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 0)"
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByJsonPathOrdered" [ testList "writeJsonByJsonPathOrdered" [
@ -1408,10 +1477,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByJsonPathOrdered try
PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ] do! conn.writeJsonByJsonPathOrdered
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") None None PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1419,10 +1491,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByJsonPathOrdered try
PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ] do! conn.writeJsonByJsonPathOrdered
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") None None PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") None None
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByFields" [ testList "writeJsonFirstByFields" [
@ -1432,9 +1507,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "another" ] try
verifyDocById (streamText stream) "two" do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "another" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1442,9 +1520,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] try
verifyAnyById (streamText stream) [ "five"; "four" ] do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ]
verifyAnyById (streamText stream) [ "five"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1452,9 +1533,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "absent" ] try
verifyNoDoc (streamText stream) do! conn.writeJsonFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "absent" ]
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByFieldsOrdered" [ testList "writeJsonFirstByFieldsOrdered" [
@ -1464,10 +1548,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFieldsOrdered try
PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ] do! conn.writeJsonFirstByFieldsOrdered
verifyDocById (streamText stream) "five" PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyDocById (streamText stream) "five"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1475,10 +1562,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFieldsOrdered try
PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ] do! conn.writeJsonFirstByFieldsOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByContains" [ testList "writeJsonFirstByContains" [
@ -1488,9 +1578,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Value = "another" |} try
verifyDocById (streamText stream) "two" do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Value = "another" |}
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1498,9 +1591,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} try
verifyAnyById (streamText stream) [ "two"; "four" ] do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |}
verifyAnyById (streamText stream) [ "two"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1508,9 +1604,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Value = "absent" |} try
verifyNoDoc (streamText stream) do! conn.writeJsonFirstByContains PostgresDb.TableName writer {| Value = "absent" |}
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByContainsOrdered" [ testList "writeJsonFirstByContainsOrdered" [
@ -1520,10 +1619,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ] do! conn.writeJsonFirstByContainsOrdered
verifyDocById (streamText stream) "two" PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1531,10 +1633,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ] do! conn.writeJsonFirstByContainsOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByJsonPath" [ testList "writeJsonFirstByJsonPath" [
@ -1544,9 +1649,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Value ? (@ == "FIRST!")""" try
verifyDocById (streamText stream) "one" do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Value ? (@ == "FIRST!")"""
verifyDocById (streamText stream) "one"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1554,9 +1662,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" try
verifyAnyById (streamText stream) [ "two"; "four" ] do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")"""
verifyAnyById (streamText stream) [ "two"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1564,9 +1675,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Id ? (@ == "nope")""" try
verifyNoDoc (streamText stream) do! conn.writeJsonFirstByJsonPath PostgresDb.TableName writer """$.Id ? (@ == "nope")"""
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByJsonPathOrdered" [ testList "writeJsonFirstByJsonPathOrdered" [
@ -1576,10 +1690,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByJsonPathOrdered try
PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ] do! conn.writeJsonFirstByJsonPathOrdered
verifyDocById (streamText stream) "two" PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1587,10 +1704,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByJsonPathOrdered try
PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ] do! conn.writeJsonFirstByJsonPathOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
testList "updateById" [ testList "updateById" [

View File

@ -1,6 +1,7 @@
module PostgresTests module PostgresTests
open System.IO open System.IO
open System.IO.Pipelines
open Expecto open Expecto
open BitBadger.Documents open BitBadger.Documents
open BitBadger.Documents.Postgres open BitBadger.Documents.Postgres
@ -278,9 +279,7 @@ let loadDocs () = backgroundTask {
/// Set up a stream writer for a test /// Set up a stream writer for a test
let writeStream (stream: Stream) = let writeStream (stream: Stream) =
let writer = new StreamWriter(stream) PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
writer.AutoFlush <- true
writer
/// Get the text of the given stream /// Get the text of the given stream
let streamText (stream: Stream) = let streamText (stream: Stream) =
@ -339,7 +338,7 @@ let customTests = testList "Custom" [
let! docs = Custom.jsonArray (Query.find PostgresDb.TableName) [] jsonFromData let! docs = Custom.jsonArray (Query.find PostgresDb.TableName) [] jsonFromData
Expect.stringStarts docs "[" "The JSON array should have started with `[`" Expect.stringStarts docs "[" "The JSON array should have started with `[`"
Expect.hasLength ((string docs).Split "{\"Id\":") 6 "There should have been 5 documents returned" Expect.hasLength ((string docs).Split "{\"Id\":") 6 "There should have been 5 documents returned"
Expect.stringEnds docs "]" "The JSON array should have ended with `[`" Expect.stringEnds docs "]" "The JSON array should have ended with `]`"
} }
testTask "succeeds when data is not found" { testTask "succeeds when data is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -359,27 +358,32 @@ let customTests = testList "Custom" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Custom.writeJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
try
let docs = streamText stream do! Custom.writeJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
Expect.stringStarts docs "[" "The JSON array should have started with `[`" let docs = streamText stream
Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned" Expect.stringStarts docs "[" "The JSON array should have started with `[`"
Expect.stringEnds docs "]" "The JSON array should have ended 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
writer.Complete()
} }
testTask "succeeds when data is not found" { testTask "succeeds when data is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Custom.writeJsonArray try
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath" do! Custom.writeJsonArray
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ] $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
writer [ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
jsonFromData writer
jsonFromData
Expect.equal (streamText stream) "[]" "There should have been no documents returned" Expect.equal (streamText stream) "[]" "There should have been no documents returned"
finally
writer.Complete()
} }
] ]
testList "single" [ testList "single" [
@ -1457,16 +1461,22 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAll PostgresDb.TableName writer try
verifyAllData (streamText stream) do! Json.writeAll PostgresDb.TableName writer
verifyAllData (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds when there is no data" { testTask "succeeds when there is no data" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAll PostgresDb.TableName writer try
verifyEmpty (streamText stream) do! Json.writeAll PostgresDb.TableName writer
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeAllOrdered" [ testList "writeAllOrdered" [
@ -1475,27 +1485,36 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue" ] try
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") (Some "four") (Some "five") do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue" ]
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") (Some "four") (Some "five")
finally
writer.Complete()
} }
testTask "succeeds when ordering numerically descending" { testTask "succeeds when ordering numerically descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue DESC" ] try
verifyExpectedOrder (streamText stream) "five" "four" (Some "two") (Some "three") (Some "one") do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder (streamText stream) "five" "four" (Some "two") (Some "three") (Some "one")
finally
writer.Complete()
} }
testTask "succeeds when ordering alphabetically" { testTask "succeeds when ordering alphabetically" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "Id DESC" ] try
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") (Some "four") (Some "five") do! Json.writeAllOrdered PostgresDb.TableName writer [ Field.Named "Id DESC" ]
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") (Some "four") (Some "five")
finally
writer.Complete()
} }
] ]
testList "writeById" [ testList "writeById" [
@ -1504,20 +1523,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeById PostgresDb.TableName writer "two" try
let json = streamText stream do! Json.writeById PostgresDb.TableName writer "two"
Expect.stringStarts json """{"Id": "two",""" "An incorrect document was returned" let json = streamText stream
Expect.stringEnds json "}" "JSON should have ended with this document" Expect.stringStarts json """{"Id": "two",""" "An incorrect document was returned"
Expect.stringEnds json "}" "JSON should have ended with this document"
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeById PostgresDb.TableName writer "three hundred eighty-seven" try
verifyNoDoc (streamText stream) do! Json.writeById PostgresDb.TableName writer "three hundred eighty-seven"
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByFields" [ testList "writeByFields" [
@ -1526,29 +1551,38 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields try
PostgresDb.TableName writer All [ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ] do! Json.writeByFields
verifySingleById (streamText stream) "four" PostgresDb.TableName writer All [ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ]
verifySingleById (streamText stream) "four"
finally
writer.Complete()
} }
testTask "succeeds when documents are found using IN with numeric field" { testTask "succeeds when documents are found using IN with numeric field" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields PostgresDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ] try
verifySingleById (streamText stream) "three" do! Json.writeByFields PostgresDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
verifySingleById (streamText stream) "three"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields try
PostgresDb.TableName writer All [ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ] do! Json.writeByFields
verifyEmpty (streamText stream) PostgresDb.TableName writer All [ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds for InArray when matching documents exist" { testTask "succeeds for InArray when matching documents exist" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1556,13 +1590,16 @@ let jsonTests = testList "Json" [
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields try
PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ] do! Json.writeByFields
let json = streamText stream PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ]
verifyBeginEnd json let json = streamText stream
verifyDocById json "first" verifyBeginEnd json
verifyDocById json "second" verifyDocById json "first"
verifyDocById json "second"
finally
writer.Complete()
} }
testTask "succeeds for InArray when no matching documents exist" { testTask "succeeds for InArray when no matching documents exist" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
@ -1570,10 +1607,13 @@ let jsonTests = testList "Json" [
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields try
PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ] do! Json.writeByFields
verifyEmpty (streamText stream) PostgresDb.TableName writer All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByFieldsOrdered" [ testList "writeByFieldsOrdered" [
@ -1582,20 +1622,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ] do! Json.writeByFieldsOrdered
verifyExpectedOrder (streamText stream) "five" "four" None None None PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyExpectedOrder (streamText stream) "five" "four" None None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ] do! Json.writeByFieldsOrdered
verifyExpectedOrder (streamText stream) "four" "five" None None None PostgresDb.TableName writer All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyExpectedOrder (streamText stream) "four" "five" None None None
finally
writer.Complete()
} }
] ]
testList "writeByContains" [ testList "writeByContains" [
@ -1604,21 +1650,27 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} try
let json = streamText stream do! Json.writeByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |}
verifyBeginEnd json let json = streamText stream
verifyDocById json "two" verifyBeginEnd json
verifyDocById json "four" verifyDocById json "two"
verifyDocById json "four"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByContains PostgresDb.TableName writer {| Value = "mauve" |} try
verifyEmpty (streamText stream) do! Json.writeByContains PostgresDb.TableName writer {| Value = "mauve" |}
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByContainsOrdered" [ testList "writeByContainsOrdered" [
@ -1628,20 +1680,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ] do! Json.writeByContainsOrdered
verifyExpectedOrder (streamText stream) "two" "four" None None None PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
verifyExpectedOrder (streamText stream) "two" "four" None None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ] do! Json.writeByContainsOrdered
verifyExpectedOrder (streamText stream) "four" "two" None None None PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
verifyExpectedOrder (streamText stream) "four" "two" None None None
finally
writer.Complete()
} }
] ]
testList "writeByJsonPath" [ testList "writeByJsonPath" [
@ -1650,22 +1708,28 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 15)" try
let json = streamText stream do! Json.writeByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 15)"
verifyBeginEnd json let json = streamText stream
verifyDocById json "one" verifyBeginEnd json
verifyDocById json "two" verifyDocById json "one"
verifyDocById json "three" verifyDocById json "two"
verifyDocById json "three"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 0)" try
verifyEmpty (streamText stream) do! Json.writeByJsonPath PostgresDb.TableName writer "$.NumValue ? (@ < 0)"
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByJsonPathOrdered" [ testList "writeByJsonPathOrdered" [
@ -1675,20 +1739,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByJsonPathOrdered try
PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ] do! Json.writeByJsonPathOrdered
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") None None PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
verifyExpectedOrder (streamText stream) "one" "three" (Some "two") None None
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByJsonPathOrdered try
PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ] do! Json.writeByJsonPathOrdered
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") None None PostgresDb.TableName writer "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder (streamText stream) "two" "three" (Some "one") None None
finally
writer.Complete()
} }
] ]
testList "writeFirstByFields" [ testList "writeFirstByFields" [
@ -1697,27 +1767,36 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "another" ] try
verifyDocById (streamText stream) "two" do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "another" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] try
verifyAnyById (streamText stream) [ "five"; "four" ] do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ]
verifyAnyById (streamText stream) [ "five"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "absent" ] try
verifyNoDoc (streamText stream) do! Json.writeFirstByFields PostgresDb.TableName writer Any [ Field.Equal "Value" "absent" ]
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeFirstByFieldsOrdered" [ testList "writeFirstByFieldsOrdered" [
@ -1726,20 +1805,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFieldsOrdered try
PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ] do! Json.writeFirstByFieldsOrdered
verifyDocById (streamText stream) "five" PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyDocById (streamText stream) "five"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFieldsOrdered try
PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ] do! Json.writeFirstByFieldsOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
testList "writeFirstByContains" [ testList "writeFirstByContains" [
@ -1748,27 +1833,36 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByContains PostgresDb.TableName writer {| Value = "another" |} try
verifyDocById (streamText stream) "two" do! Json.writeFirstByContains PostgresDb.TableName writer {| Value = "another" |}
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} try
verifyAnyById (streamText stream) [ "two"; "four" ] do! Json.writeFirstByContains PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |}
verifyAnyById (streamText stream) [ "two"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByContains PostgresDb.TableName writer {| Value = "absent" |} try
verifyNoDoc (streamText stream) do! Json.writeFirstByContains PostgresDb.TableName writer {| Value = "absent" |}
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeFirstByContainsOrdered" [ testList "writeFirstByContainsOrdered" [
@ -1777,20 +1871,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ] do! Json.writeFirstByContainsOrdered
verifyDocById (streamText stream) "two" PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByContainsOrdered try
PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ] do! Json.writeFirstByContainsOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
testList "writeFirstByJsonPath" [ testList "writeFirstByJsonPath" [
@ -1799,27 +1899,36 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Value ? (@ == "FIRST!")""" try
verifyDocById (streamText stream) "one" do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Value ? (@ == "FIRST!")"""
verifyDocById (streamText stream) "one"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" try
verifyAnyById (streamText stream) [ "two"; "four" ] do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")"""
verifyAnyById (streamText stream) [ "two"; "four" ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Id ? (@ == "nope")""" try
verifyNoDoc (streamText stream) do! Json.writeFirstByJsonPath PostgresDb.TableName writer """$.Id ? (@ == "nope")"""
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeFirstByJsonPathOrdered" [ testList "writeFirstByJsonPathOrdered" [
@ -1828,20 +1937,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByJsonPathOrdered try
PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ] do! Json.writeFirstByJsonPathOrdered
verifyDocById (streamText stream) "two" PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
verifyDocById (streamText stream) "two"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb() use db = PostgresDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByJsonPathOrdered try
PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ] do! Json.writeFirstByJsonPathOrdered
verifyDocById (streamText stream) "four" PostgresDb.TableName writer """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
verifyDocById (streamText stream) "four"
finally
writer.Complete()
} }
] ]
] ]

View File

@ -1,6 +1,7 @@
module SqliteExtensionTests module SqliteExtensionTests
open System.IO open System.IO
open System.IO.Pipelines
open System.Text.Json open System.Text.Json
open BitBadger.Documents open BitBadger.Documents
open BitBadger.Documents.Sqlite open BitBadger.Documents.Sqlite
@ -17,9 +18,7 @@ let integrationTests =
/// Set up a stream writer for a test /// Set up a stream writer for a test
let writeStream (stream: Stream) = let writeStream (stream: Stream) =
let writer = new StreamWriter(stream) PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
writer.AutoFlush <- true
writer
/// Get the text of the given stream /// Get the text of the given stream
let streamText (stream: Stream) = let streamText (stream: Stream) =
@ -600,21 +599,27 @@ let integrationTests =
do! conn.insert SqliteDb.TableName { Foo = "five"; Bar = "six" } do! conn.insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAll SqliteDb.TableName writer try
let json = streamText stream do! conn.writeJsonAll SqliteDb.TableName writer
verifyBeginEnd json let json = streamText stream
Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found" verifyBeginEnd json
Expect.stringContains json """{"Foo":"three","Bar":"four"}""" "The second document was not found" Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found"
Expect.stringContains json """{"Foo":"five","Bar":"six"}""" "The third 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
writer.Complete()
} }
testTask "succeeds when there is no data" { testTask "succeeds when there is no data" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn () use conn = Configuration.dbConn ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAll SqliteDb.TableName writer try
verifyEmpty (streamText stream) do! conn.writeJsonAll SqliteDb.TableName writer
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonAllOrdered" [ testList "writeJsonAllOrdered" [
@ -624,12 +629,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue" ] try
Expect.equal do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue" ]
(streamText stream) Expect.equal
$"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when ordering numerically descending" { testTask "succeeds when ordering numerically descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -637,12 +645,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue DESC" ] try
Expect.equal do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue DESC" ]
(streamText stream) Expect.equal
$"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when ordering alphabetically" { testTask "succeeds when ordering alphabetically" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -650,12 +661,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "Id DESC" ] try
Expect.equal do! conn.writeJsonAllOrdered SqliteDb.TableName writer [ Field.Named "Id DESC" ]
(streamText stream) Expect.equal
$"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
] ]
testList "writeJsonById" [ testList "writeJsonById" [
@ -665,9 +679,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonById SqliteDb.TableName writer "two" try
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned" do! conn.writeJsonById SqliteDb.TableName writer "two"
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -675,9 +692,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonById SqliteDb.TableName writer "three hundred eighty-seven" try
verifyNoDoc (streamText stream) do! conn.writeJsonById SqliteDb.TableName writer "three hundred eighty-seven"
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByFields" [ testList "writeJsonByFields" [
@ -687,12 +707,15 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] try
let json = streamText stream do! conn.writeJsonByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ]
verifyBeginEnd json let json = streamText stream
Expect.stringContains json JsonDocument.four "Document `four` should have been returned" verifyBeginEnd json
Expect.stringContains json JsonDocument.five "Document `five` should have been returned" Expect.stringContains json JsonDocument.four "Document `four` should have been returned"
Expect.stringContains json JsonDocument.five "Document `five` should have been returned"
finally
writer.Complete()
} }
testTask "succeeds when documents are found using IN with numeric field" { testTask "succeeds when documents are found using IN with numeric field" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -700,9 +723,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ] try
Expect.equal (streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned" do! conn.writeJsonByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
Expect.equal
(streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -710,9 +737,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ] try
verifyEmpty (streamText stream) do! conn.writeJsonByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds for InArray when matching documents exist" { testTask "succeeds for InArray when matching documents exist" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -721,15 +751,18 @@ let integrationTests =
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ] do! conn.writeJsonByFields
let json = streamText stream SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ]
verifyBeginEnd json let json = streamText stream
Expect.stringContains verifyBeginEnd json
json """{"Id":"first","Values":["a","b","c"]}""" "Document `first` should have been returned" Expect.stringContains
Expect.stringContains json """{"Id":"first","Values":["a","b","c"]}""" "Document `first` should have been returned"
json """{"Id":"second","Values":["c","d","e"]}""" "Document `second` should have been returned" Expect.stringContains
json """{"Id":"second","Values":["c","d","e"]}""" "Document `second` should have been returned"
finally
writer.Complete()
} }
testTask "succeeds for InArray when no matching documents exist" { testTask "succeeds for InArray when no matching documents exist" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -738,10 +771,13 @@ let integrationTests =
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFields try
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ] do! conn.writeJsonByFields
verifyEmpty (streamText stream) SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonByFieldsOrdered" [ testList "writeJsonByFieldsOrdered" [
@ -751,11 +787,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ] do! conn.writeJsonByFieldsOrdered
Expect.equal SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
(streamText stream) $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned" Expect.equal
(streamText stream)
$"[{JsonDocument.five},{JsonDocument.four}]"
"Incorrect documents were returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -763,11 +804,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ] do! conn.writeJsonByFieldsOrdered
Expect.equal SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
(streamText stream) $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned" Expect.equal
(streamText stream)
$"[{JsonDocument.four},{JsonDocument.five}]"
"Incorrect documents were returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting case-sensitively" { testTask "succeeds when sorting case-sensitively" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -775,13 +821,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ] do! conn.writeJsonByFieldsOrdered
Expect.equal SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ]
(streamText stream) Expect.equal
$"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]" (streamText stream)
"Documents not ordered correctly" $"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]"
"Documents not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when sorting case-insensitively" { testTask "succeeds when sorting case-insensitively" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -789,13 +838,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonByFieldsOrdered try
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ] do! conn.writeJsonByFieldsOrdered
Expect.equal SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ]
(streamText stream) Expect.equal
$"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]" (streamText stream)
"Documents not ordered correctly" $"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]"
"Documents not ordered correctly"
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByFields" [ testList "writeJsonFirstByFields" [
@ -805,9 +857,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ] try
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned" do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ]
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -815,11 +870,14 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] try
let json = streamText stream do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ]
Expect.notEqual json "{}" "There should have been a document returned" let json = streamText stream
verifyAny json [ JsonDocument.two; JsonDocument.four ] Expect.notEqual json "{}" "There should have been a document returned"
verifyAny json [ JsonDocument.two; JsonDocument.four ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -827,9 +885,12 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ] try
verifyNoDoc (streamText stream) do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ]
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeJsonFirstByFieldsOrdered" [ testList "writeJsonFirstByFieldsOrdered" [
@ -839,10 +900,13 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ] do! conn.writeJsonFirstByFieldsOrdered
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned" SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -850,10 +914,17 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeJsonFirstByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ] do! conn.writeJsonFirstByFieldsOrdered
Expect.equal (streamText stream) JsonDocument.four "An incorrect document was returned" SqliteDb.TableName
writer
Any
[ Field.Equal "Sub.Foo" "green" ]
[ Field.Named "Sub.Bar DESC" ]
Expect.equal (streamText stream) JsonDocument.four "An incorrect document was returned"
finally
writer.Complete()
} }
] ]
testList "updateById" [ testList "updateById" [
@ -1113,17 +1184,19 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeCustomJsonArray (Query.find SqliteDb.TableName) [] writer jsonFromData try
do! conn.writeCustomJsonArray (Query.find SqliteDb.TableName) [] writer jsonFromData
let json = streamText stream let json = streamText stream
Expect.stringStarts json "[" "The JSON array should have started with `[`" Expect.stringStarts json "[" "The JSON array should have started with `[`"
Expect.stringContains json JsonDocument.one "Document ID `one` should have been found" 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.two "Document ID `two` should have been found"
Expect.stringContains json JsonDocument.three "Document ID `three` 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.four "Document ID `four` should have been found"
Expect.stringContains json JsonDocument.five "Document ID `five` should have been found" Expect.stringContains json JsonDocument.five "Document ID `five` should have been found"
Expect.stringEnds json "]" "The JSON array should have ended with `[`" Expect.stringEnds json "]" "The JSON array should have ended with `]`"
finally
writer.Complete()
} }
testTask "succeeds when data is not found" { testTask "succeeds when data is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -1131,14 +1204,16 @@ let integrationTests =
do! loadDocs conn do! loadDocs conn
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! conn.writeCustomJsonArray try
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" do! conn.writeCustomJsonArray
[ SqliteParameter("@value", 100) ] $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
writer [ SqliteParameter("@value", 100) ]
jsonFromData writer
jsonFromData
Expect.equal (streamText stream) "[]" "There should have been no documents returned" Expect.equal (streamText stream) "[]" "There should have been no documents returned"
finally
writer.Complete()
} }
] ]
testList "customSingle" [ testList "customSingle" [

View File

@ -1,6 +1,7 @@
module SqliteTests module SqliteTests
open System.IO open System.IO
open System.IO.Pipelines
open System.Text.Json open System.Text.Json
open BitBadger.Documents open BitBadger.Documents
open BitBadger.Documents.Sqlite open BitBadger.Documents.Sqlite
@ -138,9 +139,7 @@ let loadDocs () = backgroundTask {
/// Set up a stream writer for a test /// Set up a stream writer for a test
let writeStream (stream: Stream) = let writeStream (stream: Stream) =
let writer = new StreamWriter(stream) PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
writer.AutoFlush <- true
writer
/// Get the text of the given stream /// Get the text of the given stream
let streamText (stream: Stream) = let streamText (stream: Stream) =
@ -217,31 +216,35 @@ let customTests = testList "Custom" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Custom.writeJsonArray (Query.find SqliteDb.TableName) [] writer jsonFromData try
do! Custom.writeJsonArray (Query.find SqliteDb.TableName) [] writer jsonFromData
let json = streamText stream let json = streamText stream
Expect.stringStarts json "[" "The JSON array should have started with `[`" Expect.stringStarts json "[" "The JSON array should have started with `[`"
Expect.stringContains json JsonDocument.one "Document ID `one` should have been found" 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.two "Document ID `two` should have been found"
Expect.stringContains json JsonDocument.three "Document ID `three` 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.four "Document ID `four` should have been found"
Expect.stringContains json JsonDocument.five "Document ID `five` should have been found" Expect.stringContains json JsonDocument.five "Document ID `five` should have been found"
Expect.stringEnds json "]" "The JSON array should have ended with `[`" Expect.stringEnds json "]" "The JSON array should have ended with `[`"
finally
writer.Complete()
} }
testTask "succeeds when data is not found" { testTask "succeeds when data is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Custom.writeJsonArray try
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" do! Custom.writeJsonArray
[ SqliteParameter("@value", 100) ] $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
writer [ SqliteParameter("@value", 100) ]
jsonFromData writer
jsonFromData
Expect.equal (streamText stream) "[]" "There should have been no documents returned" Expect.equal (streamText stream) "[]" "There should have been no documents returned"
finally
writer.Complete()
} }
] ]
testList "single" [ testList "single" [
@ -975,20 +978,26 @@ let jsonTests = testList "Json" [
do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" } do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAll SqliteDb.TableName writer try
let json = streamText stream do! Json.writeAll SqliteDb.TableName writer
verifyBeginEnd json let json = streamText stream
Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found" verifyBeginEnd json
Expect.stringContains json """{"Foo":"three","Bar":"four"}""" "The second document was not found" Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found"
Expect.stringContains json """{"Foo":"five","Bar":"six"}""" "The third 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
writer.Complete()
} }
testTask "succeeds when there is no data" { testTask "succeeds when there is no data" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAll SqliteDb.TableName writer try
verifyEmpty (streamText stream) do! Json.writeAll SqliteDb.TableName writer
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeAllOrdered" [ testList "writeAllOrdered" [
@ -997,36 +1006,45 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue" ] try
Expect.equal do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue" ]
(streamText stream) Expect.equal
$"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when ordering numerically descending" { testTask "succeeds when ordering numerically descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue DESC" ] try
Expect.equal do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue DESC" ]
(streamText stream) Expect.equal
$"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when ordering alphabetically" { testTask "succeeds when ordering alphabetically" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "Id DESC" ] try
Expect.equal do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "Id DESC" ]
(streamText stream) Expect.equal
$"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]" (streamText stream)
"The documents were not ordered correctly" $"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]"
"The documents were not ordered correctly"
finally
writer.Complete()
} }
] ]
testList "writeById" [ testList "writeById" [
@ -1035,18 +1053,24 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeById SqliteDb.TableName writer "two" try
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned" do! Json.writeById SqliteDb.TableName writer "two"
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeById SqliteDb.TableName writer "three hundred eighty-seven" try
verifyNoDoc (streamText stream) do! Json.writeById SqliteDb.TableName writer "three hundred eighty-seven"
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByFields" [ testList "writeByFields" [
@ -1055,30 +1079,40 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] try
let json = streamText stream do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ]
verifyBeginEnd json let json = streamText stream
Expect.stringContains json JsonDocument.four "Document `four` should have been returned" verifyBeginEnd json
Expect.stringContains json JsonDocument.five "Document `five` should have been returned" Expect.stringContains json JsonDocument.four "Document `four` should have been returned"
Expect.stringContains json JsonDocument.five "Document `five` should have been returned"
finally
writer.Complete()
} }
testTask "succeeds when documents are found using IN with numeric field" { testTask "succeeds when documents are found using IN with numeric field" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ] try
Expect.equal (streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned" do! Json.writeByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
Expect.equal
(streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned"
finally
writer.Complete()
} }
testTask "succeeds when documents are not found" { testTask "succeeds when documents are not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ] try
verifyEmpty (streamText stream) do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
testTask "succeeds for InArray when matching documents exist" { testTask "succeeds for InArray when matching documents exist" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -1086,14 +1120,18 @@ let jsonTests = testList "Json" [
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ] try
let json = streamText stream do! Json.writeByFields
verifyBeginEnd json SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ]
Expect.stringContains let json = streamText stream
json """{"Id":"first","Values":["a","b","c"]}""" "Document `first` should have been returned" verifyBeginEnd json
Expect.stringContains Expect.stringContains
json """{"Id":"second","Values":["c","d","e"]}""" "Document `second` should have been returned" 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
writer.Complete()
} }
testTask "succeeds for InArray when no matching documents exist" { testTask "succeeds for InArray when no matching documents exist" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
@ -1101,9 +1139,13 @@ let jsonTests = testList "Json" [
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFields SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ] try
verifyEmpty (streamText stream) do! Json.writeByFields
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
verifyEmpty (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeByFieldsOrdered" [ testList "writeByFieldsOrdered" [
@ -1112,48 +1154,60 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ] do! Json.writeByFieldsOrdered
Expect.equal SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
(streamText stream) $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned" Expect.equal
(streamText stream) $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ] do! Json.writeByFieldsOrdered
Expect.equal SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
(streamText stream) $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned" Expect.equal
(streamText stream) $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting case-sensitively" { testTask "succeeds when sorting case-sensitively" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ] do! Json.writeByFieldsOrdered
Expect.equal SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ]
(streamText stream) Expect.equal
$"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]" (streamText stream)
"Documents not ordered correctly" $"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]"
"Documents not ordered correctly"
finally
writer.Complete()
} }
testTask "succeeds when sorting case-insensitively" { testTask "succeeds when sorting case-insensitively" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeByFieldsOrdered try
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ] do! Json.writeByFieldsOrdered
Expect.equal SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ]
(streamText stream) Expect.equal
$"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]" (streamText stream)
"Documents not ordered correctly" $"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]"
"Documents not ordered correctly"
finally
writer.Complete()
} }
] ]
testList "writeFirstByFields" [ testList "writeFirstByFields" [
@ -1162,29 +1216,38 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ] try
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned" do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ]
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when multiple documents are found" { testTask "succeeds when multiple documents are found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] try
let json = streamText stream do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ]
Expect.notEqual json "{}" "There should have been a document returned" let json = streamText stream
verifyAny json [ JsonDocument.two; JsonDocument.four ] Expect.notEqual json "{}" "There should have been a document returned"
verifyAny json [ JsonDocument.two; JsonDocument.four ]
finally
writer.Complete()
} }
testTask "succeeds when a document is not found" { testTask "succeeds when a document is not found" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ] try
verifyNoDoc (streamText stream) do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ]
verifyNoDoc (streamText stream)
finally
writer.Complete()
} }
] ]
testList "writeFirstByFieldsOrdered" [ testList "writeFirstByFieldsOrdered" [
@ -1193,20 +1256,26 @@ let jsonTests = testList "Json" [
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ] do! Json.writeFirstByFieldsOrdered
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned" SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned"
finally
writer.Complete()
} }
testTask "succeeds when sorting descending" { testTask "succeeds when sorting descending" {
use! db = SqliteDb.BuildDb() use! db = SqliteDb.BuildDb()
do! loadDocs () do! loadDocs ()
use stream = new MemoryStream() use stream = new MemoryStream()
use writer = writeStream stream let writer = writeStream stream
do! Json.writeFirstByFieldsOrdered try
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ] do! Json.writeFirstByFieldsOrdered
Expect.equal (streamText stream) JsonDocument.four "An incorrect document was returned" SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
Expect.equal (streamText stream) JsonDocument.four "An incorrect document was returned"
finally
writer.Complete()
} }
] ]
] ]