Version 4 #6

Merged
danieljsummers merged 30 commits from version-four into main 2024-08-19 23:30:39 +00:00
2 changed files with 2702 additions and 2381 deletions
Showing only changes of commit 8e33299e22 - Show all commits

View File

@ -14,11 +14,9 @@ using static Runner;
public static class PostgresCSharpTests
{
/// <summary>
/// Tests which do not hit the database
/// Unit tests for the Parameters module of the PostgreSQL library
/// </summary>
private static readonly Test Unit = TestList("Unit",
[
TestList("Parameters",
private static readonly Test ParametersTests = TestList("Parameters",
[
TestList("Id",
[
@ -69,29 +67,25 @@ public static class PostgresCSharpTests
{
var it = Parameters.Id((ulong)6464);
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.int64(6464),
"Unsigned long ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.int64(6464), "Unsigned long ID parameter not constructed correctly");
}),
TestCase("succeeds for decimal ID", () =>
{
var it = Parameters.Id((decimal)4.56);
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@decimal((decimal)4.56),
"Decimal ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@decimal((decimal)4.56), "Decimal ID parameter not constructed correctly");
}),
TestCase("succeeds for single ID", () =>
{
var it = Parameters.Id((float)5.67);
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@double((float)5.67),
"Single ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@double((float)5.67), "Single ID parameter not constructed correctly");
}),
TestCase("succeeds for double ID", () =>
{
var it = Parameters.Id(6.78);
Expect.equal(it.Item1, "@id", "ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@double(6.78),
"Double ID parameter not constructed correctly");
Expect.equal(it.Item2, Sql.@double(6.78), "Double ID parameter not constructed correctly");
}),
TestCase("succeeds for string ID", () =>
{
@ -200,8 +194,12 @@ public static class PostgresCSharpTests
})
])
#pragma warning restore CS0618
]),
TestList("Query",
]);
/// <summary>
/// Unit tests for the Query module of the PostgreSQL library
/// </summary>
private static readonly Test QueryTests = TestList("Query",
[
TestList("WhereByFields",
[
@ -259,8 +257,7 @@ public static class PostgresCSharpTests
[
TestCase("succeeds for numeric ID", () =>
{
Expect.equal(Postgres.Query.WhereById(18), "(data->>'Id')::numeric = @id",
"WHERE clause not correct");
Expect.equal(Postgres.Query.WhereById(18), "(data->>'Id')::numeric = @id", "WHERE clause not correct");
}),
TestCase("succeeds for string ID", () =>
{
@ -298,8 +295,7 @@ public static class PostgresCSharpTests
]),
TestCase("WhereDataContains succeeds", () =>
{
Expect.equal(Postgres.Query.WhereDataContains("@test"), "data @> @test",
"WHERE clause not correct");
Expect.equal(Postgres.Query.WhereDataContains("@test"), "data @> @test", "WHERE clause not correct");
}),
TestCase("WhereJsonPathMatches succeeds", () =>
{
@ -318,15 +314,14 @@ public static class PostgresCSharpTests
}),
TestCase("ById succeeds", () =>
{
Expect.equal(Postgres.Query.ById("test", "14"), "test WHERE data->>'Id' = @id",
"By-ID query not correct");
Expect.equal(Postgres.Query.ById("test", "14"), "test WHERE data->>'Id' = @id", "By-ID query not correct");
}),
TestCase("ByFields succeeds", () =>
{
Expect.equal(Postgres.Query.ByFields("unit", FieldMatch.Any, [Field.GT("That", 14)]),
"unit WHERE (data->>'That')::numeric > @field0", "By-Field query not correct");
}),
TestCase ("ByContains succeeds", () =>
TestCase("ByContains succeeds", () =>
{
Expect.equal(Postgres.Query.ByContains("exam"), "exam WHERE data @> @criteria",
"By-Contains query not correct");
@ -336,7 +331,15 @@ public static class PostgresCSharpTests
Expect.equal(Postgres.Query.ByPathMatch("verify"), "verify WHERE data @? @path::jsonpath",
"By-JSON Path query not correct");
})
])
]);
/// <summary>
/// Tests which do not hit the database
/// </summary>
private static readonly Test Unit = TestList("Unit",
[
ParametersTests,
QueryTests
]);
private static readonly List<JsonDocument> TestDocuments =
@ -357,11 +360,9 @@ public static class PostgresCSharpTests
}
/// <summary>
/// Integration tests for the PostgreSQL library
/// Integration tests for the Configuration module of the PostgreSQL library
/// </summary>
private static readonly Test Integration = TestList("Integration",
[
TestList("Configuration",
private static readonly Test ConfigurationTests = TestList("Configuration",
[
TestCase("UseDataSource disposes existing source", () =>
{
@ -390,8 +391,12 @@ public static class PostgresCSharpTests
Expect.isTrue(ReferenceEquals(source, Postgres.Configuration.DataSource()),
"Data source should have been the same");
})
]),
TestList("Custom",
]);
/// <summary>
/// Integration tests for the Custom module of the PostgreSQL library
/// </summary>
private static readonly Test CustomTests = TestList("Custom",
[
TestList("List",
[
@ -411,8 +416,7 @@ public static class PostgresCSharpTests
var docs = await Custom.List(
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
new[] { Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)")) },
Results.FromData<JsonDocument>);
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))], Results.FromData<JsonDocument>);
Expect.isEmpty(docs, "There should have been no documents returned");
})
]),
@ -424,7 +428,7 @@ public static class PostgresCSharpTests
await LoadDocs();
var doc = await Custom.Single($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
new[] { Tuple.Create("@id", Sql.@string("one")) }, Results.FromData<JsonDocument>);
[Tuple.Create("@id", Sql.@string("one"))], Results.FromData<JsonDocument>);
Expect.isNotNull(doc, "There should have been a document returned");
Expect.equal(doc.Id, "one", "The incorrect document was returned");
}),
@ -434,7 +438,7 @@ public static class PostgresCSharpTests
await LoadDocs();
var doc = await Custom.Single($"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id",
new[] { Tuple.Create("@id", Sql.@string("eighty")) }, Results.FromData<JsonDocument>);
[Tuple.Create("@id", Sql.@string("eighty"))], Results.FromData<JsonDocument>);
Expect.isNull(doc, "There should not have been a document returned");
})
]),
@ -456,7 +460,7 @@ public static class PostgresCSharpTests
await LoadDocs();
await Custom.NonQuery($"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath",
new[] { Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)")) });
[Tuple.Create("@path", Sql.@string("$.NumValue ? (@ > 100)"))]);
var remaining = await Count.All(PostgresDb.TableName);
Expect.equal(remaining, 5, "There should be 5 documents remaining in the table");
@ -469,8 +473,12 @@ public static class PostgresCSharpTests
var nbr = await Custom.Scalar("SELECT 5 AS test_value", Parameters.None, row => row.@int("test_value"));
Expect.equal(nbr, 5, "The query should have returned the number 5");
})
]),
TestList("Definition",
]);
/// <summary>
/// Integration tests for the Definition module of the PostgreSQL library
/// </summary>
private static readonly Test DefinitionTests = TestList("Definition",
[
TestCase("EnsureTable succeeds", async () =>
{
@ -491,6 +499,7 @@ public static class PostgresCSharpTests
Task<bool> TableExists() => Custom.Scalar(
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'ensured') AS it", Parameters.None,
Results.ToExists);
Task<bool> KeyExists() => Custom.Scalar(
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_key') AS it", Parameters.None,
Results.ToExists);
@ -529,8 +538,12 @@ public static class PostgresCSharpTests
"SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_ensured_test') AS it", Parameters.None,
Results.ToExists);
})
]),
TestList("Document",
]);
/// <summary>
/// Integration tests for the Document module of the PostgreSQL library
/// </summary>
private static readonly Test DocumentTests = TestList("Document",
[
TestList("Insert",
[
@ -591,8 +604,12 @@ public static class PostgresCSharpTests
Expect.equal(after.Sub!.Foo, "c", "The updated document is not correct");
})
])
]),
TestList("Count",
]);
/// <summary>
/// Integration tests for the Count module of the PostgreSQL library
/// </summary>
private static readonly Test CountTests = TestList("Count",
[
TestCase("All succeeds", async () =>
{
@ -639,8 +656,12 @@ public static class PostgresCSharpTests
var theCount = await Count.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ > 5)");
Expect.equal(theCount, 3, "There should have been 3 matching documents");
})
]),
TestList("Exists",
]);
/// <summary>
/// Integration tests for the Exists module of the PostgreSQL library
/// </summary>
private static readonly Test ExistsTests = TestList("Exists",
[
TestList("ById",
[
@ -676,8 +697,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var exists = await Exists.ByFields(PostgresDb.TableName, FieldMatch.Any,
[Field.EQ("NumValue", "six")]);
var exists = await Exists.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "six")]);
Expect.isFalse(exists, "There should not have been existing documents");
})
]),
@ -719,8 +739,12 @@ public static class PostgresCSharpTests
Expect.isFalse(exists, "There should not have been any existing documents");
})
])
]),
TestList("Find",
]);
/// <summary>
/// Integration tests for the Find module of the PostgreSQL library
/// </summary>
private static readonly Test FindTests = TestList("Find",
[
TestList("All",
[
@ -733,7 +757,7 @@ public static class PostgresCSharpTests
await Document.Insert(PostgresDb.TableName, new SubDocument { Foo = "five", Bar = "six" });
var results = await Find.All<SubDocument>(PostgresDb.TableName);
Expect.equal(results.Count, 3, "There should have been 3 documents returned");
Expect.hasLength(results, 3, "There should have been 3 documents returned");
}),
TestCase("succeeds when there is no data", async () =>
{
@ -742,6 +766,41 @@ public static class PostgresCSharpTests
Expect.isEmpty(results, "There should have been no documents returned");
})
]),
TestList("AllOrdered",
[
TestCase("succeeds when ordering numerically", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var results =
await Find.AllOrdered<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 LoadDocs();
var results =
await Find.AllOrdered<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 LoadDocs();
var results = await Find.AllOrdered<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("ById",
[
TestCase("succeeds when a document is found", async () =>
@ -771,7 +830,7 @@ public static class PostgresCSharpTests
var docs = await Find.ByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
[Field.EQ("Value", "another")]);
Expect.equal(docs.Count, 1, "There should have been one document returned");
Expect.hasLength(docs, 1, "There should have been one document returned");
}),
TestCase("succeeds when documents are not found", async () =>
{
@ -783,6 +842,31 @@ public static class PostgresCSharpTests
Expect.isEmpty(docs, "There should have been no documents returned");
})
]),
TestList("ByFieldsOrdered",
[
TestCase("succeeds when documents are found", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var docs = await Find.ByFieldsOrdered<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 LoadDocs();
var docs = await Find.ByFieldsOrdered<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("ByContains",
[
TestCase("succeeds when documents are found", async () =>
@ -792,7 +876,7 @@ public static class PostgresCSharpTests
var docs = await Find.ByContains<JsonDocument>(PostgresDb.TableName,
new { Sub = new { Foo = "green" } });
Expect.equal(docs.Count, 2, "There should have been two documents returned");
Expect.hasLength(docs, 2, "There should have been two documents returned");
}),
TestCase("succeeds when documents are not found", async () =>
{
@ -803,6 +887,32 @@ public static class PostgresCSharpTests
Expect.isEmpty(docs, "There should have been no documents returned");
})
]),
TestList("ByContainsOrdered",
[
// Id = two, Sub.Bar = blue; Id = four, Sub.Bar = red
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var docs = await Find.ByContainsOrdered<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 LoadDocs();
var docs = await Find.ByContainsOrdered<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("ByJsonPath",
[
TestCase("succeeds when documents are found", async () =>
@ -811,7 +921,7 @@ public static class PostgresCSharpTests
await LoadDocs();
var docs = await Find.ByJsonPath<JsonDocument>(PostgresDb.TableName, "$.NumValue ? (@ < 15)");
Expect.equal(docs.Count, 3, "There should have been 3 documents returned");
Expect.hasLength(docs, 3, "There should have been 3 documents returned");
}),
TestCase("succeeds when documents are not found", async () =>
{
@ -822,6 +932,32 @@ public static class PostgresCSharpTests
Expect.isEmpty(docs, "There should have been no documents returned");
})
]),
TestList("ByJsonPathOrdered",
[
// 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 LoadDocs();
var docs = await Find.ByJsonPathOrdered<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 LoadDocs();
var docs = await 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(string.Join('|', docs.Select(x => x.Id)), "two|three|one",
"Documents not ordered correctly");
})
]),
TestList("FirstByFields",
[
TestCase("succeeds when a document is found", async () =>
@ -842,7 +978,7 @@ public static class PostgresCSharpTests
var doc = await Find.FirstByFields<JsonDocument>(PostgresDb.TableName, FieldMatch.Any,
[Field.EQ("Value", "purple")]);
Expect.isNotNull(doc, "There should have been a document returned");
Expect.contains(new[] { "five", "four" }, doc.Id, "An incorrect document was returned");
Expect.contains(["five", "four"], doc.Id, "An incorrect document was returned");
}),
TestCase("succeeds when a document is not found", async () =>
{
@ -854,6 +990,29 @@ public static class PostgresCSharpTests
Expect.isNull(doc, "There should not have been a document returned");
})
]),
TestList("FirstByFieldsOrdered",
[
TestCase("succeeds when sorting ascending", async () =>
{
await using var db = PostgresDb.BuildDb();
await LoadDocs();
var doc = await Find.FirstByFieldsOrdered<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 LoadDocs();
var doc = await Find.FirstByFieldsOrdered<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("FirstByContains",
[
TestCase("succeeds when a document is found", async () =>
@ -873,7 +1032,7 @@ public static class PostgresCSharpTests
var doc = await Find.FirstByContains<JsonDocument>(PostgresDb.TableName,
new { Sub = new { Foo = "green" } });
Expect.isNotNull(doc, "There should have been a document returned");
Expect.contains(new[] { "two", "four" }, doc.Id, "An incorrect document was returned");
Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned");
}),
TestCase("succeeds when a document is not found", async () =>
{
@ -904,7 +1063,7 @@ public static class PostgresCSharpTests
var doc = await Find.FirstByJsonPath<JsonDocument>(PostgresDb.TableName,
"$.Sub.Foo ? (@ == \"green\")");
Expect.isNotNull(doc, "There should have been a document returned");
Expect.contains(new[] { "two", "four" }, doc.Id, "An incorrect document was returned");
Expect.contains(["two", "four"], doc.Id, "An incorrect document was returned");
}),
TestCase("succeeds when a document is not found", async () =>
{
@ -915,8 +1074,12 @@ public static class PostgresCSharpTests
Expect.isNull(doc, "There should not have been a document returned");
})
])
]),
TestList("Update",
]);
/// <summary>
/// Integration tests for the Update module of the PostgreSQL library
/// </summary>
private static readonly Test UpdateTests = TestList("Update",
[
TestList("ById",
[
@ -976,8 +1139,12 @@ public static class PostgresCSharpTests
new JsonDocument { Id = "one", Value = "le un", NumValue = 1 });
})
])
]),
TestList("Patch",
]);
/// <summary>
/// Integration tests for the Patch module of the PostgreSQL library
/// </summary>
private static readonly Test PatchTests = TestList("Patch",
[
TestList("ById",
[
@ -1011,8 +1178,7 @@ public static class PostgresCSharpTests
await Patch.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("Value", "purple")],
new { NumValue = 77 });
var after = await Count.ByFields(PostgresDb.TableName, FieldMatch.Any,
[Field.EQ("NumValue", "77")]);
var after = await Count.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "77")]);
Expect.equal(after, 2, "There should have been 2 documents returned");
}),
TestCase("succeeds when no document is updated", async () =>
@ -1071,8 +1237,12 @@ public static class PostgresCSharpTests
await Patch.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)", new { Foo = "green" });
})
])
]),
TestList("RemoveFields",
]);
/// <summary>
/// Integration tests for the RemoveFields module of the PostgreSQL library
/// </summary>
private static readonly Test RemoveFieldsTests = TestList("RemoveFields",
[
TestList("ById",
[
@ -1081,7 +1251,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "Sub", "Value" });
await RemoveFields.ById(PostgresDb.TableName, "two", ["Sub", "Value"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.equal(updated.Value, "", "The string value should have been removed");
@ -1092,7 +1262,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "Sub" });
await RemoveFields.ById(PostgresDb.TableName, "two", ["Sub"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "two");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
@ -1104,14 +1274,14 @@ public static class PostgresCSharpTests
await LoadDocs();
// This not raising an exception is the test
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
await RemoveFields.ById(PostgresDb.TableName, "two", ["AFieldThatIsNotThere"]);
}),
TestCase("succeeds when no document is matched", async () =>
{
await using var db = PostgresDb.BuildDb();
// This not raising an exception is the test
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "Value" });
await RemoveFields.ById(PostgresDb.TableName, "two", ["Value"]);
})
]),
TestList("ByFields",
@ -1122,7 +1292,7 @@ public static class PostgresCSharpTests
await LoadDocs();
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
new[] { "Sub", "Value" });
["Sub", "Value"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.equal(updated.Value, "", "The string value should have been removed");
@ -1134,7 +1304,7 @@ public static class PostgresCSharpTests
await LoadDocs();
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
new[] { "Sub" });
["Sub"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
@ -1147,15 +1317,15 @@ public static class PostgresCSharpTests
// This not raising an exception is the test
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.EQ("NumValue", "17")],
new[] { "Nothing" });
["Nothing"]);
}),
TestCase("succeeds when no document is matched", async () =>
{
await using var db = PostgresDb.BuildDb();
// This not raising an exception is the test
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any,
[Field.NE("Abracadabra", "apple")], new[] { "Value" });
await RemoveFields.ByFields(PostgresDb.TableName, FieldMatch.Any, [Field.NE("Abracadabra", "apple")],
["Value"]);
})
]),
TestList("ByContains",
@ -1165,8 +1335,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 },
new[] { "Sub", "Value" });
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub", "Value"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.equal(updated.Value, "", "The string value should have been removed");
@ -1177,7 +1346,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "Sub" });
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Sub"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
@ -1189,15 +1358,14 @@ public static class PostgresCSharpTests
await LoadDocs();
// This not raising an exception is the test
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "Nothing" });
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, ["Nothing"]);
}),
TestCase("succeeds when no document is matched", async () =>
{
await using var db = PostgresDb.BuildDb();
// This not raising an exception is the test
await RemoveFields.ByContains(PostgresDb.TableName, new { Abracadabra = "apple" },
new[] { "Value" });
await RemoveFields.ByContains(PostgresDb.TableName, new { Abracadabra = "apple" }, ["Value"]);
})
]),
TestList("ByJsonPath",
@ -1207,8 +1375,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)",
new[] { "Sub", "Value" });
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub", "Value"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.equal(updated.Value, "", "The string value should have been removed");
@ -1219,7 +1386,7 @@ public static class PostgresCSharpTests
await using var db = PostgresDb.BuildDb();
await LoadDocs();
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "Sub" });
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Sub"]);
var updated = await Find.ById<string, JsonDocument>(PostgresDb.TableName, "four");
Expect.isNotNull(updated, "The updated document should have been retrieved");
Expect.notEqual(updated.Value, "", "The string value should not have been removed");
@ -1231,19 +1398,22 @@ public static class PostgresCSharpTests
await LoadDocs();
// This not raising an exception is the test
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "Nothing" });
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", ["Nothing"]);
}),
TestCase("succeeds when no document is matched", async () =>
{
await using var db = PostgresDb.BuildDb();
// This not raising an exception is the test
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")",
new[] { "Value" });
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")", ["Value"]);
})
])
]),
TestList("Delete",
]);
/// <summary>
/// Integration tests for the Delete module of the PostgreSQL library
/// </summary>
private static readonly Test DeleteTests = TestList("Delete",
[
TestList("ById",
[
@ -1329,12 +1499,32 @@ public static class PostgresCSharpTests
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
})
])
])
]);
/// <summary>
/// All Postgres C# tests
/// </summary>
[Tests]
public static readonly Test All = TestList("Postgres.C#", [Unit, TestSequenced(Integration)]);
public static readonly Test All = TestList("Postgres.C#",
[
TestList("Unit",
[
ParametersTests,
QueryTests
]),
TestSequenced(TestList("Integration",
[
ConfigurationTests,
CustomTests,
DefinitionTests,
DocumentTests,
CountTests,
ExistsTests,
FindTests,
UpdateTests,
PatchTests,
RemoveFieldsTests,
DeleteTests
]))
]);
}

