Add Custom Json tests
This commit is contained in:
parent
a363370342
commit
0e489617f7
@ -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>
|
||||
|
@ -319,7 +319,7 @@ public static class PostgresCSharpTests
|
||||
"By-JSON Path query not correct");
|
||||
})
|
||||
]);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add the test documents to the database
|
||||
/// </summary>
|
||||
@ -328,6 +328,22 @@ public static class PostgresCSharpTests
|
||||
foreach (var doc in JsonDocument.TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||
}
|
||||
|
||||
/// <summary>Set up a stream writer for a test</summary>
|
||||
internal static StreamWriter WriteStream(Stream stream)
|
||||
{
|
||||
StreamWriter writer = new(stream);
|
||||
writer.AutoFlush = true;
|
||||
return writer;
|
||||
}
|
||||
|
||||
/// Get the text of the given stream
|
||||
internal static string StreamText(Stream stream)
|
||||
{
|
||||
stream.Position = 0L;
|
||||
using StreamReader reader = new(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for the Configuration module of the PostgreSQL library
|
||||
/// </summary>
|
||||
@ -389,6 +405,57 @@ public static class PostgresCSharpTests
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
]),
|
||||
TestList("JsonArray",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = Custom.JsonArray(Query.Find(PostgresDb.TableName), Parameters.None, Results.JsonFromData);
|
||||
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
||||
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
||||
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = Custom.JsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
||||
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.JsonFromData);
|
||||
Expect.equal(docs, "[]", "There should have been no documents returned");
|
||||
})
|
||||
]),
|
||||
TestList("WriteJsonArray",
|
||||
[
|
||||
TestCase("succeeds when data is found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await using MemoryStream stream = new();
|
||||
await using var writer = WriteStream(stream);
|
||||
Custom.WriteJsonArray(Query.Find(PostgresDb.TableName), Parameters.None, writer, Results.JsonFromData);
|
||||
|
||||
var docs = StreamText(stream);
|
||||
Expect.stringStarts(docs, "[", "The JSON array should have started with `[`");
|
||||
Expect.hasLength(docs.Split("{\"Id\":"), 6, "There should have been 5 documents returned");
|
||||
Expect.stringEnds(docs, "]", "The JSON array should have ended with `[`");
|
||||
}),
|
||||
TestCase("succeeds when data is not found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await using MemoryStream stream = new();
|
||||
await using var writer = WriteStream(stream);
|
||||
Custom.WriteJsonArray($"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
||||
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], writer, Results.JsonFromData);
|
||||
|
||||
Expect.equal(StreamText(stream), "[]", "There should have been no documents returned");
|
||||
})
|
||||
]),
|
||||
TestList("Single",
|
||||
[
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
@ -411,6 +478,29 @@ public static class PostgresCSharpTests
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
]),
|
||||
TestList("JsonSingle",
|
||||
[
|
||||
TestCase("succeeds when a row is found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
[Tuple.Create("@id", Sql.@string("one"))], Results.JsonFromData);
|
||||
Expect.stringStarts(doc, "{", "The document should have started with an open brace");
|
||||
Expect.stringContains(doc, "\"Id\": \"one\"", "An incorrect document was returned");
|
||||
Expect.stringEnds(doc, "}", "The document should have ended with a closing brace");
|
||||
}),
|
||||
TestCase("succeeds when a row is not found", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = Custom.JsonSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||
[Tuple.Create("@id", Sql.@string("eighty"))], Results.JsonFromData);
|
||||
Expect.equal(doc, "{}", "There should not have been a document returned");
|
||||
})
|
||||
]),
|
||||
TestList("NonQuery",
|
||||
[
|
||||
TestCase("succeeds when operating on data", async () =>
|
||||
|
@ -1,5 +1,6 @@
|
||||
module PostgresTests
|
||||
|
||||
open System.IO
|
||||
open Expecto
|
||||
open BitBadger.Documents
|
||||
open BitBadger.Documents.Postgres
|
||||
@ -68,7 +69,7 @@ let parametersTests = testList "Parameters" [
|
||||
Expect.equal (idParam "99") ("@id", Sql.string "99") "String ID parameter not constructed correctly"
|
||||
}
|
||||
test "succeeds for non-numeric non-string ID" {
|
||||
let target = { new obj() with override _.ToString() = "ToString was called" }
|
||||
let target = { new obj() with override _.ToString() = "ToString was called" }
|
||||
Expect.equal
|
||||
(idParam target)
|
||||
("@id", Sql.string "ToString was called")
|
||||
@ -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()
|
||||
@ -380,7 +471,7 @@ let definitionTests = testList "Definition" [
|
||||
let keyExists () =
|
||||
Custom.scalar
|
||||
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it" [] toExists
|
||||
|
||||
|
||||
let! exists = tableExists ()
|
||||
let! alsoExists = keyExists ()
|
||||
Expect.isFalse exists "The table should not exist already"
|
||||
@ -397,7 +488,7 @@ let definitionTests = testList "Definition" [
|
||||
let indexExists () =
|
||||
Custom.scalar
|
||||
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it" [] toExists
|
||||
|
||||
|
||||
let! exists = indexExists ()
|
||||
Expect.isFalse exists "The index should not exist already"
|
||||
|
||||
@ -410,7 +501,7 @@ let definitionTests = testList "Definition" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
let indexExists () =
|
||||
Custom.scalar "SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it" [] toExists
|
||||
|
||||
|
||||
let! exists = indexExists ()
|
||||
Expect.isFalse exists "The index should not exist already"
|
||||
|
||||
@ -451,12 +542,12 @@ let documentTests = testList "Document" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should be no documents in the table"
|
||||
|
||||
|
||||
do! insert PostgresDb.TableName { Key = 0; Text = "one" }
|
||||
do! insert PostgresDb.TableName { Key = 0; Text = "two" }
|
||||
do! insert PostgresDb.TableName { Key = 77; Text = "three" }
|
||||
do! insert PostgresDb.TableName { Key = 0; Text = "four" }
|
||||
|
||||
|
||||
let! after = Find.allOrdered<NumIdDocument> PostgresDb.TableName [ Field.Named "n:Key" ]
|
||||
Expect.hasLength after 4 "There should have been 4 documents returned"
|
||||
Expect.equal (after |> List.map _.Key) [ 1; 2; 77; 78 ] "The IDs were not generated correctly"
|
||||
@ -470,12 +561,12 @@ let documentTests = testList "Document" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should be no documents in the table"
|
||||
|
||||
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "one" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "two" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Id = "abc123"; Value = "three" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "four" }
|
||||
|
||||
|
||||
let! after = Find.all<JsonDocument> PostgresDb.TableName
|
||||
Expect.hasLength after 4 "There should have been 4 documents returned"
|
||||
Expect.hasCountOf after 3u (fun doc -> doc.Id.Length = 32) "Three of the IDs should have been GUIDs"
|
||||
@ -490,12 +581,12 @@ let documentTests = testList "Document" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should be no documents in the table"
|
||||
|
||||
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "one" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "two" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Id = "abc123"; Value = "three" }
|
||||
do! insert PostgresDb.TableName { emptyDoc with Value = "four" }
|
||||
|
||||
|
||||
let! after = Find.all<JsonDocument> PostgresDb.TableName
|
||||
Expect.hasLength after 4 "There should have been 4 documents returned"
|
||||
Expect.hasCountOf
|
||||
@ -549,7 +640,7 @@ let countTests = testList "Count" [
|
||||
testTask "succeeds when items are found" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
let! theCount =
|
||||
Count.byFields PostgresDb.TableName Any [ Field.Between "NumValue" 15 20; Field.Equal "NumValue" 0 ]
|
||||
Expect.equal theCount 3 "There should have been 3 matching documents"
|
||||
@ -557,7 +648,7 @@ let countTests = testList "Count" [
|
||||
testTask "succeeds when items are not found" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
let! theCount = Count.byFields PostgresDb.TableName All [ Field.Exists "Sub"; Field.Greater "NumValue" 100 ]
|
||||
Expect.equal theCount 0 "There should have been no matching documents"
|
||||
}
|
||||
@ -672,7 +763,7 @@ let findTests = testList "Find" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
let! results = Find.allOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "n:NumValue" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
Expect.equal
|
||||
@ -683,7 +774,7 @@ let findTests = testList "Find" [
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
let! results = Find.allOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "n:NumValue DESC" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
Expect.equal
|
||||
@ -694,7 +785,7 @@ let findTests = testList "Find" [
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
let! results = Find.allOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "Id DESC" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
Expect.equal
|
||||
@ -750,7 +841,7 @@ let findTests = testList "Find" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! Definition.ensureTable PostgresDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
|
||||
|
||||
|
||||
let! docs =
|
||||
Find.byFields<ArrayDocument>
|
||||
PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ]
|
||||
@ -760,7 +851,7 @@ let findTests = testList "Find" [
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! Definition.ensureTable PostgresDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
|
||||
|
||||
|
||||
let! docs =
|
||||
Find.byFields<ArrayDocument>
|
||||
PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ]
|
||||
@ -1034,7 +1125,7 @@ let updateTests = testList "Update" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should have been no documents returned"
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Update.byId
|
||||
PostgresDb.TableName "test" { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
||||
@ -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
|
||||
@ -1058,9 +1149,9 @@ let updateTests = testList "Update" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
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 }
|
||||
}
|
||||
]
|
||||
]
|
||||
@ -1071,7 +1162,7 @@ let patchTests = testList "Patch" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
do! Patch.byId PostgresDb.TableName "one" {| NumValue = 44 |}
|
||||
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
||||
Expect.isSome after "There should have been a document returned post-update"
|
||||
@ -1082,7 +1173,7 @@ let patchTests = testList "Patch" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should have been no documents returned"
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Patch.byId PostgresDb.TableName "test" {| Foo = "green" |}
|
||||
}
|
||||
@ -1091,7 +1182,7 @@ let patchTests = testList "Patch" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
do! Patch.byFields PostgresDb.TableName Any [ Field.Equal "Value" "purple" ] {| NumValue = 77 |}
|
||||
let! after = Count.byFields PostgresDb.TableName Any [ Field.Equal "NumValue" 77 ]
|
||||
Expect.equal after 2 "There should have been 2 documents returned"
|
||||
@ -1101,7 +1192,7 @@ let patchTests = testList "Patch" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should have been no documents returned"
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Patch.byFields PostgresDb.TableName Any [ Field.Equal "Value" "burgundy" ] {| Foo = "green" |}
|
||||
}
|
||||
@ -1110,7 +1201,7 @@ let patchTests = testList "Patch" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
do! Patch.byContains PostgresDb.TableName {| Value = "purple" |} {| NumValue = 77 |}
|
||||
let! after = Count.byContains PostgresDb.TableName {| NumValue = 77 |}
|
||||
Expect.equal after 2 "There should have been 2 documents returned"
|
||||
@ -1120,7 +1211,7 @@ let patchTests = testList "Patch" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should have been no documents returned"
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Patch.byContains PostgresDb.TableName {| Value = "burgundy" |} {| Foo = "green" |}
|
||||
}
|
||||
@ -1129,7 +1220,7 @@ let patchTests = testList "Patch" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
do! Patch.byJsonPath PostgresDb.TableName "$.NumValue ? (@ > 10)" {| NumValue = 1000 |}
|
||||
let! after = Count.byJsonPath PostgresDb.TableName "$.NumValue ? (@ > 999)"
|
||||
Expect.equal after 2 "There should have been 2 documents returned"
|
||||
@ -1139,7 +1230,7 @@ let patchTests = testList "Patch" [
|
||||
|
||||
let! before = Count.all PostgresDb.TableName
|
||||
Expect.equal before 0 "There should have been no documents returned"
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Patch.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 0)" {| Foo = "green" |}
|
||||
}
|
||||
@ -1172,13 +1263,13 @@ let removeFieldsTests = testList "RemoveFields" [
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byId PostgresDb.TableName "two" [ "AFieldThatIsNotThere" ]
|
||||
}
|
||||
testTask "succeeds when no document is matched" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byId PostgresDb.TableName "two" [ "Value" ]
|
||||
}
|
||||
@ -1207,13 +1298,13 @@ let removeFieldsTests = testList "RemoveFields" [
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Nothing" ]
|
||||
}
|
||||
testTask "succeeds when no document is matched" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.NotEqual "Abracadabra" "apple" ] [ "Value" ]
|
||||
}
|
||||
@ -1242,13 +1333,13 @@ let removeFieldsTests = testList "RemoveFields" [
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Nothing" ]
|
||||
}
|
||||
testTask "succeeds when no document is matched" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byContains PostgresDb.TableName {| Abracadabra = "apple" |} [ "Value" ]
|
||||
}
|
||||
@ -1277,13 +1368,13 @@ let removeFieldsTests = testList "RemoveFields" [
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Nothing" ]
|
||||
}
|
||||
testTask "succeeds when no document is matched" {
|
||||
use db = PostgresDb.BuildDb()
|
||||
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! RemoveFields.byJsonPath PostgresDb.TableName "$.Abracadabra ? (@ == \"apple\")" [ "Value" ]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user