First cut of BT operator (#3)
This commit is contained in:
@@ -47,42 +47,48 @@ public static class PostgresCSharpTests
|
||||
{
|
||||
var it = Parameters.AddField("@it", Field.EX("It"), Enumerable.Empty<Tuple<string, SqlValue>>());
|
||||
Expect.isEmpty(it, "There should not have been any parameters added");
|
||||
}),
|
||||
TestCase("succeeds when two parameters are added", () =>
|
||||
{
|
||||
var it = Parameters.AddField("@field", Field.BT("that", "eh", "zed"),
|
||||
Enumerable.Empty<Tuple<string, SqlValue>>()).ToList();
|
||||
Expect.hasLength(it, 2, "There should have been 2 parameters added");
|
||||
Expect.equal(it[0].Item1, "@fieldmin", "Minimum field name not correct");
|
||||
Expect.isTrue(it[0].Item2.IsParameter, "Minimum field parameter value incorrect");
|
||||
Expect.equal(it[1].Item1, "@fieldmax", "Maximum field name not correct");
|
||||
Expect.isTrue(it[1].Item2.IsParameter, "Maximum field parameter value incorrect");
|
||||
})
|
||||
}),
|
||||
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", () =>
|
||||
{
|
||||
Expect.isEmpty(Parameters.None, "The no-params sequence should be empty");
|
||||
})
|
||||
}),
|
||||
TestList("Query", new[]
|
||||
{
|
||||
TestList("WhereByField", new[]
|
||||
{
|
||||
TestCase("succeeds when a logical operator is passed", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereByField(Field.GT("theField", 0), "@test"),
|
||||
"data->>'theField' > @test", "WHERE clause not correct");
|
||||
}),
|
||||
TestCase("succeeds when an existence operator is passed", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereByField(Field.NEX("thatField"), ""), "data->>'thatField' IS NULL",
|
||||
"WHERE clause not correct");
|
||||
}),
|
||||
TestCase("succeeds when a between operator is passed with numeric values", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereByField(Field.BT("aField", 50, 99), "@range"),
|
||||
"(data->>'aField')::numeric BETWEEN @rangemin AND @rangemax", "WHERE clause not correct");
|
||||
}),
|
||||
TestCase("succeeds when a between operator is passed with non-numeric values", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereByField(Field.BT("field0", "a", "b"), "@alpha"),
|
||||
"data->>'field0' BETWEEN @alphamin AND @alphamax", "WHERE clause not correct");
|
||||
})
|
||||
}),
|
||||
TestCase("WhereById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereById("@id"), "data->>'Id' = @id", "WHERE clause not correct");
|
||||
}),
|
||||
TestList("Definition", new[]
|
||||
{
|
||||
TestCase("EnsureTable succeeds", () =>
|
||||
@@ -107,6 +113,11 @@ public static class PostgresCSharpTests
|
||||
"CREATE INDEX statement not constructed correctly");
|
||||
})
|
||||
}),
|
||||
TestCase("Update succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Update("tbl"), "UPDATE tbl SET data = @data WHERE data->>'Id' = @id",
|
||||
"UPDATE full statement not correct");
|
||||
}),
|
||||
TestCase("WhereDataContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.WhereDataContains("@test"), "data @> @test",
|
||||
@@ -119,6 +130,17 @@ public static class PostgresCSharpTests
|
||||
}),
|
||||
TestList("Count", new[]
|
||||
{
|
||||
TestCase("All succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Count.All(PostgresDb.TableName),
|
||||
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName}", "Count query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Count.ByField(PostgresDb.TableName, Field.EQ("thatField", 0)),
|
||||
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName} WHERE data->>'thatField' = @field",
|
||||
"JSON field text comparison count query not correct");
|
||||
}),
|
||||
TestCase("ByContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Count.ByContains(PostgresDb.TableName),
|
||||
@@ -134,6 +156,18 @@ public static class PostgresCSharpTests
|
||||
}),
|
||||
TestList("Exists", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Exists.ById(PostgresDb.TableName),
|
||||
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data->>'Id' = @id) AS it",
|
||||
"ID existence query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Exists.ByField(PostgresDb.TableName, Field.LT("Test", 0)),
|
||||
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data->>'Test' < @field) AS it",
|
||||
"JSON field text comparison exists query not correct");
|
||||
}),
|
||||
TestCase("ByContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Exists.ByContains(PostgresDb.TableName),
|
||||
@@ -149,6 +183,18 @@ public static class PostgresCSharpTests
|
||||
}),
|
||||
TestList("Find", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Find.ById(PostgresDb.TableName),
|
||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data->>'Id' = @id",
|
||||
"SELECT by ID query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Find.ByField(PostgresDb.TableName, Field.GE("Golf", 0)),
|
||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data->>'Golf' >= @field",
|
||||
"SELECT by JSON comparison query not correct");
|
||||
}),
|
||||
TestCase("byContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Find.ByContains(PostgresDb.TableName),
|
||||
@@ -167,13 +213,13 @@ public static class PostgresCSharpTests
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Patch.ById(PostgresDb.TableName),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Id' = @id",
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data->>'Id' = @id",
|
||||
"UPDATE partial by ID statement not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Patch.ByField(PostgresDb.TableName, Field.LT("Snail", 0)),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Snail' < @field",
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data->>'Snail' < @field",
|
||||
"UPDATE partial by ID statement not correct");
|
||||
}),
|
||||
TestCase("ByContains succeeds", () =>
|
||||
@@ -189,8 +235,47 @@ public static class PostgresCSharpTests
|
||||
"UPDATE partial by JSON Path statement not correct");
|
||||
})
|
||||
}),
|
||||
TestList("RemoveFields", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ById(PostgresDb.TableName),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data->>'Id' = @id",
|
||||
"Remove field by ID query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByField(PostgresDb.TableName, Field.LT("Fly", 0)),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data->>'Fly' < @field",
|
||||
"Remove field by field query not correct");
|
||||
}),
|
||||
TestCase("ByContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByContains(PostgresDb.TableName),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data @> @criteria",
|
||||
"Remove field by contains query not correct");
|
||||
}),
|
||||
TestCase("ByJsonPath succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.RemoveFields.ByJsonPath(PostgresDb.TableName),
|
||||
$"UPDATE {PostgresDb.TableName} SET data = data - @name WHERE data @? @path::jsonpath",
|
||||
"Remove field by JSON path query not correct");
|
||||
})
|
||||
}),
|
||||
TestList("Delete", new[]
|
||||
{
|
||||
TestCase("ById succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Delete.ById(PostgresDb.TableName),
|
||||
$"DELETE FROM {PostgresDb.TableName} WHERE data->>'Id' = @id",
|
||||
"DELETE by ID query not correct");
|
||||
}),
|
||||
TestCase("ByField succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Delete.ByField(PostgresDb.TableName, Field.NEX("gone")),
|
||||
$"DELETE FROM {PostgresDb.TableName} WHERE data->>'gone' IS NULL",
|
||||
"DELETE by JSON comparison query not correct");
|
||||
}),
|
||||
TestCase("byContains succeeds", () =>
|
||||
{
|
||||
Expect.equal(Postgres.Query.Delete.ByContains(PostgresDb.TableName),
|
||||
@@ -469,8 +554,8 @@ public static class PostgresCSharpTests
|
||||
await using var db = PostgresDb.BuildDb();
|
||||
await LoadDocs();
|
||||
|
||||
var theCount = await Count.ByField(PostgresDb.TableName, Field.EQ("Value", "purple"));
|
||||
Expect.equal(theCount, 2, "There should have been 2 matching documents");
|
||||
var theCount = await Count.ByField(PostgresDb.TableName, Field.BT("NumValue", 10, 20));
|
||||
Expect.equal(theCount, 3, "There should have been 3 matching documents");
|
||||
}),
|
||||
TestCase("ByContains succeeds", async () =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user