View File

@ -7,15 +7,14 @@ open BitBadger.Documents.Tests
#nowarn "0044"
/// Tests which do not hit the database
let unitTests =
testList "Unit" [
testList "Parameters" [
(** UNIT TESTS **)
/// Unit tests for the Parameters module of the PostgreSQL library
let parametersTests = testList "Parameters" [
testList "idParam" [
// NOTE: these tests also exercise all branches of the internal parameterFor function
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" {
Expect.equal
@ -25,9 +24,7 @@ let unitTests =
}
test "succeeds for short ID" {
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" {
Expect.equal
@ -39,14 +36,11 @@ let unitTests =
Expect.equal (idParam 88) ("@id", Sql.int 88) "Int ID parameter not constructed correctly"
}
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" {
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" {
Expect.equal
@ -113,8 +107,7 @@ let unitTests =
Expect.isEmpty paramList "There should not have been any parameters added"
}
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"
let name, value = Seq.head paramList
Expect.equal name "@testmin" "Minimum field name not correct"
@ -151,8 +144,10 @@ let unitTests =
test "noParams succeeds" {
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" [
test "succeeds for a single field when a logical operator is passed" {
Expect.equal
@ -206,9 +201,7 @@ let unitTests =
}
test "succeeds for non-numeric non-string ID" {
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" [
@ -266,27 +259,29 @@ let unitTests =
Expect.equal
(Query.byPathMatch "verify") "verify WHERE data @? @path::jsonpath" "By-JSON Path query not correct"
}
]
]
]
(** INTEGRATION TESTS **)
open ThrowawayDb.Postgres
open Types
let isTrue<'T> (_ : 'T) = true
let integrationTests =
let documents = [
/// Documents to use for integration tests
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 {
]
/// Load the test documents into the database
let loadDocs () = backgroundTask {
for doc in documents do do! insert PostgresDb.TableName doc
}
testList "Integration" [
testList "Configuration" [
}
/// Integration tests for the Configuration module of the PostgreSQL library
let configurationTests = testList "Configuration" [
test "useDataSource disposes existing source" {
use db1 = ThrowawayDatabase.Create PostgresDb.ConnStr.Value
let source = PostgresDb.MkDataSource db1.ConnectionString
@ -301,26 +296,29 @@ let integrationTests =
let source = PostgresDb.MkDataSource db.ConnectionString
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" [
testTask "succeeds when data is found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! docs = Custom.list (Query.selectFromTable PostgresDb.TableName) [] fromData<JsonDocument>
Expect.hasCountOf docs 5u isTrue "There should have been 5 documents returned"
Expect.hasLength docs 5 "There should have been 5 documents returned"
}
testTask "succeeds when data is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! docs =
Custom.list $"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ] fromData<JsonDocument>
Custom.list
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
fromData<JsonDocument>
Expect.isEmpty docs "There should have been no documents returned"
}
]
@ -330,8 +328,10 @@ let integrationTests =
do! loadDocs ()
let! doc =
Custom.single $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "one"] fromData<JsonDocument>
Custom.single
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "one"]
fromData<JsonDocument>
Expect.isSome doc "There should have been a document returned"
Expect.equal doc.Value.Id "one" "The incorrect document was returned"
}
@ -340,8 +340,10 @@ let integrationTests =
do! loadDocs ()
let! doc =
Custom.single $"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "eighty" ] fromData<JsonDocument>
Custom.single
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "eighty" ]
fromData<JsonDocument>
Expect.isNone doc "There should not have been a document returned"
}
]
@ -359,7 +361,8 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! Custom.nonQuery $"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
do! Custom.nonQuery
$"DELETE FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
let! remaining = Count.all PostgresDb.TableName
@ -371,8 +374,10 @@ let integrationTests =
let! nbr = Custom.scalar "SELECT 5 AS test_value" [] (fun row -> row.int "test_value")
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" {
use db = PostgresDb.BuildDb()
let tableExists () =
@ -396,9 +401,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
let indexExists () =
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 ()
Expect.isFalse exists "The index should not exist already"
@ -411,8 +414,7 @@ let integrationTests =
testTask "ensureFieldIndex succeeds" {
use db = PostgresDb.BuildDb()
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 ()
Expect.isFalse exists "The index should not exist already"
@ -422,7 +424,10 @@ let integrationTests =
let! exists' = indexExists ()
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" [
testTask "succeeds" {
use db = PostgresDb.BuildDb()
@ -472,7 +477,10 @@ let integrationTests =
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" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
@ -485,8 +493,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
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"
}
testTask "succeeds when items are not found" {
@ -511,8 +518,10 @@ let integrationTests =
let! theCount = Count.byJsonPath PostgresDb.TableName "$.NumValue ? (@ > 5)"
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" [
testTask "succeeds when a document exists" {
use db = PostgresDb.BuildDb()
@ -541,8 +550,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
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"
}
]
@ -578,8 +586,10 @@ let integrationTests =
Expect.isFalse exists "There should not have been any existing documents"
}
]
]
testList "Find" [
]
/// Integration tests for the Find module of the PostgreSQL library
let findTests = testList "Find" [
testList "all" [
testTask "succeeds when there is data" {
use db = PostgresDb.BuildDb()
@ -589,12 +599,10 @@ let integrationTests =
do! insert PostgresDb.TableName { Foo = "five"; Bar = "six" }
let! results = Find.all<SubDocument> PostgresDb.TableName
let expected = [
{ Foo = "one"; Bar = "two" }
{ Foo = "three"; Bar = "four" }
{ Foo = "five"; Bar = "six" }
]
Expect.equal results expected "There should have been 3 documents returned"
Expect.equal
results
[ { Foo = "one"; Bar = "two" }; { Foo = "three"; Bar = "four" }; { Foo = "five"; Bar = "six" } ]
"There should have been 3 documents returned"
}
testTask "succeeds when there is no data" {
use db = PostgresDb.BuildDb()
@ -602,7 +610,7 @@ let integrationTests =
Expect.equal results [] "There should have been no documents returned"
}
]
ftestList "allOrdered" [
testList "allOrdered" [
testTask "succeeds when ordering numerically" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
@ -614,6 +622,17 @@ let integrationTests =
"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 ()
@ -649,8 +668,7 @@ let integrationTests =
do! loadDocs ()
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"
}
testTask "succeeds when documents are not found" {
@ -663,6 +681,30 @@ let integrationTests =
Expect.isEmpty docs "There should have been no documents returned"
}
]
testList "byFieldsOrdered" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let! docs =
Find.byFieldsOrdered<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()
do! loadDocs ()
let! docs =
Find.byFieldsOrdered<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 "byContains" [
testTask "succeeds when documents are found" {
use db = PostgresDb.BuildDb()
@ -679,6 +721,31 @@ let integrationTests =
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" [
testTask "succeeds when documents are found" {
use db = PostgresDb.BuildDb()
@ -695,6 +762,31 @@ let integrationTests =
Expect.isEmpty docs "There should have been no documents returned"
}
]
testList "byJsonPathOrdered" [
// 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" {
use db = PostgresDb.BuildDb()
@ -720,6 +812,28 @@ let integrationTests =
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" [
testTask "succeeds when a document is found" {
use db = PostgresDb.BuildDb()
@ -770,8 +884,10 @@ let integrationTests =
Expect.isNone doc "There should not have been a document returned"
}
]
]
testList "Update" [
]
/// Integration tests for the Update module of the PostegreSQL library
let updateTests = testList "Update" [
testList "byId" [
testTask "succeeds when a document is updated" {
use db = PostgresDb.BuildDb()
@ -791,9 +907,7 @@ let integrationTests =
// This not raising an exception is the test
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" [
@ -801,8 +915,7 @@ let integrationTests =
use db = PostgresDb.BuildDb()
do! loadDocs ()
do! Update.byFunc PostgresDb.TableName (_.Id)
{ Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
do! Update.byFunc PostgresDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
Expect.isSome after "There should have been a document returned post-update"
Expect.equal
@ -817,12 +930,13 @@ let integrationTests =
Expect.equal before 0 "There should have been no documents returned"
// This not raising an exception is the test
do! Update.byFunc
PostgresDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
do! Update.byFunc PostgresDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
}
]
]
testList "Patch" [
]
/// Integration tests for the Patch module of the PostgreSQL library
let patchTests = testList "Patch" [
testList "byId" [
testTask "succeeds when a document is updated" {
use db = PostgresDb.BuildDb()
@ -900,8 +1014,10 @@ let integrationTests =
do! Patch.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 0)" {| Foo = "green" |}
}
]
]
testList "RemoveFields" [
]
/// Integration tests for the RemoveFields module of the PostgreSQL library
let removeFieldsTests = testList "RemoveFields" [
testList "byId" [
testTask "succeeds when multiple fields are removed" {
use db = PostgresDb.BuildDb()
@ -1042,8 +1158,10 @@ let integrationTests =
do! RemoveFields.byJsonPath PostgresDb.TableName "$.Abracadabra ? (@ == \"apple\")" [ "Value" ]
}
]
]
testList "Delete" [
]
/// Integration tests for the Delete module of the PostgreSQL library
let deleteTests = testList "Delete" [
testList "byId" [
testTask "succeeds when a document is deleted" {
use db = PostgresDb.BuildDb()
@ -1116,9 +1234,22 @@ let integrationTests =
Expect.equal remaining 5 "There should have been 5 documents remaining"
}
]
]
]
|> testSequenced
]
let all = testList "Postgres" [ unitTests; integrationTests ]
/// All tests for the PostgreSQL library
let all = testList "Postgres" [
testList "Unit" [ parametersTests; queryTests ]
testSequenced <| testList "Integration" [
configurationTests
customTests
definitionTests
documentTests
countTests
existsTests
findTests
updateTests
patchTests
removeFieldsTests
deleteTests
]
]