V3 rc2 (#2)
- Implements `Field` type for by-field actions (**BREAKING**) - Adds `RemoveFields*` functions/methods for removing fields from documents
This commit was merged in pull request #2.
This commit is contained in:
@@ -105,6 +105,63 @@ public static class CommonCSharpTests
|
||||
Expect.equal(Op.NEX.ToString(), "IS NULL", "The \"not exists\" operator was not correct");
|
||||
})
|
||||
}),
|
||||
TestList("Field", new[]
|
||||
{
|
||||
TestCase("EQ succeeds", () =>
|
||||
{
|
||||
var field = Field.EQ("Test", 14);
|
||||
Expect.equal(field.Name, "Test", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.EQ, "Operator incorrect");
|
||||
Expect.equal(field.Value, 14, "Value incorrect");
|
||||
}),
|
||||
TestCase("GT succeeds", () =>
|
||||
{
|
||||
var field = Field.GT("Great", "night");
|
||||
Expect.equal(field.Name, "Great", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.GT, "Operator incorrect");
|
||||
Expect.equal(field.Value, "night", "Value incorrect");
|
||||
}),
|
||||
TestCase("GE succeeds", () =>
|
||||
{
|
||||
var field = Field.GE("Nice", 88L);
|
||||
Expect.equal(field.Name, "Nice", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.GE, "Operator incorrect");
|
||||
Expect.equal(field.Value, 88L, "Value incorrect");
|
||||
}),
|
||||
TestCase("LT succeeds", () =>
|
||||
{
|
||||
var field = Field.LT("Lesser", "seven");
|
||||
Expect.equal(field.Name, "Lesser", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.LT, "Operator incorrect");
|
||||
Expect.equal(field.Value, "seven", "Value incorrect");
|
||||
}),
|
||||
TestCase("LE succeeds", () =>
|
||||
{
|
||||
var field = Field.LE("Nobody", "KNOWS");
|
||||
Expect.equal(field.Name, "Nobody", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.LE, "Operator incorrect");
|
||||
Expect.equal(field.Value, "KNOWS", "Value incorrect");
|
||||
}),
|
||||
TestCase("NE succeeds", () =>
|
||||
{
|
||||
var field = Field.NE("Park", "here");
|
||||
Expect.equal(field.Name, "Park", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.NE, "Operator incorrect");
|
||||
Expect.equal(field.Value, "here", "Value incorrect");
|
||||
}),
|
||||
TestCase("EX succeeds", () =>
|
||||
{
|
||||
var field = Field.EX("Groovy");
|
||||
Expect.equal(field.Name, "Groovy", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.EX, "Operator incorrect");
|
||||
}),
|
||||
TestCase("NEX succeeds", () =>
|
||||
{
|
||||
var field = Field.NEX("Rad");
|
||||
Expect.equal(field.Name, "Rad", "Field name incorrect");
|
||||
Expect.equal(field.Op, Op.NEX, "Operator incorrect");
|
||||
})
|
||||
}),
|
||||
TestList("Query", new[]
|
||||
{
|
||||
TestCase("SelectFromTable succeeds", () =>
|
||||
@@ -120,12 +177,12 @@ public static class CommonCSharpTests
|
||||
{
|
||||
TestCase("succeeds when a logical operator is passed", () =>
|
||||
{
|
||||
Expect.equal(Query.WhereByField("theField", Op.GT, "@test"), "data ->> 'theField' > @test",
|
||||
Expect.equal(Query.WhereByField(Field.GT("theField", 0), "@test"), "data ->> 'theField' > @test",
|
||||
"WHERE clause not correct");
|
||||
}),
|
||||
TestCase("succeeds when an existence operator is passed", () =>
|
||||
{
|
||||
Expect.equal(Query.WhereByField("thatField", Op.NEX, ""), "data ->> 'thatField' IS NULL",
|
||||
Expect.equal(Query.WhereByField(Field.NEX("thatField"), ""), "data ->> 'thatField' IS NULL",
|
||||
"WHERE clause not correct");
|
||||
})
|
||||
}),
|
||||
@@ -185,7 +242,7 @@ public static class CommonCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Query.Count.ByField("tbl", "thatField", Op.EQ),
|
||||
Expect.equal(Query.Count.ByField("tbl", Field.EQ("thatField", 0)),
|
||||
"SELECT COUNT(*) AS it FROM tbl WHERE data ->> 'thatField' = @field",
|
||||
"JSON field text comparison count query not correct");
|
||||
})
|
||||
@@ -200,7 +257,7 @@ public static class CommonCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Query.Exists.ByField("tbl", "Test", Op.LT),
|
||||
Expect.equal(Query.Exists.ByField("tbl", Field.LT("Test", 0)),
|
||||
"SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Test' < @field) AS it",
|
||||
"JSON field text comparison exists query not correct");
|
||||
})
|
||||
@@ -214,7 +271,7 @@ public static class CommonCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Query.Find.ByField("tbl", "Golf", Op.GE),
|
||||
Expect.equal(Query.Find.ByField("tbl", Field.GE("Golf", 0)),
|
||||
"SELECT data FROM tbl WHERE data ->> 'Golf' >= @field",
|
||||
"SELECT by JSON comparison query not correct");
|
||||
})
|
||||
@@ -228,7 +285,7 @@ public static class CommonCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Query.Delete.ByField("tbl", "gone", Op.NEX),
|
||||
Expect.equal(Query.Delete.ByField("tbl", Field.NEX("gone")),
|
||||
"DELETE FROM tbl WHERE data ->> 'gone' IS NULL",
|
||||
"DELETE by JSON comparison query not correct");
|
||||
})
|
||||
|
||||
@@ -246,7 +246,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await conn.CountByField(PostgresDb.TableName, "Value", Op.EQ, "purple");
|
||||
var theCount = await conn.CountByField(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
||||
}),
|
||||
TestCase("CountByContains succeeds", async () =>
|
||||
@@ -296,7 +296,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(PostgresDb.TableName, "Sub", Op.EX, "");
|
||||
var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EX("Sub"));
|
||||
Expect.isTrue(exists, "There should have been existing documents");
|
||||
}),
|
||||
TestCase("succeeds when documents do not exist", async () =>
|
||||
@@ -305,7 +305,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(PostgresDb.TableName, "NumValue", Op.EQ, "six");
|
||||
var exists = await conn.ExistsByField(PostgresDb.TableName, Field.EQ("NumValue", "six"));
|
||||
Expect.isFalse(exists, "There should not have been existing documents");
|
||||
})
|
||||
}),
|
||||
@@ -403,7 +403,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "another");
|
||||
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.equal(docs.Count, 1, "There should have been one document returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
@@ -412,7 +412,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "mauve");
|
||||
var docs = await conn.FindByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "mauve"));
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
@@ -467,7 +467,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "another");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc.Id, "two", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -477,7 +477,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "purple");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, 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");
|
||||
}),
|
||||
@@ -487,7 +487,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "absent");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "absent"));
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
@@ -650,8 +650,8 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.PatchByField(PostgresDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
|
||||
var after = await conn.CountByField(PostgresDb.TableName, "NumValue", Op.EQ, "77");
|
||||
await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||
var after = await conn.CountByField(PostgresDb.TableName, Field.EQ("NumValue", "77"));
|
||||
Expect.equal(after, 2, "There should have been 2 documents returned");
|
||||
}),
|
||||
TestCase("succeeds when no document is updated", async () =>
|
||||
@@ -662,7 +662,7 @@ public class PostgresCSharpExtensionTests
|
||||
Expect.equal(before, 0, "There should have been no documents returned");
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.PatchByField(PostgresDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
|
||||
await conn.PatchByField(PostgresDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("PatchByContains", new[]
|
||||
@@ -711,6 +711,188 @@ public class PostgresCSharpExtensionTests
|
||||
await conn.PatchByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ < 0)", new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsById", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(PostgresDb.TableName, "two", new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsByField", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"),
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "Nothing" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(PostgresDb.TableName, Field.NE("Abracadabra", "apple"),
|
||||
new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsByContains", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 },
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "Nothing" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByContains(PostgresDb.TableName, new { Abracadabra = "apple" },
|
||||
new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsByJsonPath", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)",
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "Nothing" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await using var conn = MkConn(db);
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByJsonPath(PostgresDb.TableName, "$.Abracadabra ? (@ == \"apple\")",
|
||||
new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("DeleteById", new[]
|
||||
{
|
||||
TestCase("succeeds when a document is deleted", async () =>
|
||||
@@ -742,7 +924,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(PostgresDb.TableName, "Value", Op.NE, "purple");
|
||||
await conn.DeleteByField(PostgresDb.TableName, Field.NE("Value", "purple"));
|
||||
var remaining = await conn.CountAll(PostgresDb.TableName);
|
||||
Expect.equal(remaining, 2, "There should have been 2 documents remaining");
|
||||
}),
|
||||
@@ -752,7 +934,7 @@ public class PostgresCSharpExtensionTests
|
||||
await using var conn = MkConn(db);
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(PostgresDb.TableName, "Value", Op.EQ, "crimson");
|
||||
await conn.DeleteByField(PostgresDb.TableName, Field.EQ("Value", "crimson"));
|
||||
var remaining = await conn.CountAll(PostgresDb.TableName);
|
||||
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
||||
})
|
||||
|
||||
@@ -11,7 +11,7 @@ using static Runner;
|
||||
/// <summary>
|
||||
/// C# tests for the PostgreSQL implementation of <tt>BitBadger.Documents</tt>
|
||||
/// </summary>
|
||||
public class PostgresCSharpTests
|
||||
public static class PostgresCSharpTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests which do not hit the database
|
||||
@@ -32,11 +32,49 @@ public class PostgresCSharpTests
|
||||
Expect.equal(it.Item1, "@test", "JSON parameter not constructed correctly");
|
||||
Expect.equal(it.Item2, Sql.jsonb("{\"Something\":\"good\"}"), "JSON parameter value incorrect");
|
||||
}),
|
||||
TestCase("Field succeeds", () =>
|
||||
TestList("AddField", new []
|
||||
{
|
||||
var it = Parameters.Field(242);
|
||||
Expect.equal(it.Item1, "@field", "Field parameter not constructed correctly");
|
||||
Expect.isTrue(it.Item2.IsParameter, "Field parameter value incorrect");
|
||||
TestCase("succeeds when a parameter is added", () =>
|
||||
{
|
||||
var it = Parameters
|
||||
.AddField("@field", Field.EQ("it", "242"), Enumerable.Empty<Tuple<string, SqlValue>>())
|
||||
.ToList();
|
||||
Expect.hasLength(it, 1, "There should have been a parameter added");
|
||||
Expect.equal(it[0].Item1, "@field", "Field parameter not constructed correctly");
|
||||
Expect.isTrue(it[0].Item2.IsParameter, "Field parameter value incorrect");
|
||||
}),
|
||||
TestCase("succeeds when a parameter is not added", () =>
|
||||
{
|
||||
var it = Parameters.AddField("@it", Field.EX("It"), Enumerable.Empty<Tuple<string, SqlValue>>());
|
||||
Expect.isEmpty(it, "There should not have been any parameters added");
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFields", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ById("tbl"),
|
||||
"UPDATE tbl SET data = data - @name WHERE data ->> 'Id' = @id",
|
||||
"Remove field by ID query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByField("tbl", Field.LT("Fly", 0)),
|
||||
"UPDATE tbl SET data = data - @name WHERE data ->> 'Fly' < @field",
|
||||
"Remove field by field query not correct");
|
||||
}),
|
||||
TestCase("ByContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByContains("tbl"),
|
||||
"UPDATE tbl SET data = data - @name WHERE data @> @criteria",
|
||||
"Remove field by contains query not correct");
|
||||
}),
|
||||
TestCase("ByJsonPath succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByJsonPath("tbl"),
|
||||
"UPDATE tbl SET data = data - @name WHERE data @? @path::jsonpath",
|
||||
"Remove field by JSON path query not correct");
|
||||
})
|
||||
}),
|
||||
TestCase("None succeeds", () =>
|
||||
{
|
||||
@@ -134,7 +172,7 @@ public class PostgresCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Patch.ByField(PostgresDb.TableName, "Snail", Op.LT),
|
||||
Expect.equal(Postgres.Query.Patch.ByField(PostgresDb.TableName, Field.LT("Snail", 0)),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Snail' < @field",
|
||||
"UPDATE partial by ID statement not correct");
|
||||
}),
|
||||
@@ -431,7 +469,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await Count.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple");
|
||||
var theCount = await Count.ByField(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
||||
}),
|
||||
TestCase("ByContains succeeds", async () =>
|
||||
@@ -479,7 +517,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await Exists.ByField(PostgresDb.TableName, "Sub", Op.NEX, "");
|
||||
var exists = await Exists.ByField(PostgresDb.TableName, Field.NEX("Sub"));
|
||||
Expect.isTrue(exists, "There should have been existing documents");
|
||||
}),
|
||||
TestCase("succeeds when documents do not exist", async () =>
|
||||
@@ -487,7 +525,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await Exists.ByField(PostgresDb.TableName, "NumValue", Op.EQ, "six");
|
||||
var exists = await Exists.ByField(PostgresDb.TableName, Field.EQ("NumValue", "six"));
|
||||
Expect.isFalse(exists, "There should not have been existing documents");
|
||||
})
|
||||
}),
|
||||
@@ -578,7 +616,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Find.ByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "another");
|
||||
var docs = await Find.ByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.equal(docs.Count, 1, "There should have been one document returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
@@ -586,7 +624,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Find.ByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "mauve");
|
||||
var docs = await Find.ByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "mauve"));
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
@@ -636,7 +674,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "another");
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc.Id, "two", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -645,7 +683,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "purple");
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, 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");
|
||||
}),
|
||||
@@ -654,7 +692,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, "Value", Op.EQ, "absent");
|
||||
var doc = await Find.FirstByField<JsonDocument>(PostgresDb.TableName, Field.EQ("Value", "absent"));
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
@@ -813,8 +851,8 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Patch.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
|
||||
var after = await Count.ByField(PostgresDb.TableName, "NumValue", Op.EQ, "77");
|
||||
await Patch.ByField(PostgresDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||
var after = await Count.ByField(PostgresDb.TableName, Field.EQ("NumValue", "77"));
|
||||
Expect.equal(after, 2, "There should have been 2 documents returned");
|
||||
}),
|
||||
TestCase("succeeds when no document is updated", async () =>
|
||||
@@ -825,7 +863,7 @@ public class PostgresCSharpTests
|
||||
Expect.equal(before, 0, "There should have been no documents returned");
|
||||
|
||||
// This not raising an exception is the test
|
||||
await Patch.ByField(PostgresDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
|
||||
await Patch.ByField(PostgresDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("ByContains", new[]
|
||||
@@ -873,6 +911,175 @@ public class PostgresCSharpTests
|
||||
})
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFields", new[]
|
||||
{
|
||||
TestList("ById", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ById(PostgresDb.TableName, "two", new[] { "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" });
|
||||
})
|
||||
}),
|
||||
TestList("ByField", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByField(PostgresDb.TableName, Field.EQ("NumValue", "17"),
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ByField(PostgresDb.TableName, Field.EQ("NumValue", "17"), new[] { "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.ByField(PostgresDb.TableName, Field.NE("Abracadabra", "apple"),
|
||||
new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("ByContains", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 },
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ByContains(PostgresDb.TableName, new { NumValue = 17 }, new[] { "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" });
|
||||
})
|
||||
}),
|
||||
TestList("ByJsonPath", new[]
|
||||
{
|
||||
TestCase("succeeds when multiple fields are removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)",
|
||||
new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a single field is removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "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");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ByJsonPath(PostgresDb.TableName, "$.NumValue ? (@ == 17)", new[] { "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" });
|
||||
})
|
||||
})
|
||||
}),
|
||||
TestList("Delete", new[]
|
||||
{
|
||||
TestList("ById", new[]
|
||||
@@ -903,7 +1110,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Delete.ByField(PostgresDb.TableName, "Value", Op.EQ, "purple");
|
||||
await Delete.ByField(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||
var remaining = await Count.All(PostgresDb.TableName);
|
||||
Expect.equal(remaining, 3, "There should have been 3 documents remaining");
|
||||
}),
|
||||
@@ -912,7 +1119,7 @@ public class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Delete.ByField(PostgresDb.TableName, "Value", Op.EQ, "crimson");
|
||||
await Delete.ByField(PostgresDb.TableName, Field.EQ("Value", "crimson"));
|
||||
var remaining = await Count.All(PostgresDb.TableName);
|
||||
Expect.equal(remaining, 5, "There should have been 5 documents remaining");
|
||||
})
|
||||
|
||||
@@ -219,7 +219,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await conn.CountByField(SqliteDb.TableName, "Value", Op.EQ, "purple");
|
||||
var theCount = await conn.CountByField(SqliteDb.TableName, Field.EQ("Value", "purple"));
|
||||
Expect.equal(theCount, 2L, "There should have been 2 matching documents");
|
||||
}),
|
||||
TestList("ExistsById", new[]
|
||||
@@ -251,7 +251,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, "NumValue", Op.GE, 10);
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.GE("NumValue", 10));
|
||||
Expect.isTrue(exists, "There should have been existing documents");
|
||||
}),
|
||||
TestCase("succeeds when no matching documents exist", async () =>
|
||||
@@ -260,7 +260,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, "Nothing", Op.EQ, "none");
|
||||
var exists = await conn.ExistsByField(SqliteDb.TableName, Field.EQ("Nothing", "none"));
|
||||
Expect.isFalse(exists, "There should not have been any existing documents");
|
||||
})
|
||||
}),
|
||||
@@ -316,7 +316,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, "NumValue", Op.GT, 15);
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.GT("NumValue", 15));
|
||||
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
@@ -325,7 +325,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "mauve");
|
||||
var docs = await conn.FindByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "mauve"));
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
@@ -337,7 +337,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "another");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -347,7 +347,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, "Sub.Foo", Op.EQ, "green");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("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");
|
||||
}),
|
||||
@@ -357,7 +357,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "absent");
|
||||
var doc = await conn.FindFirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "absent"));
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
}),
|
||||
@@ -452,8 +452,8 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.PatchByField(SqliteDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
|
||||
var after = await conn.CountByField(SqliteDb.TableName, "NumValue", Op.EQ, 77);
|
||||
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||
var after = await conn.CountByField(SqliteDb.TableName, Field.EQ("NumValue", 77));
|
||||
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
||||
}),
|
||||
TestCase("succeeds when no document is updated", async () =>
|
||||
@@ -464,7 +464,70 @@ public static class SqliteCSharpExtensionTests
|
||||
Expect.isEmpty(before, "There should have been no documents returned");
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.PatchByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
|
||||
await conn.PatchByField(SqliteDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsById", new[]
|
||||
{
|
||||
TestCase("succeeds when fields are removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Sub", "Value" });
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsById(SqliteDb.TableName, "two", new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFieldsByField", new[]
|
||||
{
|
||||
TestCase("succeeds when a field is removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Sub" });
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Nothing" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await conn.RemoveFieldsByField(SqliteDb.TableName, Field.NE("Abracadabra", "apple"), new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("DeleteById", new[]
|
||||
@@ -498,7 +561,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(SqliteDb.TableName, "Value", Op.NE, "purple");
|
||||
await conn.DeleteByField(SqliteDb.TableName, Field.NE("Value", "purple"));
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
||||
}),
|
||||
@@ -508,7 +571,7 @@ public static class SqliteCSharpExtensionTests
|
||||
await using var conn = Sqlite.Configuration.DbConn();
|
||||
await LoadDocs();
|
||||
|
||||
await conn.DeleteByField(SqliteDb.TableName, "Value", Op.EQ, "crimson");
|
||||
await conn.DeleteByField(SqliteDb.TableName, Field.EQ("Value", "crimson"));
|
||||
var remaining = await conn.CountAll(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Expecto.CSharp;
|
||||
using System.Text.Json;
|
||||
using Expecto.CSharp;
|
||||
using Expecto;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.FSharp.Core;
|
||||
@@ -35,11 +36,27 @@ public static class SqliteCSharpTests
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Sqlite.Query.Patch.ByField("tbl", "Part", Op.NE),
|
||||
Expect.equal(Sqlite.Query.Patch.ByField("tbl", Field.NE("Part", 0)),
|
||||
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field",
|
||||
"UPDATE partial by JSON comparison query not correct");
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFields", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Sqlite.Query.RemoveFields.ById("tbl", new[] { new SqliteParameter("@name", "one") }),
|
||||
"UPDATE tbl SET data = json_remove(data, @name) WHERE data ->> 'Id' = @id",
|
||||
"Remove field by ID query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Sqlite.Query.RemoveFields.ByField("tbl", Field.LT("Fly", 0),
|
||||
new[] { new SqliteParameter("@name0", "one"), new SqliteParameter("@name1", "two") }),
|
||||
"UPDATE tbl SET data = json_remove(data, @name0, @name1) WHERE data ->> 'Fly' < @field",
|
||||
"Remove field by field query not correct");
|
||||
})
|
||||
})
|
||||
}),
|
||||
TestList("Parameters", new[]
|
||||
{
|
||||
@@ -55,12 +72,20 @@ public static class SqliteCSharpTests
|
||||
Expect.equal(theParam.ParameterName, "@test", "The parameter name is incorrect");
|
||||
Expect.equal(theParam.Value, "{\"Nice\":\"job\"}", "The parameter value is incorrect");
|
||||
}),
|
||||
TestCase("Field succeeds", () =>
|
||||
TestCase("AddField succeeds when adding a parameter", () =>
|
||||
{
|
||||
var theParam = Parameters.Field(99);
|
||||
var paramList = Parameters.AddField("@field", Field.EQ("it", 99), Enumerable.Empty<SqliteParameter>())
|
||||
.ToList();
|
||||
Expect.hasLength(paramList, 1, "There should have been a parameter added");
|
||||
var theParam = paramList[0];
|
||||
Expect.equal(theParam.ParameterName, "@field", "The parameter name is incorrect");
|
||||
Expect.equal(theParam.Value, 99, "The parameter value is incorrect");
|
||||
}),
|
||||
TestCase("AddField succeeds when not adding a parameter", () =>
|
||||
{
|
||||
var paramSeq = Parameters.AddField("@it", Field.EX("Coffee"), Enumerable.Empty<SqliteParameter>());
|
||||
Expect.isEmpty(paramSeq, "There should not have been any parameters added");
|
||||
}),
|
||||
TestCase("None succeeds", () =>
|
||||
{
|
||||
Expect.isEmpty(Parameters.None, "The parameter list should have been empty");
|
||||
@@ -296,7 +321,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await Count.ByField(SqliteDb.TableName, "Value", Op.EQ, "purple");
|
||||
var theCount = await Count.ByField(SqliteDb.TableName, Field.EQ("Value", "purple"));
|
||||
Expect.equal(theCount, 2L, "There should have been 2 matching documents");
|
||||
})
|
||||
}),
|
||||
@@ -328,7 +353,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await Exists.ByField(SqliteDb.TableName, "NumValue", Op.GE, 10);
|
||||
var exists = await Exists.ByField(SqliteDb.TableName, Field.GE("NumValue", 10));
|
||||
Expect.isTrue(exists, "There should have been existing documents");
|
||||
}),
|
||||
TestCase("succeeds when no matching documents exist", async () =>
|
||||
@@ -336,7 +361,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var exists = await Exists.ByField(SqliteDb.TableName, "Nothing", Op.EQ, "none");
|
||||
var exists = await Exists.ByField(SqliteDb.TableName, Field.EQ("Nothing", "none"));
|
||||
Expect.isFalse(exists, "There should not have been any existing documents");
|
||||
})
|
||||
})
|
||||
@@ -390,7 +415,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, "NumValue", Op.GT, 15);
|
||||
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, Field.GT("NumValue", 15));
|
||||
Expect.equal(docs.Count, 2, "There should have been two documents returned");
|
||||
}),
|
||||
TestCase("succeeds when documents are not found", async () =>
|
||||
@@ -398,7 +423,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "mauve");
|
||||
var docs = await Find.ByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "mauve"));
|
||||
Expect.isEmpty(docs, "There should have been no documents returned");
|
||||
})
|
||||
}),
|
||||
@@ -409,7 +434,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "another");
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "another"));
|
||||
Expect.isNotNull(doc, "There should have been a document returned");
|
||||
Expect.equal(doc!.Id, "two", "The incorrect document was returned");
|
||||
}),
|
||||
@@ -418,7 +443,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, "Sub.Foo", Op.EQ, "green");
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("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");
|
||||
}),
|
||||
@@ -427,7 +452,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, "Value", Op.EQ, "absent");
|
||||
var doc = await Find.FirstByField<JsonDocument>(SqliteDb.TableName, Field.EQ("Value", "absent"));
|
||||
Expect.isNull(doc, "There should not have been a document returned");
|
||||
})
|
||||
})
|
||||
@@ -524,8 +549,8 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Patch.ByField(SqliteDb.TableName, "Value", Op.EQ, "purple", new { NumValue = 77 });
|
||||
var after = await Count.ByField(SqliteDb.TableName, "NumValue", Op.EQ, 77);
|
||||
await Patch.ByField(SqliteDb.TableName, Field.EQ("Value", "purple"), new { NumValue = 77 });
|
||||
var after = await Count.ByField(SqliteDb.TableName, Field.EQ("NumValue", 77));
|
||||
Expect.equal(after, 2L, "There should have been 2 documents returned");
|
||||
}),
|
||||
TestCase("succeeds when no document is updated", async () =>
|
||||
@@ -536,7 +561,67 @@ public static class SqliteCSharpTests
|
||||
Expect.isEmpty(before, "There should have been no documents returned");
|
||||
|
||||
// This not raising an exception is the test
|
||||
await Patch.ByField(SqliteDb.TableName, "Value", Op.EQ, "burgundy", new { Foo = "green" });
|
||||
await Patch.ByField(SqliteDb.TableName, Field.EQ("Value", "burgundy"), new { Foo = "green" });
|
||||
})
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFields", new[]
|
||||
{
|
||||
TestList("ById", new[]
|
||||
{
|
||||
TestCase("succeeds when fields are removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "Sub", "Value" });
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "two");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.equal(updated.Value, "", "The string value should have been removed");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "AFieldThatIsNotThere" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ById(SqliteDb.TableName, "two", new[] { "Value" });
|
||||
})
|
||||
}),
|
||||
TestList("ByField", new[]
|
||||
{
|
||||
TestCase("succeeds when a field is removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await RemoveFields.ByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Sub" });
|
||||
var updated = await Find.ById<string, JsonDocument>(SqliteDb.TableName, "four");
|
||||
Expect.isNotNull(updated, "The updated document should have been retrieved");
|
||||
Expect.isNull(updated.Sub, "The sub-document should have been removed");
|
||||
}),
|
||||
TestCase("succeeds when a field is not removed", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ByField(SqliteDb.TableName, Field.EQ("NumValue", 17), new[] { "Nothing" });
|
||||
}),
|
||||
TestCase("succeeds when no document is matched", async () =>
|
||||
{
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
|
||||
// This not raising an exception is the test
|
||||
await RemoveFields.ByField(SqliteDb.TableName, Field.NE("Abracadabra", "apple"), new[] { "Value" });
|
||||
})
|
||||
})
|
||||
}),
|
||||
@@ -570,7 +655,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Delete.ByField(SqliteDb.TableName, "Value", Op.NE, "purple");
|
||||
await Delete.ByField(SqliteDb.TableName, Field.NE("Value", "purple"));
|
||||
var remaining = await Count.All(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 2L, "There should have been 2 documents remaining");
|
||||
}),
|
||||
@@ -579,7 +664,7 @@ public static class SqliteCSharpTests
|
||||
await using var db = await SqliteDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
await Delete.ByField(SqliteDb.TableName, "Value", Op.EQ, "crimson");
|
||||
await Delete.ByField(SqliteDb.TableName, Field.EQ("Value", "crimson"));
|
||||
var remaining = await Count.All(SqliteDb.TableName);
|
||||
Expect.equal(remaining, 5L, "There should have been 5 documents remaining");
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user