diff --git a/src/Postgres/Library.fs b/src/Postgres/Library.fs index 4be1a0c..d7d66f2 100644 --- a/src/Postgres/Library.fs +++ b/src/Postgres/Library.fs @@ -121,15 +121,17 @@ module Query = |> Seq.map (fun it -> match it.Op with | EX | NEX -> $"{it.PgSqlPath} {it.Op}" - | BT -> - let p = name.Derive it.ParameterName - let names = $"{p}min AND {p}max" - let values = it.Value :?> obj list - match values[0] with + | _ -> + let p = name.Derive it.ParameterName + let path, value = + match it.Op with + | BT -> $"{p}min AND {p}max", (it.Value :?> obj list)[0] + | _ -> p, it.Value + printfn $"%A{value}" + match value with | :? int8 | :? uint8 | :? int16 | :? uint16 | :? int | :? uint32 | :? int64 | :? uint64 - | :? decimal | :? single | :? double -> $"({it.PgSqlPath})::numeric {it.Op} {names}" - | _ -> $"{it.PgSqlPath} {it.Op} {names}" - | _ -> $"{it.PgSqlPath} {it.Op} {name.Derive it.ParameterName}") + | :? decimal | :? single | :? double -> $"({it.PgSqlPath})::numeric {it.Op} {path}" + | _ -> $"{it.PgSqlPath} {it.Op} {path}") |> String.concat (match howMatched with Any -> " OR " | All -> " AND ") /// Create a WHERE clause fragment to implement a comparison on a field in a JSON document diff --git a/src/Tests/PostgresTests.fs b/src/Tests/PostgresTests.fs index 229da1e..bbb9bef 100644 --- a/src/Tests/PostgresTests.fs +++ b/src/Tests/PostgresTests.fs @@ -664,20 +664,39 @@ let integrationTests = let! theCount = Count.all PostgresDb.TableName Expect.equal theCount 5 "There should have been 5 matching documents" } - testTask "byField 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 "byField 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" - } + testList "byFields" [ + testTask "succeeds when items are found" { + use db = PostgresDb.BuildDb() + do! loadDocs () + + 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" + } + testTask "succeeds when items are not found" { + use db = PostgresDb.BuildDb() + do! loadDocs () + + let! theCount = Count.byFields PostgresDb.TableName All [ Field.EX "Sub"; Field.GT "NumValue" 100 ] + 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" { use db = PostgresDb.BuildDb() do! loadDocs () @@ -710,6 +729,23 @@ let integrationTests = Expect.isFalse exists "There should not have been an existing document" } ] + testList "byFields" [ + testTask "succeeds when documents exist" { + use db = PostgresDb.BuildDb() + do! loadDocs () + + let! exists = Exists.byFields PostgresDb.TableName Any [ Field.EX "Sub"; Field.EX "Boo" ] + 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.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() @@ -782,7 +818,7 @@ let integrationTests = Expect.equal results [] "There should have been no documents returned" } ] - testList "byId" [ + ftestList "byId" [ testTask "succeeds when a document is found" { use db = PostgresDb.BuildDb() do! loadDocs () @@ -799,6 +835,26 @@ let integrationTests = Expect.isNone doc "There should not have been a document returned" } ] + testList "byFields" [ + testTask "succeeds when documents are found" { + use db = PostgresDb.BuildDb() + do! loadDocs () + + let! docs = + Find.byFields + PostgresDb.TableName All [ Field.EQ "Value" "purple"; Field.EX "Sub" ] + Expect.equal (List.length docs) 1 "There should have been one document returned" + } + testTask "succeeds when documents are not found" { + use db = PostgresDb.BuildDb() + do! loadDocs () + + let! docs = + Find.byFields + PostgresDb.TableName All [ Field.EQ "Value" "mauve"; Field.NE "NumValue" 40 ] + Expect.isEmpty docs "There should have been no documents returned" + } + ] testList "byField" [ testTask "succeeds when documents are found" { use db = PostgresDb.BuildDb()