v4.1 #11
@ -45,7 +45,7 @@ module Custom =
|
|||||||
/// <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
|
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>StreamWriter</c></summary>
|
||||||
/// <param name="query">The query to retrieve the results</param>
|
/// <param name="query">The query to retrieve the results</param>
|
||||||
|
@ -109,7 +109,7 @@ module Custom =
|
|||||||
[<CompiledName "FSharpJsonSingle">]
|
[<CompiledName "FSharpJsonSingle">]
|
||||||
let jsonSingle query parameters mapFunc sqlProps =
|
let jsonSingle query parameters mapFunc sqlProps =
|
||||||
let results = jsonArray $"%s{query} LIMIT 1" parameters mapFunc sqlProps
|
let results = jsonArray $"%s{query} LIMIT 1" parameters mapFunc sqlProps
|
||||||
if results = "[]" then "{}" else results[1..results.Length - 1]
|
if results = "[]" then "{}" else results[1..results.Length - 2]
|
||||||
|
|
||||||
/// <summary>Execute a query that returns one or no JSON documents</summary>
|
/// <summary>Execute a query that returns one or no JSON documents</summary>
|
||||||
/// <param name="query">The query to retrieve the results</param>
|
/// <param name="query">The query to retrieve the results</param>
|
||||||
@ -545,7 +545,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByFields">]
|
[<CompiledName "FSharpFirstByFields">]
|
||||||
let firstByFields<'TDoc> tableName howMatched fields sqlProps =
|
let firstByFields<'TDoc> tableName howMatched fields sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1"
|
(Query.byFields (Query.find tableName) howMatched fields)
|
||||||
(addFieldParams fields [])
|
(addFieldParams fields [])
|
||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -558,7 +558,7 @@ module Find =
|
|||||||
/// <returns>The first document, or <c>null</c> if not found</returns>
|
/// <returns>The first document, or <c>null</c> if not found</returns>
|
||||||
let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields, sqlProps) =
|
let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1",
|
Query.byFields (Query.find tableName) howMatched fields,
|
||||||
addFieldParams fields [],
|
addFieldParams fields [],
|
||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
@ -576,7 +576,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
||||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
|
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL)
|
||||||
(addFieldParams queryFields [])
|
(addFieldParams queryFields [])
|
||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -594,7 +594,7 @@ module Find =
|
|||||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||||
tableName, howMatched, queryFields, orderFields, sqlProps) =
|
tableName, howMatched, queryFields, orderFields, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL,
|
||||||
addFieldParams queryFields [],
|
addFieldParams queryFields [],
|
||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
@ -607,10 +607,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByContains">]
|
[<CompiledName "FSharpFirstByContains">]
|
||||||
let firstByContains<'TDoc> tableName (criteria: obj) sqlProps =
|
let firstByContains<'TDoc> tableName (criteria: obj) sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byContains (Query.find tableName)} LIMIT 1"
|
(Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps
|
||||||
[ jsonParam "@criteria" criteria ]
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// <summary>Retrieve the first document matching a JSON containment query (<c>@></c>)</summary>
|
/// <summary>Retrieve the first document matching a JSON containment 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>
|
||||||
@ -619,10 +616,7 @@ module Find =
|
|||||||
/// <returns>The first document, or <c>null</c> if not found</returns>
|
/// <returns>The first document, or <c>null</c> if not found</returns>
|
||||||
let FirstByContains<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, criteria: obj, sqlProps) =
|
let FirstByContains<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, criteria: obj, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byContains (Query.find tableName)} LIMIT 1",
|
Query.byContains (Query.find tableName), [ jsonParam "@criteria" criteria ], fromData<'TDoc>, sqlProps)
|
||||||
[ jsonParam "@criteria" criteria ],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieve the first document matching a JSON containment query (<c>@></c>) ordered by the given fields in the
|
/// Retrieve the first document matching a JSON containment query (<c>@></c>) ordered by the given fields in the
|
||||||
@ -636,7 +630,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByContainsOrdered">]
|
[<CompiledName "FSharpFirstByContainsOrdered">]
|
||||||
let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
|
let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
(Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
||||||
[ jsonParam "@criteria" criteria ]
|
[ jsonParam "@criteria" criteria ]
|
||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -653,7 +647,7 @@ module Find =
|
|||||||
let FirstByContainsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
let FirstByContainsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||||
tableName, criteria: obj, orderFields, sqlProps) =
|
tableName, criteria: obj, orderFields, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL,
|
||||||
[ jsonParam "@criteria" criteria ],
|
[ jsonParam "@criteria" criteria ],
|
||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
@ -666,7 +660,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByJsonPath">]
|
[<CompiledName "FSharpFirstByJsonPath">]
|
||||||
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
|
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byPathMatch (Query.find tableName)} LIMIT 1"
|
(Query.byPathMatch (Query.find tableName))
|
||||||
[ "@path", Sql.string jsonPath ]
|
[ "@path", Sql.string jsonPath ]
|
||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -678,7 +672,7 @@ module Find =
|
|||||||
/// <returns>The first document, or <c>null</c> if not found</returns>
|
/// <returns>The first document, or <c>null</c> if not found</returns>
|
||||||
let FirstByJsonPath<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, jsonPath, sqlProps) =
|
let FirstByJsonPath<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, jsonPath, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byPathMatch (Query.find tableName)} LIMIT 1",
|
Query.byPathMatch (Query.find tableName),
|
||||||
[ "@path", Sql.string jsonPath ],
|
[ "@path", Sql.string jsonPath ],
|
||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
@ -695,7 +689,7 @@ module Find =
|
|||||||
[<CompiledName "FSharpFirstByJsonPathOrdered">]
|
[<CompiledName "FSharpFirstByJsonPathOrdered">]
|
||||||
let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
|
let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
|
||||||
Custom.single<'TDoc>
|
Custom.single<'TDoc>
|
||||||
$"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
(Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
||||||
[ "@path", Sql.string jsonPath ]
|
[ "@path", Sql.string jsonPath ]
|
||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -712,7 +706,7 @@ module Find =
|
|||||||
let FirstByJsonPathOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
let FirstByJsonPathOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||||
tableName, jsonPath, orderFields, sqlProps) =
|
tableName, jsonPath, orderFields, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
$"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL,
|
||||||
[ "@path", Sql.string jsonPath ],
|
[ "@path", Sql.string jsonPath ],
|
||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
@ -994,7 +988,7 @@ module Json =
|
|||||||
[<CompiledName "FirstByFieldsOrdered">]
|
[<CompiledName "FirstByFieldsOrdered">]
|
||||||
let firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps =
|
let firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps =
|
||||||
Custom.jsonSingle
|
Custom.jsonSingle
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL}"
|
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL)
|
||||||
(addFieldParams queryFields [])
|
(addFieldParams queryFields [])
|
||||||
jsonFromData
|
jsonFromData
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -1046,7 +1040,7 @@ module Json =
|
|||||||
[<CompiledName "FirstByContainsOrdered">]
|
[<CompiledName "FirstByContainsOrdered">]
|
||||||
let firstByContainsOrdered tableName (criteria: obj) orderFields sqlProps =
|
let firstByContainsOrdered tableName (criteria: obj) orderFields sqlProps =
|
||||||
Custom.jsonSingle
|
Custom.jsonSingle
|
||||||
$"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL}"
|
(Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
||||||
[ jsonParam "@criteria" criteria ]
|
[ jsonParam "@criteria" criteria ]
|
||||||
jsonFromData
|
jsonFromData
|
||||||
sqlProps
|
sqlProps
|
||||||
@ -1097,7 +1091,7 @@ module Json =
|
|||||||
[<CompiledName "FirstByJsonPathOrdered">]
|
[<CompiledName "FirstByJsonPathOrdered">]
|
||||||
let firstByJsonPathOrdered tableName jsonPath orderFields sqlProps =
|
let firstByJsonPathOrdered tableName jsonPath orderFields sqlProps =
|
||||||
Custom.jsonSingle
|
Custom.jsonSingle
|
||||||
$"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL}"
|
(Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
||||||
[ "@path", Sql.string jsonPath ]
|
[ "@path", Sql.string jsonPath ]
|
||||||
jsonFromData
|
jsonFromData
|
||||||
sqlProps
|
sqlProps
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
|
<NoWarn>1591</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -328,6 +328,22 @@ public static class PostgresCSharpTests
|
|||||||
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Set up a stream writer for a test</summary>
|
||||||
|
internal static StreamWriter WriteStream(Stream stream)
|
||||||
|
{
|
||||||
|
StreamWriter writer = new(stream);
|
||||||
|
writer.AutoFlush = true;
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the text of the given stream
|
||||||
|
internal static string StreamText(Stream stream)
|
||||||
|
{
|
||||||
|
stream.Position = 0L;
|
||||||
|
using StreamReader reader = new(stream);
|
||||||
|
return reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Integration tests for the Configuration module of the PostgreSQL library
|
/// Integration tests for the Configuration module of the PostgreSQL library
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -389,6 +405,57 @@ public static class PostgresCSharpTests
|
|||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
TestList("JsonArray",
|
||||||
|
[
|
||||||
|
TestCase("succeeds when data is found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
var docs = Custom.JsonArray(Query.Find(PostgresDb.TableName), Parameters.None, Results.JsonFromData);
|
||||||
|
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
||||||
|
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
||||||
|
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
||||||
|
}),
|
||||||
|
TestCase("succeeds when data is not found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
var docs = Custom.JsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
||||||
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData);
|
||||||
|
Expect.equal(docs, "[]", "There should have been no documents returned");
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
TestList("WriteJsonArray",
|
||||||
|
[
|
||||||
|
TestCase("succeeds when data is found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
await using MemoryStream stream = new();
|
||||||
|
await using var writer = WriteStream(stream);
|
||||||
|
Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, Results.JsonFromData);
|
||||||
|
|
||||||
|
var docs = StreamText(stream);
|
||||||
|
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
||||||
|
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
||||||
|
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
||||||
|
}),
|
||||||
|
TestCase("succeeds when data is not found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
await using MemoryStream stream = new();
|
||||||
|
await using var writer = WriteStream(stream);
|
||||||
|
Custom.WriteJsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
||||||
|
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData);
|
||||||
|
|
||||||
|
Expect.equal(StreamText(stream), "[]", "There should have been no documents returned");
|
||||||
|
})
|
||||||
|
]),
|
||||||
TestList("Single",
|
TestList("Single",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a row is found", async () =>
|
TestCase("succeeds when a row is found", async () =>
|
||||||
@ -411,6 +478,29 @@ public static class PostgresCSharpTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
TestList("JsonSingle",
|
||||||
|
[
|
||||||
|
TestCase("succeeds when a row is found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
|
[Tuple.Create("@id", Sql.@string("one"))], Results.JsonFromData);
|
||||||
|
Expect.stringStarts(doc, "{", "The document should have started with an open brace");
|
||||||
|
Expect.stringContains(doc, "\"Id\": \"one\"", "An incorrect document was returned");
|
||||||
|
Expect.stringEnds(doc, "}", "The document should have ended with a closing brace");
|
||||||
|
}),
|
||||||
|
TestCase("succeeds when a row is not found", async () =>
|
||||||
|
{
|
||||||
|
await using var db = PostgresDb.BuildDb();
|
||||||
|
await LoadDocs();
|
||||||
|
|
||||||
|
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
|
[Tuple.Create("@id", Sql.@string("eighty"))], Results.JsonFromData);
|
||||||
|
Expect.equal(doc, "{}", "There should not have been a document returned");
|
||||||
|
})
|
||||||
|
]),
|
||||||
TestList("NonQuery",
|
TestList("NonQuery",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when operating on data", async () =>
|
TestCase("succeeds when operating on data", async () =>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
module PostgresTests
|
module PostgresTests
|
||||||
|
|
||||||
|
open System.IO
|
||||||
open Expecto
|
open Expecto
|
||||||
open BitBadger.Documents
|
open BitBadger.Documents
|
||||||
open BitBadger.Documents.Postgres
|
open BitBadger.Documents.Postgres
|
||||||
@ -275,6 +276,19 @@ let loadDocs () = backgroundTask {
|
|||||||
for doc in testDocuments do do! insert PostgresDb.TableName doc
|
for doc in testDocuments do do! insert PostgresDb.TableName doc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set up a stream writer for a test
|
||||||
|
let writeStream (stream: Stream) =
|
||||||
|
let writer = new StreamWriter(stream)
|
||||||
|
writer.AutoFlush <- true
|
||||||
|
writer
|
||||||
|
|
||||||
|
/// Get the text of the given stream
|
||||||
|
let streamText (stream: Stream) =
|
||||||
|
stream.Position <- 0L
|
||||||
|
use reader = new StreamReader(stream)
|
||||||
|
reader.ReadToEnd()
|
||||||
|
|
||||||
|
|
||||||
/// Integration tests for the Configuration module of the PostgreSQL library
|
/// Integration tests for the Configuration module of the PostgreSQL library
|
||||||
let configurationTests = testList "Configuration" [
|
let configurationTests = testList "Configuration" [
|
||||||
test "useDataSource disposes existing source" {
|
test "useDataSource disposes existing source" {
|
||||||
@ -317,6 +331,57 @@ let customTests = testList "Custom" [
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "jsonArray" [
|
||||||
|
testTask "succeeds when data is found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let docs = Custom.jsonArray (Query.find PostgresDb.TableName) [] jsonFromData
|
||||||
|
Expect.stringStarts docs "[" "The JSON array should have started with `[`"
|
||||||
|
Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned"
|
||||||
|
Expect.stringEnds docs "]" "The JSON array should have ended with `[`"
|
||||||
|
}
|
||||||
|
testTask "succeeds when data is not found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let docs =
|
||||||
|
Custom.jsonArray
|
||||||
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
||||||
|
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
|
||||||
|
jsonFromData
|
||||||
|
Expect.equal docs "[]" "There should have been no documents returned"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
testList "writeJsonArray" [
|
||||||
|
testTask "succeeds when data is found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
use stream = new MemoryStream()
|
||||||
|
use writer = writeStream stream
|
||||||
|
Custom.writeJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
|
||||||
|
|
||||||
|
let docs = streamText stream
|
||||||
|
Expect.stringStarts docs "[" "The JSON array should have started with `[`"
|
||||||
|
Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned"
|
||||||
|
Expect.stringEnds docs "]" "The JSON array should have ended with `[`"
|
||||||
|
}
|
||||||
|
testTask "succeeds when data is not found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
use stream = new MemoryStream()
|
||||||
|
use writer = writeStream stream
|
||||||
|
Custom.writeJsonArray
|
||||||
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
||||||
|
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
|
||||||
|
writer
|
||||||
|
jsonFromData
|
||||||
|
|
||||||
|
Expect.equal (streamText stream) "[]" "There should have been no documents returned"
|
||||||
|
}
|
||||||
|
]
|
||||||
testList "single" [
|
testList "single" [
|
||||||
testTask "succeeds when a row is found" {
|
testTask "succeeds when a row is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@ -342,6 +407,32 @@ let customTests = testList "Custom" [
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "jsonSingle" [
|
||||||
|
testTask "succeeds when a row is found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let doc =
|
||||||
|
Custom.jsonSingle
|
||||||
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
||||||
|
[ "@id", Sql.string "one"]
|
||||||
|
jsonFromData
|
||||||
|
Expect.stringStarts doc "{" "The document should have started with an open brace"
|
||||||
|
Expect.stringContains doc "\"Id\": \"one\"" "An incorrect document was returned"
|
||||||
|
Expect.stringEnds doc "}" "The document should have ended with a closing brace"
|
||||||
|
}
|
||||||
|
testTask "succeeds when a row is not found" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let doc =
|
||||||
|
Custom.jsonSingle
|
||||||
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
||||||
|
[ "@id", Sql.string "eighty" ]
|
||||||
|
jsonFromData
|
||||||
|
Expect.equal doc "{}" "There should not have been a document returned"
|
||||||
|
}
|
||||||
|
]
|
||||||
testList "nonQuery" [
|
testList "nonQuery" [
|
||||||
testTask "succeeds when operating on data" {
|
testTask "succeeds when operating on data" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@ -1045,7 +1136,7 @@ let updateTests = testList "Update" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Update.byFunc PostgresDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
do! Update.byFunc PostgresDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
||||||
Expect.isSome after "There should have been a document returned post-update"
|
Expect.isSome after "There should have been a document returned post-update"
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@ -1060,7 +1151,7 @@ let updateTests = testList "Update" [
|
|||||||
Expect.equal before 0 "There should have been no documents returned"
|
Expect.equal before 0 "There should have been no documents returned"
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! Update.byFunc PostgresDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
do! Update.byFunc PostgresDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user