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