Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support long, DateTime, and DateTimeOffset criteria in filter expressions #101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Postgrest/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
case int intCriterion:
_filters.Add(new QueryFilter(columnName, op, intCriterion));
return this;
case long longCriterion:
_filters.Add(new QueryFilter(columnName, op, longCriterion));
return this;
case float floatCriterion:
_filters.Add(new QueryFilter(columnName, op, floatCriterion));
return this;
Expand All @@ -159,9 +162,15 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
case FullTextSearchConfig fullTextSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, fullTextSearchCriteria));
return this;
case DateTime dtSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, dtSearchCriteria));
return this;
case DateTimeOffset dtoSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, dtoSearchCriteria));
return this;
default:
throw new PostgrestException(
"Unknown criterion type, is it of type `string`, `int`, `float`, `List`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
"Unknown criterion type, is it of type `string`, `int`, `long`, `float`, `List`, `DateTime`, `DateTimeOffset`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
{
Reason = FailureHint.Reason.InvalidArgument
};
Expand Down
31 changes: 31 additions & 0 deletions PostgrestTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,37 @@ public async Task TestMatchFilter()
CollectionAssert.AreEqual(expected, filteredResponse.Models);
}

[TestMethod("filters: dt")]
public async Task TestDateTimeFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTime(2022, 08, 20))
.Filter("created_at", Operator.LessThan, new DateTime(2022, 08, 21))
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
}

[TestMethod("filters: dto")]
public async Task TestDateTimeOffsetFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTimeOffset(new DateTime(2022, 08, 20)))
.Filter("created_at", Operator.LessThan, new DateTimeOffset(new DateTime(2022, 08, 21)))
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
}

[TestMethod("filters: long")]
public async Task TestLongIntFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<KitchenSink>().Filter("long_value", Operator.Equals, 2147483648L)
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
}

[TestMethod("select: basic")]
public async Task TestSelect()
{
Expand Down
2 changes: 2 additions & 0 deletions PostgrestTests/Models/KitchenSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class KitchenSink : BaseModel

[Column("int_value")] public int? IntValue { get; set; }

[Column("long_value")] public long? LongValue { get; set; }

[Column("float_value")] public float FloatValue { get; set; }

[Column("double_value")] public double DoubleValue { get; set; }
Expand Down
1 change: 1 addition & 0 deletions PostgrestTests/db/00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ create table "public"."kitchen_sink"
"bool_value" BOOL DEFAULT false,
"unique_value" varchar(255) UNIQUE,
"int_value" INT null,
"long_value" BIGINT null,
"float_value" FLOAT null,
"double_value" DOUBLE PRECISION null,
"datetime_value" timestamp null,
Expand Down
2 changes: 2 additions & 0 deletions PostgrestTests/db/01-dummy-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ INSERT INTO public.kitchen_sink (id,
string_value,
bool_value,
int_value,
long_value,
float_value,
double_value,
datetime_value,
Expand All @@ -41,6 +42,7 @@ VALUES ('f3ff356d-5803-43a7-b125-ba10cf10fdcd',
'Im the Kitchen Sink!',
false,
99999,
2147483648,
'99999.0'::float4,
'99999.0'::float8,
'Tue May 24 06:30:00 2022'::timestamp,
Expand Down
Loading