Compare commits
15 Commits
v4-rc1
...
0c308c5f33
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c308c5f33 | |||
| d9d37f110d | |||
| e0fb793ec9 | |||
| 74e5b77edb | |||
| 98bc83ac17 | |||
| 35755df99a | |||
| b1c3991e11 | |||
| 85750e19f2 | |||
| d8f64417e5 | |||
| 202fca272e | |||
| 8a15bdce2e | |||
| d131eda56e | |||
| e2232e91bb | |||
| e96c449324 | |||
| 433302d995 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -396,4 +396,3 @@ FodyWeavers.xsd
|
|||||||
|
|
||||||
# JetBrains Rider
|
# JetBrains Rider
|
||||||
*.sln.iml
|
*.sln.iml
|
||||||
**/.idea
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
namespace BitBadger.Documents
|
namespace BitBadger.Documents
|
||||||
|
|
||||||
open System.Security.Cryptography
|
|
||||||
|
|
||||||
/// The types of logical operations available for JSON fields
|
/// The types of logical operations available for JSON fields
|
||||||
[<Struct>]
|
[<Struct>]
|
||||||
type Op =
|
type Op =
|
||||||
@@ -107,10 +105,6 @@ type Field = {
|
|||||||
else $"->>'{name}'"
|
else $"->>'{name}'"
|
||||||
$"data{path}"
|
$"data{path}"
|
||||||
|
|
||||||
/// Create a field with a given name, but no other properties filled (op will be EQ, value will be "")
|
|
||||||
static member Named name =
|
|
||||||
{ Name = name; Op = EQ; Value = ""; ParameterName = None; Qualifier = None }
|
|
||||||
|
|
||||||
/// Specify the name of the parameter for this field
|
/// Specify the name of the parameter for this field
|
||||||
member this.WithParameterName name =
|
member this.WithParameterName name =
|
||||||
{ this with ParameterName = Some name }
|
{ this with ParameterName = Some name }
|
||||||
@@ -150,70 +144,6 @@ type ParameterName() =
|
|||||||
currentIdx <- currentIdx + 1
|
currentIdx <- currentIdx + 1
|
||||||
$"@field{currentIdx}"
|
$"@field{currentIdx}"
|
||||||
|
|
||||||
#if NET6_0
|
|
||||||
open System.Text
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// Automatically-generated document ID strategies
|
|
||||||
[<Struct>]
|
|
||||||
type AutoId =
|
|
||||||
/// No automatic IDs will be generated
|
|
||||||
| Disabled
|
|
||||||
/// Generate a MAX-plus-1 numeric value for documents
|
|
||||||
| Number
|
|
||||||
/// Generate a GUID for each document (as a lowercase, no-dashes, 32-character string)
|
|
||||||
| Guid
|
|
||||||
/// Generate a random string of hexadecimal characters for each document
|
|
||||||
| RandomString
|
|
||||||
with
|
|
||||||
/// Generate a GUID string
|
|
||||||
static member GenerateGuid () =
|
|
||||||
System.Guid.NewGuid().ToString "N"
|
|
||||||
|
|
||||||
/// Generate a string of random hexadecimal characters
|
|
||||||
static member GenerateRandomString (length: int) =
|
|
||||||
#if NET8_0_OR_GREATER
|
|
||||||
RandomNumberGenerator.GetHexString(length, lowercase = true)
|
|
||||||
#else
|
|
||||||
RandomNumberGenerator.GetBytes((length / 2) + 1)
|
|
||||||
|> Array.fold (fun (str: StringBuilder) byt -> str.Append(byt.ToString "x2")) (StringBuilder length)
|
|
||||||
|> function it -> it.Length <- length; it.ToString()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// Does the given document need an automatic ID generated?
|
|
||||||
static member NeedsAutoId<'T> strategy (document: 'T) idProp =
|
|
||||||
match strategy with
|
|
||||||
| Disabled -> false
|
|
||||||
| _ ->
|
|
||||||
let prop = document.GetType().GetProperty idProp
|
|
||||||
if isNull prop then invalidOp $"{idProp} not found in document"
|
|
||||||
else
|
|
||||||
match strategy with
|
|
||||||
| Number ->
|
|
||||||
if prop.PropertyType = typeof<int8> then
|
|
||||||
let value = prop.GetValue document :?> int8
|
|
||||||
value = int8 0
|
|
||||||
elif prop.PropertyType = typeof<int16> then
|
|
||||||
let value = prop.GetValue document :?> int16
|
|
||||||
value = int16 0
|
|
||||||
elif prop.PropertyType = typeof<int> then
|
|
||||||
let value = prop.GetValue document :?> int
|
|
||||||
value = 0
|
|
||||||
elif prop.PropertyType = typeof<int64> then
|
|
||||||
let value = prop.GetValue document :?> int64
|
|
||||||
value = int64 0
|
|
||||||
else invalidOp "Document ID was not a number; cannot auto-generate a Number ID"
|
|
||||||
| Guid | RandomString ->
|
|
||||||
if prop.PropertyType = typeof<string> then
|
|
||||||
let value =
|
|
||||||
prop.GetValue document
|
|
||||||
|> Option.ofObj
|
|
||||||
|> Option.map (fun it -> it :?> string)
|
|
||||||
|> Option.defaultValue ""
|
|
||||||
value = ""
|
|
||||||
else invalidOp "Document ID was not a string; cannot auto-generate GUID or random string"
|
|
||||||
| Disabled -> false
|
|
||||||
|
|
||||||
|
|
||||||
/// The required document serialization implementation
|
/// The required document serialization implementation
|
||||||
type IDocumentSerializer =
|
type IDocumentSerializer =
|
||||||
@@ -266,7 +196,7 @@ module Configuration =
|
|||||||
serializerValue
|
serializerValue
|
||||||
|
|
||||||
/// The serialized name of the ID field for documents
|
/// The serialized name of the ID field for documents
|
||||||
let mutable private idFieldValue = "Id"
|
let mutable idFieldValue = "Id"
|
||||||
|
|
||||||
/// Specify the name of the ID field for documents
|
/// Specify the name of the ID field for documents
|
||||||
[<CompiledName "UseIdField">]
|
[<CompiledName "UseIdField">]
|
||||||
@@ -278,32 +208,6 @@ module Configuration =
|
|||||||
let idField () =
|
let idField () =
|
||||||
idFieldValue
|
idFieldValue
|
||||||
|
|
||||||
/// The automatic ID strategy used by the library
|
|
||||||
let mutable private autoIdValue = Disabled
|
|
||||||
|
|
||||||
/// Specify the automatic ID generation strategy used by the library
|
|
||||||
[<CompiledName "UseAutoIdStrategy">]
|
|
||||||
let useAutoIdStrategy it =
|
|
||||||
autoIdValue <- it
|
|
||||||
|
|
||||||
/// Retrieve the currently configured automatic ID generation strategy
|
|
||||||
[<CompiledName "AutoIdStrategy">]
|
|
||||||
let autoIdStrategy () =
|
|
||||||
autoIdValue
|
|
||||||
|
|
||||||
/// The length of automatically generated random strings
|
|
||||||
let mutable private idStringLengthValue = 16
|
|
||||||
|
|
||||||
/// Specify the length of automatically generated random strings
|
|
||||||
[<CompiledName "UseIdStringLength">]
|
|
||||||
let useIdStringLength length =
|
|
||||||
idStringLengthValue <- length
|
|
||||||
|
|
||||||
/// Retrieve the currently configured length of automatically generated random strings
|
|
||||||
[<CompiledName "IdStringLength">]
|
|
||||||
let idStringLength () =
|
|
||||||
idStringLengthValue
|
|
||||||
|
|
||||||
|
|
||||||
/// Query construction functions
|
/// Query construction functions
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -388,23 +292,3 @@ module Query =
|
|||||||
[<System.Obsolete "Use Find instead">]
|
[<System.Obsolete "Use Find instead">]
|
||||||
let selectFromTable tableName =
|
let selectFromTable tableName =
|
||||||
find tableName
|
find tableName
|
||||||
|
|
||||||
/// Create an ORDER BY clause for the given fields
|
|
||||||
[<CompiledName "OrderBy">]
|
|
||||||
let orderBy fields dialect =
|
|
||||||
if Seq.isEmpty fields then ""
|
|
||||||
else
|
|
||||||
fields
|
|
||||||
|> Seq.map (fun it ->
|
|
||||||
if it.Name.Contains ' ' then
|
|
||||||
let parts = it.Name.Split ' '
|
|
||||||
{ it with Name = parts[0] }, Some $" {parts[1]}"
|
|
||||||
else it, None)
|
|
||||||
|> Seq.map (fun (field, direction) ->
|
|
||||||
match dialect, field.Name.StartsWith "n:" with
|
|
||||||
| PostgreSQL, true -> $"({ { field with Name = field.Name[2..] }.Path PostgreSQL})::numeric"
|
|
||||||
| SQLite, true -> { field with Name = field.Name[2..] }.Path SQLite
|
|
||||||
| _, _ -> field.Path dialect
|
|
||||||
|> function path -> path + defaultArg direction "")
|
|
||||||
|> String.concat ", "
|
|
||||||
|> function it -> $" ORDER BY {it}"
|
|
||||||
|
|||||||
@@ -3,11 +3,10 @@
|
|||||||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
|
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
|
||||||
<DebugType>embedded</DebugType>
|
<DebugType>embedded</DebugType>
|
||||||
<GenerateDocumentationFile>false</GenerateDocumentationFile>
|
<GenerateDocumentationFile>false</GenerateDocumentationFile>
|
||||||
<AssemblyVersion>4.0.0.0</AssemblyVersion>
|
<AssemblyVersion>3.1.0.0</AssemblyVersion>
|
||||||
<FileVersion>4.0.0.0</FileVersion>
|
<FileVersion>3.1.0.0</FileVersion>
|
||||||
<VersionPrefix>4.0.0</VersionPrefix>
|
<VersionPrefix>3.1.0</VersionPrefix>
|
||||||
<VersionSuffix>rc1</VersionSuffix>
|
<PackageReleaseNotes>Add BT (between) operator; drop .NET 7 support</PackageReleaseNotes>
|
||||||
<PackageReleaseNotes>Change ByField to ByFields; support dot-access to nested document fields; add Find*Ordered functions/methods; see project site for breaking changes and compatibility</PackageReleaseNotes>
|
|
||||||
<Authors>danieljsummers</Authors>
|
<Authors>danieljsummers</Authors>
|
||||||
<Company>Bit Badger Solutions</Company>
|
<Company>Bit Badger Solutions</Company>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Library.fs" />
|
<Compile Include="Library.fs" />
|
||||||
<Compile Include="Extensions.fs" />
|
<Compile Include="Extensions.fs" />
|
||||||
<Compile Include="Compat.fs" />
|
|
||||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,270 +0,0 @@
|
|||||||
namespace BitBadger.Documents.Postgres.Compat
|
|
||||||
|
|
||||||
open BitBadger.Documents
|
|
||||||
open BitBadger.Documents.Postgres
|
|
||||||
|
|
||||||
[<AutoOpen>]
|
|
||||||
module Parameters =
|
|
||||||
|
|
||||||
/// Create a JSON field parameter
|
|
||||||
[<CompiledName "AddField">]
|
|
||||||
[<System.Obsolete "Use addFieldParams (F#) / AddFields (C#) instead ~ will be removed in v4.1">]
|
|
||||||
let addFieldParam name field parameters =
|
|
||||||
addFieldParams [ { field with ParameterName = Some name } ] parameters
|
|
||||||
|
|
||||||
/// Append JSON field name parameters for the given field names to the given parameters
|
|
||||||
[<CompiledName "FieldName">]
|
|
||||||
[<System.Obsolete "Use fieldNameParams (F#) / FieldNames (C#) instead ~ will be removed in v4.1">]
|
|
||||||
let fieldNameParam fieldNames =
|
|
||||||
fieldNameParams fieldNames
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Query =
|
|
||||||
|
|
||||||
/// Create a WHERE clause fragment to implement a comparison on a field in a JSON document
|
|
||||||
[<CompiledName "WhereByField">]
|
|
||||||
[<System.Obsolete "Use WhereByFields instead ~ will be removed in v4.1">]
|
|
||||||
let whereByField field paramName =
|
|
||||||
Query.whereByFields Any [ { field with ParameterName = Some paramName } ]
|
|
||||||
|
|
||||||
|
|
||||||
module WithProps =
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Count =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field sqlProps =
|
|
||||||
WithProps.Count.byFields tableName Any [ field ] sqlProps
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Exists =
|
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field sqlProps =
|
|
||||||
WithProps.Exists.byFields tableName Any [ field ] sqlProps
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Find =
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "FSharpByField">]
|
|
||||||
[<System.Obsolete "Use byFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField<'TDoc> tableName field sqlProps =
|
|
||||||
WithProps.Find.byFields<'TDoc> tableName Any [ field ] sqlProps
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let ByField<'TDoc>(tableName, field, sqlProps) =
|
|
||||||
WithProps.Find.ByFields<'TDoc>(tableName, Any, Seq.singleton field, sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByField">]
|
|
||||||
[<System.Obsolete "Use firstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let firstByField<'TDoc> tableName field sqlProps =
|
|
||||||
WithProps.Find.firstByFields<'TDoc> tableName Any [ field ] sqlProps
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
|
||||||
[<System.Obsolete "Use FirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let FirstByField<'TDoc when 'TDoc: null>(tableName, field, sqlProps) =
|
|
||||||
WithProps.Find.FirstByFields<'TDoc>(tableName, Any, Seq.singleton field, sqlProps)
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Patch =
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field (patch: 'TPatch) sqlProps =
|
|
||||||
WithProps.Patch.byFields tableName Any [ field ] patch sqlProps
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module RemoveFields =
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field fieldNames sqlProps =
|
|
||||||
WithProps.RemoveFields.byFields tableName Any [ field ] fieldNames sqlProps
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Delete =
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field sqlProps =
|
|
||||||
WithProps.Delete.byFields tableName Any [ field ] sqlProps
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Count =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Count.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Exists =
|
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Exists.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Find =
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "FSharpByField">]
|
|
||||||
[<System.Obsolete "Use byFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField<'TDoc> tableName field =
|
|
||||||
Find.byFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let ByField<'TDoc>(tableName, field) =
|
|
||||||
Find.ByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByField">]
|
|
||||||
[<System.Obsolete "Use firstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let firstByField<'TDoc> tableName field =
|
|
||||||
Find.firstByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
|
||||||
[<System.Obsolete "Use FirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let FirstByField<'TDoc when 'TDoc: null>(tableName, field) =
|
|
||||||
Find.FirstByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Patch =
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field (patch: 'TPatch) =
|
|
||||||
Patch.byFields tableName Any [ field ] patch
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module RemoveFields =
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field fieldNames =
|
|
||||||
RemoveFields.byFields tableName Any [ field ] fieldNames
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Delete =
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Delete.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
open Npgsql
|
|
||||||
|
|
||||||
/// F# Extensions for the NpgsqlConnection type
|
|
||||||
[<AutoOpen>]
|
|
||||||
module Extensions =
|
|
||||||
|
|
||||||
type NpgsqlConnection with
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use countByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.countByField tableName field =
|
|
||||||
conn.countByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use existsByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.existsByField tableName field =
|
|
||||||
conn.existsByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use findByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.findByField<'TDoc> tableName field =
|
|
||||||
conn.findByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
|
||||||
[<System.Obsolete "Use findFirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.findFirstByField<'TDoc> tableName field =
|
|
||||||
conn.findFirstByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<System.Obsolete "Use patchByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.patchByField tableName field (patch: 'TPatch) =
|
|
||||||
conn.patchByFields tableName Any [ field ] patch
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<System.Obsolete "Use removeFieldsByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.removeFieldsByField tableName field fieldNames =
|
|
||||||
conn.removeFieldsByFields tableName Any [ field ] fieldNames
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use deleteByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.deleteByField tableName field =
|
|
||||||
conn.deleteByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
open System.Runtime.CompilerServices
|
|
||||||
open Npgsql.FSharp
|
|
||||||
|
|
||||||
type NpgsqlConnectionCSharpCompatExtensions =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use CountByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline CountByField(conn, tableName, field) =
|
|
||||||
WithProps.Count.byFields tableName Any [ field ] (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use ExistsByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline ExistsByField(conn, tableName, field) =
|
|
||||||
WithProps.Exists.byFields tableName Any [ field ] (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use FindByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline FindByField<'TDoc>(conn, tableName, field) =
|
|
||||||
WithProps.Find.ByFields<'TDoc>(tableName, Any, [ field ], Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use FindFirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, field) =
|
|
||||||
WithProps.Find.FirstByFields<'TDoc>(tableName, Any, [ field ], Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use PatchByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline PatchByField(conn, tableName, field, patch: 'TPatch) =
|
|
||||||
WithProps.Patch.byFields tableName Any [ field ] patch (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use RemoveFieldsByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline RemoveFieldsByField(conn, tableName, field, fieldNames) =
|
|
||||||
WithProps.RemoveFields.byFields tableName Any [ field ] fieldNames (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use DeleteByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline DeleteByField(conn, tableName, field) =
|
|
||||||
WithProps.Delete.byFields tableName Any [ field ] (Sql.existingConnection conn)
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
namespace BitBadger.Documents.Postgres
|
namespace BitBadger.Documents.Postgres
|
||||||
|
|
||||||
|
open BitBadger.Documents
|
||||||
open Npgsql
|
open Npgsql
|
||||||
open Npgsql.FSharp
|
open Npgsql.FSharp
|
||||||
|
|
||||||
@@ -53,6 +54,11 @@ module Extensions =
|
|||||||
member conn.countByFields tableName howMatched fields =
|
member conn.countByFields tableName howMatched fields =
|
||||||
WithProps.Count.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Count.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Count matching documents using a JSON field comparison query (->> =)
|
||||||
|
[<System.Obsolete "Use countByFields instead; will be removed in v4">]
|
||||||
|
member conn.countByField tableName field =
|
||||||
|
conn.countByFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Count matching documents using a JSON containment query (@>)
|
/// Count matching documents using a JSON containment query (@>)
|
||||||
member conn.countByContains tableName criteria =
|
member conn.countByContains tableName criteria =
|
||||||
WithProps.Count.byContains tableName criteria (Sql.existingConnection conn)
|
WithProps.Count.byContains tableName criteria (Sql.existingConnection conn)
|
||||||
@@ -69,6 +75,11 @@ module Extensions =
|
|||||||
member conn.existsByFields tableName howMatched fields =
|
member conn.existsByFields tableName howMatched fields =
|
||||||
WithProps.Exists.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Exists.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Determine if documents exist using a JSON field comparison query (->> =)
|
||||||
|
[<System.Obsolete "Use existsByFields instead; will be removed in v4">]
|
||||||
|
member conn.existsByField tableName field =
|
||||||
|
conn.existsByFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON containment query (@>)
|
/// Determine if documents exist using a JSON containment query (@>)
|
||||||
member conn.existsByContains tableName criteria =
|
member conn.existsByContains tableName criteria =
|
||||||
WithProps.Exists.byContains tableName criteria (Sql.existingConnection conn)
|
WithProps.Exists.byContains tableName criteria (Sql.existingConnection conn)
|
||||||
@@ -81,10 +92,6 @@ module Extensions =
|
|||||||
member conn.findAll<'TDoc> tableName =
|
member conn.findAll<'TDoc> tableName =
|
||||||
WithProps.Find.all<'TDoc> tableName (Sql.existingConnection conn)
|
WithProps.Find.all<'TDoc> tableName (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
member conn.findAllOrdered<'TDoc> tableName orderFields =
|
|
||||||
WithProps.Find.allOrdered<'TDoc> tableName orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID; returns None if not found
|
/// Retrieve a document by its ID; returns None if not found
|
||||||
member conn.findById<'TKey, 'TDoc> tableName docId =
|
member conn.findById<'TKey, 'TDoc> tableName docId =
|
||||||
WithProps.Find.byId<'TKey, 'TDoc> tableName docId (Sql.existingConnection conn)
|
WithProps.Find.byId<'TKey, 'TDoc> tableName docId (Sql.existingConnection conn)
|
||||||
@@ -93,56 +100,36 @@ module Extensions =
|
|||||||
member conn.findByFields<'TDoc> tableName howMatched fields =
|
member conn.findByFields<'TDoc> tableName howMatched fields =
|
||||||
WithProps.Find.byFields<'TDoc> tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Find.byFields<'TDoc> tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
/// document
|
[<System.Obsolete "Use findByFields instead; will be removed in v4">]
|
||||||
member conn.findByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
member conn.findByField<'TDoc> tableName field =
|
||||||
WithProps.Find.byFieldsOrdered<'TDoc>
|
conn.findByFields<'TDoc> tableName Any [ field ]
|
||||||
tableName howMatched queryFields orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>)
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
member conn.findByContains<'TDoc> tableName (criteria: obj) =
|
member conn.findByContains<'TDoc> tableName (criteria: obj) =
|
||||||
WithProps.Find.byContains<'TDoc> tableName criteria (Sql.existingConnection conn)
|
WithProps.Find.byContains<'TDoc> tableName criteria (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
member conn.findByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields =
|
|
||||||
WithProps.Find.byContainsOrdered<'TDoc> tableName criteria orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?)
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
member conn.findByJsonPath<'TDoc> tableName jsonPath =
|
member conn.findByJsonPath<'TDoc> tableName jsonPath =
|
||||||
WithProps.Find.byJsonPath<'TDoc> tableName jsonPath (Sql.existingConnection conn)
|
WithProps.Find.byJsonPath<'TDoc> tableName jsonPath (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
member conn.findByJsonPathOrdered<'TDoc> tableName jsonPath orderFields =
|
|
||||||
WithProps.Find.byJsonPathOrdered<'TDoc> tableName jsonPath orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
||||||
member conn.findFirstByFields<'TDoc> tableName howMatched fields =
|
member conn.findFirstByFields<'TDoc> tableName howMatched fields =
|
||||||
WithProps.Find.firstByFields<'TDoc> tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Find.firstByFields<'TDoc> tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
||||||
/// the document; returns None if not found
|
[<System.Obsolete "Use findFirstByFields instead; will be removed in v4">]
|
||||||
member conn.findFirstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
member conn.findFirstByField<'TDoc> tableName field =
|
||||||
WithProps.Find.firstByFieldsOrdered<'TDoc>
|
conn.findFirstByFields<'TDoc> tableName Any [ field ]
|
||||||
tableName howMatched queryFields orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
member conn.findFirstByContains<'TDoc> tableName (criteria: obj) =
|
member conn.findFirstByContains<'TDoc> tableName (criteria: obj) =
|
||||||
WithProps.Find.firstByContains<'TDoc> tableName criteria (Sql.existingConnection conn)
|
WithProps.Find.firstByContains<'TDoc> tableName criteria (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the
|
|
||||||
/// document; returns None if not found
|
|
||||||
member conn.findFirstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields =
|
|
||||||
WithProps.Find.firstByContainsOrdered<'TDoc> tableName criteria orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
member conn.findFirstByJsonPath<'TDoc> tableName jsonPath =
|
member conn.findFirstByJsonPath<'TDoc> tableName jsonPath =
|
||||||
WithProps.Find.firstByJsonPath<'TDoc> tableName jsonPath (Sql.existingConnection conn)
|
WithProps.Find.firstByJsonPath<'TDoc> tableName jsonPath (Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
|
|
||||||
/// document; returns None if not found
|
|
||||||
member conn.findFirstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields =
|
|
||||||
WithProps.Find.firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields (Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Update an entire document by its ID
|
/// Update an entire document by its ID
|
||||||
member conn.updateById tableName (docId: 'TKey) (document: 'TDoc) =
|
member conn.updateById tableName (docId: 'TKey) (document: 'TDoc) =
|
||||||
WithProps.Update.byId tableName docId document (Sql.existingConnection conn)
|
WithProps.Update.byId tableName docId document (Sql.existingConnection conn)
|
||||||
@@ -159,6 +146,11 @@ module Extensions =
|
|||||||
member conn.patchByFields tableName howMatched fields (patch: 'TPatch) =
|
member conn.patchByFields tableName howMatched fields (patch: 'TPatch) =
|
||||||
WithProps.Patch.byFields tableName howMatched fields patch (Sql.existingConnection conn)
|
WithProps.Patch.byFields tableName howMatched fields patch (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<System.Obsolete "Use patchByFields instead; will be removed in v4">]
|
||||||
|
member conn.patchByField tableName field (patch: 'TPatch) =
|
||||||
|
conn.patchByFields tableName Any [ field ] patch
|
||||||
|
|
||||||
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
||||||
member conn.patchByContains tableName (criteria: 'TCriteria) (patch: 'TPatch) =
|
member conn.patchByContains tableName (criteria: 'TCriteria) (patch: 'TPatch) =
|
||||||
WithProps.Patch.byContains tableName criteria patch (Sql.existingConnection conn)
|
WithProps.Patch.byContains tableName criteria patch (Sql.existingConnection conn)
|
||||||
@@ -175,6 +167,11 @@ module Extensions =
|
|||||||
member conn.removeFieldsByFields tableName howMatched fields fieldNames =
|
member conn.removeFieldsByFields tableName howMatched fields fieldNames =
|
||||||
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (Sql.existingConnection conn)
|
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<System.Obsolete "Use removeFieldsByFields instead; will be removed in v4">]
|
||||||
|
member conn.removeFieldsByField tableName field fieldNames =
|
||||||
|
conn.removeFieldsByFields tableName Any [ field ] fieldNames
|
||||||
|
|
||||||
/// Remove fields from documents via a JSON containment query (@>)
|
/// Remove fields from documents via a JSON containment query (@>)
|
||||||
member conn.removeFieldsByContains tableName (criteria: 'TContains) fieldNames =
|
member conn.removeFieldsByContains tableName (criteria: 'TContains) fieldNames =
|
||||||
WithProps.RemoveFields.byContains tableName criteria fieldNames (Sql.existingConnection conn)
|
WithProps.RemoveFields.byContains tableName criteria fieldNames (Sql.existingConnection conn)
|
||||||
@@ -190,6 +187,11 @@ module Extensions =
|
|||||||
member conn.deleteByFields tableName howMatched fields =
|
member conn.deleteByFields tableName howMatched fields =
|
||||||
WithProps.Delete.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Delete.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
|
[<System.Obsolete "Use deleteByFields instead; will be removed in v4">]
|
||||||
|
member conn.deleteByField tableName field =
|
||||||
|
conn.deleteByFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Delete documents by matching a JSON containment query (@>)
|
/// Delete documents by matching a JSON containment query (@>)
|
||||||
member conn.deleteByContains tableName (criteria: 'TContains) =
|
member conn.deleteByContains tableName (criteria: 'TContains) =
|
||||||
WithProps.Delete.byContains tableName criteria (Sql.existingConnection conn)
|
WithProps.Delete.byContains tableName criteria (Sql.existingConnection conn)
|
||||||
@@ -261,6 +263,12 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline CountByFields(conn, tableName, howMatched, fields) =
|
static member inline CountByFields(conn, tableName, howMatched, fields) =
|
||||||
WithProps.Count.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Count.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Count matching documents using a JSON field comparison query (->> =)
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use CountByFields instead; will be removed in v4">]
|
||||||
|
static member inline CountByField(conn, tableName, field) =
|
||||||
|
conn.CountByFields(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Count matching documents using a JSON containment query (@>)
|
/// Count matching documents using a JSON containment query (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline CountByContains(conn, tableName, criteria: 'TCriteria) =
|
static member inline CountByContains(conn, tableName, criteria: 'TCriteria) =
|
||||||
@@ -281,6 +289,12 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline ExistsByFields(conn, tableName, howMatched, fields) =
|
static member inline ExistsByFields(conn, tableName, howMatched, fields) =
|
||||||
WithProps.Exists.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Exists.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Determine if documents exist using a JSON field comparison query (->> =)
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use ExistsByFields instead; will be removed in v4">]
|
||||||
|
static member inline ExistsByField(conn, tableName, field) =
|
||||||
|
conn.ExistsByFields(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON containment query (@>)
|
/// Determine if documents exist using a JSON containment query (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline ExistsByContains(conn, tableName, criteria: 'TCriteria) =
|
static member inline ExistsByContains(conn, tableName, criteria: 'TCriteria) =
|
||||||
@@ -296,11 +310,6 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline FindAll<'TDoc>(conn, tableName) =
|
static member inline FindAll<'TDoc>(conn, tableName) =
|
||||||
WithProps.Find.All<'TDoc>(tableName, Sql.existingConnection conn)
|
WithProps.Find.All<'TDoc>(tableName, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindAllOrdered<'TDoc>(conn, tableName, orderFields) =
|
|
||||||
WithProps.Find.AllOrdered<'TDoc>(tableName, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID; returns None if not found
|
/// Retrieve a document by its ID; returns None if not found
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindById<'TKey, 'TDoc when 'TDoc: null>(conn, tableName, docId: 'TKey) =
|
static member inline FindById<'TKey, 'TDoc when 'TDoc: null>(conn, tableName, docId: 'TKey) =
|
||||||
@@ -311,68 +320,43 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline FindByFields<'TDoc>(conn, tableName, howMatched, fields) =
|
static member inline FindByFields<'TDoc>(conn, tableName, howMatched, fields) =
|
||||||
WithProps.Find.ByFields<'TDoc>(tableName, howMatched, fields, Sql.existingConnection conn)
|
WithProps.Find.ByFields<'TDoc>(tableName, howMatched, fields, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the document
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindByFieldsOrdered<'TDoc>(conn, tableName, howMatched, queryFields, orderFields) =
|
[<System.Obsolete "Use FindByFields instead; will be removed in v4">]
|
||||||
WithProps.Find.ByFieldsOrdered<'TDoc>(
|
static member inline FindByField<'TDoc>(conn, tableName, field) =
|
||||||
tableName, howMatched, queryFields, orderFields, Sql.existingConnection conn)
|
conn.FindByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>)
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindByContains<'TDoc>(conn, tableName, criteria: obj) =
|
static member inline FindByContains<'TDoc>(conn, tableName, criteria: obj) =
|
||||||
WithProps.Find.ByContains<'TDoc>(tableName, criteria, Sql.existingConnection conn)
|
WithProps.Find.ByContains<'TDoc>(tableName, criteria, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindByContainsOrdered<'TDoc>(conn, tableName, criteria: obj, orderFields) =
|
|
||||||
WithProps.Find.ByContainsOrdered<'TDoc>(tableName, criteria, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?)
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindByJsonPath<'TDoc>(conn, tableName, jsonPath) =
|
static member inline FindByJsonPath<'TDoc>(conn, tableName, jsonPath) =
|
||||||
WithProps.Find.ByJsonPath<'TDoc>(tableName, jsonPath, Sql.existingConnection conn)
|
WithProps.Find.ByJsonPath<'TDoc>(tableName, jsonPath, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindByJsonPathOrdered<'TDoc>(conn, tableName, jsonPath, orderFields) =
|
|
||||||
WithProps.Find.ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByFields<'TDoc when 'TDoc: null>(conn, tableName, howMatched, fields) =
|
static member inline FindFirstByFields<'TDoc when 'TDoc: null>(conn, tableName, howMatched, fields) =
|
||||||
WithProps.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, Sql.existingConnection conn)
|
WithProps.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in the
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
||||||
/// document; returns null if not found
|
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByFieldsOrdered<'TDoc when 'TDoc: null>(
|
[<System.Obsolete "Use FindFirstByFields instead; will be removed in v4">]
|
||||||
conn, tableName, howMatched, queryFields, orderFields) =
|
static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, field) =
|
||||||
WithProps.Find.FirstByFieldsOrdered<'TDoc>(
|
conn.FindFirstByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
tableName, howMatched, queryFields, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByContains<'TDoc when 'TDoc: null>(conn, tableName, criteria: obj) =
|
static member inline FindFirstByContains<'TDoc when 'TDoc: null>(conn, tableName, criteria: obj) =
|
||||||
WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, Sql.existingConnection conn)
|
WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the document;
|
|
||||||
/// returns None if not found
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindFirstByContainsOrdered<'TDoc when 'TDoc: null>(
|
|
||||||
conn, tableName, criteria: obj, orderFields) =
|
|
||||||
WithProps.Find.FirstByContainsOrdered<'TDoc>(tableName, criteria, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByJsonPath<'TDoc when 'TDoc: null>(conn, tableName, jsonPath) =
|
static member inline FindFirstByJsonPath<'TDoc when 'TDoc: null>(conn, tableName, jsonPath) =
|
||||||
WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, Sql.existingConnection conn)
|
WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, Sql.existingConnection conn)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the document;
|
|
||||||
/// returns None if not found
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindFirstByJsonPathOrdered<'TDoc when 'TDoc: null>(conn, tableName, jsonPath, orderFields) =
|
|
||||||
WithProps.Find.FirstByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, Sql.existingConnection conn)
|
|
||||||
|
|
||||||
/// Update an entire document by its ID
|
/// Update an entire document by its ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline UpdateById(conn, tableName, docId: 'TKey, document: 'TDoc) =
|
static member inline UpdateById(conn, tableName, docId: 'TKey, document: 'TDoc) =
|
||||||
@@ -393,6 +377,12 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline PatchByFields(conn, tableName, howMatched, fields, patch: 'TPatch) =
|
static member inline PatchByFields(conn, tableName, howMatched, fields, patch: 'TPatch) =
|
||||||
WithProps.Patch.byFields tableName howMatched fields patch (Sql.existingConnection conn)
|
WithProps.Patch.byFields tableName howMatched fields patch (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use PatchByFields instead; will be removed in v4">]
|
||||||
|
static member inline PatchByField(conn, tableName, field, patch: 'TPatch) =
|
||||||
|
conn.PatchByFields(tableName, Any, [ field ], patch)
|
||||||
|
|
||||||
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline PatchByContains(conn, tableName, criteria: 'TCriteria, patch: 'TPatch) =
|
static member inline PatchByContains(conn, tableName, criteria: 'TCriteria, patch: 'TPatch) =
|
||||||
@@ -413,6 +403,12 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline RemoveFieldsByFields(conn, tableName, howMatched, fields, fieldNames) =
|
static member inline RemoveFieldsByFields(conn, tableName, howMatched, fields, fieldNames) =
|
||||||
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (Sql.existingConnection conn)
|
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use RemoveFieldsByFields instead; will be removed in v4">]
|
||||||
|
static member inline RemoveFieldsByField(conn, tableName, field, fieldNames) =
|
||||||
|
conn.RemoveFieldsByFields(tableName, Any, [ field ], fieldNames)
|
||||||
|
|
||||||
/// Remove fields from documents via a JSON containment query (@>)
|
/// Remove fields from documents via a JSON containment query (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline RemoveFieldsByContains(conn, tableName, criteria: 'TContains, fieldNames) =
|
static member inline RemoveFieldsByContains(conn, tableName, criteria: 'TContains, fieldNames) =
|
||||||
@@ -433,6 +429,12 @@ type NpgsqlConnectionCSharpExtensions =
|
|||||||
static member inline DeleteByFields(conn, tableName, howMatched, fields) =
|
static member inline DeleteByFields(conn, tableName, howMatched, fields) =
|
||||||
WithProps.Delete.byFields tableName howMatched fields (Sql.existingConnection conn)
|
WithProps.Delete.byFields tableName howMatched fields (Sql.existingConnection conn)
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use DeleteByFields instead; will be removed in v4">]
|
||||||
|
static member inline DeleteByField(conn, tableName, field) =
|
||||||
|
conn.DeleteByFields(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Delete documents by matching a JSON containment query (@>)
|
/// Delete documents by matching a JSON containment query (@>)
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline DeleteByContains(conn, tableName, criteria: 'TContains) =
|
static member inline DeleteByContains(conn, tableName, criteria: 'TContains) =
|
||||||
|
|||||||
@@ -104,12 +104,24 @@ module Parameters =
|
|||||||
|> Seq.toList
|
|> Seq.toList
|
||||||
|> Seq.ofList
|
|> Seq.ofList
|
||||||
|
|
||||||
|
/// Create a JSON field parameter
|
||||||
|
[<CompiledName "AddField">]
|
||||||
|
[<System.Obsolete "Use addFieldParams (F#) / AddFields (C#) instead; will be removed in v4">]
|
||||||
|
let addFieldParam name field parameters =
|
||||||
|
addFieldParams [ { field with ParameterName = Some name } ] parameters
|
||||||
|
|
||||||
/// Append JSON field name parameters for the given field names to the given parameters
|
/// Append JSON field name parameters for the given field names to the given parameters
|
||||||
[<CompiledName "FieldNames">]
|
[<CompiledName "FieldNames">]
|
||||||
let fieldNameParams (fieldNames: string seq) =
|
let fieldNameParams (fieldNames: string seq) =
|
||||||
if Seq.length fieldNames = 1 then "@name", Sql.string (Seq.head fieldNames)
|
if Seq.length fieldNames = 1 then "@name", Sql.string (Seq.head fieldNames)
|
||||||
else "@name", Sql.stringArray (Array.ofSeq fieldNames)
|
else "@name", Sql.stringArray (Array.ofSeq fieldNames)
|
||||||
|
|
||||||
|
/// Append JSON field name parameters for the given field names to the given parameters
|
||||||
|
[<CompiledName "FieldName">]
|
||||||
|
[<System.Obsolete "Use fieldNameParams (F#) / FieldNames (C#) instead; will be removed in v4">]
|
||||||
|
let fieldNameParam fieldNames =
|
||||||
|
fieldNameParams fieldNames
|
||||||
|
|
||||||
/// An empty parameter sequence
|
/// An empty parameter sequence
|
||||||
[<CompiledName "None">]
|
[<CompiledName "None">]
|
||||||
let noParams =
|
let noParams =
|
||||||
@@ -312,23 +324,7 @@ module WithProps =
|
|||||||
/// Insert a new document
|
/// Insert a new document
|
||||||
[<CompiledName "Insert">]
|
[<CompiledName "Insert">]
|
||||||
let insert<'TDoc> tableName (document: 'TDoc) sqlProps =
|
let insert<'TDoc> tableName (document: 'TDoc) sqlProps =
|
||||||
let query =
|
Custom.nonQuery (Query.insert tableName) [ jsonParam "@data" document ] sqlProps
|
||||||
match Configuration.autoIdStrategy () with
|
|
||||||
| Disabled -> Query.insert tableName
|
|
||||||
| strategy ->
|
|
||||||
let idField = Configuration.idField ()
|
|
||||||
let dataParam =
|
|
||||||
if AutoId.NeedsAutoId strategy document idField then
|
|
||||||
match strategy with
|
|
||||||
| Number ->
|
|
||||||
$"' || (SELECT COALESCE(MAX((data->>'{idField}')::numeric), 0) + 1 FROM {tableName}) || '"
|
|
||||||
| Guid -> $"\"{AutoId.GenerateGuid()}\""
|
|
||||||
| RandomString -> $"\"{AutoId.GenerateRandomString(Configuration.idStringLength ())}\""
|
|
||||||
| Disabled -> "@data"
|
|
||||||
|> function it -> $"""@data::jsonb || ('{{"{idField}":{it}}}')::jsonb"""
|
|
||||||
else "@data"
|
|
||||||
(Query.insert tableName).Replace("@data", dataParam)
|
|
||||||
Custom.nonQuery query [ jsonParam "@data" document ] sqlProps
|
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
[<CompiledName "Save">]
|
[<CompiledName "Save">]
|
||||||
@@ -411,16 +407,6 @@ module WithProps =
|
|||||||
let All<'TDoc>(tableName, sqlProps) =
|
let All<'TDoc>(tableName, sqlProps) =
|
||||||
Custom.List<'TDoc>(Query.find tableName, [], fromData<'TDoc>, sqlProps)
|
Custom.List<'TDoc>(Query.find tableName, [], fromData<'TDoc>, sqlProps)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpAllOrdered">]
|
|
||||||
let allOrdered<'TDoc> tableName orderFields sqlProps =
|
|
||||||
Custom.list<'TDoc> (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] fromData<'TDoc> sqlProps
|
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
let AllOrdered<'TDoc>(tableName, orderFields, sqlProps) =
|
|
||||||
Custom.List<'TDoc>(
|
|
||||||
Query.find tableName + Query.orderBy orderFields PostgreSQL, [], fromData<'TDoc>, sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID (returns None if not found)
|
/// Retrieve a document by its ID (returns None if not found)
|
||||||
[<CompiledName "FSharpById">]
|
[<CompiledName "FSharpById">]
|
||||||
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) sqlProps =
|
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) sqlProps =
|
||||||
@@ -440,6 +426,12 @@ module WithProps =
|
|||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON field comparison (->> =)
|
||||||
|
[<CompiledName "FSharpByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField<'TDoc> tableName field sqlProps =
|
||||||
|
byFields<'TDoc> tableName Any [ field ] sqlProps
|
||||||
|
|
||||||
/// Retrieve documents matching JSON field comparisons (->> =)
|
/// Retrieve documents matching JSON field comparisons (->> =)
|
||||||
let ByFields<'TDoc>(tableName, howMatched, fields, sqlProps) =
|
let ByFields<'TDoc>(tableName, howMatched, fields, sqlProps) =
|
||||||
Custom.List<'TDoc>(
|
Custom.List<'TDoc>(
|
||||||
@@ -448,22 +440,10 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve documents matching JSON field comparisons (->> =) ordered by the given fields in the document
|
/// Retrieve documents matching a JSON field comparison (->> =)
|
||||||
[<CompiledName "FSharpByFieldsOrdered">]
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
|
let ByField<'TDoc>(tableName, field, sqlProps) =
|
||||||
Custom.list<'TDoc>
|
ByFields<'TDoc>(tableName, Any, Seq.singleton field, sqlProps)
|
||||||
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL)
|
|
||||||
(addFieldParams queryFields [])
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve documents matching JSON field comparisons (->> =) ordered by the given fields in the document
|
|
||||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, sqlProps) =
|
|
||||||
Custom.List<'TDoc>(
|
|
||||||
Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL,
|
|
||||||
addFieldParams queryFields [],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>)
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
[<CompiledName "FSharpByContains">]
|
[<CompiledName "FSharpByContains">]
|
||||||
@@ -479,23 +459,6 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpByContainsOrdered">]
|
|
||||||
let byContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
|
|
||||||
Custom.list<'TDoc>
|
|
||||||
(Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
|
||||||
[ jsonParam "@criteria" criteria ]
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
let ByContainsOrdered<'TDoc>(tableName, criteria: obj, orderFields, sqlProps) =
|
|
||||||
Custom.List<'TDoc>(
|
|
||||||
Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL,
|
|
||||||
[ jsonParam "@criteria" criteria ],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?)
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
[<CompiledName "FSharpByJsonPath">]
|
[<CompiledName "FSharpByJsonPath">]
|
||||||
let byJsonPath<'TDoc> tableName jsonPath sqlProps =
|
let byJsonPath<'TDoc> tableName jsonPath sqlProps =
|
||||||
@@ -510,23 +473,6 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpByJsonPathOrdered">]
|
|
||||||
let byJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
|
|
||||||
Custom.list<'TDoc>
|
|
||||||
(Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
|
|
||||||
[ "@path", Sql.string jsonPath ]
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
let ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, sqlProps) =
|
|
||||||
Custom.List<'TDoc>(
|
|
||||||
Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL,
|
|
||||||
[ "@path", Sql.string jsonPath ],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching JSON field comparisons (->> =); returns None if not found
|
/// Retrieve the first document matching JSON field comparisons (->> =); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByFields">]
|
[<CompiledName "FSharpFirstByFields">]
|
||||||
let firstByFields<'TDoc> tableName howMatched fields sqlProps =
|
let firstByFields<'TDoc> tableName howMatched fields sqlProps =
|
||||||
@@ -536,6 +482,12 @@ module WithProps =
|
|||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
sqlProps
|
sqlProps
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
[<System.Obsolete "Use firstByFields instead; will be removed in v4">]
|
||||||
|
let firstByField<'TDoc> tableName field sqlProps =
|
||||||
|
firstByFields<'TDoc> tableName Any [ field ] sqlProps
|
||||||
|
|
||||||
/// Retrieve the first document matching JSON field comparisons (->> =); returns null if not found
|
/// Retrieve the first document matching JSON field comparisons (->> =); returns null if not found
|
||||||
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields, sqlProps) =
|
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields, sqlProps) =
|
||||||
Custom.Single<'TDoc>(
|
Custom.Single<'TDoc>(
|
||||||
@@ -544,24 +496,10 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve the first document matching JSON field comparisons (->> =) ordered by the given fields in the
|
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
||||||
/// document; returns None if not found
|
[<System.Obsolete "Use FirstByFields instead; will be removed in v4">]
|
||||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, field, sqlProps) =
|
||||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
|
FirstByFields<'TDoc>(tableName, Any, Seq.singleton field, sqlProps)
|
||||||
Custom.single<'TDoc>
|
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
|
||||||
(addFieldParams queryFields [])
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve the first document matching JSON field comparisons (->> =) ordered by the given fields in the
|
|
||||||
/// document; returns null if not found
|
|
||||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null>(tableName, howMatched, queryFields, orderFields, sqlProps) =
|
|
||||||
Custom.Single<'TDoc>(
|
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
|
||||||
addFieldParams queryFields [],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByContains">]
|
[<CompiledName "FSharpFirstByContains">]
|
||||||
@@ -580,25 +518,6 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the
|
|
||||||
/// document; returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByContainsOrdered">]
|
|
||||||
let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
|
|
||||||
Custom.single<'TDoc>
|
|
||||||
$"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
|
||||||
[ jsonParam "@criteria" criteria ]
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the
|
|
||||||
/// document; returns null if not found
|
|
||||||
let FirstByContainsOrdered<'TDoc when 'TDoc: null>(tableName, criteria: obj, orderFields, sqlProps) =
|
|
||||||
Custom.Single<'TDoc>(
|
|
||||||
$"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
|
||||||
[ jsonParam "@criteria" criteria ],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByJsonPath">]
|
[<CompiledName "FSharpFirstByJsonPath">]
|
||||||
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
|
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
|
||||||
@@ -616,25 +535,6 @@ module WithProps =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
sqlProps)
|
sqlProps)
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
|
|
||||||
/// document; returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByJsonPathOrdered">]
|
|
||||||
let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
|
|
||||||
Custom.single<'TDoc>
|
|
||||||
$"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1"
|
|
||||||
[ "@path", Sql.string jsonPath ]
|
|
||||||
fromData<'TDoc>
|
|
||||||
sqlProps
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
|
|
||||||
/// document; returns null if not found
|
|
||||||
let FirstByJsonPathOrdered<'TDoc when 'TDoc: null>(tableName, jsonPath, orderFields, sqlProps) =
|
|
||||||
Custom.Single<'TDoc>(
|
|
||||||
$"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1",
|
|
||||||
[ "@path", Sql.string jsonPath ],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
sqlProps)
|
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Update =
|
module Update =
|
||||||
@@ -672,6 +572,12 @@ module WithProps =
|
|||||||
(addFieldParams fields [ jsonParam "@data" patch ])
|
(addFieldParams fields [ jsonParam "@data" patch ])
|
||||||
sqlProps
|
sqlProps
|
||||||
|
|
||||||
|
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field (patch: 'TPatch) sqlProps =
|
||||||
|
byFields tableName Any [ field ] patch sqlProps
|
||||||
|
|
||||||
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TContains) (patch: 'TPatch) sqlProps =
|
let byContains tableName (criteria: 'TContains) (patch: 'TPatch) sqlProps =
|
||||||
@@ -706,6 +612,12 @@ module WithProps =
|
|||||||
(addFieldParams fields [ fieldNameParams fieldNames ])
|
(addFieldParams fields [ fieldNameParams fieldNames ])
|
||||||
sqlProps
|
sqlProps
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field fieldNames sqlProps =
|
||||||
|
byFields tableName Any [ field ] fieldNames sqlProps
|
||||||
|
|
||||||
/// Remove fields from documents via a JSON containment query (@>)
|
/// Remove fields from documents via a JSON containment query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TContains) fieldNames sqlProps =
|
let byContains tableName (criteria: 'TContains) fieldNames sqlProps =
|
||||||
@@ -737,6 +649,12 @@ module WithProps =
|
|||||||
Custom.nonQuery
|
Custom.nonQuery
|
||||||
(Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) sqlProps
|
(Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) sqlProps
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field sqlProps =
|
||||||
|
byFields tableName Any [ field ] sqlProps
|
||||||
|
|
||||||
/// Delete documents by matching a JSON contains query (@>)
|
/// Delete documents by matching a JSON contains query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TCriteria) sqlProps =
|
let byContains tableName (criteria: 'TCriteria) sqlProps =
|
||||||
@@ -834,6 +752,12 @@ module Count =
|
|||||||
let byFields tableName howMatched fields =
|
let byFields tableName howMatched fields =
|
||||||
WithProps.Count.byFields tableName howMatched fields (fromDataSource ())
|
WithProps.Count.byFields tableName howMatched fields (fromDataSource ())
|
||||||
|
|
||||||
|
/// Count matching documents using a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Count matching documents using a JSON containment query (@>)
|
/// Count matching documents using a JSON containment query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName criteria =
|
let byContains tableName criteria =
|
||||||
@@ -859,6 +783,12 @@ module Exists =
|
|||||||
let byFields tableName howMatched fields =
|
let byFields tableName howMatched fields =
|
||||||
WithProps.Exists.byFields tableName howMatched fields (fromDataSource ())
|
WithProps.Exists.byFields tableName howMatched fields (fromDataSource ())
|
||||||
|
|
||||||
|
/// Determine if documents exist using a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON containment query (@>)
|
/// Determine if documents exist using a JSON containment query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName criteria =
|
let byContains tableName criteria =
|
||||||
@@ -883,15 +813,6 @@ module Find =
|
|||||||
let All<'TDoc> tableName =
|
let All<'TDoc> tableName =
|
||||||
WithProps.Find.All<'TDoc>(tableName, fromDataSource ())
|
WithProps.Find.All<'TDoc>(tableName, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpAllOrdered">]
|
|
||||||
let allOrdered<'TDoc> tableName orderFields =
|
|
||||||
WithProps.Find.allOrdered<'TDoc> tableName orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
let AllOrdered<'TDoc> tableName orderFields =
|
|
||||||
WithProps.Find.AllOrdered<'TDoc>(tableName, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID; returns None if not found
|
/// Retrieve a document by its ID; returns None if not found
|
||||||
[<CompiledName "FSharpById">]
|
[<CompiledName "FSharpById">]
|
||||||
let byId<'TKey, 'TDoc> tableName docId =
|
let byId<'TKey, 'TDoc> tableName docId =
|
||||||
@@ -906,18 +827,20 @@ module Find =
|
|||||||
let byFields<'TDoc> tableName howMatched fields =
|
let byFields<'TDoc> tableName howMatched fields =
|
||||||
WithProps.Find.byFields<'TDoc> tableName howMatched fields (fromDataSource ())
|
WithProps.Find.byFields<'TDoc> tableName howMatched fields (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "FSharpByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField<'TDoc> tableName field =
|
||||||
|
byFields<'TDoc> tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =)
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
let ByFields<'TDoc>(tableName, howMatched, fields) =
|
let ByFields<'TDoc>(tableName, howMatched, fields) =
|
||||||
WithProps.Find.ByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ())
|
WithProps.Find.ByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the document
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
[<CompiledName "FSharpByFieldsOrdered">]
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
let byFieldsOrdered<'TDoc> tableName howMatched queryField orderFields =
|
let ByField<'TDoc>(tableName, field) =
|
||||||
WithProps.Find.byFieldsOrdered<'TDoc> tableName howMatched queryField orderFields (fromDataSource ())
|
ByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the document
|
|
||||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields) =
|
|
||||||
WithProps.Find.ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>)
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
[<CompiledName "FSharpByContains">]
|
[<CompiledName "FSharpByContains">]
|
||||||
@@ -928,15 +851,6 @@ module Find =
|
|||||||
let ByContains<'TDoc>(tableName, criteria: obj) =
|
let ByContains<'TDoc>(tableName, criteria: obj) =
|
||||||
WithProps.Find.ByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
WithProps.Find.ByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpByContainsOrdered">]
|
|
||||||
let byContainsOrdered<'TDoc> tableName (criteria: obj) orderFields =
|
|
||||||
WithProps.Find.byContainsOrdered<'TDoc> tableName criteria orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
|
|
||||||
let ByContainsOrdered<'TDoc>(tableName, criteria: obj, orderFields) =
|
|
||||||
WithProps.Find.ByContainsOrdered<'TDoc>(tableName, criteria, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?)
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
[<CompiledName "FSharpByJsonPath">]
|
[<CompiledName "FSharpByJsonPath">]
|
||||||
let byJsonPath<'TDoc> tableName jsonPath =
|
let byJsonPath<'TDoc> tableName jsonPath =
|
||||||
@@ -946,34 +860,25 @@ module Find =
|
|||||||
let ByJsonPath<'TDoc>(tableName, jsonPath) =
|
let ByJsonPath<'TDoc>(tableName, jsonPath) =
|
||||||
WithProps.Find.ByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
WithProps.Find.ByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpByJsonPathOrdered">]
|
|
||||||
let byJsonPathOrdered<'TDoc> tableName jsonPath orderFields =
|
|
||||||
WithProps.Find.byJsonPathOrdered<'TDoc> tableName jsonPath orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
|
|
||||||
let ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields) =
|
|
||||||
WithProps.Find.ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByFields">]
|
[<CompiledName "FSharpFirstByFields">]
|
||||||
let firstByFields<'TDoc> tableName howMatched fields =
|
let firstByFields<'TDoc> tableName howMatched fields =
|
||||||
WithProps.Find.firstByFields<'TDoc> tableName howMatched fields (fromDataSource ())
|
WithProps.Find.firstByFields<'TDoc> tableName howMatched fields (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
[<System.Obsolete "Use firstByFields instead; will be removed in v4">]
|
||||||
|
let firstByField<'TDoc> tableName field =
|
||||||
|
firstByFields<'TDoc> tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
||||||
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields) =
|
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields) =
|
||||||
WithProps.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ())
|
WithProps.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in the
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
||||||
/// document; returns None if not found
|
[<System.Obsolete "Use FirstByFields instead; will be removed in v4">]
|
||||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, field) =
|
||||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
FirstByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
||||||
WithProps.Find.firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in the
|
|
||||||
/// document; returns null if not found
|
|
||||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null>(tableName, howMatched, queryFields, orderFields) =
|
|
||||||
WithProps.Find.FirstByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByContains">]
|
[<CompiledName "FSharpFirstByContains">]
|
||||||
@@ -984,17 +889,6 @@ module Find =
|
|||||||
let FirstByContains<'TDoc when 'TDoc: null>(tableName, criteria: obj) =
|
let FirstByContains<'TDoc when 'TDoc: null>(tableName, criteria: obj) =
|
||||||
WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the document;
|
|
||||||
/// returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByContainsOrdered">]
|
|
||||||
let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields =
|
|
||||||
WithProps.Find.firstByContainsOrdered<'TDoc> tableName criteria orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the document;
|
|
||||||
/// returns null if not found
|
|
||||||
let FirstByContainsOrdered<'TDoc when 'TDoc: null>(tableName, criteria: obj, orderFields) =
|
|
||||||
WithProps.Find.FirstByContainsOrdered<'TDoc>(tableName, criteria, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
[<CompiledName "FSharpFirstByJsonPath">]
|
[<CompiledName "FSharpFirstByJsonPath">]
|
||||||
let firstByJsonPath<'TDoc> tableName jsonPath =
|
let firstByJsonPath<'TDoc> tableName jsonPath =
|
||||||
@@ -1004,17 +898,6 @@ module Find =
|
|||||||
let FirstByJsonPath<'TDoc when 'TDoc: null>(tableName, jsonPath) =
|
let FirstByJsonPath<'TDoc when 'TDoc: null>(tableName, jsonPath) =
|
||||||
WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the document;
|
|
||||||
/// returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByJsonPathOrdered">]
|
|
||||||
let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields =
|
|
||||||
WithProps.Find.firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields (fromDataSource ())
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the document;
|
|
||||||
/// returns null if not found
|
|
||||||
let FirstByJsonPathOrdered<'TDoc when 'TDoc: null>(tableName, jsonPath, orderFields) =
|
|
||||||
WithProps.Find.FirstByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, fromDataSource ())
|
|
||||||
|
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -1049,6 +932,12 @@ module Patch =
|
|||||||
let byFields tableName howMatched fields (patch: 'TPatch) =
|
let byFields tableName howMatched fields (patch: 'TPatch) =
|
||||||
WithProps.Patch.byFields tableName howMatched fields patch (fromDataSource ())
|
WithProps.Patch.byFields tableName howMatched fields patch (fromDataSource ())
|
||||||
|
|
||||||
|
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field (patch: 'TPatch) =
|
||||||
|
byFields tableName Any [ field ] patch
|
||||||
|
|
||||||
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
/// Patch documents using a JSON containment query in the WHERE clause (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TCriteria) (patch: 'TPatch) =
|
let byContains tableName (criteria: 'TCriteria) (patch: 'TPatch) =
|
||||||
@@ -1074,6 +963,12 @@ module RemoveFields =
|
|||||||
let byFields tableName howMatched fields fieldNames =
|
let byFields tableName howMatched fields fieldNames =
|
||||||
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (fromDataSource ())
|
WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (fromDataSource ())
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field fieldNames =
|
||||||
|
byFields tableName Any [ field ] fieldNames
|
||||||
|
|
||||||
/// Remove fields from documents via a JSON containment query (@>)
|
/// Remove fields from documents via a JSON containment query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TContains) fieldNames =
|
let byContains tableName (criteria: 'TContains) fieldNames =
|
||||||
@@ -1099,6 +994,12 @@ module Delete =
|
|||||||
let byFields tableName howMatched fields =
|
let byFields tableName howMatched fields =
|
||||||
WithProps.Delete.byFields tableName howMatched fields (fromDataSource ())
|
WithProps.Delete.byFields tableName howMatched fields (fromDataSource ())
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Delete documents by matching a JSON containment query (@>)
|
/// Delete documents by matching a JSON containment query (@>)
|
||||||
[<CompiledName "ByContains">]
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName (criteria: 'TContains) =
|
let byContains tableName (criteria: 'TContains) =
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Library.fs" />
|
<Compile Include="Library.fs" />
|
||||||
<Compile Include="Extensions.fs" />
|
<Compile Include="Extensions.fs" />
|
||||||
<Compile Include="Compat.fs" />
|
|
||||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,269 +0,0 @@
|
|||||||
namespace BitBadger.Documents.Sqlite.Compat
|
|
||||||
|
|
||||||
open BitBadger.Documents
|
|
||||||
open BitBadger.Documents.Sqlite
|
|
||||||
|
|
||||||
[<AutoOpen>]
|
|
||||||
module Parameters =
|
|
||||||
|
|
||||||
/// Create a JSON field parameter
|
|
||||||
[<CompiledName "AddField">]
|
|
||||||
[<System.Obsolete "Use addFieldParams (F#) / AddFields (C#) instead ~ will be removed in v4.1">]
|
|
||||||
let addFieldParam name field parameters =
|
|
||||||
addFieldParams [ { field with ParameterName = Some name } ] parameters
|
|
||||||
|
|
||||||
/// Append JSON field name parameters for the given field names to the given parameters
|
|
||||||
[<CompiledName "FieldName">]
|
|
||||||
[<System.Obsolete "Use fieldNameParams (F#) / FieldNames (C#) instead ~ will be removed in v4.1">]
|
|
||||||
let fieldNameParam fieldNames =
|
|
||||||
fieldNameParams fieldNames
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Query =
|
|
||||||
|
|
||||||
/// Create a WHERE clause fragment to implement a comparison on a field in a JSON document
|
|
||||||
[<CompiledName "WhereByField">]
|
|
||||||
[<System.Obsolete "Use WhereByFields instead ~ will be removed in v4.1">]
|
|
||||||
let whereByField field paramName =
|
|
||||||
Query.whereByFields Any [ { field with ParameterName = Some paramName } ]
|
|
||||||
|
|
||||||
|
|
||||||
module WithConn =
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Count =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field conn =
|
|
||||||
WithConn.Count.byFields tableName Any [ field ] conn
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Exists =
|
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field conn =
|
|
||||||
WithConn.Exists.byFields tableName Any [ field ] conn
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Find =
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "FSharpByField">]
|
|
||||||
[<System.Obsolete "Use byFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField<'TDoc> tableName field conn =
|
|
||||||
WithConn.Find.byFields<'TDoc> tableName Any [ field ] conn
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let ByField<'TDoc>(tableName, field, conn) =
|
|
||||||
WithConn.Find.ByFields<'TDoc>(tableName, Any, Seq.singleton field, conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByField">]
|
|
||||||
[<System.Obsolete "Use firstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let firstByField<'TDoc> tableName field conn =
|
|
||||||
WithConn.Find.firstByFields<'TDoc> tableName Any [ field ] conn
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
|
||||||
[<System.Obsolete "Use FirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let FirstByField<'TDoc when 'TDoc: null>(tableName, field, conn) =
|
|
||||||
WithConn.Find.FirstByFields<'TDoc>(tableName, Any, Seq.singleton field, conn)
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Patch =
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field (patch: 'TPatch) conn =
|
|
||||||
WithConn.Patch.byFields tableName Any [ field ] patch conn
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module RemoveFields =
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field fieldNames conn =
|
|
||||||
WithConn.RemoveFields.byFields tableName Any [ field ] fieldNames conn
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Delete =
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field conn =
|
|
||||||
WithConn.Delete.byFields tableName Any [ field ] conn
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Count =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Count.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Exists =
|
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Exists.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Find =
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<CompiledName "FSharpByField">]
|
|
||||||
[<System.Obsolete "Use byFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField<'TDoc> tableName field =
|
|
||||||
Find.byFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison (->> =)
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let ByField<'TDoc>(tableName, field) =
|
|
||||||
Find.ByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
|
||||||
[<CompiledName "FSharpFirstByField">]
|
|
||||||
[<System.Obsolete "Use firstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let firstByField<'TDoc> tableName field =
|
|
||||||
Find.firstByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
|
||||||
[<System.Obsolete "Use FirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
let FirstByField<'TDoc when 'TDoc: null>(tableName, field) =
|
|
||||||
Find.FirstByFields<'TDoc>(tableName, Any, Seq.singleton field)
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Patch =
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field (patch: 'TPatch) =
|
|
||||||
Patch.byFields tableName Any [ field ] patch
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module RemoveFields =
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field fieldNames =
|
|
||||||
RemoveFields.byFields tableName Any [ field ] fieldNames
|
|
||||||
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Delete =
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<CompiledName "ByField">]
|
|
||||||
[<System.Obsolete "Use ByFields instead ~ will be removed in v4.1">]
|
|
||||||
let byField tableName field =
|
|
||||||
Delete.byFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
open Microsoft.Data.Sqlite
|
|
||||||
|
|
||||||
/// F# Extensions for the NpgsqlConnection type
|
|
||||||
[<AutoOpen>]
|
|
||||||
module Extensions =
|
|
||||||
|
|
||||||
type SqliteConnection with
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use countByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.countByField tableName field =
|
|
||||||
conn.countByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use existsByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.existsByField tableName field =
|
|
||||||
conn.existsByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use findByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.findByField<'TDoc> tableName field =
|
|
||||||
conn.findByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
|
||||||
[<System.Obsolete "Use findFirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.findFirstByField<'TDoc> tableName field =
|
|
||||||
conn.findFirstByFields<'TDoc> tableName Any [ field ]
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<System.Obsolete "Use patchByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.patchByField tableName field (patch: 'TPatch) =
|
|
||||||
conn.patchByFields tableName Any [ field ] patch
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<System.Obsolete "Use removeFieldsByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.removeFieldsByField tableName field fieldNames =
|
|
||||||
conn.removeFieldsByFields tableName Any [ field ] fieldNames
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<System.Obsolete "Use deleteByFields instead ~ will be removed in v4.1">]
|
|
||||||
member conn.deleteByField tableName field =
|
|
||||||
conn.deleteByFields tableName Any [ field ]
|
|
||||||
|
|
||||||
|
|
||||||
open System.Runtime.CompilerServices
|
|
||||||
|
|
||||||
type SqliteConnectionCSharpCompatExtensions =
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use CountByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline CountByField(conn, tableName, field) =
|
|
||||||
WithConn.Count.byFields tableName Any [ field ] conn
|
|
||||||
|
|
||||||
/// Determine if documents exist using a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use ExistsByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline ExistsByField(conn, tableName, field) =
|
|
||||||
WithConn.Exists.byFields tableName Any [ field ] conn
|
|
||||||
|
|
||||||
/// Retrieve documents matching a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use FindByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline FindByField<'TDoc>(conn, tableName, field) =
|
|
||||||
WithConn.Find.ByFields<'TDoc>(tableName, Any, [ field ], conn)
|
|
||||||
|
|
||||||
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use FindFirstByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, field) =
|
|
||||||
WithConn.Find.FirstByFields<'TDoc>(tableName, Any, [ field ], conn)
|
|
||||||
|
|
||||||
/// Patch documents using a JSON field comparison query in the WHERE clause (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use PatchByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline PatchByField(conn, tableName, field, patch: 'TPatch) =
|
|
||||||
WithConn.Patch.byFields tableName Any [ field ] patch conn
|
|
||||||
|
|
||||||
/// Remove fields from documents via a comparison on a JSON field in the document
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use RemoveFieldsByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline RemoveFieldsByField(conn, tableName, field, fieldNames) =
|
|
||||||
WithConn.RemoveFields.byFields tableName Any [ field ] fieldNames conn
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON field comparison query (->> =)
|
|
||||||
[<Extension>]
|
|
||||||
[<System.Obsolete "Use DeleteByFields instead ~ will be removed in v4.1">]
|
|
||||||
static member inline DeleteByField(conn, tableName, field) =
|
|
||||||
WithConn.Delete.byFields tableName Any [ field ] conn
|
|
||||||
@@ -35,11 +35,11 @@ module Extensions =
|
|||||||
|
|
||||||
/// Insert a new document
|
/// Insert a new document
|
||||||
member conn.insert<'TDoc> tableName (document: 'TDoc) =
|
member conn.insert<'TDoc> tableName (document: 'TDoc) =
|
||||||
WithConn.Document.insert<'TDoc> tableName document conn
|
WithConn.insert<'TDoc> tableName document conn
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
member conn.save<'TDoc> tableName (document: 'TDoc) =
|
member conn.save<'TDoc> tableName (document: 'TDoc) =
|
||||||
WithConn.Document.save tableName document conn
|
WithConn.save tableName document conn
|
||||||
|
|
||||||
/// Count all documents in a table
|
/// Count all documents in a table
|
||||||
member conn.countAll tableName =
|
member conn.countAll tableName =
|
||||||
@@ -49,6 +49,11 @@ module Extensions =
|
|||||||
member conn.countByFields tableName howMatched fields =
|
member conn.countByFields tableName howMatched fields =
|
||||||
WithConn.Count.byFields tableName howMatched fields conn
|
WithConn.Count.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Count matching documents using a comparison on a JSON field
|
||||||
|
[<System.Obsolete "Use countByFields instead; will be removed in v4">]
|
||||||
|
member conn.countByField tableName field =
|
||||||
|
conn.countByFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Determine if a document exists for the given ID
|
/// Determine if a document exists for the given ID
|
||||||
member conn.existsById tableName (docId: 'TKey) =
|
member conn.existsById tableName (docId: 'TKey) =
|
||||||
WithConn.Exists.byId tableName docId conn
|
WithConn.Exists.byId tableName docId conn
|
||||||
@@ -57,14 +62,15 @@ module Extensions =
|
|||||||
member conn.existsByFields tableName howMatched fields =
|
member conn.existsByFields tableName howMatched fields =
|
||||||
WithConn.Exists.byFields tableName howMatched fields conn
|
WithConn.Exists.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Determine if a document exists using a comparison on a JSON field
|
||||||
|
[<System.Obsolete "Use existsByFields instead; will be removed in v4">]
|
||||||
|
member conn.existsByField tableName field =
|
||||||
|
conn.existsByFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve all documents in the given table
|
/// Retrieve all documents in the given table
|
||||||
member conn.findAll<'TDoc> tableName =
|
member conn.findAll<'TDoc> tableName =
|
||||||
WithConn.Find.all<'TDoc> tableName conn
|
WithConn.Find.all<'TDoc> tableName conn
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
member conn.findAllOrdered<'TDoc> tableName orderFields =
|
|
||||||
WithConn.Find.allOrdered<'TDoc> tableName orderFields conn
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID
|
/// Retrieve a document by its ID
|
||||||
member conn.findById<'TKey, 'TDoc> tableName (docId: 'TKey) =
|
member conn.findById<'TKey, 'TDoc> tableName (docId: 'TKey) =
|
||||||
WithConn.Find.byId<'TKey, 'TDoc> tableName docId conn
|
WithConn.Find.byId<'TKey, 'TDoc> tableName docId conn
|
||||||
@@ -73,18 +79,19 @@ module Extensions =
|
|||||||
member conn.findByFields<'TDoc> tableName howMatched fields =
|
member conn.findByFields<'TDoc> tableName howMatched fields =
|
||||||
WithConn.Find.byFields<'TDoc> tableName howMatched fields conn
|
WithConn.Find.byFields<'TDoc> tableName howMatched fields conn
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
member conn.findByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
[<System.Obsolete "Use findByFields instead; will be removed in v4">]
|
||||||
WithConn.Find.byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
member conn.findByField<'TDoc> tableName field =
|
||||||
|
conn.findByFields<'TDoc> tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
member conn.findFirstByFields<'TDoc> tableName howMatched fields =
|
member conn.findFirstByFields<'TDoc> tableName howMatched fields =
|
||||||
WithConn.Find.firstByFields<'TDoc> tableName howMatched fields conn
|
WithConn.Find.firstByFields<'TDoc> tableName howMatched fields conn
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
/// only the first result
|
[<System.Obsolete "Use findFirstByFields instead; will be removed in v4">]
|
||||||
member conn.findFirstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
member conn.findFirstByField<'TDoc> tableName field =
|
||||||
WithConn.Find.firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
conn.findFirstByFields<'TDoc> tableName Any [ field ]
|
||||||
|
|
||||||
/// Update an entire document by its ID
|
/// Update an entire document by its ID
|
||||||
member conn.updateById tableName (docId: 'TKey) (document: 'TDoc) =
|
member conn.updateById tableName (docId: 'TKey) (document: 'TDoc) =
|
||||||
@@ -102,6 +109,11 @@ module Extensions =
|
|||||||
member conn.patchByFields tableName howMatched fields (patch: 'TPatch) =
|
member conn.patchByFields tableName howMatched fields (patch: 'TPatch) =
|
||||||
WithConn.Patch.byFields tableName howMatched fields patch conn
|
WithConn.Patch.byFields tableName howMatched fields patch conn
|
||||||
|
|
||||||
|
/// Patch documents using a comparison on a JSON field
|
||||||
|
[<System.Obsolete "Use patchByFields instead; will be removed in v4">]
|
||||||
|
member conn.patchByField tableName field (patch: 'TPatch) =
|
||||||
|
conn.patchByFields tableName Any [ field ] patch
|
||||||
|
|
||||||
/// Remove fields from a document by the document's ID
|
/// Remove fields from a document by the document's ID
|
||||||
member conn.removeFieldsById tableName (docId: 'TKey) fieldNames =
|
member conn.removeFieldsById tableName (docId: 'TKey) fieldNames =
|
||||||
WithConn.RemoveFields.byId tableName docId fieldNames conn
|
WithConn.RemoveFields.byId tableName docId fieldNames conn
|
||||||
@@ -110,6 +122,11 @@ module Extensions =
|
|||||||
member conn.removeFieldsByFields tableName howMatched fields fieldNames =
|
member conn.removeFieldsByFields tableName howMatched fields fieldNames =
|
||||||
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
||||||
|
|
||||||
|
/// Remove a field from a document via a comparison on a JSON field in the document
|
||||||
|
[<System.Obsolete "Use removeFieldsByFields instead; will be removed in v4">]
|
||||||
|
member conn.removeFieldsByField tableName field fieldNames =
|
||||||
|
conn.removeFieldsByFields tableName Any [ field ] fieldNames
|
||||||
|
|
||||||
/// Delete a document by its ID
|
/// Delete a document by its ID
|
||||||
member conn.deleteById tableName (docId: 'TKey) =
|
member conn.deleteById tableName (docId: 'TKey) =
|
||||||
WithConn.Delete.byId tableName docId conn
|
WithConn.Delete.byId tableName docId conn
|
||||||
@@ -118,6 +135,11 @@ module Extensions =
|
|||||||
member conn.deleteByFields tableName howMatched fields =
|
member conn.deleteByFields tableName howMatched fields =
|
||||||
WithConn.Delete.byFields tableName howMatched fields conn
|
WithConn.Delete.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Delete documents by matching a comparison on a JSON field
|
||||||
|
[<System.Obsolete "Use deleteByFields instead; will be removed in v4">]
|
||||||
|
member conn.deleteByField tableName field =
|
||||||
|
conn.deleteByFields tableName Any [ field ]
|
||||||
|
|
||||||
|
|
||||||
open System.Runtime.CompilerServices
|
open System.Runtime.CompilerServices
|
||||||
|
|
||||||
@@ -159,12 +181,12 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
/// Insert a new document
|
/// Insert a new document
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline Insert<'TDoc>(conn, tableName, document: 'TDoc) =
|
static member inline Insert<'TDoc>(conn, tableName, document: 'TDoc) =
|
||||||
WithConn.Document.insert<'TDoc> tableName document conn
|
WithConn.insert<'TDoc> tableName document conn
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline Save<'TDoc>(conn, tableName, document: 'TDoc) =
|
static member inline Save<'TDoc>(conn, tableName, document: 'TDoc) =
|
||||||
WithConn.Document.save<'TDoc> tableName document conn
|
WithConn.save<'TDoc> tableName document conn
|
||||||
|
|
||||||
/// Count all documents in a table
|
/// Count all documents in a table
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
@@ -176,6 +198,12 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
static member inline CountByFields(conn, tableName, howMatched, fields) =
|
static member inline CountByFields(conn, tableName, howMatched, fields) =
|
||||||
WithConn.Count.byFields tableName howMatched fields conn
|
WithConn.Count.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Count matching documents using a comparison on a JSON field
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use CountByFields instead; will be removed in v4">]
|
||||||
|
static member inline CountByField(conn, tableName, field) =
|
||||||
|
conn.CountByFields(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Determine if a document exists for the given ID
|
/// Determine if a document exists for the given ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline ExistsById<'TKey>(conn, tableName, docId: 'TKey) =
|
static member inline ExistsById<'TKey>(conn, tableName, docId: 'TKey) =
|
||||||
@@ -186,16 +214,17 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
static member inline ExistsByFields(conn, tableName, howMatched, fields) =
|
static member inline ExistsByFields(conn, tableName, howMatched, fields) =
|
||||||
WithConn.Exists.byFields tableName howMatched fields conn
|
WithConn.Exists.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Determine if a document exists using a comparison on a JSON field
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use ExistsByFields instead; will be removed in v4">]
|
||||||
|
static member inline ExistsByField(conn, tableName, field) =
|
||||||
|
conn.ExistsByFields(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Retrieve all documents in the given table
|
/// Retrieve all documents in the given table
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindAll<'TDoc>(conn, tableName) =
|
static member inline FindAll<'TDoc>(conn, tableName) =
|
||||||
WithConn.Find.All<'TDoc>(tableName, conn)
|
WithConn.Find.All<'TDoc>(tableName, conn)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<Extension>]
|
|
||||||
static member inline FindAllOrdered<'TDoc>(conn, tableName, orderFields) =
|
|
||||||
WithConn.Find.AllOrdered<'TDoc>(tableName, orderFields, conn)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID
|
/// Retrieve a document by its ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindById<'TKey, 'TDoc when 'TDoc: null>(conn, tableName, docId: 'TKey) =
|
static member inline FindById<'TKey, 'TDoc when 'TDoc: null>(conn, tableName, docId: 'TKey) =
|
||||||
@@ -206,22 +235,22 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
static member inline FindByFields<'TDoc>(conn, tableName, howMatched, fields) =
|
static member inline FindByFields<'TDoc>(conn, tableName, howMatched, fields) =
|
||||||
WithConn.Find.ByFields<'TDoc>(tableName, howMatched, fields, conn)
|
WithConn.Find.ByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindByFieldsOrdered<'TDoc>(conn, tableName, howMatched, queryFields, orderFields) =
|
[<System.Obsolete "Use FindByFields instead; will be removed in v4">]
|
||||||
WithConn.Find.ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
static member inline FindByField<'TDoc>(conn, tableName, field) =
|
||||||
|
conn.FindByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByFields<'TDoc when 'TDoc: null>(conn, tableName, howMatched, fields) =
|
static member inline FindFirstByFields<'TDoc when 'TDoc: null>(conn, tableName, howMatched, fields) =
|
||||||
WithConn.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, conn)
|
WithConn.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning only
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
/// the first result
|
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline FindFirstByFieldsOrdered<'TDoc when 'TDoc: null>(
|
[<System.Obsolete "Use FindFirstByFields instead; will be removed in v4">]
|
||||||
conn, tableName, howMatched, queryFields, orderFields) =
|
static member inline FindFirstByField<'TDoc when 'TDoc: null>(conn, tableName, field) =
|
||||||
WithConn.Find.FirstByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
conn.FindFirstByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
|
|
||||||
/// Update an entire document by its ID
|
/// Update an entire document by its ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
@@ -243,6 +272,12 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
static member inline PatchByFields<'TPatch>(conn, tableName, howMatched, fields, patch: 'TPatch) =
|
static member inline PatchByFields<'TPatch>(conn, tableName, howMatched, fields, patch: 'TPatch) =
|
||||||
WithConn.Patch.byFields tableName howMatched fields patch conn
|
WithConn.Patch.byFields tableName howMatched fields patch conn
|
||||||
|
|
||||||
|
/// Patch documents using a comparison on a JSON field
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use PatchByFields instead; will be removed in v4">]
|
||||||
|
static member inline PatchByField<'TPatch>(conn, tableName, field, patch: 'TPatch) =
|
||||||
|
conn.PatchByFields(tableName, Any, [ field ], patch)
|
||||||
|
|
||||||
/// Remove fields from a document by the document's ID
|
/// Remove fields from a document by the document's ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline RemoveFieldsById<'TKey>(conn, tableName, docId: 'TKey, fieldNames) =
|
static member inline RemoveFieldsById<'TKey>(conn, tableName, docId: 'TKey, fieldNames) =
|
||||||
@@ -253,6 +288,12 @@ type SqliteConnectionCSharpExtensions =
|
|||||||
static member inline RemoveFieldsByFields(conn, tableName, howMatched, fields, fieldNames) =
|
static member inline RemoveFieldsByFields(conn, tableName, howMatched, fields, fieldNames) =
|
||||||
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<Extension>]
|
||||||
|
[<System.Obsolete "Use RemoveFieldsByFields instead; will be removed in v4">]
|
||||||
|
static member inline RemoveFieldsByField(conn, tableName, field, fieldNames) =
|
||||||
|
conn.RemoveFieldsByFields(tableName, Any, [ field ], fieldNames)
|
||||||
|
|
||||||
/// Delete a document by its ID
|
/// Delete a document by its ID
|
||||||
[<Extension>]
|
[<Extension>]
|
||||||
static member inline DeleteById<'TKey>(conn, tableName, docId: 'TKey) =
|
static member inline DeleteById<'TKey>(conn, tableName, docId: 'TKey) =
|
||||||
|
|||||||
@@ -258,29 +258,10 @@ module WithConn =
|
|||||||
let ensureFieldIndex tableName indexName fields conn =
|
let ensureFieldIndex tableName indexName fields conn =
|
||||||
Custom.nonQuery (Query.Definition.ensureIndexOn tableName indexName fields SQLite) [] conn
|
Custom.nonQuery (Query.Definition.ensureIndexOn tableName indexName fields SQLite) [] conn
|
||||||
|
|
||||||
/// Commands to add documents
|
|
||||||
[<AutoOpen>]
|
|
||||||
module Document =
|
|
||||||
|
|
||||||
/// Insert a new document
|
/// Insert a new document
|
||||||
[<CompiledName "Insert">]
|
[<CompiledName "Insert">]
|
||||||
let insert<'TDoc> tableName (document: 'TDoc) conn =
|
let insert<'TDoc> tableName (document: 'TDoc) conn =
|
||||||
let query =
|
Custom.nonQuery (Query.insert tableName) [ jsonParam "@data" document ] conn
|
||||||
match Configuration.autoIdStrategy () with
|
|
||||||
| Disabled -> Query.insert tableName
|
|
||||||
| strategy ->
|
|
||||||
let idField = Configuration.idField ()
|
|
||||||
let dataParam =
|
|
||||||
if AutoId.NeedsAutoId strategy document idField then
|
|
||||||
match strategy with
|
|
||||||
| Number -> $"(SELECT coalesce(max(data->>'{idField}'), 0) + 1 FROM {tableName})"
|
|
||||||
| Guid -> $"'{AutoId.GenerateGuid()}'"
|
|
||||||
| RandomString -> $"'{AutoId.GenerateRandomString(Configuration.idStringLength ())}'"
|
|
||||||
| Disabled -> "@data"
|
|
||||||
|> function it -> $"json_set(@data, '$.{idField}', {it})"
|
|
||||||
else "@data"
|
|
||||||
(Query.insert tableName).Replace("@data", dataParam)
|
|
||||||
Custom.nonQuery query [ jsonParam "@data" document ] conn
|
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
[<CompiledName "Save">]
|
[<CompiledName "Save">]
|
||||||
@@ -333,15 +314,6 @@ module WithConn =
|
|||||||
let All<'TDoc>(tableName, conn) =
|
let All<'TDoc>(tableName, conn) =
|
||||||
Custom.List(Query.find tableName, [], fromData<'TDoc>, conn)
|
Custom.List(Query.find tableName, [], fromData<'TDoc>, conn)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpAllOrdered">]
|
|
||||||
let allOrdered<'TDoc> tableName orderFields conn =
|
|
||||||
Custom.list<'TDoc> (Query.find tableName + Query.orderBy orderFields SQLite) [] fromData<'TDoc> conn
|
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
let AllOrdered<'TDoc>(tableName, orderFields, conn) =
|
|
||||||
Custom.List(Query.find tableName + Query.orderBy orderFields SQLite, [], fromData<'TDoc>, conn)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID (returns None if not found)
|
/// Retrieve a document by its ID (returns None if not found)
|
||||||
[<CompiledName "FSharpById">]
|
[<CompiledName "FSharpById">]
|
||||||
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) conn =
|
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) conn =
|
||||||
@@ -360,6 +332,12 @@ module WithConn =
|
|||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
conn
|
conn
|
||||||
|
|
||||||
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
|
[<CompiledName "FSharpByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField<'TDoc> tableName field conn =
|
||||||
|
byFields<'TDoc> tableName Any [ field ] conn
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields
|
/// Retrieve documents via a comparison on JSON fields
|
||||||
let ByFields<'TDoc>(tableName, howMatched, fields, conn) =
|
let ByFields<'TDoc>(tableName, howMatched, fields, conn) =
|
||||||
Custom.List<'TDoc>(
|
Custom.List<'TDoc>(
|
||||||
@@ -368,22 +346,10 @@ module WithConn =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
conn)
|
conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
[<CompiledName "FSharpByFieldsOrdered">]
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn =
|
let ByField<'TDoc>(tableName, field, conn) =
|
||||||
Custom.list<'TDoc>
|
ByFields<'TDoc>(tableName, Any, [ field ], conn)
|
||||||
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite)
|
|
||||||
(addFieldParams queryFields [])
|
|
||||||
fromData<'TDoc>
|
|
||||||
conn
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
|
||||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn) =
|
|
||||||
Custom.List<'TDoc>(
|
|
||||||
Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite,
|
|
||||||
addFieldParams queryFields [],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
conn)
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
[<CompiledName "FSharpFirstByFields">]
|
[<CompiledName "FSharpFirstByFields">]
|
||||||
@@ -394,6 +360,12 @@ module WithConn =
|
|||||||
fromData<'TDoc>
|
fromData<'TDoc>
|
||||||
conn
|
conn
|
||||||
|
|
||||||
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
[<System.Obsolete "Use firstByFields instead; will be removed in v4">]
|
||||||
|
let firstByField<'TDoc> tableName field conn =
|
||||||
|
firstByFields<'TDoc> tableName Any [ field ] conn
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields, conn) =
|
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields, conn) =
|
||||||
Custom.Single(
|
Custom.Single(
|
||||||
@@ -402,24 +374,10 @@ module WithConn =
|
|||||||
fromData<'TDoc>,
|
fromData<'TDoc>,
|
||||||
conn)
|
conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
/// only the first result
|
[<System.Obsolete "Use FirstByFields instead; will be removed in v4">]
|
||||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, field, conn) =
|
||||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn =
|
FirstByFields(tableName, Any, [ field ], conn)
|
||||||
Custom.single
|
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields SQLite} LIMIT 1"
|
|
||||||
(addFieldParams queryFields [])
|
|
||||||
fromData<'TDoc>
|
|
||||||
conn
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning
|
|
||||||
/// only the first result
|
|
||||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null>(tableName, howMatched, queryFields, orderFields, conn) =
|
|
||||||
Custom.Single(
|
|
||||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields SQLite} LIMIT 1",
|
|
||||||
addFieldParams queryFields [],
|
|
||||||
fromData<'TDoc>,
|
|
||||||
conn)
|
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -460,6 +418,12 @@ module WithConn =
|
|||||||
(addFieldParams fields [ jsonParam "@data" patch ])
|
(addFieldParams fields [ jsonParam "@data" patch ])
|
||||||
conn
|
conn
|
||||||
|
|
||||||
|
/// Patch documents using a comparison on a JSON field
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field (patch: 'TPatch) conn =
|
||||||
|
byFields tableName Any [ field ] patch conn
|
||||||
|
|
||||||
/// Commands to remove fields from documents
|
/// Commands to remove fields from documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module RemoveFields =
|
module RemoveFields =
|
||||||
@@ -482,6 +446,12 @@ module WithConn =
|
|||||||
(addFieldParams fields nameParams)
|
(addFieldParams fields nameParams)
|
||||||
conn
|
conn
|
||||||
|
|
||||||
|
/// Remove fields from documents via a comparison on a JSON field in the document
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field fieldNames conn =
|
||||||
|
byFields tableName Any [ field ] fieldNames conn
|
||||||
|
|
||||||
/// Commands to delete documents
|
/// Commands to delete documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Delete =
|
module Delete =
|
||||||
@@ -496,6 +466,12 @@ module WithConn =
|
|||||||
let byFields tableName howMatched fields conn =
|
let byFields tableName howMatched fields conn =
|
||||||
Custom.nonQuery (Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) conn
|
Custom.nonQuery (Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) conn
|
||||||
|
|
||||||
|
/// Delete documents by matching a comparison on a JSON field
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field conn =
|
||||||
|
byFields tableName Any [ field ] conn
|
||||||
|
|
||||||
|
|
||||||
/// Commands to execute custom SQL queries
|
/// Commands to execute custom SQL queries
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -540,7 +516,6 @@ module Custom =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Custom.Scalar<'T>(query, parameters, mapFunc, conn)
|
WithConn.Custom.Scalar<'T>(query, parameters, mapFunc, conn)
|
||||||
|
|
||||||
|
|
||||||
/// Functions to create tables and indexes
|
/// Functions to create tables and indexes
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Definition =
|
module Definition =
|
||||||
@@ -557,7 +532,6 @@ module Definition =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Definition.ensureFieldIndex tableName indexName fields conn
|
WithConn.Definition.ensureFieldIndex tableName indexName fields conn
|
||||||
|
|
||||||
|
|
||||||
/// Document insert/save functions
|
/// Document insert/save functions
|
||||||
[<AutoOpen>]
|
[<AutoOpen>]
|
||||||
module Document =
|
module Document =
|
||||||
@@ -566,14 +540,13 @@ module Document =
|
|||||||
[<CompiledName "Insert">]
|
[<CompiledName "Insert">]
|
||||||
let insert<'TDoc> tableName (document: 'TDoc) =
|
let insert<'TDoc> tableName (document: 'TDoc) =
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Document.insert tableName document conn
|
WithConn.insert tableName document conn
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
[<CompiledName "Save">]
|
[<CompiledName "Save">]
|
||||||
let save<'TDoc> tableName (document: 'TDoc) =
|
let save<'TDoc> tableName (document: 'TDoc) =
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Document.save tableName document conn
|
WithConn.save tableName document conn
|
||||||
|
|
||||||
|
|
||||||
/// Commands to count documents
|
/// Commands to count documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -591,6 +564,11 @@ module Count =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Count.byFields tableName howMatched fields conn
|
WithConn.Count.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Count matching documents using a comparison on a JSON field
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Commands to determine if documents exist
|
/// Commands to determine if documents exist
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -608,6 +586,11 @@ module Exists =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Exists.byFields tableName howMatched fields conn
|
WithConn.Exists.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Determine if a document exists using a comparison on a JSON field
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Commands to determine if documents exist
|
/// Commands to determine if documents exist
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -624,17 +607,6 @@ module Find =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Find.All<'TDoc>(tableName, conn)
|
WithConn.Find.All<'TDoc>(tableName, conn)
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
[<CompiledName "FSharpAllOrdered">]
|
|
||||||
let allOrdered<'TDoc> tableName orderFields =
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
WithConn.Find.allOrdered<'TDoc> tableName orderFields conn
|
|
||||||
|
|
||||||
/// Retrieve all documents in the given table ordered by the given fields in the document
|
|
||||||
let AllOrdered<'TDoc> tableName orderFields =
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
WithConn.Find.AllOrdered<'TDoc>(tableName, orderFields, conn)
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID (returns None if not found)
|
/// Retrieve a document by its ID (returns None if not found)
|
||||||
[<CompiledName "FSharpById">]
|
[<CompiledName "FSharpById">]
|
||||||
let byId<'TKey, 'TDoc> tableName docId =
|
let byId<'TKey, 'TDoc> tableName docId =
|
||||||
@@ -652,21 +624,21 @@ module Find =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Find.byFields<'TDoc> tableName howMatched fields conn
|
WithConn.Find.byFields<'TDoc> tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
|
[<CompiledName "FSharpByField">]
|
||||||
|
[<System.Obsolete "Use byFields instead; will be removed in v4">]
|
||||||
|
let byField<'TDoc> tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields
|
/// Retrieve documents via a comparison on JSON fields
|
||||||
let ByFields<'TDoc>(tableName, howMatched, fields) =
|
let ByFields<'TDoc>(tableName, howMatched, fields) =
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Find.ByFields<'TDoc>(tableName, howMatched, fields, conn)
|
WithConn.Find.ByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
/// Retrieve documents via a comparison on a JSON field
|
||||||
[<CompiledName "FSharpByFieldsOrdered">]
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
let ByField<'TDoc>(tableName, field) =
|
||||||
use conn = Configuration.dbConn ()
|
ByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
WithConn.Find.byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document
|
|
||||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields) =
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
WithConn.Find.ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
[<CompiledName "FSharpFirstByFields">]
|
[<CompiledName "FSharpFirstByFields">]
|
||||||
@@ -674,24 +646,21 @@ module Find =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Find.firstByFields<'TDoc> tableName howMatched fields conn
|
WithConn.Find.firstByFields<'TDoc> tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
[<System.Obsolete "Use firstByFields instead; will be removed in v4">]
|
||||||
|
let firstByField<'TDoc> tableName field =
|
||||||
|
firstByFields<'TDoc> tableName Any [ field ]
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
/// Retrieve documents via a comparison on JSON fields, returning only the first result
|
||||||
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields) =
|
let FirstByFields<'TDoc when 'TDoc: null>(tableName, howMatched, fields) =
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, conn)
|
WithConn.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning only
|
/// Retrieve documents via a comparison on a JSON field, returning only the first result
|
||||||
/// the first result
|
[<System.Obsolete "Use FirstByFields instead; will be removed in v4">]
|
||||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, field) =
|
||||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
FirstByFields<'TDoc>(tableName, Any, [ field ])
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
WithConn.Find.firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
|
||||||
|
|
||||||
/// Retrieve documents via a comparison on JSON fields ordered by the given fields in the document, returning only
|
|
||||||
/// the first result
|
|
||||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null>(tableName, howMatched, queryFields, orderFields) =
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
WithConn.Find.FirstByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
|
||||||
|
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -714,7 +683,6 @@ module Update =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Update.ByFunc(tableName, idFunc, document, conn)
|
WithConn.Update.ByFunc(tableName, idFunc, document, conn)
|
||||||
|
|
||||||
|
|
||||||
/// Commands to patch (partially update) documents
|
/// Commands to patch (partially update) documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Patch =
|
module Patch =
|
||||||
@@ -731,6 +699,11 @@ module Patch =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Patch.byFields tableName howMatched fields patch conn
|
WithConn.Patch.byFields tableName howMatched fields patch conn
|
||||||
|
|
||||||
|
/// Patch documents using a comparison on a JSON field in the WHERE clause
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field (patch: 'TPatch) =
|
||||||
|
byFields tableName Any [ field ] patch
|
||||||
|
|
||||||
/// Commands to remove fields from documents
|
/// Commands to remove fields from documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -748,6 +721,11 @@ module RemoveFields =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
||||||
|
|
||||||
|
/// Remove field from documents via a comparison on a JSON field in the document
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field fieldNames =
|
||||||
|
byFields tableName Any [ field ] fieldNames
|
||||||
|
|
||||||
/// Commands to delete documents
|
/// Commands to delete documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
@@ -764,3 +742,9 @@ module Delete =
|
|||||||
let byFields tableName howMatched fields =
|
let byFields tableName howMatched fields =
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
WithConn.Delete.byFields tableName howMatched fields conn
|
WithConn.Delete.byFields tableName howMatched fields conn
|
||||||
|
|
||||||
|
/// Delete documents by matching a comparison on a JSON field
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
[<System.Obsolete "Use ByFields instead; will be removed in v4">]
|
||||||
|
let byField tableName field =
|
||||||
|
byFields tableName Any [ field ]
|
||||||
|
|||||||
@@ -22,9 +22,57 @@ internal class TestSerializer : IDocumentSerializer
|
|||||||
public static class CommonCSharpTests
|
public static class CommonCSharpTests
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unit tests for the Op enum
|
/// Unit tests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Test OpTests = TestList("Op",
|
[Tests]
|
||||||
|
public static readonly Test Unit = TestList("Common.C# Unit",
|
||||||
|
[
|
||||||
|
TestSequenced(
|
||||||
|
TestList("Configuration",
|
||||||
|
[
|
||||||
|
TestCase("UseSerializer succeeds", () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Configuration.UseSerializer(new TestSerializer());
|
||||||
|
|
||||||
|
var serialized = Configuration.Serializer().Serialize(new SubDocument
|
||||||
|
{
|
||||||
|
Foo = "howdy",
|
||||||
|
Bar = "bye"
|
||||||
|
});
|
||||||
|
Expect.equal(serialized, "{\"Overridden\":true}", "Specified serializer was not used");
|
||||||
|
|
||||||
|
var deserialized = Configuration.Serializer()
|
||||||
|
.Deserialize<object>("{\"Something\":\"here\"}");
|
||||||
|
Expect.isNull(deserialized, "Specified serializer should have returned null");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Configuration.UseSerializer(DocumentSerializer.Default);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
TestCase("Serializer returns configured serializer", () =>
|
||||||
|
{
|
||||||
|
Expect.isTrue(ReferenceEquals(DocumentSerializer.Default, Configuration.Serializer()),
|
||||||
|
"Serializer should have been the same");
|
||||||
|
}),
|
||||||
|
TestCase("UseIdField / IdField succeeds", () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Expect.equal(Configuration.IdField(), "Id",
|
||||||
|
"The default configured ID field was incorrect");
|
||||||
|
Configuration.UseIdField("id");
|
||||||
|
Expect.equal(Configuration.IdField(), "id", "UseIdField did not set the ID field");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Configuration.UseIdField("Id");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
])),
|
||||||
|
TestList("Op",
|
||||||
[
|
[
|
||||||
TestCase("EQ succeeds", () =>
|
TestCase("EQ succeeds", () =>
|
||||||
{
|
{
|
||||||
@@ -62,12 +110,8 @@ public static class CommonCSharpTests
|
|||||||
{
|
{
|
||||||
Expect.equal(Op.NEX.ToString(), "IS NULL", "The \"not exists\" operator was not correct");
|
Expect.equal(Op.NEX.ToString(), "IS NULL", "The \"not exists\" operator was not correct");
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("Field",
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the Field class
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test FieldTests = TestList("Field",
|
|
||||||
[
|
[
|
||||||
TestCase("EQ succeeds", () =>
|
TestCase("EQ succeeds", () =>
|
||||||
{
|
{
|
||||||
@@ -130,31 +174,6 @@ public static class CommonCSharpTests
|
|||||||
Expect.equal(field.Name, "Rad", "Field name incorrect");
|
Expect.equal(field.Name, "Rad", "Field name incorrect");
|
||||||
Expect.equal(field.Op, Op.NEX, "Operator incorrect");
|
Expect.equal(field.Op, Op.NEX, "Operator incorrect");
|
||||||
}),
|
}),
|
||||||
TestList("NameToPath",
|
|
||||||
[
|
|
||||||
TestCase("succeeds for PostgreSQL and a simple name", () =>
|
|
||||||
{
|
|
||||||
Expect.equal("data->>'Simple'", Field.NameToPath("Simple", Dialect.PostgreSQL),
|
|
||||||
"Path not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for SQLite and a simple name", () =>
|
|
||||||
{
|
|
||||||
Expect.equal("data->>'Simple'", Field.NameToPath("Simple", Dialect.SQLite),
|
|
||||||
"Path not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for PostgreSQL and a nested name", () =>
|
|
||||||
{
|
|
||||||
Expect.equal("data#>>'{A,Long,Path,to,the,Property}'",
|
|
||||||
Field.NameToPath("A.Long.Path.to.the.Property", Dialect.PostgreSQL),
|
|
||||||
"Path not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for SQLite and a nested name", () =>
|
|
||||||
{
|
|
||||||
Expect.equal("data->>'A'->>'Long'->>'Path'->>'to'->>'the'->>'Property'",
|
|
||||||
Field.NameToPath("A.Long.Path.to.the.Property", Dialect.SQLite),
|
|
||||||
"Path not constructed correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestCase("WithParameterName succeeds", () =>
|
TestCase("WithParameterName succeeds", () =>
|
||||||
{
|
{
|
||||||
var field = Field.EQ("Bob", "Tom").WithParameterName("@name");
|
var field = Field.EQ("Bob", "Tom").WithParameterName("@name");
|
||||||
@@ -201,7 +220,8 @@ public static class CommonCSharpTests
|
|||||||
TestCase("succeeds for a SQLite single field with a qualifier", () =>
|
TestCase("succeeds for a SQLite single field with a qualifier", () =>
|
||||||
{
|
{
|
||||||
var field = Field.LT("SomethingElse", 9).WithQualifier("this");
|
var field = Field.LT("SomethingElse", 9).WithQualifier("this");
|
||||||
Expect.equal("this.data->>'SomethingElse'", field.Path(Dialect.SQLite), "The SQLite path is incorrect");
|
Expect.equal("this.data->>'SomethingElse'", field.Path(Dialect.SQLite),
|
||||||
|
"The SQLite path is incorrect");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds for a SQLite nested field with no qualifier", () =>
|
TestCase("succeeds for a SQLite nested field with no qualifier", () =>
|
||||||
{
|
{
|
||||||
@@ -212,15 +232,12 @@ public static class CommonCSharpTests
|
|||||||
TestCase("succeeds for a SQLite nested field with a qualifier", () =>
|
TestCase("succeeds for a SQLite nested field with a qualifier", () =>
|
||||||
{
|
{
|
||||||
var field = Field.EQ("Nest.Away", "doc").WithQualifier("bird");
|
var field = Field.EQ("Nest.Away", "doc").WithQualifier("bird");
|
||||||
Expect.equal("bird.data->>'Nest'->>'Away'", field.Path(Dialect.SQLite), "The SQLite path is incorrect");
|
Expect.equal("bird.data->>'Nest'->>'Away'", field.Path(Dialect.SQLite),
|
||||||
|
"The SQLite path is incorrect");
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
]);
|
]),
|
||||||
|
TestList("FieldMatch.ToString",
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the FieldMatch enum
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test FieldMatchTests = TestList("FieldMatch.ToString",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds for Any", () =>
|
TestCase("succeeds for Any", () =>
|
||||||
{
|
{
|
||||||
@@ -230,12 +247,8 @@ public static class CommonCSharpTests
|
|||||||
{
|
{
|
||||||
Expect.equal(FieldMatch.All.ToString(), "AND", "SQL for All is incorrect");
|
Expect.equal(FieldMatch.All.ToString(), "AND", "SQL for All is incorrect");
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("ParameterName.Derive",
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the ParameterName class
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test ParameterNameTests = TestList("ParameterName.Derive",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds with existing name", () =>
|
TestCase("succeeds with existing name", () =>
|
||||||
{
|
{
|
||||||
@@ -256,229 +269,8 @@ public static class CommonCSharpTests
|
|||||||
Expect.equal(name.Derive(FSharpOption<string>.None), "@field3",
|
Expect.equal(name.Derive(FSharpOption<string>.None), "@field3",
|
||||||
"Counter should have advanced from previous call");
|
"Counter should have advanced from previous call");
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("Query",
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the AutoId enum
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test AutoIdTests = TestList("AutoId",
|
|
||||||
[
|
|
||||||
TestCase("GenerateGuid succeeds", () =>
|
|
||||||
{
|
|
||||||
var autoId = AutoId.GenerateGuid();
|
|
||||||
Expect.isNotNull(autoId, "The GUID auto-ID should not have been null");
|
|
||||||
Expect.stringHasLength(autoId, 32, "The GUID auto-ID should have been 32 characters long");
|
|
||||||
Expect.equal(autoId, autoId.ToLowerInvariant(), "The GUID auto-ID should have been lowercase");
|
|
||||||
}),
|
|
||||||
TestCase("GenerateRandomString succeeds", () =>
|
|
||||||
{
|
|
||||||
foreach (var length in (int[]) [6, 8, 12, 20, 32, 57, 64])
|
|
||||||
{
|
|
||||||
var autoId = AutoId.GenerateRandomString(length);
|
|
||||||
Expect.isNotNull(autoId, $"Random string ({length}) should not have been null");
|
|
||||||
Expect.stringHasLength(autoId, length, $"Random string should have been {length} characters long");
|
|
||||||
Expect.equal(autoId, autoId.ToLowerInvariant(),
|
|
||||||
$"Random string ({length}) should have been lowercase");
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestList("NeedsAutoId",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when no auto ID is configured", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Disabled, new object(), "id"),
|
|
||||||
"Disabled auto-ID never needs an automatic ID");
|
|
||||||
}),
|
|
||||||
TestCase("fails for any when the ID property is not found", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_ = AutoId.NeedsAutoId(AutoId.Number, new { Key = "" }, "Id");
|
|
||||||
Expect.isTrue(false, "Non-existent ID property should have thrown an exception");
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for byte when the ID is zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = (sbyte)0 }, "Id"),
|
|
||||||
"Zero ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for byte when the ID is non-zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = (sbyte)4 }, "Id"),
|
|
||||||
"Non-zero ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for short when the ID is zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = (short)0 }, "Id"),
|
|
||||||
"Zero ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for short when the ID is non-zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = (short)7 }, "Id"),
|
|
||||||
"Non-zero ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for int when the ID is zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = 0 }, "Id"),
|
|
||||||
"Zero ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for int when the ID is non-zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = 32 }, "Id"),
|
|
||||||
"Non-zero ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for long when the ID is zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = 0L }, "Id"),
|
|
||||||
"Zero ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for long when the ID is non-zero", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = 80L }, "Id"),
|
|
||||||
"Non-zero ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("fails for number when the ID is not a number", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_ = AutoId.NeedsAutoId(AutoId.Number, new { Id = "" }, "Id");
|
|
||||||
Expect.isTrue(false, "Numeric ID against a string should have thrown an exception");
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for GUID when the ID is blank", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Guid, new { Id = "" }, "Id"),
|
|
||||||
"Blank ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for GUID when the ID is filled", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Guid, new { Id = "abc" }, "Id"),
|
|
||||||
"Filled ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("fails for GUID when the ID is not a string", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_ = AutoId.NeedsAutoId(AutoId.Guid, new { Id = 8 }, "Id");
|
|
||||||
Expect.isTrue(false, "String ID against a number should have thrown an exception");
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for RandomString when the ID is blank", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(AutoId.NeedsAutoId(AutoId.RandomString, new { Id = "" }, "Id"),
|
|
||||||
"Blank ID should have returned true");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for RandomString when the ID is filled", () =>
|
|
||||||
{
|
|
||||||
Expect.isFalse(AutoId.NeedsAutoId(AutoId.RandomString, new { Id = "x" }, "Id"),
|
|
||||||
"Filled ID should have returned false");
|
|
||||||
}),
|
|
||||||
TestCase("fails for RandomString when the ID is not a string", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_ = AutoId.NeedsAutoId(AutoId.RandomString, new { Id = 33 }, "Id");
|
|
||||||
Expect.isTrue(false, "String ID against a number should have thrown an exception");
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
})
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the Configuration static class
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test ConfigurationTests = TestList("Configuration",
|
|
||||||
[
|
|
||||||
TestCase("UseSerializer succeeds", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Configuration.UseSerializer(new TestSerializer());
|
|
||||||
|
|
||||||
var serialized = Configuration.Serializer().Serialize(new SubDocument
|
|
||||||
{
|
|
||||||
Foo = "howdy",
|
|
||||||
Bar = "bye"
|
|
||||||
});
|
|
||||||
Expect.equal(serialized, "{\"Overridden\":true}", "Specified serializer was not used");
|
|
||||||
|
|
||||||
var deserialized = Configuration.Serializer()
|
|
||||||
.Deserialize<object>("{\"Something\":\"here\"}");
|
|
||||||
Expect.isNull(deserialized, "Specified serializer should have returned null");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseSerializer(DocumentSerializer.Default);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("Serializer returns configured serializer", () =>
|
|
||||||
{
|
|
||||||
Expect.isTrue(ReferenceEquals(DocumentSerializer.Default, Configuration.Serializer()),
|
|
||||||
"Serializer should have been the same");
|
|
||||||
}),
|
|
||||||
TestCase("UseIdField / IdField succeeds", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Expect.equal(Configuration.IdField(), "Id",
|
|
||||||
"The default configured ID field was incorrect");
|
|
||||||
Configuration.UseIdField("id");
|
|
||||||
Expect.equal(Configuration.IdField(), "id", "UseIdField did not set the ID field");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseIdField("Id");
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("UseAutoIdStrategy / AutoIdStrategy succeeds", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Expect.equal(Configuration.AutoIdStrategy(), AutoId.Disabled,
|
|
||||||
"The default auto-ID strategy was incorrect");
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Guid);
|
|
||||||
Expect.equal(Configuration.AutoIdStrategy(), AutoId.Guid,
|
|
||||||
"The auto-ID strategy was not set correctly");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("UseIdStringLength / IdStringLength succeeds", () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Expect.equal(Configuration.IdStringLength(), 16, "The default ID string length was incorrect");
|
|
||||||
Configuration.UseIdStringLength(33);
|
|
||||||
Expect.equal(Configuration.IdStringLength(), 33, "The ID string length was not set correctly");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseIdStringLength(16);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the Query static class
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test QueryTests = TestList("Query",
|
|
||||||
[
|
[
|
||||||
TestCase("StatementWhere succeeds", () =>
|
TestCase("StatementWhere succeeds", () =>
|
||||||
{
|
{
|
||||||
@@ -513,7 +305,7 @@ public static class CommonCSharpTests
|
|||||||
{
|
{
|
||||||
Expect.equal(
|
Expect.equal(
|
||||||
Query.Definition.EnsureIndexOn("test.table", "gibberish",
|
Query.Definition.EnsureIndexOn("test.table", "gibberish",
|
||||||
["taco", "guac DESC", "salsa ASC"], Dialect.SQLite),
|
new[] { "taco", "guac DESC", "salsa ASC" }, Dialect.SQLite),
|
||||||
"CREATE INDEX IF NOT EXISTS idx_table_gibberish ON test.table "
|
"CREATE INDEX IF NOT EXISTS idx_table_gibberish ON test.table "
|
||||||
+ "((data->>'taco'), (data->>'guac') DESC, (data->>'salsa') ASC)",
|
+ "((data->>'taco'), (data->>'guac') DESC, (data->>'salsa') ASC)",
|
||||||
"CREATE INDEX for multiple field statement incorrect");
|
"CREATE INDEX for multiple field statement incorrect");
|
||||||
@@ -564,71 +356,7 @@ public static class CommonCSharpTests
|
|||||||
TestCase("Delete succeeds", () =>
|
TestCase("Delete succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Delete("tbl"), "DELETE FROM tbl", "Delete query not correct");
|
Expect.equal(Query.Delete("tbl"), "DELETE FROM tbl", "Delete query not correct");
|
||||||
}),
|
|
||||||
TestList("OrderBy",
|
|
||||||
[
|
|
||||||
TestCase("succeeds for no fields", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.OrderBy([], Dialect.PostgreSQL), "", "Order By should have been blank (PostgreSQL)");
|
|
||||||
Expect.equal(Query.OrderBy([], Dialect.SQLite), "", "Order By should have been blank (SQLite)");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for PostgreSQL with one field and no direction", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.OrderBy([Field.Named("TestField")], Dialect.PostgreSQL),
|
|
||||||
" ORDER BY data->>'TestField'", "Order By not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for SQLite with one field and no direction", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.OrderBy([Field.Named("TestField")], Dialect.SQLite),
|
|
||||||
" ORDER BY data->>'TestField'", "Order By not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for PostgreSQL with multiple fields and direction", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(
|
|
||||||
Query.OrderBy(
|
|
||||||
[
|
|
||||||
Field.Named("Nested.Test.Field DESC"), Field.Named("AnotherField"),
|
|
||||||
Field.Named("It DESC")
|
|
||||||
], Dialect.PostgreSQL),
|
|
||||||
" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC",
|
|
||||||
"Order By not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for SQLite with multiple fields and direction", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(
|
|
||||||
Query.OrderBy(
|
|
||||||
[
|
|
||||||
Field.Named("Nested.Test.Field DESC"), Field.Named("AnotherField"),
|
|
||||||
Field.Named("It DESC")
|
|
||||||
], Dialect.SQLite),
|
|
||||||
" ORDER BY data->>'Nested'->>'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC",
|
|
||||||
"Order By not constructed correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for PostgreSQL numeric fields", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.OrderBy([Field.Named("n:Test")], Dialect.PostgreSQL),
|
|
||||||
" ORDER BY (data->>'Test')::numeric", "Order By not constructed correctly for numeric field");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds for SQLite numeric fields", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.OrderBy([Field.Named("n:Test")], Dialect.SQLite), " ORDER BY data->>'Test'",
|
|
||||||
"Order By not constructed correctly for numeric field");
|
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unit tests
|
|
||||||
/// </summary>
|
|
||||||
[Tests]
|
|
||||||
public static readonly Test Unit = TestList("Common.C# Unit",
|
|
||||||
[
|
|
||||||
OpTests,
|
|
||||||
FieldTests,
|
|
||||||
FieldMatchTests,
|
|
||||||
ParameterNameTests,
|
|
||||||
AutoIdTests,
|
|
||||||
QueryTests,
|
|
||||||
TestSequenced(ConfigurationTests)
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.CustomList(Query.Find(PostgresDb.TableName), Parameters.None,
|
var docs = await conn.CustomList(Query.SelectFromTable(PostgresDb.TableName), Parameters.None,
|
||||||
Results.FromData<JsonDocument>);
|
Results.FromData<JsonDocument>);
|
||||||
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
||||||
}),
|
}),
|
||||||
@@ -53,7 +53,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
|
|
||||||
var docs = await conn.CustomList(
|
var docs = await conn.CustomList(
|
||||||
$"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)"))],
|
new[] { Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)")) },
|
||||||
Results.FromData<JsonDocument>);
|
Results.FromData<JsonDocument>);
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
@@ -67,7 +67,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Tuple.Create("@id", Sql.@string("one"))], Results.FromData<JsonDocument>);
|
new[] { Tuple.Create("@id", Sql.@string("one")) }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc.Id, "one", "The incorrect document was returned");
|
Expect.equal(doc.Id, "one", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -78,7 +78,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await conn.CustomSingle($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Tuple.Create("@id", Sql.@string("eighty"))], Results.FromData<JsonDocument>);
|
new[] { Tuple.Create("@id", Sql.@string("eighty")) }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
@@ -102,7 +102,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.CustomNonQuery($"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
await conn.CustomNonQuery($"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
|
||||||
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))]);
|
new[] { Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)")) });
|
||||||
|
|
||||||
var remaining = await conn.CountAll(PostgresDb.TableName);
|
var remaining = await conn.CountAll(PostgresDb.TableName);
|
||||||
Expect.equal(remaining, 5, "There should be 5 documents remaining in the table");
|
Expect.equal(remaining, 5, "There should be 5 documents remaining in the table");
|
||||||
@@ -119,61 +119,55 @@ public class PostgresCSharpExtensionTests
|
|||||||
{
|
{
|
||||||
await using var db = PostgresDb.BuildDb();
|
await using var db = PostgresDb.BuildDb();
|
||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
|
var tableExists = () => conn.CustomScalar(
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'ensured') AS it", Parameters.None,
|
||||||
|
Results.ToExists);
|
||||||
|
var keyExists = () => conn.CustomScalar(
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it", Parameters.None,
|
||||||
|
Results.ToExists);
|
||||||
|
|
||||||
var exists = await TableExists();
|
var exists = await tableExists();
|
||||||
var alsoExists = await KeyExists();
|
var alsoExists = await keyExists();
|
||||||
Expect.isFalse(exists, "The table should not exist already");
|
Expect.isFalse(exists, "The table should not exist already");
|
||||||
Expect.isFalse(alsoExists, "The key index should not exist already");
|
Expect.isFalse(alsoExists, "The key index should not exist already");
|
||||||
|
|
||||||
await conn.EnsureTable("ensured");
|
await conn.EnsureTable("ensured");
|
||||||
exists = await TableExists();
|
exists = await tableExists();
|
||||||
alsoExists = await KeyExists();
|
alsoExists = await keyExists();
|
||||||
Expect.isTrue(exists, "The table should now exist");
|
Expect.isTrue(exists, "The table should now exist");
|
||||||
Expect.isTrue(alsoExists, "The key index should now exist");
|
Expect.isTrue(alsoExists, "The key index should now exist");
|
||||||
return;
|
|
||||||
|
|
||||||
Task<bool> KeyExists() =>
|
|
||||||
conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it",
|
|
||||||
Parameters.None, Results.ToExists);
|
|
||||||
Task<bool> TableExists() =>
|
|
||||||
conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'ensured') AS it",
|
|
||||||
Parameters.None, Results.ToExists);
|
|
||||||
}),
|
}),
|
||||||
TestCase("EnsureDocumentIndex succeeds", async () =>
|
TestCase("EnsureDocumentIndex succeeds", 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);
|
||||||
|
var indexExists = () => conn.CustomScalar(
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it", Parameters.None,
|
||||||
|
Results.ToExists);
|
||||||
|
|
||||||
var exists = await IndexExists();
|
var exists = await indexExists();
|
||||||
Expect.isFalse(exists, "The index should not exist already");
|
Expect.isFalse(exists, "The index should not exist already");
|
||||||
|
|
||||||
await conn.EnsureTable("ensured");
|
await conn.EnsureTable("ensured");
|
||||||
await conn.EnsureDocumentIndex("ensured", DocumentIndex.Optimized);
|
await conn.EnsureDocumentIndex("ensured", DocumentIndex.Optimized);
|
||||||
exists = await IndexExists();
|
exists = await indexExists();
|
||||||
Expect.isTrue(exists, "The index should now exist");
|
Expect.isTrue(exists, "The index should now exist");
|
||||||
return;
|
|
||||||
|
|
||||||
Task<bool> IndexExists() =>
|
|
||||||
conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it",
|
|
||||||
Parameters.None, Results.ToExists);
|
|
||||||
}),
|
}),
|
||||||
TestCase("EnsureFieldIndex succeeds", async () =>
|
TestCase("EnsureFieldIndex succeeds", 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);
|
||||||
|
var indexExists = () => conn.CustomScalar(
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it", Parameters.None,
|
||||||
|
Results.ToExists);
|
||||||
|
|
||||||
var exists = await IndexExists();
|
var exists = await indexExists();
|
||||||
Expect.isFalse(exists, "The index should not exist already");
|
Expect.isFalse(exists, "The index should not exist already");
|
||||||
|
|
||||||
await conn.EnsureTable("ensured");
|
await conn.EnsureTable("ensured");
|
||||||
await conn.EnsureFieldIndex("ensured", "test", ["Id", "Category"]);
|
await conn.EnsureFieldIndex("ensured", "test", new[] { "Id", "Category" });
|
||||||
exists = await IndexExists();
|
exists = await indexExists();
|
||||||
Expect.isTrue(exists, "The index should now exist");
|
Expect.isTrue(exists, "The index should now exist");
|
||||||
return;
|
|
||||||
|
|
||||||
Task<bool> IndexExists() =>
|
|
||||||
conn.CustomScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it",
|
|
||||||
Parameters.None, Results.ToExists);
|
|
||||||
}),
|
}),
|
||||||
TestList("Insert",
|
TestList("Insert",
|
||||||
[
|
[
|
||||||
@@ -246,16 +240,17 @@ public class PostgresCSharpExtensionTests
|
|||||||
var theCount = await conn.CountAll(PostgresDb.TableName);
|
var theCount = await conn.CountAll(PostgresDb.TableName);
|
||||||
Expect.equal(theCount, 5, "There should have been 5 matching documents");
|
Expect.equal(theCount, 5, "There should have been 5 matching documents");
|
||||||
}),
|
}),
|
||||||
TestCase("CountByFields succeeds", async () =>
|
#pragma warning disable CS0618
|
||||||
|
TestCase("CountByField succeeds", 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 LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var theCount = await conn.CountByFields(PostgresDb.TableName, FieldMatch.Any,
|
var theCount = await conn.CountByField(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||||
[Field.EQ("Value", "purple")]);
|
|
||||||
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
||||||
}),
|
}),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestCase("CountByContains succeeds", async () =>
|
TestCase("CountByContains succeeds", async () =>
|
||||||
{
|
{
|
||||||
await using var db = PostgresDb.BuildDb();
|
await using var db = PostgresDb.BuildDb();
|
||||||
@@ -295,6 +290,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isFalse(exists, "There should not have been an existing document");
|
Expect.isFalse(exists, "There should not have been an existing document");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning disable CS0618
|
||||||
TestList("ExistsByField",
|
TestList("ExistsByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents exist", async () =>
|
TestCase("succeeds when documents exist", async () =>
|
||||||
@@ -303,7 +299,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists = await conn.ExistsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EX("Sub")]);
|
var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EX("Sub"));
|
||||||
Expect.isTrue(exists, "There should have been existing documents");
|
Expect.isTrue(exists, "There should have been existing documents");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when documents do not exist", async () =>
|
TestCase("succeeds when documents do not exist", async () =>
|
||||||
@@ -312,11 +308,11 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists =
|
var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EQ("NumValue", "six"));
|
||||||
await conn.ExistsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "six")]);
|
|
||||||
Expect.isFalse(exists, "There should not have been existing documents");
|
Expect.isFalse(exists, "There should not have been existing documents");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("ExistsByContains",
|
TestList("ExistsByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents exist", async () =>
|
TestCase("succeeds when documents exist", async () =>
|
||||||
@@ -381,44 +377,6 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isEmpty(results, "There should have been no documents returned");
|
Expect.isEmpty(results, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindAllOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when ordering numerically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results =
|
|
||||||
await conn.FindAllOrdered<JsonDocument>(PostgresDb.TableName, [Field.Named("n:NumValue")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "one|three|two|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering numerically descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results =
|
|
||||||
await conn.FindAllOrdered<JsonDocument>(PostgresDb.TableName, [Field.Named("n:NumValue DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "five|four|two|three|one",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering alphabetically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await conn.FindAllOrdered<JsonDocument>(PostgresDb.TableName, [Field.Named("Id DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "two|three|one|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindById",
|
TestList("FindById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
@@ -441,7 +399,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("FindByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are found", async () =>
|
TestCase("succeeds when documents are found", async () =>
|
||||||
{
|
{
|
||||||
@@ -449,8 +408,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.FindByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||||
[Field.EQ("Value", "another")]);
|
|
||||||
Expect.equal(docs.Count, 1, "There should have been one document returned");
|
Expect.equal(docs.Count, 1, "There should have been one document returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
TestCase("succeeds when documents are not found", async () =>
|
||||||
@@ -459,38 +417,11 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.FindByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "mauve"));
|
||||||
[Field.EQ("Value", "mauve")]);
|
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindByFieldsOrdered",
|
#pragma warning restore CS0618
|
||||||
[
|
|
||||||
TestCase("succeeds when documents are found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Value", "purple")], [Field.Named("Id")]);
|
|
||||||
Expect.hasLength(docs, 2, "There should have been two document returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "five|four",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Value", "purple")], [Field.Named("Id DESC")]);
|
|
||||||
Expect.hasLength(docs, 2, "There should have been two document returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindByContains",
|
TestList("FindByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are found", async () =>
|
TestCase("succeeds when documents are found", async () =>
|
||||||
@@ -513,34 +444,6 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindByContainsOrdered",
|
|
||||||
[
|
|
||||||
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByContainsOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar")]);
|
|
||||||
Expect.hasLength(docs, 2, "There should have been two documents returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "two|four",
|
|
||||||
"Documents not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByContainsOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
new { Sub = new { Foo = "green" } }, [Field.Named("Sub.Bar DESC")]);
|
|
||||||
Expect.hasLength(docs, 2, "There should have been two documents returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|two",
|
|
||||||
"Documents not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindByJsonPath",
|
TestList("FindByJsonPath",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are found", async () =>
|
TestCase("succeeds when documents are found", async () =>
|
||||||
@@ -562,35 +465,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindByJsonPathOrdered",
|
#pragma warning disable CS0618
|
||||||
[
|
TestList("FindFirstByField",
|
||||||
// Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByJsonPathOrdered<JsonDocument>(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
|
|
||||||
[Field.Named("n:NumValue")]);
|
|
||||||
Expect.hasLength(docs, 3, "There should have been 3 documents returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "one|three|two",
|
|
||||||
"Documents not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByJsonPathOrdered<JsonDocument>(PostgresDb.TableName, "$.NumValue ? (@ < 15)",
|
|
||||||
[Field.Named("n:NumValue DESC")]);
|
|
||||||
Expect.hasLength(docs, 3, "There should have been 3 documents returned");
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "two|three|one",
|
|
||||||
"Documents not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindFirstByFields",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
{
|
{
|
||||||
@@ -598,8 +474,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||||
[Field.EQ("Value", "another")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc.Id, "two", "The incorrect document was returned");
|
Expect.equal(doc.Id, "two", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -609,10 +484,9 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||||
[Field.EQ("Value", "purple")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.contains(["five", "four"], doc.Id, "An incorrect document was returned");
|
Expect.contains(new[] { "five", "four" }, doc.Id, "An incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
TestCase("succeeds when a document is not found", async () =>
|
||||||
{
|
{
|
||||||
@@ -620,36 +494,11 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "absent"));
|
||||||
[Field.EQ("Value", "absent")]);
|
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindFirstByFieldsOrdered",
|
#pragma warning restore CS0618
|
||||||
[
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Value", "purple")], [Field.Named("Id")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("five", doc.Id, "An incorrect document was returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Value", "purple")], [Field.Named("Id DESC")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("four", doc.Id, "An incorrect document was returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindFirstByContains",
|
TestList("FindFirstByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
@@ -671,7 +520,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
var doc = await conn.FindFirstByContains<JsonDocument>(PostgresDb.TableName,
|
var doc = await conn.FindFirstByContains<JsonDocument>(PostgresDb.TableName,
|
||||||
new { Sub = new { Foo = "green" } });
|
new { Sub = new { Foo = "green" } });
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned");
|
Expect.contains(new[] { "two", "four" }, doc.Id, "An incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
TestCase("succeeds when a document is not found", async () =>
|
||||||
{
|
{
|
||||||
@@ -683,31 +532,6 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindFirstByContainsOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByContainsOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
new { Sub = new { Foo = "green" } }, [Field.Named("Value")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("two", doc.Id, "An incorrect document was returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByContainsOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
new { Sub = new { Foo = "green" } }, [Field.Named("Value DESC")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("four", doc.Id, "An incorrect document was returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindFirstByJsonPath",
|
TestList("FindFirstByJsonPath",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
@@ -730,7 +554,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
var doc = await conn.FindFirstByJsonPath<JsonDocument>(PostgresDb.TableName,
|
var doc = await conn.FindFirstByJsonPath<JsonDocument>(PostgresDb.TableName,
|
||||||
"$.Sub.Foo ? (@ == \"green\")");
|
"$.Sub.Foo ? (@ == \"green\")");
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned");
|
Expect.contains(new[] { "two", "four" }, doc.Id, "An incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
TestCase("succeeds when a document is not found", async () =>
|
||||||
{
|
{
|
||||||
@@ -742,31 +566,6 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindFirstByJsonPathOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByJsonPathOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
"$.Sub.Foo ? (@ == \"green\")", [Field.Named("Sub.Bar")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("two", doc.Id, "An incorrect document was returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = PostgresDb.BuildDb();
|
|
||||||
await using var conn = MkConn(db);
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByJsonPathOrdered<JsonDocument>(PostgresDb.TableName,
|
|
||||||
"$.Sub.Foo ? (@ == \"green\")", [Field.Named("Sub.Bar DESC")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("four", doc.Id, "An incorrect document was returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("UpdateById",
|
TestList("UpdateById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
@@ -851,7 +650,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await conn.PatchById(PostgresDb.TableName, "test", new { Foo = "green" });
|
await conn.PatchById(PostgresDb.TableName, "test", new { Foo = "green" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("PatchByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("PatchByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
{
|
{
|
||||||
@@ -859,10 +659,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.PatchByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")],
|
await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||||
new { NumValue = 77 });
|
var after = await conn.CountByField(PostgresDb.TableName, Field.EQ("NumValue", "77"));
|
||||||
var after = await conn.CountByFields(PostgresDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("NumValue", "77")]);
|
|
||||||
Expect.equal(after, 2, "There should have been 2 documents returned");
|
Expect.equal(after, 2, "There should have been 2 documents returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is updated", async () =>
|
TestCase("succeeds when no document is updated", async () =>
|
||||||
@@ -873,10 +671,10 @@ public class PostgresCSharpExtensionTests
|
|||||||
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
|
||||||
await conn.PatchByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("Value", "burgundy")],
|
await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||||
new { Foo = "green" });
|
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("PatchByContains",
|
TestList("PatchByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
@@ -931,7 +729,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Sub", "Value"]);
|
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -943,7 +741,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Sub"]);
|
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "Sub" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
||||||
@@ -956,7 +754,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["AFieldThatIsNotThere"]);
|
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -964,10 +762,11 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", ["Value"]);
|
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("RemoveFieldsByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("RemoveFieldsByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when multiple fields are removed", async () =>
|
TestCase("succeeds when multiple fields are removed", async () =>
|
||||||
{
|
{
|
||||||
@@ -975,8 +774,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
|
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"),
|
||||||
["Sub", "Value"]);
|
new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -988,8 +787,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
|
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "Sub" });
|
||||||
["Sub"]);
|
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
||||||
@@ -1002,8 +800,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
|
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "Nothing" });
|
||||||
["Nothing"]);
|
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -1011,10 +808,11 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByFields(PostgresDb.TableName, FieldMatch.Any,
|
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.NE("Abracadabra", "apple"),
|
||||||
[Field.NE("Abracadabra", "apple")], ["Value"]);
|
new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("RemoveFieldsByContains",
|
TestList("RemoveFieldsByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when multiple fields are removed", async () =>
|
TestCase("succeeds when multiple fields are removed", async () =>
|
||||||
@@ -1023,7 +821,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub", "Value"]);
|
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 },
|
||||||
|
new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -1035,7 +834,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub"]);
|
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "Sub" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
||||||
@@ -1048,7 +847,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Nothing"]);
|
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "Nothing" });
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -1056,7 +855,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { Abracadabra = "apple" }, ["Value"]);
|
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { Abracadabra = "apple" },
|
||||||
|
new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("RemoveFieldsByJsonPath",
|
TestList("RemoveFieldsByJsonPath",
|
||||||
@@ -1067,7 +867,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub", "Value"]);
|
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)",
|
||||||
|
new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -1079,7 +880,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub"]);
|
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "Sub" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
|
||||||
@@ -1092,7 +893,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Nothing"]);
|
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "Nothing" });
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -1100,7 +901,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")", ["Value"]);
|
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")",
|
||||||
|
new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("DeleteById",
|
TestList("DeleteById",
|
||||||
@@ -1126,7 +928,8 @@ public class PostgresCSharpExtensionTests
|
|||||||
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("DeleteByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("DeleteByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are deleted", async () =>
|
TestCase("succeeds when documents are deleted", async () =>
|
||||||
{
|
{
|
||||||
@@ -1134,7 +937,7 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.DeleteByFields(PostgresDb.TableName, FieldMatch.Any, [Field.NE("Value", "purple")]);
|
await conn.DeleteByField(PostgresDb.TableName, Field.NE("Value", "purple"));
|
||||||
var remaining = await conn.CountAll(PostgresDb.TableName);
|
var remaining = await conn.CountAll(PostgresDb.TableName);
|
||||||
Expect.equal(remaining, 2, "There should have been 2 documents remaining");
|
Expect.equal(remaining, 2, "There should have been 2 documents remaining");
|
||||||
}),
|
}),
|
||||||
@@ -1144,11 +947,12 @@ public class PostgresCSharpExtensionTests
|
|||||||
await using var conn = MkConn(db);
|
await using var conn = MkConn(db);
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.DeleteByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("Value", "crimson")]);
|
await conn.DeleteByField(PostgresDb.TableName, Field.EQ("Value", "crimson"));
|
||||||
var remaining = await conn.CountAll(PostgresDb.TableName);
|
var remaining = await conn.CountAll(PostgresDb.TableName);
|
||||||
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("DeleteByContains",
|
TestList("DeleteByContains",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are deleted", async () =>
|
TestCase("succeeds when documents are deleted", async () =>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
|||||||
using Expecto.CSharp;
|
using Expecto.CSharp;
|
||||||
using Expecto;
|
using Expecto;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
using BitBadger.Documents.Sqlite;
|
using BitBadger.Documents.Sqlite;
|
||||||
|
|
||||||
namespace BitBadger.Documents.Tests.CSharp;
|
namespace BitBadger.Documents.Tests.CSharp;
|
||||||
@@ -28,7 +29,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Parameters.Id("one")], Results.FromData<JsonDocument>);
|
new[] { Parameters.Id("one") }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc!.Id, "one", "The incorrect document was returned");
|
Expect.equal(doc!.Id, "one", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -39,7 +40,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await conn.CustomSingle($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Parameters.Id("eighty")], Results.FromData<JsonDocument>);
|
new[] { Parameters.Id("eighty") }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
@@ -51,7 +52,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.CustomList(Query.Find(SqliteDb.TableName), Parameters.None,
|
var docs = await conn.CustomList(Query.SelectFromTable(SqliteDb.TableName), Parameters.None,
|
||||||
Results.FromData<JsonDocument>);
|
Results.FromData<JsonDocument>);
|
||||||
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
||||||
}),
|
}),
|
||||||
@@ -62,8 +63,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.CustomList(
|
var docs = await conn.CustomList(
|
||||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", [new("@value", 100)],
|
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||||
Results.FromData<JsonDocument>);
|
new[] { new SqliteParameter("@value", 100) }, Results.FromData<JsonDocument>);
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
@@ -87,7 +88,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.CustomNonQuery($"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
await conn.CustomNonQuery($"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||||
[new("@value", 100)]);
|
new[] { new SqliteParameter("@value", 100) });
|
||||||
|
|
||||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 5L, "There should be 5 documents remaining in the table");
|
Expect.equal(remaining, 5L, "There should be 5 documents remaining in the table");
|
||||||
@@ -106,41 +107,38 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
|
|
||||||
var exists = await ItExists("ensured");
|
Func<string, ValueTask<bool>> itExists = async name =>
|
||||||
var alsoExists = await ItExists("idx_ensured_key");
|
await conn.CustomScalar(
|
||||||
|
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
||||||
|
new SqliteParameter[] { new("@name", name) }, Results.ToExists);
|
||||||
|
|
||||||
|
var exists = await itExists("ensured");
|
||||||
|
var alsoExists = await itExists("idx_ensured_key");
|
||||||
Expect.isFalse(exists, "The table should not exist already");
|
Expect.isFalse(exists, "The table should not exist already");
|
||||||
Expect.isFalse(alsoExists, "The key index should not exist already");
|
Expect.isFalse(alsoExists, "The key index should not exist already");
|
||||||
|
|
||||||
await conn.EnsureTable("ensured");
|
await conn.EnsureTable("ensured");
|
||||||
|
|
||||||
exists = await ItExists("ensured");
|
exists = await itExists("ensured");
|
||||||
alsoExists = await ItExists("idx_ensured_key");
|
alsoExists = await itExists("idx_ensured_key");
|
||||||
Expect.isTrue(exists, "The table should now exist");
|
Expect.isTrue(exists, "The table should now exist");
|
||||||
Expect.isTrue(alsoExists, "The key index should now exist");
|
Expect.isTrue(alsoExists, "The key index should now exist");
|
||||||
return;
|
|
||||||
|
|
||||||
Task<bool> ItExists(string name) =>
|
|
||||||
conn.CustomScalar($"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
|
||||||
[new("@name", name)], Results.ToExists);
|
|
||||||
}),
|
}),
|
||||||
TestCase("EnsureFieldIndex succeeds", async () =>
|
TestCase("EnsureFieldIndex succeeds", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
|
var indexExists = () => conn.CustomScalar(
|
||||||
|
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
||||||
|
Parameters.None, Results.ToExists);
|
||||||
|
|
||||||
var exists = await IndexExists();
|
var exists = await indexExists();
|
||||||
Expect.isFalse(exists, "The index should not exist already");
|
Expect.isFalse(exists, "The index should not exist already");
|
||||||
|
|
||||||
await conn.EnsureTable("ensured");
|
await conn.EnsureTable("ensured");
|
||||||
await conn.EnsureFieldIndex("ensured", "test", ["Id", "Category"]);
|
await conn.EnsureFieldIndex("ensured", "test", new[] { "Id", "Category" });
|
||||||
exists = await IndexExists();
|
exists = await indexExists();
|
||||||
Expect.isTrue(exists, "The index should now exist");
|
Expect.isTrue(exists, "The index should now exist");
|
||||||
return;
|
|
||||||
|
|
||||||
Task<bool> IndexExists() =>
|
|
||||||
conn.CustomScalar(
|
|
||||||
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
|
||||||
Parameters.None, Results.ToExists);
|
|
||||||
}),
|
}),
|
||||||
TestList("Insert",
|
TestList("Insert",
|
||||||
[
|
[
|
||||||
@@ -215,15 +213,17 @@ public static class SqliteCSharpExtensionTests
|
|||||||
var theCount = await conn.CountAll(SqliteDb.TableName);
|
var theCount = await conn.CountAll(SqliteDb.TableName);
|
||||||
Expect.equal(theCount, 5L, "There should have been 5 matching documents");
|
Expect.equal(theCount, 5L, "There should have been 5 matching documents");
|
||||||
}),
|
}),
|
||||||
|
#pragma warning disable CS0618
|
||||||
TestCase("CountByField succeeds", async () =>
|
TestCase("CountByField succeeds", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var theCount = await conn.CountByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")]);
|
var theCount = await conn.CountByField(SqliteDb.TableName, Field.EQ("Value", "purple"));
|
||||||
Expect.equal(theCount, 2L, "There should have been 2 matching documents");
|
Expect.equal(theCount, 2L, "There should have been 2 matching documents");
|
||||||
}),
|
}),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("ExistsById",
|
TestList("ExistsById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document exists", async () =>
|
TestCase("succeeds when a document exists", async () =>
|
||||||
@@ -245,7 +245,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
Expect.isFalse(exists, "There should not have been an existing document");
|
Expect.isFalse(exists, "There should not have been an existing document");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ExistsByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ExistsByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents exist", async () =>
|
TestCase("succeeds when documents exist", async () =>
|
||||||
{
|
{
|
||||||
@@ -253,7 +254,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists = await conn.ExistsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.GE("NumValue", 10)]);
|
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.GE("NumValue", 10));
|
||||||
Expect.isTrue(exists, "There should have been existing documents");
|
Expect.isTrue(exists, "There should have been existing documents");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no matching documents exist", async () =>
|
TestCase("succeeds when no matching documents exist", async () =>
|
||||||
@@ -262,11 +263,11 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists =
|
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.EQ("Nothing", "none"));
|
||||||
await conn.ExistsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Nothing", "none")]);
|
|
||||||
Expect.isFalse(exists, "There should not have been any existing documents");
|
Expect.isFalse(exists, "There should not have been any existing documents");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("FindAll",
|
TestList("FindAll",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when there is data", async () =>
|
TestCase("succeeds when there is data", async () =>
|
||||||
@@ -289,43 +290,6 @@ public static class SqliteCSharpExtensionTests
|
|||||||
Expect.isEmpty(results, "There should have been no documents returned");
|
Expect.isEmpty(results, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindAllOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when ordering numerically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "one|three|two|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering numerically descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results =
|
|
||||||
await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "five|four|two|three|one",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering alphabetically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await conn.FindAllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("Id DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "two|three|one|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindById",
|
TestList("FindById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
@@ -348,7 +312,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("FindByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are found", async () =>
|
TestCase("succeeds when documents are found", async () =>
|
||||||
{
|
{
|
||||||
@@ -356,8 +321,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.FindByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.GT("NumValue", 15));
|
||||||
[Field.GT("NumValue", 15)]);
|
|
||||||
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
TestCase("succeeds when documents are not found", async () =>
|
||||||
@@ -366,37 +330,11 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await conn.FindByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "mauve"));
|
||||||
[Field.EQ("Value", "mauve")]);
|
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFieldsOrdered",
|
TestList("FindFirstByField",
|
||||||
[
|
|
||||||
TestCase("succeeds when documents are found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.GT("NumValue", 15)], [Field.Named("Id")]);
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "five|four",
|
|
||||||
"There should have been two documents returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await conn.FindByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.GT("NumValue", 15)], [Field.Named("Id DESC")]);
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|five",
|
|
||||||
"There should have been two documents returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FindFirstByFields",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
{
|
{
|
||||||
@@ -404,8 +342,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "another"));
|
||||||
[Field.EQ("Value", "another")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -415,10 +352,9 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Sub.Foo", "green"));
|
||||||
[Field.EQ("Sub.Foo", "green")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.contains(["two", "four"], doc!.Id, "An incorrect document was returned");
|
Expect.contains(new[] { "two", "four" }, doc!.Id, "An incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
TestCase("succeeds when a document is not found", async () =>
|
||||||
{
|
{
|
||||||
@@ -426,36 +362,11 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "absent"));
|
||||||
[Field.EQ("Value", "absent")]);
|
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("FindFirstByFieldsOrdered",
|
#pragma warning restore CS0618
|
||||||
[
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("two", doc!.Id, "An incorrect document was returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await using var conn = Sqlite.Configuration.DbConn();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await conn.FindFirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("four", doc!.Id, "An incorrect document was returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("UpdateById",
|
TestList("UpdateById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
@@ -539,7 +450,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await conn.PatchById(SqliteDb.TableName, "test", new { Foo = "green" });
|
await conn.PatchById(SqliteDb.TableName, "test", new { Foo = "green" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("PatchByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("PatchByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
{
|
{
|
||||||
@@ -547,9 +459,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.PatchByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")],
|
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||||
new { NumValue = 77 });
|
var after = await conn.CountByField(SqliteDb.TableName, Field.EQ("NumValue", 77));
|
||||||
var after = await conn.CountByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 77)]);
|
|
||||||
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is updated", async () =>
|
TestCase("succeeds when no document is updated", async () =>
|
||||||
@@ -560,10 +471,10 @@ public static class SqliteCSharpExtensionTests
|
|||||||
Expect.isEmpty(before, "There should have been no documents returned");
|
Expect.isEmpty(before, "There should have been no documents returned");
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.PatchByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "burgundy")],
|
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||||
new { Foo = "green" });
|
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("RemoveFieldsById",
|
TestList("RemoveFieldsById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when fields are removed", async () =>
|
TestCase("succeeds when fields are removed", async () =>
|
||||||
@@ -572,7 +483,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["Sub", "Value"]);
|
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -585,7 +496,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["AFieldThatIsNotThere"]);
|
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -593,10 +504,11 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", ["Value"]);
|
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("RemoveFieldsByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("RemoveFieldsByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a field is removed", async () =>
|
TestCase("succeeds when a field is removed", async () =>
|
||||||
{
|
{
|
||||||
@@ -604,8 +516,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)],
|
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Sub" });
|
||||||
["Sub"]);
|
|
||||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||||
@@ -617,8 +528,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)],
|
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Nothing" });
|
||||||
["Nothing"]);
|
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
@@ -626,10 +536,10 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await conn.RemoveFieldsByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Abracadabra", "apple")],
|
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.NE("Abracadabra", "apple"), new[] { "Value" });
|
||||||
["Value"]);
|
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestList("DeleteById",
|
TestList("DeleteById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is deleted", async () =>
|
TestCase("succeeds when a document is deleted", async () =>
|
||||||
@@ -653,7 +563,8 @@ public static class SqliteCSharpExtensionTests
|
|||||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("DeleteByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("DeleteByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are deleted", async () =>
|
TestCase("succeeds when documents are deleted", async () =>
|
||||||
{
|
{
|
||||||
@@ -661,7 +572,7 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.DeleteByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Value", "purple")]);
|
await conn.DeleteByField(SqliteDb.TableName, Field.NE("Value", "purple"));
|
||||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
||||||
}),
|
}),
|
||||||
@@ -671,11 +582,12 @@ public static class SqliteCSharpExtensionTests
|
|||||||
await using var conn = Sqlite.Configuration.DbConn();
|
await using var conn = Sqlite.Configuration.DbConn();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await conn.DeleteByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "crimson")]);
|
await conn.DeleteByField(SqliteDb.TableName, Field.EQ("Value", "crimson"));
|
||||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
|
#pragma warning restore CS0618
|
||||||
TestCase("Clean up database", () => Sqlite.Configuration.UseConnectionString("data source=:memory:"))
|
TestCase("Clean up database", () => Sqlite.Configuration.UseConnectionString("data source=:memory:"))
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Expecto.CSharp;
|
using Expecto.CSharp;
|
||||||
using Expecto;
|
using Expecto;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
using Microsoft.FSharp.Core;
|
using Microsoft.FSharp.Core;
|
||||||
using BitBadger.Documents.Sqlite;
|
using BitBadger.Documents.Sqlite;
|
||||||
|
|
||||||
@@ -13,16 +14,19 @@ using static Runner;
|
|||||||
public static class SqliteCSharpTests
|
public static class SqliteCSharpTests
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unit tests for the Query module of the SQLite library
|
/// Unit tests for the SQLite library
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Test QueryTests = TestList("Query",
|
private static readonly Test Unit = TestList("Unit",
|
||||||
|
[
|
||||||
|
TestList("Query",
|
||||||
[
|
[
|
||||||
TestList("WhereByFields",
|
TestList("WhereByFields",
|
||||||
[
|
[
|
||||||
TestCase("succeeds for a single field when a logical operator is passed", () =>
|
TestCase("succeeds for a single field when a logical operator is passed", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(
|
Expect.equal(
|
||||||
Sqlite.Query.WhereByFields(FieldMatch.Any, [Field.GT("theField", 0).WithParameterName("@test")]),
|
Sqlite.Query.WhereByFields(FieldMatch.Any,
|
||||||
|
[Field.GT("theField", 0).WithParameterName("@test")]),
|
||||||
"data->>'theField' > @test", "WHERE clause not correct");
|
"data->>'theField' > @test", "WHERE clause not correct");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds for a single field when an existence operator is passed", () =>
|
TestCase("succeeds for a single field when an existence operator is passed", () =>
|
||||||
@@ -40,7 +44,8 @@ public static class SqliteCSharpTests
|
|||||||
TestCase("succeeds for all multiple fields with logical operators", () =>
|
TestCase("succeeds for all multiple fields with logical operators", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(
|
Expect.equal(
|
||||||
Sqlite.Query.WhereByFields(FieldMatch.All, [Field.EQ("theFirst", "1"), Field.EQ("numberTwo", "2")]),
|
Sqlite.Query.WhereByFields(FieldMatch.All,
|
||||||
|
[Field.EQ("theFirst", "1"), Field.EQ("numberTwo", "2")]),
|
||||||
"data->>'theFirst' = @field0 AND data->>'numberTwo' = @field1", "WHERE clause not correct");
|
"data->>'theFirst' = @field0 AND data->>'numberTwo' = @field1", "WHERE clause not correct");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds for any multiple fields with an existence operator", () =>
|
TestCase("succeeds for any multiple fields with an existence operator", () =>
|
||||||
@@ -75,7 +80,8 @@ public static class SqliteCSharpTests
|
|||||||
}),
|
}),
|
||||||
TestCase("ById succeeds", () =>
|
TestCase("ById succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Sqlite.Query.ById("test", "14"), "test WHERE data->>'Id' = @id", "By-ID query not correct");
|
Expect.equal(Sqlite.Query.ById("test", "14"), "test WHERE data->>'Id' = @id",
|
||||||
|
"By-ID query not correct");
|
||||||
}),
|
}),
|
||||||
TestCase("ByFields succeeds", () =>
|
TestCase("ByFields succeeds", () =>
|
||||||
{
|
{
|
||||||
@@ -87,12 +93,8 @@ public static class SqliteCSharpTests
|
|||||||
Expect.equal(Sqlite.Query.Definition.EnsureTable("tbl"),
|
Expect.equal(Sqlite.Query.Definition.EnsureTable("tbl"),
|
||||||
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)", "CREATE TABLE statement not correct");
|
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)", "CREATE TABLE statement not correct");
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("Parameters",
|
||||||
/// <summary>
|
|
||||||
/// Unit tests for the Parameters module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test ParametersTests = TestList("Parameters",
|
|
||||||
[
|
[
|
||||||
TestCase("Id succeeds", () =>
|
TestCase("Id succeeds", () =>
|
||||||
{
|
{
|
||||||
@@ -125,13 +127,10 @@ public static class SqliteCSharpTests
|
|||||||
{
|
{
|
||||||
Expect.isEmpty(Parameters.None, "The parameter list should have been empty");
|
Expect.isEmpty(Parameters.None, "The parameter list should have been empty");
|
||||||
})
|
})
|
||||||
|
])
|
||||||
|
// Results are exhaustively executed in the context of other tests
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Results are exhaustively executed in the context of other tests
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Documents used for integration tests
|
|
||||||
/// </summary>
|
|
||||||
private static readonly List<JsonDocument> TestDocuments =
|
private static readonly List<JsonDocument> TestDocuments =
|
||||||
[
|
[
|
||||||
new() { Id = "one", Value = "FIRST!", NumValue = 0 },
|
new() { Id = "one", Value = "FIRST!", NumValue = 0 },
|
||||||
@@ -149,10 +148,9 @@ public static class SqliteCSharpTests
|
|||||||
foreach (var doc in TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
foreach (var doc in TestDocuments) await Document.Insert(SqliteDb.TableName, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private static readonly Test Integration = TestList("Integration",
|
||||||
/// Integration tests for the Configuration module of the SQLite library
|
[
|
||||||
/// </summary>
|
TestCase("Configuration.UseConnectionString succeeds", () =>
|
||||||
private static readonly Test ConfigurationTests = TestCase("Configuration.UseConnectionString succeeds", () =>
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -164,12 +162,8 @@ public static class SqliteCSharpTests
|
|||||||
{
|
{
|
||||||
Sqlite.Configuration.UseConnectionString("Data Source=:memory:");
|
Sqlite.Configuration.UseConnectionString("Data Source=:memory:");
|
||||||
}
|
}
|
||||||
});
|
}),
|
||||||
|
TestList("Custom",
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Custom module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test CustomTests = TestList("Custom",
|
|
||||||
[
|
[
|
||||||
TestList("Single",
|
TestList("Single",
|
||||||
[
|
[
|
||||||
@@ -179,7 +173,7 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await Custom.Single($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await Custom.Single($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Parameters.Id("one")], Results.FromData<JsonDocument>);
|
new[] { Parameters.Id("one") }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc!.Id, "one", "The incorrect document was returned");
|
Expect.equal(doc!.Id, "one", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -189,7 +183,7 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await Custom.Single($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
var doc = await Custom.Single($"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id",
|
||||||
[Parameters.Id("eighty")], Results.FromData<JsonDocument>);
|
new[] { Parameters.Id("eighty") }, Results.FromData<JsonDocument>);
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
@@ -200,7 +194,7 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await Custom.List(Query.Find(SqliteDb.TableName), Parameters.None,
|
var docs = await Custom.List(Query.SelectFromTable(SqliteDb.TableName), Parameters.None,
|
||||||
Results.FromData<JsonDocument>);
|
Results.FromData<JsonDocument>);
|
||||||
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
Expect.equal(docs.Count, 5, "There should have been 5 documents returned");
|
||||||
}),
|
}),
|
||||||
@@ -210,8 +204,8 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await Custom.List(
|
var docs = await Custom.List(
|
||||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value", [new("@value", 100)],
|
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||||
Results.FromData<JsonDocument>);
|
new[] { new SqliteParameter("@value", 100) }, Results.FromData<JsonDocument>);
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
@@ -233,7 +227,7 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await Custom.NonQuery($"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
await Custom.NonQuery($"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value",
|
||||||
[new("@value", 100)]);
|
new[] { new SqliteParameter("@value", 100) });
|
||||||
|
|
||||||
var remaining = await Count.All(SqliteDb.TableName);
|
var remaining = await Count.All(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 5L, "There should be 5 documents remaining in the table");
|
Expect.equal(remaining, 5L, "There should be 5 documents remaining in the table");
|
||||||
@@ -246,12 +240,8 @@ public static class SqliteCSharpTests
|
|||||||
var nbr = await Custom.Scalar("SELECT 5 AS test_value", Parameters.None, rdr => rdr.GetInt32(0));
|
var nbr = await Custom.Scalar("SELECT 5 AS test_value", Parameters.None, rdr => rdr.GetInt32(0));
|
||||||
Expect.equal(nbr, 5, "The query should have returned the number 5");
|
Expect.equal(nbr, 5, "The query should have returned the number 5");
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("Definition",
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Definition module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test DefinitionTests = TestList("Definition",
|
|
||||||
[
|
[
|
||||||
TestCase("EnsureTable succeeds", async () =>
|
TestCase("EnsureTable succeeds", async () =>
|
||||||
{
|
{
|
||||||
@@ -272,8 +262,9 @@ public static class SqliteCSharpTests
|
|||||||
|
|
||||||
async ValueTask<bool> ItExists(string name)
|
async ValueTask<bool> ItExists(string name)
|
||||||
{
|
{
|
||||||
return await Custom.Scalar($"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
return await Custom.Scalar(
|
||||||
[new("@name", name)], Results.ToExists);
|
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it",
|
||||||
|
new SqliteParameter[] { new("@name", name) }, Results.ToExists);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
TestCase("EnsureFieldIndex succeeds", async () =>
|
TestCase("EnsureFieldIndex succeeds", async () =>
|
||||||
@@ -284,7 +275,7 @@ public static class SqliteCSharpTests
|
|||||||
Expect.isFalse(exists, "The index should not exist already");
|
Expect.isFalse(exists, "The index should not exist already");
|
||||||
|
|
||||||
await Definition.EnsureTable("ensured");
|
await Definition.EnsureTable("ensured");
|
||||||
await Definition.EnsureFieldIndex("ensured", "test", ["Id", "Category"]);
|
await Definition.EnsureFieldIndex("ensured", "test", new[] { "Id", "Category" });
|
||||||
exists = await IndexExists();
|
exists = await IndexExists();
|
||||||
Expect.isTrue(exists, "The index should now exist");
|
Expect.isTrue(exists, "The index should now exist");
|
||||||
return;
|
return;
|
||||||
@@ -293,14 +284,8 @@ public static class SqliteCSharpTests
|
|||||||
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it",
|
||||||
Parameters.None, Results.ToExists);
|
Parameters.None, Results.ToExists);
|
||||||
})
|
})
|
||||||
]);
|
]),
|
||||||
|
TestList("Document.Insert",
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Document module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test DocumentTests = TestList("Document",
|
|
||||||
[
|
|
||||||
TestList("Insert",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds", async () =>
|
TestCase("succeeds", async () =>
|
||||||
{
|
{
|
||||||
@@ -325,86 +310,9 @@ public static class SqliteCSharpTests
|
|||||||
{
|
{
|
||||||
// This is what is supposed to happen
|
// This is what is supposed to happen
|
||||||
}
|
}
|
||||||
}),
|
|
||||||
TestCase("succeeds when adding a numeric auto ID", async () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Number);
|
|
||||||
Configuration.UseIdField("Key");
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
var before = await Count.All(SqliteDb.TableName);
|
|
||||||
Expect.equal(before, 0L, "There should be no documents in the table");
|
|
||||||
|
|
||||||
await Document.Insert(SqliteDb.TableName, new NumIdDocument { Text = "one" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new NumIdDocument { Text = "two" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new NumIdDocument { Key = 77, Text = "three" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new NumIdDocument { Text = "four" });
|
|
||||||
|
|
||||||
var after = await Find.AllOrdered<NumIdDocument>(SqliteDb.TableName, [Field.Named("Key")]);
|
|
||||||
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
||||||
Expect.sequenceEqual(after.Select(x => x.Key), [1, 2, 77, 78],
|
|
||||||
"The IDs were not generated correctly");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
||||||
Configuration.UseIdField("Id");
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when adding a GUID auto ID", async () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Guid);
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
var before = await Count.All(SqliteDb.TableName);
|
|
||||||
Expect.equal(before, 0L, "There should be no documents in the table");
|
|
||||||
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "one" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "two" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Id = "abc123", Value = "three" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "four" });
|
|
||||||
|
|
||||||
var after = await Find.All<JsonDocument>(SqliteDb.TableName);
|
|
||||||
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
||||||
Expect.equal(after.Count(x => x.Id.Length == 32), 3, "Three of the IDs should have been GUIDs");
|
|
||||||
Expect.equal(after.Count(x => x.Id == "abc123"), 1, "The provided ID should have been used as-is");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when adding a RandomString auto ID", async () =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.RandomString);
|
|
||||||
Configuration.UseIdStringLength(44);
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
var before = await Count.All(SqliteDb.TableName);
|
|
||||||
Expect.equal(before, 0L, "There should be no documents in the table");
|
|
||||||
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "one" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "two" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Id = "abc123", Value = "three" });
|
|
||||||
await Document.Insert(SqliteDb.TableName, new JsonDocument { Value = "four" });
|
|
||||||
|
|
||||||
var after = await Find.All<JsonDocument>(SqliteDb.TableName);
|
|
||||||
Expect.hasLength(after, 4, "There should have been 4 documents returned");
|
|
||||||
Expect.equal(after.Count(x => x.Id.Length == 44), 3,
|
|
||||||
"Three of the IDs should have been 44-character random strings");
|
|
||||||
Expect.equal(after.Count(x => x.Id == "abc123"), 1, "The provided ID should have been used as-is");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Configuration.UseAutoIdStrategy(AutoId.Disabled);
|
|
||||||
Configuration.UseIdStringLength(16);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("Save",
|
TestList("Document.Save",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is inserted", async () =>
|
TestCase("succeeds when a document is inserted", async () =>
|
||||||
{
|
{
|
||||||
@@ -436,13 +344,8 @@ public static class SqliteCSharpTests
|
|||||||
Expect.equal(after!.Id, "test", "The updated document is not correct");
|
Expect.equal(after!.Id, "test", "The updated document is not correct");
|
||||||
Expect.isNull(after.Sub, "There should not have been a sub-document in the updated document");
|
Expect.isNull(after.Sub, "There should not have been a sub-document in the updated document");
|
||||||
})
|
})
|
||||||
])
|
]),
|
||||||
]);
|
TestList("Count",
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Count module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test CountTests = TestList("Count",
|
|
||||||
[
|
[
|
||||||
TestCase("All succeeds", async () =>
|
TestCase("All succeeds", async () =>
|
||||||
{
|
{
|
||||||
@@ -452,32 +355,26 @@ public static class SqliteCSharpTests
|
|||||||
var theCount = await Count.All(SqliteDb.TableName);
|
var theCount = await Count.All(SqliteDb.TableName);
|
||||||
Expect.equal(theCount, 5L, "There should have been 5 matching documents");
|
Expect.equal(theCount, 5L, "There should have been 5 matching documents");
|
||||||
}),
|
}),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
[
|
TestCase("ByField succeeds for numeric range", async () =>
|
||||||
TestCase("succeeds for numeric range", async () =>
|
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var theCount = await Count.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.BT("NumValue", 10, 20)]);
|
var theCount = await Count.ByField(SqliteDb.TableName, Field.BT("NumValue", 10, 20));
|
||||||
Expect.equal(theCount, 3L, "There should have been 3 matching documents");
|
Expect.equal(theCount, 3L, "There should have been 3 matching documents");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds for non-numeric range", async () =>
|
TestCase("ByField succeeds for non-numeric range", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var theCount = await Count.ByFields(SqliteDb.TableName, FieldMatch.Any,
|
var theCount = await Count.ByField(SqliteDb.TableName, Field.BT("Value", "aardvark", "apple"));
|
||||||
[Field.BT("Value", "aardvark", "apple")]);
|
|
||||||
Expect.equal(theCount, 1L, "There should have been 1 matching document");
|
Expect.equal(theCount, 1L, "There should have been 1 matching document");
|
||||||
})
|
})
|
||||||
])
|
#pragma warning restore CS0618
|
||||||
]);
|
]),
|
||||||
|
TestList("Exists",
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Exists module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test ExistsTests = TestList("Exists",
|
|
||||||
[
|
[
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
@@ -498,14 +395,15 @@ public static class SqliteCSharpTests
|
|||||||
Expect.isFalse(exists, "There should not have been an existing document");
|
Expect.isFalse(exists, "There should not have been an existing document");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents exist", async () =>
|
TestCase("succeeds when documents exist", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists = await Exists.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.GE("NumValue", 10)]);
|
var exists = await Exists.ByField(SqliteDb.TableName, Field.GE("NumValue", 10));
|
||||||
Expect.isTrue(exists, "There should have been existing documents");
|
Expect.isTrue(exists, "There should have been existing documents");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no matching documents exist", async () =>
|
TestCase("succeeds when no matching documents exist", async () =>
|
||||||
@@ -513,16 +411,13 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var exists = await Exists.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Nothing", "none")]);
|
var exists = await Exists.ByField(SqliteDb.TableName, Field.EQ("Nothing", "none"));
|
||||||
Expect.isFalse(exists, "There should not have been any existing documents");
|
Expect.isFalse(exists, "There should not have been any existing documents");
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
]);
|
#pragma warning restore CS0618
|
||||||
|
]),
|
||||||
/// <summary>
|
TestList("Find",
|
||||||
/// Integration tests for the Find module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test FindTests = TestList("Find",
|
|
||||||
[
|
[
|
||||||
TestList("All",
|
TestList("All",
|
||||||
[
|
[
|
||||||
@@ -544,39 +439,6 @@ public static class SqliteCSharpTests
|
|||||||
Expect.isEmpty(results, "There should have been no documents returned");
|
Expect.isEmpty(results, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("AllOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when ordering numerically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await Find.AllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "one|three|two|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering numerically descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await Find.AllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("n:NumValue DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "five|four|two|three|one",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when ordering alphabetically", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var results = await Find.AllOrdered<JsonDocument>(SqliteDb.TableName, [Field.Named("Id DESC")]);
|
|
||||||
Expect.hasLength(results, 5, "There should have been 5 documents returned");
|
|
||||||
Expect.equal(string.Join('|', results.Select(x => x.Id)), "two|three|one|four|five",
|
|
||||||
"The documents were not ordered correctly");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
@@ -597,15 +459,15 @@ public static class SqliteCSharpTests
|
|||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are found", async () =>
|
TestCase("succeeds when documents are found", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await Find.ByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, Field.GT("NumValue", 15));
|
||||||
[Field.GT("NumValue", 15)]);
|
|
||||||
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
TestCase("succeeds when documents are not found", async () =>
|
||||||
@@ -613,43 +475,18 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var docs = await Find.ByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "mauve"));
|
||||||
[Field.EQ("Value", "mauve")]);
|
|
||||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFieldsOrdered",
|
TestList("FirstByField",
|
||||||
[
|
|
||||||
TestCase("succeeds when documents are found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await Find.ByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.GT("NumValue", 15)], [Field.Named("Id")]);
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "five|four",
|
|
||||||
"There should have been two documents returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when documents are not found", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var docs = await Find.ByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.GT("NumValue", 15)], [Field.Named("Id DESC")]);
|
|
||||||
Expect.equal(string.Join('|', docs.Select(x => x.Id)), "four|five",
|
|
||||||
"There should have been two documents returned");
|
|
||||||
})
|
|
||||||
]),
|
|
||||||
TestList("FirstByFields",
|
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is found", async () =>
|
TestCase("succeeds when a document is found", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await Find.FirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "another"));
|
||||||
[Field.EQ("Value", "another")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
@@ -658,50 +495,22 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await Find.FirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Sub.Foo", "green"));
|
||||||
[Field.EQ("Sub.Foo", "green")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
Expect.isNotNull(doc, "There should have been a document returned");
|
||||||
Expect.contains(["two", "four"], doc!.Id, "An incorrect document was returned");
|
Expect.contains(new[] { "two", "four" }, doc!.Id, "An incorrect document was returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when a document is not found", async () =>
|
TestCase("succeeds when a document is not found", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
var doc = await Find.FirstByFields<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "absent"));
|
||||||
[Field.EQ("Value", "absent")]);
|
|
||||||
Expect.isNull(doc, "There should not have been a document returned");
|
Expect.isNull(doc, "There should not have been a document returned");
|
||||||
})
|
})
|
||||||
]),
|
|
||||||
TestList("FirstByFieldsOrdered",
|
|
||||||
[
|
|
||||||
TestCase("succeeds when sorting ascending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await Find.FirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("two", doc!.Id, "An incorrect document was returned");
|
|
||||||
}),
|
|
||||||
TestCase("succeeds when sorting descending", async () =>
|
|
||||||
{
|
|
||||||
await using var db = await SqliteDb.BuildDb();
|
|
||||||
await LoadDocs();
|
|
||||||
|
|
||||||
var doc = await Find.FirstByFieldsOrdered<JsonDocument>(SqliteDb.TableName, FieldMatch.Any,
|
|
||||||
[Field.EQ("Sub.Foo", "green")], [Field.Named("Sub.Bar DESC")]);
|
|
||||||
Expect.isNotNull(doc, "There should have been a document returned");
|
|
||||||
Expect.equal("four", doc!.Id, "An incorrect document was returned");
|
|
||||||
})
|
|
||||||
])
|
])
|
||||||
]);
|
#pragma warning restore CS0618
|
||||||
|
]),
|
||||||
/// <summary>
|
TestList("Update",
|
||||||
/// Integration tests for the Update module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test UpdateTests = TestList("Update",
|
|
||||||
[
|
[
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
@@ -759,12 +568,8 @@ public static class SqliteCSharpTests
|
|||||||
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
|
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
]);
|
]),
|
||||||
|
TestList("Patch",
|
||||||
/// <summary>
|
|
||||||
/// Integration tests for the Patch module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test PatchTests = TestList("Patch",
|
|
||||||
[
|
[
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
@@ -790,16 +595,16 @@ public static class SqliteCSharpTests
|
|||||||
await Patch.ById(SqliteDb.TableName, "test", new { Foo = "green" });
|
await Patch.ById(SqliteDb.TableName, "test", new { Foo = "green" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a document is updated", async () =>
|
TestCase("succeeds when a document is updated", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await Patch.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")],
|
await Patch.ByField(SqliteDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||||
new { NumValue = 77 });
|
var after = await Count.ByField(SqliteDb.TableName, Field.EQ("NumValue", 77));
|
||||||
var after = await Count.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 77)]);
|
|
||||||
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is updated", async () =>
|
TestCase("succeeds when no document is updated", async () =>
|
||||||
@@ -810,16 +615,12 @@ public static class SqliteCSharpTests
|
|||||||
Expect.isEmpty(before, "There should have been no documents returned");
|
Expect.isEmpty(before, "There should have been no documents returned");
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await Patch.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("Value", "burgundy")],
|
await Patch.ByField(SqliteDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||||
new { Foo = "green" });
|
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
]);
|
#pragma warning restore CS0618
|
||||||
|
]),
|
||||||
/// <summary>
|
TestList("RemoveFields",
|
||||||
/// Integration tests for the RemoveFields module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test RemoveFieldsTests = TestList("RemoveFields",
|
|
||||||
[
|
[
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
@@ -828,7 +629,7 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await RemoveFields.ById(SqliteDb.TableName, "two", ["Sub", "Value"]);
|
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "Sub", "Value" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||||
@@ -840,24 +641,25 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await RemoveFields.ById(SqliteDb.TableName, "two", ["AFieldThatIsNotThere"]);
|
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await RemoveFields.ById(SqliteDb.TableName, "two", ["Value"]);
|
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "Value" });
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when a field is removed", async () =>
|
TestCase("succeeds when a field is removed", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await RemoveFields.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)], ["Sub"]);
|
await RemoveFields.ByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Sub" });
|
||||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
||||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||||
@@ -868,24 +670,19 @@ public static class SqliteCSharpTests
|
|||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await RemoveFields.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", 17)],
|
await RemoveFields.ByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Nothing" });
|
||||||
["Nothing"]);
|
|
||||||
}),
|
}),
|
||||||
TestCase("succeeds when no document is matched", async () =>
|
TestCase("succeeds when no document is matched", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
await RemoveFields.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Abracadabra", "apple")],
|
await RemoveFields.ByField(SqliteDb.TableName, Field.NE("Abracadabra", "apple"), new[] { "Value" });
|
||||||
["Value"]);
|
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
]);
|
#pragma warning restore CS0618
|
||||||
|
]),
|
||||||
/// <summary>
|
TestList("Delete",
|
||||||
/// Integration tests for the Delete module of the SQLite library
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Test DeleteTests = TestList("Delete",
|
|
||||||
[
|
[
|
||||||
TestList("ById",
|
TestList("ById",
|
||||||
[
|
[
|
||||||
@@ -908,14 +705,15 @@ public static class SqliteCSharpTests
|
|||||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
]),
|
]),
|
||||||
TestList("ByFields",
|
#pragma warning disable CS0618
|
||||||
|
TestList("ByField",
|
||||||
[
|
[
|
||||||
TestCase("succeeds when documents are deleted", async () =>
|
TestCase("succeeds when documents are deleted", async () =>
|
||||||
{
|
{
|
||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await Delete.ByFields(SqliteDb.TableName, FieldMatch.Any, [Field.NE("Value", "purple")]);
|
await Delete.ByField(SqliteDb.TableName, Field.NE("Value", "purple"));
|
||||||
var remaining = await Count.All(SqliteDb.TableName);
|
var remaining = await Count.All(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
||||||
}),
|
}),
|
||||||
@@ -924,34 +722,19 @@ public static class SqliteCSharpTests
|
|||||||
await using var db = await SqliteDb.BuildDb();
|
await using var db = await SqliteDb.BuildDb();
|
||||||
await LoadDocs();
|
await LoadDocs();
|
||||||
|
|
||||||
await Delete.ByFields(SqliteDb.TableName, FieldMatch.All, [Field.EQ("Value", "crimson")]);
|
await Delete.ByField(SqliteDb.TableName, Field.EQ("Value", "crimson"));
|
||||||
var remaining = await Count.All(SqliteDb.TableName);
|
var remaining = await Count.All(SqliteDb.TableName);
|
||||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
|
#pragma warning restore CS0618
|
||||||
|
]),
|
||||||
|
TestCase("Clean up database", () => Sqlite.Configuration.UseConnectionString("data source=:memory:"))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All tests for SQLite C# functions and methods
|
/// All tests for SQLite C# functions and methods
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Tests]
|
[Tests]
|
||||||
public static readonly Test All = TestList("Sqlite.C#",
|
public static readonly Test All = TestList("Sqlite.C#", [Unit, TestSequenced(Integration)]);
|
||||||
[
|
|
||||||
TestList("Unit", [QueryTests, ParametersTests]),
|
|
||||||
TestSequenced(TestList("Integration",
|
|
||||||
[
|
|
||||||
ConfigurationTests,
|
|
||||||
CustomTests,
|
|
||||||
DefinitionTests,
|
|
||||||
DocumentTests,
|
|
||||||
CountTests,
|
|
||||||
ExistsTests,
|
|
||||||
FindTests,
|
|
||||||
UpdateTests,
|
|
||||||
PatchTests,
|
|
||||||
RemoveFieldsTests,
|
|
||||||
DeleteTests,
|
|
||||||
TestCase("Clean up database", () => Sqlite.Configuration.UseConnectionString("data source=:memory:"))
|
|
||||||
]))
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
namespace BitBadger.Documents.Tests.CSharp;
|
namespace BitBadger.Documents.Tests.CSharp;
|
||||||
|
|
||||||
public class NumIdDocument
|
|
||||||
{
|
|
||||||
public int Key { get; set; } = 0;
|
|
||||||
public string Text { get; set; } = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SubDocument
|
public class SubDocument
|
||||||
{
|
{
|
||||||
public string Foo { get; set; } = "";
|
public string Foo { get; set; } = "";
|
||||||
|
|||||||
@@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<NoWarn>1182</NoWarn>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Types.fs" />
|
|
||||||
<Compile Include="CommonTests.fs" />
|
<Compile Include="CommonTests.fs" />
|
||||||
|
<Compile Include="Types.fs" />
|
||||||
<Compile Include="PostgresTests.fs" />
|
<Compile Include="PostgresTests.fs" />
|
||||||
<Compile Include="PostgresExtensionTests.fs" />
|
<Compile Include="PostgresExtensionTests.fs" />
|
||||||
<Compile Include="SqliteTests.fs" />
|
<Compile Include="SqliteTests.fs" />
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ open Expecto
|
|||||||
/// Test table name
|
/// Test table name
|
||||||
let tbl = "test_table"
|
let tbl = "test_table"
|
||||||
|
|
||||||
/// Unit tests for the Op DU
|
/// Tests which do not hit the database
|
||||||
let opTests = testList "Op" [
|
let all =
|
||||||
|
testList "Common" [
|
||||||
|
testList "Op" [
|
||||||
test "EQ succeeds" {
|
test "EQ succeeds" {
|
||||||
Expect.equal (string EQ) "=" "The equals operator was not correct"
|
Expect.equal (string EQ) "=" "The equals operator was not correct"
|
||||||
}
|
}
|
||||||
@@ -36,9 +38,7 @@ let opTests = testList "Op" [
|
|||||||
Expect.equal (string NEX) "IS NULL" """The "not exists" operator was not correct"""
|
Expect.equal (string NEX) "IS NULL" """The "not exists" operator was not correct"""
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Field" [
|
||||||
/// Unit tests for the Field class
|
|
||||||
let fieldTests = testList "Field" [
|
|
||||||
test "EQ succeeds" {
|
test "EQ succeeds" {
|
||||||
let field = Field.EQ "Test" 14
|
let field = Field.EQ "Test" 14
|
||||||
Expect.equal field.Name "Test" "Field name incorrect"
|
Expect.equal field.Name "Test" "Field name incorrect"
|
||||||
@@ -109,26 +109,6 @@ let fieldTests = testList "Field" [
|
|||||||
Expect.isNone field.ParameterName "The default parameter name should be None"
|
Expect.isNone field.ParameterName "The default parameter name should be None"
|
||||||
Expect.isNone field.Qualifier "The default table qualifier should be None"
|
Expect.isNone field.Qualifier "The default table qualifier should be None"
|
||||||
}
|
}
|
||||||
testList "NameToPath" [
|
|
||||||
test "succeeds for PostgreSQL and a simple name" {
|
|
||||||
Expect.equal "data->>'Simple'" (Field.NameToPath "Simple" PostgreSQL) "Path not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for SQLite and a simple name" {
|
|
||||||
Expect.equal "data->>'Simple'" (Field.NameToPath "Simple" SQLite) "Path not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for PostgreSQL and a nested name" {
|
|
||||||
Expect.equal
|
|
||||||
"data#>>'{A,Long,Path,to,the,Property}'"
|
|
||||||
(Field.NameToPath "A.Long.Path.to.the.Property" PostgreSQL)
|
|
||||||
"Path not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for SQLite and a nested name" {
|
|
||||||
Expect.equal
|
|
||||||
"data->>'A'->>'Long'->>'Path'->>'to'->>'the'->>'Property'"
|
|
||||||
(Field.NameToPath "A.Long.Path.to.the.Property" SQLite)
|
|
||||||
"Path not constructed correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
test "WithParameterName succeeds" {
|
test "WithParameterName succeeds" {
|
||||||
let field = (Field.EQ "Bob" "Tom").WithParameterName "@name"
|
let field = (Field.EQ "Bob" "Tom").WithParameterName "@name"
|
||||||
Expect.isSome field.ParameterName "The parameter name should have been filled"
|
Expect.isSome field.ParameterName "The parameter name should have been filled"
|
||||||
@@ -146,7 +126,8 @@ let fieldTests = testList "Field" [
|
|||||||
}
|
}
|
||||||
test "succeeds for a PostgreSQL single field with a qualifier" {
|
test "succeeds for a PostgreSQL single field with a qualifier" {
|
||||||
let field = { Field.LT "SomethingElse" 9 with Qualifier = Some "this" }
|
let field = { Field.LT "SomethingElse" 9 with Qualifier = Some "this" }
|
||||||
Expect.equal "this.data->>'SomethingElse'" (field.Path PostgreSQL) "The PostgreSQL path is incorrect"
|
Expect.equal
|
||||||
|
"this.data->>'SomethingElse'" (field.Path PostgreSQL) "The PostgreSQL path is incorrect"
|
||||||
}
|
}
|
||||||
test "succeeds for a PostgreSQL nested field with no qualifier" {
|
test "succeeds for a PostgreSQL nested field with no qualifier" {
|
||||||
let field = Field.EQ "My.Nested.Field" "howdy"
|
let field = Field.EQ "My.Nested.Field" "howdy"
|
||||||
@@ -174,9 +155,7 @@ let fieldTests = testList "Field" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "FieldMatch.ToString" [
|
||||||
/// Unit tests for the FieldMatch DU
|
|
||||||
let fieldMatchTests = testList "FieldMatch.ToString" [
|
|
||||||
test "succeeds for Any" {
|
test "succeeds for Any" {
|
||||||
Expect.equal (string Any) "OR" "SQL for Any is incorrect"
|
Expect.equal (string Any) "OR" "SQL for Any is incorrect"
|
||||||
}
|
}
|
||||||
@@ -184,9 +163,7 @@ let fieldMatchTests = testList "FieldMatch.ToString" [
|
|||||||
Expect.equal (string All) "AND" "SQL for All is incorrect"
|
Expect.equal (string All) "AND" "SQL for All is incorrect"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "ParameterName.Derive" [
|
||||||
/// Unit tests for the ParameterName class
|
|
||||||
let parameterNameTests = testList "ParameterName.Derive" [
|
|
||||||
test "succeeds with existing name" {
|
test "succeeds with existing name" {
|
||||||
let name = ParameterName()
|
let name = ParameterName()
|
||||||
Expect.equal (name.Derive(Some "@taco")) "@taco" "Name should have been @taco"
|
Expect.equal (name.Derive(Some "@taco")) "@taco" "Name should have been @taco"
|
||||||
@@ -200,136 +177,7 @@ let parameterNameTests = testList "ParameterName.Derive" [
|
|||||||
Expect.equal (name.Derive None) "@field3" "Counter should have advanced from previous call"
|
Expect.equal (name.Derive None) "@field3" "Counter should have advanced from previous call"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Query" [
|
||||||
/// Unit tests for the AutoId DU
|
|
||||||
let autoIdTests = testList "AutoId" [
|
|
||||||
test "GenerateGuid succeeds" {
|
|
||||||
let autoId = AutoId.GenerateGuid()
|
|
||||||
Expect.isNotNull autoId "The GUID auto-ID should not have been null"
|
|
||||||
Expect.stringHasLength autoId 32 "The GUID auto-ID should have been 32 characters long"
|
|
||||||
Expect.equal autoId (autoId.ToLowerInvariant ()) "The GUID auto-ID should have been lowercase"
|
|
||||||
}
|
|
||||||
test "GenerateRandomString succeeds" {
|
|
||||||
[ 6; 8; 12; 20; 32; 57; 64 ]
|
|
||||||
|> List.iter (fun length ->
|
|
||||||
let autoId = AutoId.GenerateRandomString length
|
|
||||||
Expect.isNotNull autoId $"Random string ({length}) should not have been null"
|
|
||||||
Expect.stringHasLength autoId length $"Random string should have been {length} characters long"
|
|
||||||
Expect.equal autoId (autoId.ToLowerInvariant ()) $"Random string ({length}) should have been lowercase")
|
|
||||||
}
|
|
||||||
testList "NeedsAutoId" [
|
|
||||||
test "succeeds when no auto ID is configured" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Disabled (obj ()) "id") "Disabled auto-ID never needs an automatic ID"
|
|
||||||
}
|
|
||||||
test "fails for any when the ID property is not found" {
|
|
||||||
Expect.throwsT<System.InvalidOperationException>
|
|
||||||
(fun () -> AutoId.NeedsAutoId Number {| Key = "" |} "Id" |> ignore)
|
|
||||||
"Non-existent ID property should have thrown an exception"
|
|
||||||
}
|
|
||||||
test "succeeds for byte when the ID is zero" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId Number {| Id = int8 0 |} "Id") "Zero ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for byte when the ID is non-zero" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Number {| Id = int8 4 |} "Id") "Non-zero ID should have returned false"
|
|
||||||
}
|
|
||||||
test "succeeds for short when the ID is zero" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId Number {| Id = int16 0 |} "Id") "Zero ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for short when the ID is non-zero" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Number {| Id = int16 7 |} "Id") "Non-zero ID should have returned false"
|
|
||||||
}
|
|
||||||
test "succeeds for int when the ID is zero" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId Number {| Id = 0 |} "Id") "Zero ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for int when the ID is non-zero" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Number {| Id = 32 |} "Id") "Non-zero ID should have returned false"
|
|
||||||
}
|
|
||||||
test "succeeds for long when the ID is zero" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId Number {| Id = 0L |} "Id") "Zero ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for long when the ID is non-zero" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Number {| Id = 80L |} "Id") "Non-zero ID should have returned false"
|
|
||||||
}
|
|
||||||
test "fails for number when the ID is not a number" {
|
|
||||||
Expect.throwsT<System.InvalidOperationException>
|
|
||||||
(fun () -> AutoId.NeedsAutoId Number {| Id = "" |} "Id" |> ignore)
|
|
||||||
"Numeric ID against a string should have thrown an exception"
|
|
||||||
}
|
|
||||||
test "succeeds for GUID when the ID is blank" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId Guid {| Id = "" |} "Id") "Blank ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for GUID when the ID is filled" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId Guid {| Id = "abc" |} "Id") "Filled ID should have returned false"
|
|
||||||
}
|
|
||||||
test "fails for GUID when the ID is not a string" {
|
|
||||||
Expect.throwsT<System.InvalidOperationException>
|
|
||||||
(fun () -> AutoId.NeedsAutoId Guid {| Id = 8 |} "Id" |> ignore)
|
|
||||||
"String ID against a number should have thrown an exception"
|
|
||||||
}
|
|
||||||
test "succeeds for RandomString when the ID is blank" {
|
|
||||||
Expect.isTrue (AutoId.NeedsAutoId RandomString {| Id = "" |} "Id") "Blank ID should have returned true"
|
|
||||||
}
|
|
||||||
test "succeeds for RandomString when the ID is filled" {
|
|
||||||
Expect.isFalse (AutoId.NeedsAutoId RandomString {| Id = "x" |} "Id") "Filled ID should have returned false"
|
|
||||||
}
|
|
||||||
test "fails for RandomString when the ID is not a string" {
|
|
||||||
Expect.throwsT<System.InvalidOperationException>
|
|
||||||
(fun () -> AutoId.NeedsAutoId RandomString {| Id = 33 |} "Id" |> ignore)
|
|
||||||
"String ID against a number should have thrown an exception"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|
||||||
/// Unit tests for the Configuration module
|
|
||||||
let configurationTests = testList "Configuration" [
|
|
||||||
test "useSerializer succeeds" {
|
|
||||||
try
|
|
||||||
Configuration.useSerializer
|
|
||||||
{ new IDocumentSerializer with
|
|
||||||
member _.Serialize<'T>(it: 'T) : string = """{"Overridden":true}"""
|
|
||||||
member _.Deserialize<'T>(it: string) : 'T = Unchecked.defaultof<'T>
|
|
||||||
}
|
|
||||||
|
|
||||||
let serialized = Configuration.serializer().Serialize {| Foo = "howdy"; Bar = "bye" |}
|
|
||||||
Expect.equal serialized """{"Overridden":true}""" "Specified serializer was not used"
|
|
||||||
|
|
||||||
let deserialized = Configuration.serializer().Deserialize<obj> """{"Something":"here"}"""
|
|
||||||
Expect.isNull deserialized "Specified serializer should have returned null"
|
|
||||||
finally
|
|
||||||
Configuration.useSerializer DocumentSerializer.``default``
|
|
||||||
}
|
|
||||||
test "serializer returns configured serializer" {
|
|
||||||
Expect.isTrue (obj.ReferenceEquals(DocumentSerializer.``default``, Configuration.serializer ()))
|
|
||||||
"Serializer should have been the same"
|
|
||||||
}
|
|
||||||
test "useIdField / idField succeeds" {
|
|
||||||
try
|
|
||||||
Expect.equal (Configuration.idField ()) "Id" "The default configured ID field was incorrect"
|
|
||||||
Configuration.useIdField "id"
|
|
||||||
Expect.equal (Configuration.idField ()) "id" "useIdField did not set the ID field"
|
|
||||||
finally
|
|
||||||
Configuration.useIdField "Id"
|
|
||||||
}
|
|
||||||
test "useAutoIdStrategy / autoIdStrategy succeeds" {
|
|
||||||
try
|
|
||||||
Expect.equal (Configuration.autoIdStrategy ()) Disabled "The default auto-ID strategy was incorrect"
|
|
||||||
Configuration.useAutoIdStrategy Guid
|
|
||||||
Expect.equal (Configuration.autoIdStrategy ()) Guid "The auto-ID strategy was not set correctly"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
}
|
|
||||||
test "useIdStringLength / idStringLength succeeds" {
|
|
||||||
try
|
|
||||||
Expect.equal (Configuration.idStringLength ()) 16 "The default ID string length was incorrect"
|
|
||||||
Configuration.useIdStringLength 33
|
|
||||||
Expect.equal (Configuration.idStringLength ()) 33 "The ID string length was not set correctly"
|
|
||||||
finally
|
|
||||||
Configuration.useIdStringLength 16
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
/// Unit tests for the Query module
|
|
||||||
let queryTests = testList "Query" [
|
|
||||||
test "statementWhere succeeds" {
|
test "statementWhere succeeds" {
|
||||||
Expect.equal (Query.statementWhere "x" "y") "x WHERE y" "Statements not combined correctly"
|
Expect.equal (Query.statementWhere "x" "y") "x WHERE y" "Statements not combined correctly"
|
||||||
}
|
}
|
||||||
@@ -405,61 +253,5 @@ let queryTests = testList "Query" [
|
|||||||
test "delete succeeds" {
|
test "delete succeeds" {
|
||||||
Expect.equal (Query.delete tbl) $"DELETE FROM {tbl}" "Delete query not correct"
|
Expect.equal (Query.delete tbl) $"DELETE FROM {tbl}" "Delete query not correct"
|
||||||
}
|
}
|
||||||
testList "orderBy" [
|
|
||||||
test "succeeds for no fields" {
|
|
||||||
Expect.equal (Query.orderBy [] PostgreSQL) "" "Order By should have been blank (PostgreSQL)"
|
|
||||||
Expect.equal (Query.orderBy [] SQLite) "" "Order By should have been blank (SQLite)"
|
|
||||||
}
|
|
||||||
test "succeeds for PostgreSQL with one field and no direction" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy [ Field.Named "TestField" ] PostgreSQL)
|
|
||||||
" ORDER BY data->>'TestField'"
|
|
||||||
"Order By not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for SQLite with one field and no direction" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy [ Field.Named "TestField" ] SQLite)
|
|
||||||
" ORDER BY data->>'TestField'"
|
|
||||||
"Order By not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for PostgreSQL with multiple fields and direction" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy
|
|
||||||
[ Field.Named "Nested.Test.Field DESC"; Field.Named "AnotherField"; Field.Named "It DESC" ]
|
|
||||||
PostgreSQL)
|
|
||||||
" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC"
|
|
||||||
"Order By not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for SQLite with multiple fields and direction" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy
|
|
||||||
[ Field.Named "Nested.Test.Field DESC"; Field.Named "AnotherField"; Field.Named "It DESC" ]
|
|
||||||
SQLite)
|
|
||||||
" ORDER BY data->>'Nested'->>'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC"
|
|
||||||
"Order By not constructed correctly"
|
|
||||||
}
|
|
||||||
test "succeeds for PostgreSQL numeric fields" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy [ Field.Named "n:Test" ] PostgreSQL)
|
|
||||||
" ORDER BY (data->>'Test')::numeric"
|
|
||||||
"Order By not constructed correctly for numeric field"
|
|
||||||
}
|
|
||||||
test "succeeds for SQLite numeric fields" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.orderBy [ Field.Named "n:Test" ] SQLite)
|
|
||||||
" ORDER BY data->>'Test'"
|
|
||||||
"Order By not constructed correctly for numeric field"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
/// Tests which do not hit the database
|
|
||||||
let all = testList "Common" [
|
|
||||||
opTests
|
|
||||||
fieldTests
|
|
||||||
fieldMatchTests
|
|
||||||
parameterNameTests
|
|
||||||
autoIdTests
|
|
||||||
queryTests
|
|
||||||
testSequenced configurationTests
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ open Expecto
|
|||||||
open Npgsql
|
open Npgsql
|
||||||
open Types
|
open Types
|
||||||
|
|
||||||
|
#nowarn "0044"
|
||||||
|
|
||||||
/// Open a connection to the throwaway database
|
/// Open a connection to the throwaway database
|
||||||
let private mkConn (db: ThrowawayPostgresDb) =
|
let private mkConn (db: ThrowawayPostgresDb) =
|
||||||
let conn = new NpgsqlConnection(db.ConnectionString)
|
let conn = new NpgsqlConnection(db.ConnectionString)
|
||||||
@@ -25,7 +27,7 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! docs = conn.customList (Query.find PostgresDb.TableName) [] fromData<JsonDocument>
|
let! docs = conn.customList (Query.selectFromTable PostgresDb.TableName) [] fromData<JsonDocument>
|
||||||
Expect.equal (List.length docs) 5 "There should have been 5 documents returned"
|
Expect.equal (List.length docs) 5 "There should have been 5 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when data is not found" {
|
testTask "succeeds when data is not found" {
|
||||||
@@ -209,12 +211,12 @@ let integrationTests =
|
|||||||
let! theCount = conn.countAll PostgresDb.TableName
|
let! theCount = conn.countAll PostgresDb.TableName
|
||||||
Expect.equal theCount 5 "There should have been 5 matching documents"
|
Expect.equal theCount 5 "There should have been 5 matching documents"
|
||||||
}
|
}
|
||||||
testTask "countByFields succeeds" {
|
testTask "countByField succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! theCount = conn.countByFields PostgresDb.TableName Any [ Field.EQ "Value" "purple" ]
|
let! theCount = conn.countByField PostgresDb.TableName (Field.EQ "Value" "purple")
|
||||||
Expect.equal theCount 2 "There should have been 2 matching documents"
|
Expect.equal theCount 2 "There should have been 2 matching documents"
|
||||||
}
|
}
|
||||||
testTask "countByContains succeeds" {
|
testTask "countByContains succeeds" {
|
||||||
@@ -251,13 +253,13 @@ let integrationTests =
|
|||||||
Expect.isFalse exists "There should not have been an existing document"
|
Expect.isFalse exists "There should not have been an existing document"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "existsByFields" [
|
testList "existsByField" [
|
||||||
testTask "succeeds when documents exist" {
|
testTask "succeeds when documents exist" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! exists = conn.existsByFields PostgresDb.TableName Any [ Field.EX "Sub" ]
|
let! exists = conn.existsByField PostgresDb.TableName (Field.EX "Sub")
|
||||||
Expect.isTrue exists "There should have been existing documents"
|
Expect.isTrue exists "There should have been existing documents"
|
||||||
}
|
}
|
||||||
testTask "succeeds when documents do not exist" {
|
testTask "succeeds when documents do not exist" {
|
||||||
@@ -265,7 +267,7 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! exists = conn.existsByFields PostgresDb.TableName Any [ Field.EQ "NumValue" "six" ]
|
let! exists = conn.existsByField PostgresDb.TableName (Field.EQ "NumValue" "six")
|
||||||
Expect.isFalse exists "There should not have been existing documents"
|
Expect.isFalse exists "There should not have been existing documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -315,10 +317,12 @@ let integrationTests =
|
|||||||
do! conn.insert PostgresDb.TableName { Foo = "five"; Bar = "six" }
|
do! conn.insert PostgresDb.TableName { Foo = "five"; Bar = "six" }
|
||||||
|
|
||||||
let! results = conn.findAll<SubDocument> PostgresDb.TableName
|
let! results = conn.findAll<SubDocument> PostgresDb.TableName
|
||||||
Expect.equal
|
let expected = [
|
||||||
results
|
{ Foo = "one"; Bar = "two" }
|
||||||
[ { Foo = "one"; Bar = "two" }; { Foo = "three"; Bar = "four" }; { Foo = "five"; Bar = "six" } ]
|
{ Foo = "three"; Bar = "four" }
|
||||||
"There should have been 3 documents returned"
|
{ Foo = "five"; Bar = "six" }
|
||||||
|
]
|
||||||
|
Expect.equal results expected "There should have been 3 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when there is no data" {
|
testTask "succeeds when there is no data" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -327,44 +331,6 @@ let integrationTests =
|
|||||||
Expect.equal results [] "There should have been no documents returned"
|
Expect.equal results [] "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findAllOrdered" [
|
|
||||||
testTask "succeeds when ordering numerically" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "n:NumValue" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"one|three|two|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering numerically descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "n:NumValue DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"five|four|two|three|one"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering alphabetically" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> PostgresDb.TableName [ Field.Named "Id DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"two|three|one|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findById" [
|
testList "findById" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -384,13 +350,13 @@ let integrationTests =
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByFields" [
|
testList "findByField" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! docs = conn.findByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "another" ]
|
let! docs = conn.findByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
|
||||||
Expect.equal (List.length docs) 1 "There should have been one document returned"
|
Expect.equal (List.length docs) 1 "There should have been one document returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when documents are not found" {
|
testTask "succeeds when documents are not found" {
|
||||||
@@ -398,36 +364,10 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! docs = conn.findByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "mauve" ]
|
let! docs = conn.findByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "mauve")
|
||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByFieldsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName All [ Field.EQ "Value" "purple" ] [ Field.Named "Id" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "five|four" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName All [ Field.EQ "Value" "purple" ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|five" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findByContains" [
|
testList "findByContains" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -446,33 +386,6 @@ let integrationTests =
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByContainsOrdered" [
|
|
||||||
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "two|four" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|two" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findByJsonPath" [
|
testList "findByJsonPath" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -491,40 +404,13 @@ let integrationTests =
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByJsonPathOrdered" [
|
testList "findFirstByField" [
|
||||||
// Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
|
|
||||||
Expect.hasLength docs 3 "There should have been 3 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "one|three|two" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
|
|
||||||
Expect.hasLength docs 3 "There should have been 3 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "two|three|one" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findFirstByFields" [
|
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "another" ]
|
let! doc = conn.findFirstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isSome doc "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
@@ -533,7 +419,7 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "purple" ]
|
let! doc = conn.findFirstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "purple")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isSome doc "There should have been a document returned"
|
||||||
Expect.contains [ "five"; "four" ] doc.Value.Id "An incorrect document was returned"
|
Expect.contains [ "five"; "four" ] doc.Value.Id "An incorrect document was returned"
|
||||||
}
|
}
|
||||||
@@ -542,34 +428,10 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "absent" ]
|
let! doc = conn.findFirstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "absent")
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findFirstByFieldsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] [ Field.Named "Id" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "five" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findFirstByContains" [
|
testList "findFirstByContains" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -598,30 +460,6 @@ let integrationTests =
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findFirstByContainsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findFirstByJsonPath" [
|
testList "findFirstByJsonPath" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -650,30 +488,6 @@ let integrationTests =
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findFirstByJsonPathOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
use conn = mkConn db
|
|
||||||
do! loadDocs conn
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "updateById" [
|
testList "updateById" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -744,14 +558,14 @@ let integrationTests =
|
|||||||
do! conn.patchById PostgresDb.TableName "test" {| Foo = "green" |}
|
do! conn.patchById PostgresDb.TableName "test" {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "patchByFields" [
|
testList "patchByField" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.patchByFields PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] {| NumValue = 77 |}
|
do! conn.patchByField PostgresDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |}
|
||||||
let! after = conn.countByFields PostgresDb.TableName Any [ Field.EQ "NumValue" "77" ]
|
let! after = conn.countByField PostgresDb.TableName (Field.EQ "NumValue" "77")
|
||||||
Expect.equal after 2 "There should have been 2 documents returned"
|
Expect.equal after 2 "There should have been 2 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
@@ -761,7 +575,7 @@ let integrationTests =
|
|||||||
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! conn.patchByFields PostgresDb.TableName Any [ Field.EQ "Value" "burgundy" ] {| Foo = "green" |}
|
do! conn.patchByField PostgresDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "patchByContains" [
|
testList "patchByContains" [
|
||||||
@@ -811,9 +625,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Sub"; "Value" ]
|
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Sub"; "Value" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -822,9 +636,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Sub" ]
|
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Sub" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -843,16 +657,16 @@ let integrationTests =
|
|||||||
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Value" ]
|
do! conn.removeFieldsById PostgresDb.TableName "two" [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "removeFieldsByFields" [
|
testList "removeFieldsByField" [
|
||||||
testTask "succeeds when multiple fields are removed" {
|
testTask "succeeds when multiple fields are removed" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Sub"; "Value" ]
|
do! conn.removeFieldsByField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub"; "Value" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -860,10 +674,10 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Sub" ]
|
do! conn.removeFieldsByField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -872,14 +686,14 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! conn.removeFieldsByFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Nothing" ]
|
do! conn.removeFieldsByField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Nothing" ]
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is matched" {
|
testTask "succeeds when no document is matched" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! conn.removeFieldsByFields PostgresDb.TableName Any [ Field.NE "Abracadabra" "apple" ] [ "Value" ]
|
do! conn.removeFieldsByField PostgresDb.TableName (Field.NE "Abracadabra" "apple") [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "removeFieldsByContains" [
|
testList "removeFieldsByContains" [
|
||||||
@@ -889,9 +703,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub"; "Value" ]
|
do! conn.removeFieldsByContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub"; "Value" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -900,9 +714,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub" ]
|
do! conn.removeFieldsByContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -928,9 +742,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub"; "Value" ]
|
do! conn.removeFieldsByJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub"; "Value" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -939,9 +753,9 @@ let integrationTests =
|
|||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.removeFieldsByJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub" ]
|
do! conn.removeFieldsByJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub" ]
|
||||||
let! noSubs = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = conn.countByField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = conn.countByFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = conn.countByField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -980,13 +794,13 @@ let integrationTests =
|
|||||||
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "deleteByFields" [
|
testList "deleteByField" [
|
||||||
testTask "succeeds when documents are deleted" {
|
testTask "succeeds when documents are deleted" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.deleteByFields PostgresDb.TableName Any [ Field.EQ "Value" "purple" ]
|
do! conn.deleteByField PostgresDb.TableName (Field.EQ "Value" "purple")
|
||||||
let! remaining = conn.countAll PostgresDb.TableName
|
let! remaining = conn.countAll PostgresDb.TableName
|
||||||
Expect.equal remaining 3 "There should have been 3 documents remaining"
|
Expect.equal remaining 3 "There should have been 3 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -995,7 +809,7 @@ let integrationTests =
|
|||||||
use conn = mkConn db
|
use conn = mkConn db
|
||||||
do! loadDocs conn
|
do! loadDocs conn
|
||||||
|
|
||||||
do! conn.deleteByFields PostgresDb.TableName Any [ Field.EQ "Value" "crimson" ]
|
do! conn.deleteByField PostgresDb.TableName (Field.EQ "Value" "crimson")
|
||||||
let! remaining = conn.countAll PostgresDb.TableName
|
let! remaining = conn.countAll PostgresDb.TableName
|
||||||
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,17 @@ open BitBadger.Documents
|
|||||||
open BitBadger.Documents.Postgres
|
open BitBadger.Documents.Postgres
|
||||||
open BitBadger.Documents.Tests
|
open BitBadger.Documents.Tests
|
||||||
|
|
||||||
(** UNIT TESTS **)
|
#nowarn "0044"
|
||||||
|
|
||||||
/// Unit tests for the Parameters module of the PostgreSQL library
|
/// Tests which do not hit the database
|
||||||
let parametersTests = testList "Parameters" [
|
let unitTests =
|
||||||
|
testList "Unit" [
|
||||||
|
testList "Parameters" [
|
||||||
testList "idParam" [
|
testList "idParam" [
|
||||||
// NOTE: these tests also exercise all branches of the internal parameterFor function
|
// NOTE: these tests also exercise all branches of the internal parameterFor function
|
||||||
test "succeeds for byte ID" {
|
test "succeeds for byte ID" {
|
||||||
Expect.equal (idParam (sbyte 7)) ("@id", Sql.int8 (sbyte 7)) "Byte ID parameter not constructed correctly"
|
Expect.equal
|
||||||
|
(idParam (sbyte 7)) ("@id", Sql.int8 (sbyte 7)) "Byte ID parameter not constructed correctly"
|
||||||
}
|
}
|
||||||
test "succeeds for unsigned byte ID" {
|
test "succeeds for unsigned byte ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@@ -22,7 +25,9 @@ let parametersTests = testList "Parameters" [
|
|||||||
}
|
}
|
||||||
test "succeeds for short ID" {
|
test "succeeds for short ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
(idParam (int16 44)) ("@id", Sql.int16 (int16 44)) "Short ID parameter not constructed correctly"
|
(idParam (int16 44))
|
||||||
|
("@id", Sql.int16 (int16 44))
|
||||||
|
"Short ID parameter not constructed correctly"
|
||||||
}
|
}
|
||||||
test "succeeds for unsigned short ID" {
|
test "succeeds for unsigned short ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@@ -34,11 +39,14 @@ let parametersTests = testList "Parameters" [
|
|||||||
Expect.equal (idParam 88) ("@id", Sql.int 88) "Int ID parameter not constructed correctly"
|
Expect.equal (idParam 88) ("@id", Sql.int 88) "Int ID parameter not constructed correctly"
|
||||||
}
|
}
|
||||||
test "succeeds for unsigned integer ID" {
|
test "succeeds for unsigned integer ID" {
|
||||||
Expect.equal (idParam (uint 889)) ("@id", Sql.int 889) "Unsigned int ID parameter not constructed correctly"
|
Expect.equal
|
||||||
|
(idParam (uint 889)) ("@id", Sql.int 889) "Unsigned int ID parameter not constructed correctly"
|
||||||
}
|
}
|
||||||
test "succeeds for long ID" {
|
test "succeeds for long ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
(idParam (int64 123)) ("@id", Sql.int64 (int64 123)) "Long ID parameter not constructed correctly"
|
(idParam (int64 123))
|
||||||
|
("@id", Sql.int64 (int64 123))
|
||||||
|
"Long ID parameter not constructed correctly"
|
||||||
}
|
}
|
||||||
test "succeeds for unsigned long ID" {
|
test "succeeds for unsigned long ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@@ -105,7 +113,8 @@ let parametersTests = testList "Parameters" [
|
|||||||
Expect.isEmpty paramList "There should not have been any parameters added"
|
Expect.isEmpty paramList "There should not have been any parameters added"
|
||||||
}
|
}
|
||||||
test "succeeds when two parameters are added for one field" {
|
test "succeeds when two parameters are added for one field" {
|
||||||
let paramList = addFieldParams [ { Field.BT "that" "eh" "zed" with ParameterName = Some "@test" } ] []
|
let paramList =
|
||||||
|
addFieldParams [ { Field.BT "that" "eh" "zed" with ParameterName = Some "@test" } ] []
|
||||||
Expect.hasLength paramList 2 "There should have been 2 parameters added"
|
Expect.hasLength paramList 2 "There should have been 2 parameters added"
|
||||||
let name, value = Seq.head paramList
|
let name, value = Seq.head paramList
|
||||||
Expect.equal name "@testmin" "Minimum field name not correct"
|
Expect.equal name "@testmin" "Minimum field name not correct"
|
||||||
@@ -127,13 +136,23 @@ let parametersTests = testList "Parameters" [
|
|||||||
Expect.equal value (Sql.stringArray [| "bob"; "tom"; "mike" |]) "The parameter value was incorrect"
|
Expect.equal value (Sql.stringArray [| "bob"; "tom"; "mike" |]) "The parameter value was incorrect"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "fieldNameParam" [
|
||||||
|
test "succeeds for one name" {
|
||||||
|
let name, value = fieldNameParam [ "bob" ]
|
||||||
|
Expect.equal name "@name" "The parameter name was incorrect"
|
||||||
|
Expect.equal value (Sql.string "bob") "The parameter value was incorrect"
|
||||||
|
}
|
||||||
|
test "succeeds for multiple names" {
|
||||||
|
let name, value = fieldNameParam [ "bob"; "tom"; "mike" ]
|
||||||
|
Expect.equal name "@name" "The parameter name was incorrect"
|
||||||
|
Expect.equal value (Sql.stringArray [| "bob"; "tom"; "mike" |]) "The parameter value was incorrect"
|
||||||
|
}
|
||||||
|
]
|
||||||
test "noParams succeeds" {
|
test "noParams succeeds" {
|
||||||
Expect.isEmpty noParams "The no-params sequence should be empty"
|
Expect.isEmpty noParams "The no-params sequence should be empty"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Query" [
|
||||||
/// Unit tests for the Query module of the PostgreSQL library
|
|
||||||
let queryTests = testList "Query" [
|
|
||||||
testList "whereByFields" [
|
testList "whereByFields" [
|
||||||
test "succeeds for a single field when a logical operator is passed" {
|
test "succeeds for a single field when a logical operator is passed" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@@ -187,7 +206,9 @@ let queryTests = testList "Query" [
|
|||||||
}
|
}
|
||||||
test "succeeds for non-numeric non-string ID" {
|
test "succeeds for non-numeric non-string ID" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
(Query.whereById (System.Uri "https://example.com")) "data->>'Id' = @id" "WHERE clause not correct"
|
(Query.whereById (System.Uri "https://example.com"))
|
||||||
|
"data->>'Id' = @id"
|
||||||
|
"WHERE clause not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Definition" [
|
testList "Definition" [
|
||||||
@@ -246,19 +267,26 @@ let queryTests = testList "Query" [
|
|||||||
(Query.byPathMatch "verify") "verify WHERE data @? @path::jsonpath" "By-JSON Path query not correct"
|
(Query.byPathMatch "verify") "verify WHERE data @? @path::jsonpath" "By-JSON Path query not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
]
|
||||||
(** INTEGRATION TESTS **)
|
|
||||||
|
|
||||||
open ThrowawayDb.Postgres
|
open ThrowawayDb.Postgres
|
||||||
open Types
|
open Types
|
||||||
|
|
||||||
/// Load the test documents into the database
|
let isTrue<'T> (_ : 'T) = true
|
||||||
let loadDocs () = backgroundTask {
|
|
||||||
for doc in testDocuments do do! insert PostgresDb.TableName doc
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Integration tests for the Configuration module of the PostgreSQL library
|
let integrationTests =
|
||||||
let configurationTests = testList "Configuration" [
|
let documents = [
|
||||||
|
{ Id = "one"; Value = "FIRST!"; NumValue = 0; Sub = None }
|
||||||
|
{ Id = "two"; Value = "another"; NumValue = 10; Sub = Some { Foo = "green"; Bar = "blue" } }
|
||||||
|
{ Id = "three"; Value = ""; NumValue = 4; Sub = None }
|
||||||
|
{ Id = "four"; Value = "purple"; NumValue = 17; Sub = Some { Foo = "green"; Bar = "red" } }
|
||||||
|
{ Id = "five"; Value = "purple"; NumValue = 18; Sub = None }
|
||||||
|
]
|
||||||
|
let loadDocs () = backgroundTask {
|
||||||
|
for doc in documents do do! insert PostgresDb.TableName doc
|
||||||
|
}
|
||||||
|
testList "Integration" [
|
||||||
|
testList "Configuration" [
|
||||||
test "useDataSource disposes existing source" {
|
test "useDataSource disposes existing source" {
|
||||||
use db1 = ThrowawayDatabase.Create PostgresDb.ConnStr.Value
|
use db1 = ThrowawayDatabase.Create PostgresDb.ConnStr.Value
|
||||||
let source = PostgresDb.MkDataSource db1.ConnectionString
|
let source = PostgresDb.MkDataSource db1.ConnectionString
|
||||||
@@ -273,29 +301,26 @@ let configurationTests = testList "Configuration" [
|
|||||||
let source = PostgresDb.MkDataSource db.ConnectionString
|
let source = PostgresDb.MkDataSource db.ConnectionString
|
||||||
Configuration.useDataSource source
|
Configuration.useDataSource source
|
||||||
|
|
||||||
Expect.isTrue (obj.ReferenceEquals(source, Configuration.dataSource ())) "Data source should have been the same"
|
Expect.isTrue (obj.ReferenceEquals(source, Configuration.dataSource ()))
|
||||||
|
"Data source should have been the same"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Custom" [
|
||||||
/// Integration tests for the Custom module of the PostgreSQL library
|
|
||||||
let customTests = testList "Custom" [
|
|
||||||
testList "list" [
|
testList "list" [
|
||||||
testTask "succeeds when data is found" {
|
testTask "succeeds when data is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = Custom.list (Query.find PostgresDb.TableName) [] fromData<JsonDocument>
|
let! docs = Custom.list (Query.selectFromTable PostgresDb.TableName) [] fromData<JsonDocument>
|
||||||
Expect.hasLength docs 5 "There should have been 5 documents returned"
|
Expect.hasCountOf docs 5u isTrue "There should have been 5 documents returned"
|
||||||
}
|
}
|
||||||
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 ()
|
||||||
|
|
||||||
let! docs =
|
let! docs =
|
||||||
Custom.list
|
Custom.list $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
||||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ] fromData<JsonDocument>
|
||||||
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
|
|
||||||
fromData<JsonDocument>
|
|
||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -305,10 +330,8 @@ let customTests = testList "Custom" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc =
|
let! doc =
|
||||||
Custom.single
|
Custom.single $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
||||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
[ "@id", Sql.string "one"] fromData<JsonDocument>
|
||||||
[ "@id", Sql.string "one"]
|
|
||||||
fromData<JsonDocument>
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isSome doc "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "one" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "one" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
@@ -317,10 +340,8 @@ let customTests = testList "Custom" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc =
|
let! doc =
|
||||||
Custom.single
|
Custom.single $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
||||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
[ "@id", Sql.string "eighty" ] fromData<JsonDocument>
|
||||||
[ "@id", Sql.string "eighty" ]
|
|
||||||
fromData<JsonDocument>
|
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -338,8 +359,7 @@ let customTests = testList "Custom" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Custom.nonQuery
|
do! Custom.nonQuery $"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
||||||
$"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
|
|
||||||
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
|
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
|
||||||
|
|
||||||
let! remaining = Count.all PostgresDb.TableName
|
let! remaining = Count.all PostgresDb.TableName
|
||||||
@@ -352,9 +372,7 @@ let customTests = testList "Custom" [
|
|||||||
Expect.equal nbr 5 "The query should have returned the number 5"
|
Expect.equal nbr 5 "The query should have returned the number 5"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Definition" [
|
||||||
/// Integration tests for the Definition module of the PostgreSQL library
|
|
||||||
let definitionTests = testList "Definition" [
|
|
||||||
testTask "ensureTable succeeds" {
|
testTask "ensureTable succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
let tableExists () =
|
let tableExists () =
|
||||||
@@ -378,7 +396,9 @@ let definitionTests = testList "Definition" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
let indexExists () =
|
let indexExists () =
|
||||||
Custom.scalar
|
Custom.scalar
|
||||||
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it" [] toExists
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_document') AS it"
|
||||||
|
[]
|
||||||
|
toExists
|
||||||
|
|
||||||
let! exists = indexExists ()
|
let! exists = indexExists ()
|
||||||
Expect.isFalse exists "The index should not exist already"
|
Expect.isFalse exists "The index should not exist already"
|
||||||
@@ -391,7 +411,8 @@ let definitionTests = testList "Definition" [
|
|||||||
testTask "ensureFieldIndex succeeds" {
|
testTask "ensureFieldIndex succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
let indexExists () =
|
let indexExists () =
|
||||||
Custom.scalar "SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it" [] toExists
|
Custom.scalar
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it" [] toExists
|
||||||
|
|
||||||
let! exists = indexExists ()
|
let! exists = indexExists ()
|
||||||
Expect.isFalse exists "The index should not exist already"
|
Expect.isFalse exists "The index should not exist already"
|
||||||
@@ -402,9 +423,6 @@ let definitionTests = testList "Definition" [
|
|||||||
Expect.isTrue exists' "The index should now exist"
|
Expect.isTrue exists' "The index should now exist"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
/// Integration tests for the (auto-opened) Document module of the PostgreSQL library
|
|
||||||
let documentTests = testList "Document" [
|
|
||||||
testList "insert" [
|
testList "insert" [
|
||||||
testTask "succeeds" {
|
testTask "succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -426,68 +444,6 @@ let documentTests = testList "Document" [
|
|||||||
|> Async.RunSynchronously)
|
|> Async.RunSynchronously)
|
||||||
"An exception should have been raised for duplicate document ID insert"
|
"An exception should have been raised for duplicate document ID insert"
|
||||||
}
|
}
|
||||||
testTask "succeeds when adding a numeric auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy Number
|
|
||||||
Configuration.useIdField "Key"
|
|
||||||
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"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
Configuration.useIdField "Id"
|
|
||||||
}
|
|
||||||
testTask "succeeds when adding a GUID auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy Guid
|
|
||||||
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"
|
|
||||||
Expect.hasCountOf after 1u (fun doc -> doc.Id = "abc123") "The provided ID should have been used as-is"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
}
|
|
||||||
testTask "succeeds when adding a RandomString auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy RandomString
|
|
||||||
Configuration.useIdStringLength 44
|
|
||||||
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 = 44)
|
|
||||||
"Three of the IDs should have been 44-character random strings"
|
|
||||||
Expect.hasCountOf after 1u (fun doc -> doc.Id = "abc123") "The provided ID should have been used as-is"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
Configuration.useIdStringLength 16
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
testList "save" [
|
testList "save" [
|
||||||
testTask "succeeds when a document is inserted" {
|
testTask "succeeds when a document is inserted" {
|
||||||
@@ -516,10 +472,7 @@ let documentTests = testList "Document" [
|
|||||||
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
testList "Count" [
|
||||||
|
|
||||||
/// Integration tests for the Count module of the PostgreSQL library
|
|
||||||
let countTests = testList "Count" [
|
|
||||||
testTask "all succeeds" {
|
testTask "all succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
@@ -532,7 +485,8 @@ let countTests = testList "Count" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! theCount = Count.byFields PostgresDb.TableName Any [ Field.BT "NumValue" 15 20; Field.EQ "NumValue" 0 ]
|
let! theCount =
|
||||||
|
Count.byFields PostgresDb.TableName Any [ Field.BT "NumValue" 15 20; Field.EQ "NumValue" 0 ]
|
||||||
Expect.equal theCount 3 "There should have been 3 matching documents"
|
Expect.equal theCount 3 "There should have been 3 matching documents"
|
||||||
}
|
}
|
||||||
testTask "succeeds when items are not found" {
|
testTask "succeeds when items are not found" {
|
||||||
@@ -543,6 +497,22 @@ let countTests = testList "Count" [
|
|||||||
Expect.equal theCount 0 "There should have been no matching documents"
|
Expect.equal theCount 0 "There should have been no matching documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "byField" [
|
||||||
|
testTask "succeeds for numeric range" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let! theCount = Count.byField PostgresDb.TableName (Field.BT "NumValue" 10 20)
|
||||||
|
Expect.equal theCount 3 "There should have been 3 matching documents"
|
||||||
|
}
|
||||||
|
testTask "succeeds for non-numeric range" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let! theCount = Count.byField PostgresDb.TableName (Field.BT "Value" "aardvark" "apple")
|
||||||
|
Expect.equal theCount 1 "There should have been 1 matching document"
|
||||||
|
}
|
||||||
|
]
|
||||||
testTask "byContains succeeds" {
|
testTask "byContains succeeds" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
@@ -558,9 +528,7 @@ let countTests = testList "Count" [
|
|||||||
Expect.equal theCount 3 "There should have been 3 matching documents"
|
Expect.equal theCount 3 "There should have been 3 matching documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Exists" [
|
||||||
/// Integration tests for the Exists module of the PostgreSQL library
|
|
||||||
let existsTests = testList "Exists" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document exists" {
|
testTask "succeeds when a document exists" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -589,7 +557,24 @@ let existsTests = testList "Exists" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! exists = Exists.byFields PostgresDb.TableName All [ Field.EQ "NumValue" "six"; Field.EX "Nope" ]
|
let! exists =
|
||||||
|
Exists.byFields PostgresDb.TableName All [ Field.EQ "NumValue" "six"; Field.EX "Nope" ]
|
||||||
|
Expect.isFalse exists "There should not have been existing documents"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
testList "byField" [
|
||||||
|
testTask "succeeds when documents exist" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let! exists = Exists.byField PostgresDb.TableName (Field.EX "Sub")
|
||||||
|
Expect.isTrue exists "There should have been existing documents"
|
||||||
|
}
|
||||||
|
testTask "succeeds when documents do not exist" {
|
||||||
|
use db = PostgresDb.BuildDb()
|
||||||
|
do! loadDocs ()
|
||||||
|
|
||||||
|
let! exists = Exists.byField PostgresDb.TableName (Field.EQ "NumValue" "six")
|
||||||
Expect.isFalse exists "There should not have been existing documents"
|
Expect.isFalse exists "There should not have been existing documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -626,9 +611,7 @@ let existsTests = testList "Exists" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Find" [
|
||||||
/// Integration tests for the Find module of the PostgreSQL library
|
|
||||||
let findTests = testList "Find" [
|
|
||||||
testList "all" [
|
testList "all" [
|
||||||
testTask "succeeds when there is data" {
|
testTask "succeeds when there is data" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -638,10 +621,12 @@ let findTests = testList "Find" [
|
|||||||
do! insert PostgresDb.TableName { Foo = "five"; Bar = "six" }
|
do! insert PostgresDb.TableName { Foo = "five"; Bar = "six" }
|
||||||
|
|
||||||
let! results = Find.all<SubDocument> PostgresDb.TableName
|
let! results = Find.all<SubDocument> PostgresDb.TableName
|
||||||
Expect.equal
|
let expected = [
|
||||||
results
|
{ Foo = "one"; Bar = "two" }
|
||||||
[ { Foo = "one"; Bar = "two" }; { Foo = "three"; Bar = "four" }; { Foo = "five"; Bar = "six" } ]
|
{ Foo = "three"; Bar = "four" }
|
||||||
"There should have been 3 documents returned"
|
{ Foo = "five"; Bar = "six" }
|
||||||
|
]
|
||||||
|
Expect.equal results expected "There should have been 3 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when there is no data" {
|
testTask "succeeds when there is no data" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -649,41 +634,6 @@ let findTests = testList "Find" [
|
|||||||
Expect.equal results [] "There should have been no documents returned"
|
Expect.equal results [] "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "allOrdered" [
|
|
||||||
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
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"one|three|two|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
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
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"five|four|two|three|one"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
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
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"two|three|one|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -707,7 +657,8 @@ let findTests = testList "Find" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs =
|
let! docs =
|
||||||
Find.byFields<JsonDocument> PostgresDb.TableName All [ Field.EQ "Value" "purple"; Field.EX "Sub" ]
|
Find.byFields<JsonDocument>
|
||||||
|
PostgresDb.TableName All [ Field.EQ "Value" "purple"; Field.EX "Sub" ]
|
||||||
Expect.equal (List.length docs) 1 "There should have been one document returned"
|
Expect.equal (List.length docs) 1 "There should have been one document returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when documents are not found" {
|
testTask "succeeds when documents are not found" {
|
||||||
@@ -720,28 +671,20 @@ let findTests = testList "Find" [
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFieldsOrdered" [
|
testList "byField" [
|
||||||
testTask "succeeds when sorting ascending" {
|
testTask "succeeds when documents are found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs =
|
let! docs = Find.byField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
|
||||||
Find.byFieldsOrdered<JsonDocument>
|
Expect.equal (List.length docs) 1 "There should have been one document returned"
|
||||||
PostgresDb.TableName All [ Field.EQ "Value" "purple" ] [ Field.Named "Id" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "five|four" "Documents not ordered correctly"
|
|
||||||
}
|
}
|
||||||
testTask "succeeds when sorting descending" {
|
testTask "succeeds when documents are not found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs =
|
let! docs = Find.byField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "mauve")
|
||||||
Find.byFieldsOrdered<JsonDocument>
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
PostgresDb.TableName All [ Field.EQ "Value" "purple" ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|five" "Documents not ordered correctly"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byContains" [
|
testList "byContains" [
|
||||||
@@ -760,31 +703,6 @@ let findTests = testList "Find" [
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byContainsOrdered" [
|
|
||||||
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "two|four" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|two" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "byJsonPath" [
|
testList "byJsonPath" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -801,37 +719,12 @@ let findTests = testList "Find" [
|
|||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isEmpty docs "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byJsonPathOrdered" [
|
testList "firstByField" [
|
||||||
// Id = one, NumValue = 0; Id = two, NumValue = 10; Id = three, NumValue = 4
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
|
|
||||||
Expect.hasLength docs 3 "There should have been 3 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "one|three|two" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
|
|
||||||
Expect.hasLength docs 3 "There should have been 3 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "two|three|one" "Documents not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "firstByFields" [
|
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "another" ]
|
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "another")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isSome doc "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
@@ -839,7 +732,7 @@ let findTests = testList "Find" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "purple" ]
|
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "purple")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isSome doc "There should have been a document returned"
|
||||||
Expect.contains [ "five"; "four" ] doc.Value.Id "An incorrect document was returned"
|
Expect.contains [ "five"; "four" ] doc.Value.Id "An incorrect document was returned"
|
||||||
}
|
}
|
||||||
@@ -847,32 +740,10 @@ let findTests = testList "Find" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> PostgresDb.TableName Any [ Field.EQ "Value" "absent" ]
|
let! doc = Find.firstByField<JsonDocument> PostgresDb.TableName (Field.EQ "Value" "absent")
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "firstByFieldsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] [ Field.Named "Id" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "five" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByFieldsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "firstByContains" [
|
testList "firstByContains" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -898,28 +769,6 @@ let findTests = testList "Find" [
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "firstByContainsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByContainsOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "firstByJsonPath" [
|
testList "firstByJsonPath" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -945,32 +794,8 @@ let findTests = testList "Find" [
|
|||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isNone doc "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "firstByJsonPathOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use db = PostgresDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByJsonPathOrdered<JsonDocument>
|
|
||||||
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
]
|
testList "Update" [
|
||||||
|
|
||||||
/// Integration tests for the Update module of the PostgreSQL library
|
|
||||||
let updateTests = testList "Update" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -990,7 +815,9 @@ let updateTests = testList "Update" [
|
|||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! Update.byId
|
do! Update.byId
|
||||||
PostgresDb.TableName "test" { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
PostgresDb.TableName
|
||||||
|
"test"
|
||||||
|
{ emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFunc" [
|
testList "byFunc" [
|
||||||
@@ -998,7 +825,8 @@ 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
|
||||||
@@ -1013,13 +841,12 @@ 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 }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Patch" [
|
||||||
/// Integration tests for the Patch module of the PostgreSQL library
|
|
||||||
let patchTests = testList "Patch" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -1040,13 +867,13 @@ let patchTests = testList "Patch" [
|
|||||||
do! Patch.byId PostgresDb.TableName "test" {| Foo = "green" |}
|
do! Patch.byId PostgresDb.TableName "test" {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Patch.byFields PostgresDb.TableName Any [ Field.EQ "Value" "purple" ] {| NumValue = 77 |}
|
do! Patch.byField PostgresDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |}
|
||||||
let! after = Count.byFields PostgresDb.TableName Any [ Field.EQ "NumValue" 77 ]
|
let! after = Count.byField PostgresDb.TableName (Field.EQ "NumValue" "77")
|
||||||
Expect.equal after 2 "There should have been 2 documents returned"
|
Expect.equal after 2 "There should have been 2 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
@@ -1056,7 +883,7 @@ let patchTests = testList "Patch" [
|
|||||||
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! Patch.byFields PostgresDb.TableName Any [ Field.EQ "Value" "burgundy" ] {| Foo = "green" |}
|
do! Patch.byField PostgresDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byContains" [
|
testList "byContains" [
|
||||||
@@ -1098,18 +925,16 @@ let patchTests = testList "Patch" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "RemoveFields" [
|
||||||
/// Integration tests for the RemoveFields module of the PostgreSQL library
|
|
||||||
let removeFieldsTests = testList "RemoveFields" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when multiple fields are removed" {
|
testTask "succeeds when multiple fields are removed" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub"; "Value" ]
|
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub"; "Value" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -1117,9 +942,9 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub" ]
|
do! RemoveFields.byId PostgresDb.TableName "two" [ "Sub" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -1136,25 +961,25 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! RemoveFields.byId PostgresDb.TableName "two" [ "Value" ]
|
do! RemoveFields.byId PostgresDb.TableName "two" [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when multiple fields are removed" {
|
testTask "succeeds when multiple fields are removed" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Sub"; "Value" ]
|
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub"; "Value" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Sub" ]
|
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Sub" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -1162,13 +987,13 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.EQ "NumValue" "17" ] [ "Nothing" ]
|
do! RemoveFields.byField PostgresDb.TableName (Field.EQ "NumValue" "17") [ "Nothing" ]
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is matched" {
|
testTask "succeeds when no document is matched" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! RemoveFields.byFields PostgresDb.TableName Any [ Field.NE "Abracadabra" "apple" ] [ "Value" ]
|
do! RemoveFields.byField PostgresDb.TableName (Field.NE "Abracadabra" "apple") [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byContains" [
|
testList "byContains" [
|
||||||
@@ -1177,9 +1002,9 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub"; "Value" ]
|
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub"; "Value" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -1187,9 +1012,9 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub" ]
|
do! RemoveFields.byContains PostgresDb.TableName {| NumValue = 17 |} [ "Sub" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -1212,9 +1037,9 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub"; "Value" ]
|
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub"; "Value" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
Expect.equal noValue 1 "There should be 1 document without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a single field is removed" {
|
testTask "succeeds when a single field is removed" {
|
||||||
@@ -1222,9 +1047,9 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub" ]
|
do! RemoveFields.byJsonPath PostgresDb.TableName "$.NumValue ? (@ == 17)" [ "Sub" ]
|
||||||
let! noSubs = Count.byFields PostgresDb.TableName Any [ Field.NEX "Sub" ]
|
let! noSubs = Count.byField PostgresDb.TableName (Field.NEX "Sub")
|
||||||
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
Expect.equal noSubs 4 "There should now be 4 documents without Sub fields"
|
||||||
let! noValue = Count.byFields PostgresDb.TableName Any [ Field.NEX "Value" ]
|
let! noValue = Count.byField PostgresDb.TableName (Field.NEX "Value")
|
||||||
Expect.equal noValue 0 "There should be no documents without Value fields"
|
Expect.equal noValue 0 "There should be no documents without Value fields"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a field is not removed" {
|
testTask "succeeds when a field is not removed" {
|
||||||
@@ -1242,9 +1067,7 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Delete" [
|
||||||
/// Integration tests for the Delete module of the PostgreSQL library
|
|
||||||
let deleteTests = testList "Delete" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is deleted" {
|
testTask "succeeds when a document is deleted" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
@@ -1263,12 +1086,12 @@ let deleteTests = testList "Delete" [
|
|||||||
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when documents are deleted" {
|
testTask "succeeds when documents are deleted" {
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Delete.byFields PostgresDb.TableName Any [ Field.EQ "Value" "purple" ]
|
do! Delete.byField PostgresDb.TableName (Field.EQ "Value" "purple")
|
||||||
let! remaining = Count.all PostgresDb.TableName
|
let! remaining = Count.all PostgresDb.TableName
|
||||||
Expect.equal remaining 3 "There should have been 3 documents remaining"
|
Expect.equal remaining 3 "There should have been 3 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -1276,7 +1099,7 @@ let deleteTests = testList "Delete" [
|
|||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Delete.byFields PostgresDb.TableName Any [ Field.EQ "Value" "crimson" ]
|
do! Delete.byField PostgresDb.TableName (Field.EQ "Value" "crimson")
|
||||||
let! remaining = Count.all PostgresDb.TableName
|
let! remaining = Count.all PostgresDb.TableName
|
||||||
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
Expect.equal remaining 5 "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -1318,21 +1141,8 @@ let deleteTests = testList "Delete" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
]
|
||||||
|
|> testSequenced
|
||||||
|
|
||||||
/// All tests for the PostgreSQL library
|
|
||||||
let all = testList "Postgres" [
|
let all = testList "Postgres" [ unitTests; integrationTests ]
|
||||||
testList "Unit" [ parametersTests; queryTests ]
|
|
||||||
testSequenced <| testList "Integration" [
|
|
||||||
configurationTests
|
|
||||||
customTests
|
|
||||||
definitionTests
|
|
||||||
documentTests
|
|
||||||
countTests
|
|
||||||
existsTests
|
|
||||||
findTests
|
|
||||||
updateTests
|
|
||||||
patchTests
|
|
||||||
removeFieldsTests
|
|
||||||
deleteTests
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ open Expecto
|
|||||||
open Microsoft.Data.Sqlite
|
open Microsoft.Data.Sqlite
|
||||||
open Types
|
open Types
|
||||||
|
|
||||||
|
#nowarn "0044"
|
||||||
|
|
||||||
/// Integration tests for the F# extensions on the SqliteConnection data type
|
/// Integration tests for the F# extensions on the SqliteConnection data type
|
||||||
let integrationTests =
|
let integrationTests =
|
||||||
let loadDocs () = backgroundTask {
|
let loadDocs () = backgroundTask {
|
||||||
@@ -113,12 +115,12 @@ let integrationTests =
|
|||||||
let! theCount = conn.countAll SqliteDb.TableName
|
let! theCount = conn.countAll SqliteDb.TableName
|
||||||
Expect.equal theCount 5L "There should have been 5 matching documents"
|
Expect.equal theCount 5L "There should have been 5 matching documents"
|
||||||
}
|
}
|
||||||
testTask "countByFields succeeds" {
|
testTask "countByField succeeds" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! theCount = conn.countByFields SqliteDb.TableName Any [ Field.EQ "Value" "purple" ]
|
let! theCount = conn.countByField SqliteDb.TableName (Field.EQ "Value" "purple")
|
||||||
Expect.equal theCount 2L "There should have been 2 matching documents"
|
Expect.equal theCount 2L "There should have been 2 matching documents"
|
||||||
}
|
}
|
||||||
testList "existsById" [
|
testList "existsById" [
|
||||||
@@ -139,13 +141,13 @@ let integrationTests =
|
|||||||
Expect.isFalse exists "There should not have been an existing document"
|
Expect.isFalse exists "There should not have been an existing document"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "existsByFields" [
|
testList "existsByField" [
|
||||||
testTask "succeeds when documents exist" {
|
testTask "succeeds when documents exist" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! exists = conn.existsByFields SqliteDb.TableName Any [ Field.EQ "NumValue" 10 ]
|
let! exists = conn.existsByField SqliteDb.TableName (Field.EQ "NumValue" 10)
|
||||||
Expect.isTrue exists "There should have been existing documents"
|
Expect.isTrue exists "There should have been existing documents"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no matching documents exist" {
|
testTask "succeeds when no matching documents exist" {
|
||||||
@@ -153,7 +155,7 @@ let integrationTests =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! exists = conn.existsByFields SqliteDb.TableName Any [ Field.EQ "Nothing" "none" ]
|
let! exists = conn.existsByField SqliteDb.TableName (Field.EQ "Nothing" "none")
|
||||||
Expect.isFalse exists "There should not have been any existing documents"
|
Expect.isFalse exists "There should not have been any existing documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -181,44 +183,6 @@ let integrationTests =
|
|||||||
Expect.equal results [] "There should have been no documents returned"
|
Expect.equal results [] "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findAllOrdered" [
|
|
||||||
testTask "succeeds when ordering numerically" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"one|three|two|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering numerically descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"five|four|two|three|one"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering alphabetically" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "Id DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"two|three|one|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findById" [
|
testList "findById" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -226,7 +190,7 @@ let integrationTests =
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "two"
|
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "two"
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a document is not found" {
|
testTask "succeeds when a document is not found" {
|
||||||
@@ -235,59 +199,35 @@ let integrationTests =
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "three hundred eighty-seven"
|
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "three hundred eighty-seven"
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByFields" [
|
testList "findByField" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = conn.findByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ]
|
let! docs = conn.findByField<JsonDocument> SqliteDb.TableName (Field.EQ "Sub.Foo" "green")
|
||||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
Expect.equal (List.length docs) 2 "There should have been two documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when documents are not found" {
|
testTask "succeeds when documents are not found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = conn.findByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "mauve" ]
|
let! docs = conn.findByField<JsonDocument> SqliteDb.TableName (Field.EQ "Value" "mauve")
|
||||||
Expect.isEmpty docs "There should have been no documents returned"
|
Expect.isTrue (List.isEmpty docs) "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "findByFieldsOrdered" [
|
testList "findFirstByField" [
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id" ]
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "five|four" "The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
conn.findByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|five" "The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findFirstByFields" [
|
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "another" ]
|
let! doc = conn.findFirstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Value" "another")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when multiple documents are found" {
|
testTask "succeeds when multiple documents are found" {
|
||||||
@@ -295,8 +235,8 @@ let integrationTests =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ]
|
let! doc = conn.findFirstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Sub.Foo" "green")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
|
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a document is not found" {
|
testTask "succeeds when a document is not found" {
|
||||||
@@ -304,32 +244,8 @@ let integrationTests =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "absent" ]
|
let! doc = conn.findFirstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Value" "absent")
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "findFirstByFieldsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
use conn = Configuration.dbConn ()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "updateById" [
|
testList "updateById" [
|
||||||
@@ -410,14 +326,14 @@ let integrationTests =
|
|||||||
do! conn.patchById SqliteDb.TableName "test" {| Foo = "green" |}
|
do! conn.patchById SqliteDb.TableName "test" {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "patchByFields" [
|
testList "patchByField" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! conn.patchByFields SqliteDb.TableName Any [ Field.EQ "Value" "purple" ] {| NumValue = 77 |}
|
do! conn.patchByField SqliteDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |}
|
||||||
let! after = conn.countByFields SqliteDb.TableName Any [ Field.EQ "NumValue" 77 ]
|
let! after = conn.countByField SqliteDb.TableName (Field.EQ "NumValue" 77)
|
||||||
Expect.equal after 2L "There should have been 2 documents returned"
|
Expect.equal after 2L "There should have been 2 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
@@ -428,7 +344,7 @@ let integrationTests =
|
|||||||
Expect.isEmpty before "There should have been no documents returned"
|
Expect.isEmpty before "There should have been no documents returned"
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! conn.patchByFields SqliteDb.TableName Any [ Field.EQ "Value" "burgundy" ] {| Foo = "green" |}
|
do! conn.patchByField SqliteDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "removeFieldsById" [
|
testList "removeFieldsById" [
|
||||||
@@ -461,13 +377,13 @@ let integrationTests =
|
|||||||
do! conn.removeFieldsById SqliteDb.TableName "two" [ "Value" ]
|
do! conn.removeFieldsById SqliteDb.TableName "two" [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "removeFieldByFields" [
|
testList "removeFieldByField" [
|
||||||
testTask "succeeds when a field is removed" {
|
testTask "succeeds when a field is removed" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Sub" ]
|
do! conn.removeFieldsByField SqliteDb.TableName (Field.EQ "NumValue" 17) [ "Sub" ]
|
||||||
try
|
try
|
||||||
let! _ = conn.findById<string, JsonDocument> SqliteDb.TableName "four"
|
let! _ = conn.findById<string, JsonDocument> SqliteDb.TableName "four"
|
||||||
Expect.isTrue false "The updated document should have failed to parse"
|
Expect.isTrue false "The updated document should have failed to parse"
|
||||||
@@ -481,14 +397,14 @@ let integrationTests =
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Nothing" ]
|
do! conn.removeFieldsByField SqliteDb.TableName (Field.EQ "NumValue" 17) [ "Nothing" ]
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is matched" {
|
testTask "succeeds when no document is matched" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.NE "Abracadabra" "apple" ] [ "Value" ]
|
do! conn.removeFieldsByField SqliteDb.TableName (Field.NE "Abracadabra" "apple") [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "deleteById" [
|
testList "deleteById" [
|
||||||
@@ -511,13 +427,13 @@ let integrationTests =
|
|||||||
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "deleteByFields" [
|
testList "deleteByField" [
|
||||||
testTask "succeeds when documents are deleted" {
|
testTask "succeeds when documents are deleted" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! conn.deleteByFields SqliteDb.TableName Any [ Field.NE "Value" "purple" ]
|
do! conn.deleteByField SqliteDb.TableName (Field.NE "Value" "purple")
|
||||||
let! remaining = conn.countAll SqliteDb.TableName
|
let! remaining = conn.countAll SqliteDb.TableName
|
||||||
Expect.equal remaining 2L "There should have been 2 documents remaining"
|
Expect.equal remaining 2L "There should have been 2 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -526,7 +442,7 @@ let integrationTests =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! conn.deleteByFields SqliteDb.TableName Any [ Field.EQ "Value" "crimson" ]
|
do! conn.deleteByField SqliteDb.TableName (Field.EQ "Value" "crimson")
|
||||||
let! remaining = conn.countAll SqliteDb.TableName
|
let! remaining = conn.countAll SqliteDb.TableName
|
||||||
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -564,8 +480,8 @@ let integrationTests =
|
|||||||
use conn = Configuration.dbConn ()
|
use conn = Configuration.dbConn ()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = conn.customList (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
let! docs = conn.customList (Query.selectFromTable SqliteDb.TableName) [] fromData<JsonDocument>
|
||||||
Expect.hasLength docs 5 "There should have been 5 documents returned"
|
Expect.hasCountOf docs 5u (fun _ -> true) "There should have been 5 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when data is not found" {
|
testTask "succeeds when data is not found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ open Types
|
|||||||
|
|
||||||
#nowarn "0044"
|
#nowarn "0044"
|
||||||
|
|
||||||
(** UNIT TESTS **)
|
/// Unit tests for the SQLite library
|
||||||
|
let unitTests =
|
||||||
/// Unit tests for the Query module of the SQLite library
|
testList "Unit" [
|
||||||
let queryTests = testList "Query" [
|
testList "Query" [
|
||||||
testList "whereByFields" [
|
testList "whereByFields" [
|
||||||
test "succeeds for a single field when a logical operator is passed" {
|
test "succeeds for a single field when a logical operator is passed" {
|
||||||
Expect.equal
|
Expect.equal
|
||||||
@@ -83,9 +83,7 @@ let queryTests = testList "Query" [
|
|||||||
"CREATE TABLE statement not correct"
|
"CREATE TABLE statement not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Parameters" [
|
||||||
/// Unit tests for the Parameters module of the SQLite library
|
|
||||||
let parametersTests = testList "Parameters" [
|
|
||||||
test "idParam succeeds" {
|
test "idParam succeeds" {
|
||||||
let theParam = idParam 7
|
let theParam = idParam 7
|
||||||
Expect.equal theParam.ParameterName "@id" "The parameter name is incorrect"
|
Expect.equal theParam.ParameterName "@id" "The parameter name is incorrect"
|
||||||
@@ -114,17 +112,22 @@ let parametersTests = testList "Parameters" [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
// Results are exhaustively executed in the context of other tests
|
// Results are exhaustively executed in the context of other tests
|
||||||
|
]
|
||||||
|
|
||||||
|
/// These tests each use a fresh copy of a SQLite database
|
||||||
(** INTEGRATION TESTS **)
|
let integrationTests =
|
||||||
|
let documents = [
|
||||||
/// Load a table with the test documents
|
{ Id = "one"; Value = "FIRST!"; NumValue = 0; Sub = None }
|
||||||
|
{ Id = "two"; Value = "another"; NumValue = 10; Sub = Some { Foo = "green"; Bar = "blue" } }
|
||||||
|
{ Id = "three"; Value = ""; NumValue = 4; Sub = None }
|
||||||
|
{ Id = "four"; Value = "purple"; NumValue = 17; Sub = Some { Foo = "green"; Bar = "red" } }
|
||||||
|
{ Id = "five"; Value = "purple"; NumValue = 18; Sub = None }
|
||||||
|
]
|
||||||
let loadDocs () = backgroundTask {
|
let loadDocs () = backgroundTask {
|
||||||
for doc in testDocuments do do! insert SqliteDb.TableName doc
|
for doc in documents do do! insert SqliteDb.TableName doc
|
||||||
}
|
}
|
||||||
|
testList "Integration" [
|
||||||
/// Integration tests for the Configuration module of the SQLite library
|
testList "Configuration" [
|
||||||
let configurationTests = testList "Configuration" [
|
|
||||||
test "useConnectionString / connectionString succeed" {
|
test "useConnectionString / connectionString succeed" {
|
||||||
try
|
try
|
||||||
Configuration.useConnectionString "Data Source=test.db"
|
Configuration.useConnectionString "Data Source=test.db"
|
||||||
@@ -135,10 +138,34 @@ let configurationTests = testList "Configuration" [
|
|||||||
finally
|
finally
|
||||||
Configuration.useConnectionString "Data Source=:memory:"
|
Configuration.useConnectionString "Data Source=:memory:"
|
||||||
}
|
}
|
||||||
]
|
test "useSerializer succeeds" {
|
||||||
|
try
|
||||||
|
Configuration.useSerializer
|
||||||
|
{ new IDocumentSerializer with
|
||||||
|
member _.Serialize<'T>(it: 'T) : string = """{"Overridden":true}"""
|
||||||
|
member _.Deserialize<'T>(it: string) : 'T = Unchecked.defaultof<'T>
|
||||||
|
}
|
||||||
|
|
||||||
/// Integration tests for the Custom module of the SQLite library
|
let serialized = Configuration.serializer().Serialize { Foo = "howdy"; Bar = "bye"}
|
||||||
let customTests = testList "Custom" [
|
Expect.equal serialized """{"Overridden":true}""" "Specified serializer was not used"
|
||||||
|
|
||||||
|
let deserialized = Configuration.serializer().Deserialize<obj> """{"Something":"here"}"""
|
||||||
|
Expect.isNull deserialized "Specified serializer should have returned null"
|
||||||
|
finally
|
||||||
|
Configuration.useSerializer DocumentSerializer.``default``
|
||||||
|
}
|
||||||
|
test "serializer returns configured serializer" {
|
||||||
|
Expect.isTrue (obj.ReferenceEquals(DocumentSerializer.``default``, Configuration.serializer ()))
|
||||||
|
"Serializer should have been the same"
|
||||||
|
}
|
||||||
|
test "useIdField / idField succeeds" {
|
||||||
|
Expect.equal (Configuration.idField ()) "Id" "The default configured ID field was incorrect"
|
||||||
|
Configuration.useIdField "id"
|
||||||
|
Expect.equal (Configuration.idField ()) "id" "useIdField did not set the ID field"
|
||||||
|
Configuration.useIdField "Id"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
testList "Custom" [
|
||||||
testList "single" [
|
testList "single" [
|
||||||
testTask "succeeds when a row is found" {
|
testTask "succeeds when a row is found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -169,7 +196,7 @@ let customTests = testList "Custom" [
|
|||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = Custom.list (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
let! docs = Custom.list (Query.selectFromTable SqliteDb.TableName) [] fromData<JsonDocument>
|
||||||
Expect.hasCountOf docs 5u (fun _ -> true) "There should have been 5 documents returned"
|
Expect.hasCountOf docs 5u (fun _ -> true) "There should have been 5 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when data is not found" {
|
testTask "succeeds when data is not found" {
|
||||||
@@ -213,9 +240,7 @@ let customTests = testList "Custom" [
|
|||||||
Expect.equal nbr 5 "The query should have returned the number 5"
|
Expect.equal nbr 5 "The query should have returned the number 5"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
testList "Definition" [
|
||||||
/// Integration tests for the Definition module of the SQLite library
|
|
||||||
let definitionTests = testList "Definition" [
|
|
||||||
testTask "ensureTable succeeds" {
|
testTask "ensureTable succeeds" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
let itExists (name: string) =
|
let itExists (name: string) =
|
||||||
@@ -252,9 +277,6 @@ let definitionTests = testList "Definition" [
|
|||||||
Expect.isTrue exists' "The index should now exist"
|
Expect.isTrue exists' "The index should now exist"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
/// Integration tests for the (auto-opened) Document module of the SQLite library
|
|
||||||
let documentTests = testList "Document" [
|
|
||||||
testList "insert" [
|
testList "insert" [
|
||||||
testTask "succeeds" {
|
testTask "succeeds" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -274,68 +296,6 @@ let documentTests = testList "Document" [
|
|||||||
insert SqliteDb.TableName {emptyDoc with Id = "test" } |> Async.AwaitTask |> Async.RunSynchronously)
|
insert SqliteDb.TableName {emptyDoc with Id = "test" } |> Async.AwaitTask |> Async.RunSynchronously)
|
||||||
"An exception should have been raised for duplicate document ID insert"
|
"An exception should have been raised for duplicate document ID insert"
|
||||||
}
|
}
|
||||||
testTask "succeeds when adding a numeric auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy Number
|
|
||||||
Configuration.useIdField "Key"
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
let! before = Count.all SqliteDb.TableName
|
|
||||||
Expect.equal before 0L "There should be no documents in the table"
|
|
||||||
|
|
||||||
do! insert SqliteDb.TableName { Key = 0; Text = "one" }
|
|
||||||
do! insert SqliteDb.TableName { Key = 0; Text = "two" }
|
|
||||||
do! insert SqliteDb.TableName { Key = 77; Text = "three" }
|
|
||||||
do! insert SqliteDb.TableName { Key = 0; Text = "four" }
|
|
||||||
|
|
||||||
let! after = Find.allOrdered<NumIdDocument> SqliteDb.TableName [ Field.Named "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"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
Configuration.useIdField "Id"
|
|
||||||
}
|
|
||||||
testTask "succeeds when adding a GUID auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy Guid
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
let! before = Count.all SqliteDb.TableName
|
|
||||||
Expect.equal before 0L "There should be no documents in the table"
|
|
||||||
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "one" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "two" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Id = "abc123"; Value = "three" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "four" }
|
|
||||||
|
|
||||||
let! after = Find.all<JsonDocument> SqliteDb.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"
|
|
||||||
Expect.hasCountOf after 1u (fun doc -> doc.Id = "abc123") "The provided ID should have been used as-is"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
}
|
|
||||||
testTask "succeeds when adding a RandomString auto ID" {
|
|
||||||
try
|
|
||||||
Configuration.useAutoIdStrategy RandomString
|
|
||||||
Configuration.useIdStringLength 44
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
let! before = Count.all SqliteDb.TableName
|
|
||||||
Expect.equal before 0L "There should be no documents in the table"
|
|
||||||
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "one" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "two" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Id = "abc123"; Value = "three" }
|
|
||||||
do! insert SqliteDb.TableName { emptyDoc with Value = "four" }
|
|
||||||
|
|
||||||
let! after = Find.all<JsonDocument> SqliteDb.TableName
|
|
||||||
Expect.hasLength after 4 "There should have been 4 documents returned"
|
|
||||||
Expect.hasCountOf
|
|
||||||
after 3u (fun doc -> doc.Id.Length = 44)
|
|
||||||
"Three of the IDs should have been 44-character random strings"
|
|
||||||
Expect.hasCountOf after 1u (fun doc -> doc.Id = "abc123") "The provided ID should have been used as-is"
|
|
||||||
finally
|
|
||||||
Configuration.useAutoIdStrategy Disabled
|
|
||||||
Configuration.useIdStringLength 16
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
testList "save" [
|
testList "save" [
|
||||||
testTask "succeeds when a document is inserted" {
|
testTask "succeeds when a document is inserted" {
|
||||||
@@ -364,10 +324,7 @@ let documentTests = testList "Document" [
|
|||||||
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
testList "Count" [
|
||||||
|
|
||||||
/// Integration tests for the Count module of the SQLite library
|
|
||||||
let countTests = testList "Count" [
|
|
||||||
testTask "all succeeds" {
|
testTask "all succeeds" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
@@ -375,26 +332,22 @@ let countTests = testList "Count" [
|
|||||||
let! theCount = Count.all SqliteDb.TableName
|
let! theCount = Count.all SqliteDb.TableName
|
||||||
Expect.equal theCount 5L "There should have been 5 matching documents"
|
Expect.equal theCount 5L "There should have been 5 matching documents"
|
||||||
}
|
}
|
||||||
testList "byFields" [
|
testTask "byField succeeds for a numeric range" {
|
||||||
testTask "succeeds for a numeric range" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.BT "NumValue" 10 20 ]
|
let! theCount = Count.byField SqliteDb.TableName (Field.BT "NumValue" 10 20)
|
||||||
Expect.equal theCount 3L "There should have been 3 matching documents"
|
Expect.equal theCount 3L "There should have been 3 matching documents"
|
||||||
}
|
}
|
||||||
testTask "succeeds for a non-numeric range" {
|
testTask "byField succeeds for a non-numeric range" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! theCount = Count.byFields SqliteDb.TableName Any [ Field.BT "Value" "aardvark" "apple" ]
|
let! theCount = Count.byField SqliteDb.TableName (Field.BT "Value" "aardvark" "apple")
|
||||||
Expect.equal theCount 1L "There should have been 1 matching document"
|
Expect.equal theCount 1L "There should have been 1 matching document"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
testList "Exists" [
|
||||||
|
|
||||||
/// Integration tests for the Exists module of the SQLite library
|
|
||||||
let existsTests = testList "Exists" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document exists" {
|
testTask "succeeds when a document exists" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -411,26 +364,24 @@ let existsTests = testList "Exists" [
|
|||||||
Expect.isFalse exists "There should not have been an existing document"
|
Expect.isFalse exists "There should not have been an existing document"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when documents exist" {
|
testTask "succeeds when documents exist" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 10 ]
|
let! exists = Exists.byField SqliteDb.TableName (Field.EQ "NumValue" 10)
|
||||||
Expect.isTrue exists "There should have been existing documents"
|
Expect.isTrue exists "There should have been existing documents"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no matching documents exist" {
|
testTask "succeeds when no matching documents exist" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! exists = Exists.byFields SqliteDb.TableName Any [ Field.LT "Nothing" "none" ]
|
let! exists = Exists.byField SqliteDb.TableName (Field.LT "Nothing" "none")
|
||||||
Expect.isFalse exists "There should not have been any existing documents"
|
Expect.isFalse exists "There should not have been any existing documents"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Find" [
|
||||||
/// Integration tests for the Find module of the SQLite library
|
|
||||||
let findTests = testList "Find" [
|
|
||||||
testList "all" [
|
testList "all" [
|
||||||
testTask "succeeds when there is data" {
|
testTask "succeeds when there is data" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -440,10 +391,12 @@ let findTests = testList "Find" [
|
|||||||
do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
||||||
|
|
||||||
let! results = Find.all<SubDocument> SqliteDb.TableName
|
let! results = Find.all<SubDocument> SqliteDb.TableName
|
||||||
Expect.equal
|
let expected = [
|
||||||
results
|
{ Foo = "one"; Bar = "two" }
|
||||||
[ { Foo = "one"; Bar = "two" }; { Foo = "three"; Bar = "four" }; { Foo = "five"; Bar = "six" } ]
|
{ Foo = "three"; Bar = "four" }
|
||||||
"There should have been 3 documents returned"
|
{ Foo = "five"; Bar = "six" }
|
||||||
|
]
|
||||||
|
Expect.equal results expected "There should have been 3 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when there is no data" {
|
testTask "succeeds when there is no data" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -451,48 +404,13 @@ let findTests = testList "Find" [
|
|||||||
Expect.equal results [] "There should have been no documents returned"
|
Expect.equal results [] "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "allOrdered" [
|
|
||||||
testTask "succeeds when ordering numerically" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = Find.allOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"one|three|two|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering numerically descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = Find.allOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"five|four|two|three|one"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when ordering alphabetically" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! results = Find.allOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "Id DESC" ]
|
|
||||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
|
||||||
Expect.equal
|
|
||||||
(results |> List.map _.Id |> String.concat "|")
|
|
||||||
"two|three|one|four|five"
|
|
||||||
"The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.byId<string, JsonDocument> SqliteDb.TableName "two"
|
let! doc = Find.byId<string, JsonDocument> SqliteDb.TableName "two"
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when a document is not found" {
|
testTask "succeeds when a document is not found" {
|
||||||
@@ -500,98 +418,52 @@ let findTests = testList "Find" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.byId<string, JsonDocument> SqliteDb.TableName "three hundred eighty-seven"
|
let! doc = Find.byId<string, JsonDocument> SqliteDb.TableName "three hundred eighty-seven"
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when documents are found" {
|
testTask "succeeds when documents are found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.GT "NumValue" 15 ]
|
let! docs = Find.byField<JsonDocument> SqliteDb.TableName (Field.GT "NumValue" 15)
|
||||||
Expect.equal (List.length docs) 2 "There should have been two documents returned"
|
Expect.equal (List.length docs) 2 "There should have been two documents returned"
|
||||||
}
|
}
|
||||||
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 ()
|
||||||
|
|
||||||
let! docs = Find.byFields<JsonDocument> SqliteDb.TableName Any [ Field.GT "NumValue" 100 ]
|
let! docs = Find.byField<JsonDocument> SqliteDb.TableName (Field.GT "NumValue" 100)
|
||||||
Expect.isTrue (List.isEmpty docs) "There should have been no documents returned"
|
Expect.isTrue (List.isEmpty docs) "There should have been no documents returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFieldsOrdered" [
|
testList "firstByField" [
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id" ]
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "five|four" "The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! docs =
|
|
||||||
Find.byFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.GT "NumValue" 15 ] [ Field.Named "Id DESC" ]
|
|
||||||
Expect.equal
|
|
||||||
(docs |> List.map _.Id |> String.concat "|") "four|five" "The documents were not ordered correctly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "firstByFields" [
|
|
||||||
testTask "succeeds when a document is found" {
|
testTask "succeeds when a document is found" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "another" ]
|
let! doc = Find.firstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Value" "another")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
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 ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ]
|
let! doc = Find.firstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Sub.Foo" "green")
|
||||||
Expect.isSome doc "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
|
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
|
||||||
}
|
}
|
||||||
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 ()
|
||||||
|
|
||||||
let! doc = Find.firstByFields<JsonDocument> SqliteDb.TableName Any [ Field.EQ "Value" "absent" ]
|
let! doc = Find.firstByField<JsonDocument> SqliteDb.TableName (Field.EQ "Value" "absent")
|
||||||
Expect.isNone doc "There should not have been a document returned"
|
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
||||||
}
|
|
||||||
]
|
|
||||||
testList "firstByFieldsOrdered" [
|
|
||||||
testTask "succeeds when sorting ascending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "two" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
|
||||||
testTask "succeeds when sorting descending" {
|
|
||||||
use! db = SqliteDb.BuildDb()
|
|
||||||
do! loadDocs ()
|
|
||||||
|
|
||||||
let! doc =
|
|
||||||
Find.firstByFieldsOrdered<JsonDocument>
|
|
||||||
SqliteDb.TableName Any [ Field.EQ "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
|
|
||||||
Expect.isSome doc "There should have been a document returned"
|
|
||||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Update" [
|
||||||
/// Integration tests for the Update module of the SQLite library
|
|
||||||
let updateTests = testList "Update" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -611,7 +483,9 @@ let updateTests = testList "Update" [
|
|||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! Update.byId
|
do! Update.byId
|
||||||
SqliteDb.TableName "test" { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
SqliteDb.TableName
|
||||||
|
"test"
|
||||||
|
{ emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFunc" [
|
testList "byFunc" [
|
||||||
@@ -619,7 +493,8 @@ let updateTests = testList "Update" [
|
|||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Update.byFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
do! Update.byFunc
|
||||||
|
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one"
|
let! after = Find.byId<string, JsonDocument> SqliteDb.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
|
||||||
@@ -634,13 +509,12 @@ let updateTests = testList "Update" [
|
|||||||
Expect.isEmpty before "There should have been no documents returned"
|
Expect.isEmpty before "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 SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
do! Update.byFunc
|
||||||
|
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Patch" [
|
||||||
/// Integration tests for the Patch module of the SQLite library
|
|
||||||
let patchTests = testList "Patch" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -661,13 +535,13 @@ let patchTests = testList "Patch" [
|
|||||||
do! Patch.byId SqliteDb.TableName "test" {| Foo = "green" |}
|
do! Patch.byId SqliteDb.TableName "test" {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when a document is updated" {
|
testTask "succeeds when a document is updated" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Patch.byFields SqliteDb.TableName Any [ Field.EQ "Value" "purple" ] {| NumValue = 77 |}
|
do! Patch.byField SqliteDb.TableName (Field.EQ "Value" "purple") {| NumValue = 77 |}
|
||||||
let! after = Count.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 77 ]
|
let! after = Count.byField SqliteDb.TableName (Field.EQ "NumValue" 77)
|
||||||
Expect.equal after 2L "There should have been 2 documents returned"
|
Expect.equal after 2L "There should have been 2 documents returned"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
@@ -677,13 +551,11 @@ let patchTests = testList "Patch" [
|
|||||||
Expect.isEmpty before "There should have been no documents returned"
|
Expect.isEmpty before "There should have been no documents returned"
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! Patch.byFields SqliteDb.TableName Any [ Field.EQ "Value" "burgundy" ] {| Foo = "green" |}
|
do! Patch.byField SqliteDb.TableName (Field.EQ "Value" "burgundy") {| Foo = "green" |}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "RemoveFields" [
|
||||||
/// Integration tests for the RemoveFields module of the SQLite library
|
|
||||||
let removeFieldsTests = testList "RemoveFields" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when fields is removed" {
|
testTask "succeeds when fields is removed" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -711,12 +583,12 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! RemoveFields.byId SqliteDb.TableName "two" [ "Value" ]
|
do! RemoveFields.byId SqliteDb.TableName "two" [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when a field is removed" {
|
testTask "succeeds when a field is removed" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Sub" ]
|
do! RemoveFields.byField SqliteDb.TableName (Field.EQ "NumValue" 17) [ "Sub" ]
|
||||||
try
|
try
|
||||||
let! _ = Find.byId<string, JsonDocument> SqliteDb.TableName "four"
|
let! _ = Find.byId<string, JsonDocument> SqliteDb.TableName "four"
|
||||||
Expect.isTrue false "The updated document should have failed to parse"
|
Expect.isTrue false "The updated document should have failed to parse"
|
||||||
@@ -729,19 +601,17 @@ let removeFieldsTests = testList "RemoveFields" [
|
|||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.EQ "NumValue" 17 ] [ "Nothing" ]
|
do! RemoveFields.byField SqliteDb.TableName (Field.EQ "NumValue" 17) [ "Nothing" ]
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is matched" {
|
testTask "succeeds when no document is matched" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
|
|
||||||
// This not raising an exception is the test
|
// This not raising an exception is the test
|
||||||
do! RemoveFields.byFields SqliteDb.TableName Any [ Field.NE "Abracadabra" "apple" ] [ "Value" ]
|
do! RemoveFields.byField SqliteDb.TableName (Field.NE "Abracadabra" "apple") [ "Value" ]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
testList "Delete" [
|
||||||
/// Integration tests for the Delete module of the SQLite library
|
|
||||||
let deleteTests = testList "Delete" [
|
|
||||||
testList "byId" [
|
testList "byId" [
|
||||||
testTask "succeeds when a document is deleted" {
|
testTask "succeeds when a document is deleted" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
@@ -760,12 +630,12 @@ let deleteTests = testList "Delete" [
|
|||||||
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "byFields" [
|
testList "byField" [
|
||||||
testTask "succeeds when documents are deleted" {
|
testTask "succeeds when documents are deleted" {
|
||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Delete.byFields SqliteDb.TableName Any [ Field.NE "Value" "purple" ]
|
do! Delete.byField SqliteDb.TableName (Field.NE "Value" "purple")
|
||||||
let! remaining = Count.all SqliteDb.TableName
|
let! remaining = Count.all SqliteDb.TableName
|
||||||
Expect.equal remaining 2L "There should have been 2 documents remaining"
|
Expect.equal remaining 2L "There should have been 2 documents remaining"
|
||||||
}
|
}
|
||||||
@@ -773,28 +643,16 @@ let deleteTests = testList "Delete" [
|
|||||||
use! db = SqliteDb.BuildDb()
|
use! db = SqliteDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Delete.byFields SqliteDb.TableName Any [ Field.EQ "Value" "crimson" ]
|
do! Delete.byField SqliteDb.TableName (Field.EQ "Value" "crimson")
|
||||||
let! remaining = Count.all SqliteDb.TableName
|
let! remaining = Count.all SqliteDb.TableName
|
||||||
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
test "clean up database" {
|
||||||
|
Configuration.useConnectionString "data source=:memory:"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|> testSequenced
|
||||||
|
|
||||||
/// All tests for the SQLite library
|
let all = testList "Sqlite" [ unitTests; integrationTests ]
|
||||||
let all = testList "Sqlite" [
|
|
||||||
testList "Unit" [ queryTests; parametersTests ]
|
|
||||||
testSequenced <| testList "Integration" [
|
|
||||||
configurationTests
|
|
||||||
customTests
|
|
||||||
definitionTests
|
|
||||||
documentTests
|
|
||||||
countTests
|
|
||||||
existsTests
|
|
||||||
findTests
|
|
||||||
updateTests
|
|
||||||
patchTests
|
|
||||||
removeFieldsTests
|
|
||||||
deleteTests
|
|
||||||
test "clean up database" { Configuration.useConnectionString "data source=:memory:" }
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
module Types
|
module Types
|
||||||
|
|
||||||
type NumIdDocument =
|
|
||||||
{ Key: int
|
|
||||||
Text: string }
|
|
||||||
|
|
||||||
type SubDocument =
|
type SubDocument =
|
||||||
{ Foo: string
|
{ Foo: string
|
||||||
Bar: string }
|
Bar: string }
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
echo --- Package Common library
|
echo --- Package Common library
|
||||||
rm Common/bin/Release/BitBadger.Documents.Common.*.nupkg || true
|
|
||||||
dotnet pack Common/BitBadger.Documents.Common.fsproj -c Release
|
dotnet pack Common/BitBadger.Documents.Common.fsproj -c Release
|
||||||
cp Common/bin/Release/BitBadger.Documents.Common.*.nupkg .
|
cp Common/bin/Release/BitBadger.Documents.Common.*.nupkg .
|
||||||
|
|
||||||
echo --- Package PostgreSQL library
|
echo --- Package PostgreSQL library
|
||||||
rm Postgres/bin/Release/BitBadger.Documents.Postgres*.nupkg || true
|
|
||||||
dotnet pack Postgres/BitBadger.Documents.Postgres.fsproj -c Release
|
dotnet pack Postgres/BitBadger.Documents.Postgres.fsproj -c Release
|
||||||
cp Postgres/bin/Release/BitBadger.Documents.Postgres.*.nupkg .
|
cp Postgres/bin/Release/BitBadger.Documents.Postgres.*.nupkg .
|
||||||
|
|
||||||
echo --- Package SQLite library
|
echo --- Package SQLite library
|
||||||
rm Sqlite/bin/Release/BitBadger.Documents.Sqlite*.nupkg || true
|
|
||||||
dotnet pack Sqlite/BitBadger.Documents.Sqlite.fsproj -c Release
|
dotnet pack Sqlite/BitBadger.Documents.Sqlite.fsproj -c Release
|
||||||
cp Sqlite/bin/Release/BitBadger.Documents.Sqlite.*.nupkg .
|
cp Sqlite/bin/Release/BitBadger.Documents.Sqlite.*.nupkg .
|
||||||
|
|||||||
Reference in New Issue
Block a user