Skip to content

Commit

Permalink
Better naming + cql fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Oct 30, 2023
1 parent d838163 commit 1d4893c
Show file tree
Hide file tree
Showing 28 changed files with 339 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Newtonsoft.Json.Linq;
using Stac.Api.Attributes;
using Stac.Api.Clients.ItemSearch;
using Stac.Api.Extensions.Sort.Context;
using Stac.Api.Models.Extensions.Sort.Context;
using Stac.Api.Interfaces;

namespace Stac.Api.Clients.Extensions.Sort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Newtonsoft.Json.Linq;
using Stac.Api.Attributes;
using Stac.Api.Clients.ItemSearch;
using Stac.Api.Extensions.Sort.Context;
using Stac.Api.Models.Extensions.Sort.Context;
using Stac.Api.Interfaces;

namespace Stac.Api.Clients.Extensions.Sort
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Stac.Api.Extensions.Sort.Context;
using Stac.Api.Models.Extensions.Sort.Context;
using Stac.Api.Services.Queryable;

namespace Stac.Api.Clients.Extensions.Sort
Expand Down
2 changes: 1 addition & 1 deletion src/Stac.Api.FileSystem/Extensions/StacWebApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using Stac.Api.WebApi.Implementations.Default.Services;
using Stac.Api.WebApi.Services.Context;
using Stac.Api.Services.Default;
using Stac.Api.Extensions.Sort.Context;
using Stac.Api.Models.Extensions.Sort.Context;
using Stac.Api.Services.Debugging;

namespace Stac.Api.FileSystem.Extensions
Expand Down
22 changes: 11 additions & 11 deletions src/Stac.Api.Tests/CQL2FilteringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,18 @@ public async Task Example9Test()
Assert.Equal(3, cql.AsAndOrExpression().Args.Count);

Assert.Equal(ComparisonPredicateOp.Gt, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(50, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(ComparisonPredicateOp.Lt, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Op);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(10, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(AndOrExpressionOp.And, cql.AsAndOrExpression().Args[2].AsAndOrExpression().Op);
Assert.IsType<IsNullPredicate>(cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0]);
Assert.IsType<IsNullPredicate>(cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1]);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0].AsComparison().AsIsNullPredicate().Args.AsChar().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1].AsComparison().AsIsNullPredicate().Args.AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0].AsComparison().AsIsNullPredicate().Args.AsCharExpression().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1].AsComparison().AsIsNullPredicate().Args.AsCharExpression().AsPropertyRef().Property);

json = GetJson("CQL2", "SampleItem", "CQL2Tests");
StacItem item = JsonConvert.DeserializeObject<StacItem>(json);
Expand All @@ -256,7 +256,7 @@ public async Task Example10Test()
Assert.Equal(IsBetweenPredicateOp.Between, cql.AsComparison().AsIsBetweenPredicate().Op);
Assert.Equal(3, cql.AsComparison().AsIsBetweenPredicate().Args.Count);

Assert.Equal("eo:cloud_cover", cql.AsComparison().AsIsBetweenPredicate().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("eo:cloud_cover", cql.AsComparison().AsIsBetweenPredicate().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal((double)0, cql.AsComparison().AsIsBetweenPredicate().Args[1].AsNumeric().Value);
Assert.Equal((double)50, cql.AsComparison().AsIsBetweenPredicate().Args[2].AsNumeric().Value);

Expand All @@ -278,13 +278,13 @@ public async Task Example11Test()
JObject jObject = JObject.Parse(json);
var cql = JsonConvert.DeserializeObject<BooleanExpression>(jObject["filter"].ToString(), _settings);
Assert.IsType<IsLikePredicate>(cql);
Assert.NotNull(cql.AsComparison().AsIsLike());
Assert.Equal(IsLikePredicateOp.Like, cql.AsComparison().AsIsLike().Op);
Assert.Equal(2, cql.AsComparison().AsIsLike().Args.Count);
Assert.NotNull(cql.AsComparison().AsIsLikePredicate());
Assert.Equal(IsLikePredicateOp.Like, cql.AsComparison().AsIsLikePredicate().Op);
Assert.Equal(2, cql.AsComparison().AsIsLikePredicate().Args.Count);

Assert.Equal("mission", cql.AsComparison().AsIsLike().Args[0].AsPropertyRef().Property);
Assert.IsType<Models.Cql2.String>(cql.AsComparison().AsIsLike().Args[1]);
Assert.Equal("sentinel%", cql.AsComparison().AsIsLike().Args[1].ToString());
Assert.Equal("mission", cql.AsComparison().AsIsLikePredicate().Args[0].AsPropertyRef().Property);
Assert.IsType<Models.Cql2.String>(cql.AsComparison().AsIsLikePredicate().Args[1]);
Assert.Equal("sentinel%", cql.AsComparison().AsIsLikePredicate().Args[1].ToString());

json = GetJson("CQL2", "SampleItem", "CQL2Tests");
StacItem item = JsonConvert.DeserializeObject<StacItem>(json);
Expand Down
65 changes: 42 additions & 23 deletions src/Stac.Api.Tests/CQL2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using Stac.Api.Converters;
using GeoJSON.Net.Geometry;
using System.Globalization;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

namespace Stac.Api.Tests
{
Expand Down Expand Up @@ -36,8 +39,8 @@ public async Task Example1Test()
Assert.Equal(ComparisonPredicateOp.Eq, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("id", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("S2A_60HWB_20201111_0_L2A", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsChar().ToString());
Assert.Equal("id", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal("S2A_60HWB_20201111_0_L2A", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsCharExpression().ToString());
}

[Fact]
Expand All @@ -56,19 +59,19 @@ public async Task Example2Test()
Assert.Equal(ComparisonPredicateOp.Eq, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("collection", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("landsat8_l1tp", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsChar().ToString());
Assert.Equal("collection", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal("landsat8_l1tp", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsCharExpression().ToString());

Assert.Equal(ComparisonPredicateOp.Le, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(10, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(ComparisonPredicateOp.Ge, cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("datetime", cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("datetime", cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
DateTimeOffset datetime = DateTimeOffset.Parse("2021-04-08T04:39:23Z");
Assert.Equal(datetime, cql.AsAndOrExpression().Args[2].AsComparison().AsBinaryComparison().Args[1].AsTemporalInstant().DateTime);

Expand All @@ -95,13 +98,13 @@ public async Task Example3Test()
Assert.Equal(ComparisonPredicateOp.Gt, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(50, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(ComparisonPredicateOp.Lt, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(10, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

}
Expand All @@ -122,13 +125,13 @@ public async Task Example4Test()
Assert.Equal(ComparisonPredicateOp.Gt, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(50, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(ComparisonPredicateOp.Lt, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args.Count);
Assert.IsType<PropertyRef>(cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0]);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("eo:cloud_cover", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(10, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

}
Expand All @@ -144,8 +147,8 @@ public async Task Example5Test()
Assert.Equal(ComparisonPredicateOp.Eq, cql.AsComparison().AsBinaryComparison().Op);
Assert.Equal(2, cql.AsComparison().AsBinaryComparison().Args.Count);

Assert.Equal("prop1", cql.AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("prop2", cql.AsComparison().AsBinaryComparison().Args[1].AsChar().AsPropertyRef().Property);
Assert.Equal("prop1", cql.AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal("prop2", cql.AsComparison().AsBinaryComparison().Args[1].AsCharExpression().AsPropertyRef().Property);

}

Expand Down Expand Up @@ -217,18 +220,18 @@ public async Task Example9Test()
Assert.Equal(3, cql.AsAndOrExpression().Args.Count);

Assert.Equal(ComparisonPredicateOp.Gt, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Op);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(50, cql.AsAndOrExpression().Args[0].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(ComparisonPredicateOp.Lt, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Op);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(10, cql.AsAndOrExpression().Args[1].AsComparison().AsBinaryComparison().Args[1].AsNumericExpression().AsNumber().Num);

Assert.Equal(AndOrExpressionOp.And, cql.AsAndOrExpression().Args[2].AsAndOrExpression().Op);
Assert.IsType<IsNullPredicate>(cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0]);
Assert.IsType<IsNullPredicate>(cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1]);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0].AsComparison().AsIsNullPredicate().Args.AsChar().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1].AsComparison().AsIsNullPredicate().Args.AsChar().AsPropertyRef().Property);
Assert.Equal("sentinel:data_coverage", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[0].AsComparison().AsIsNullPredicate().Args.AsCharExpression().AsPropertyRef().Property);
Assert.Equal("landsat:coverage_percent", cql.AsAndOrExpression().Args[2].AsAndOrExpression().Args[1].AsComparison().AsIsNullPredicate().Args.AsCharExpression().AsPropertyRef().Property);

}

Expand All @@ -243,7 +246,7 @@ public async Task Example10Test()
Assert.Equal(IsBetweenPredicateOp.Between, cql.AsComparison().AsIsBetweenPredicate().Op);
Assert.Equal(3, cql.AsComparison().AsIsBetweenPredicate().Args.Count);

Assert.Equal("eo:cloud_cover", cql.AsComparison().AsIsBetweenPredicate().Args[0].AsChar().AsPropertyRef().Property);
Assert.Equal("eo:cloud_cover", cql.AsComparison().AsIsBetweenPredicate().Args[0].AsCharExpression().AsPropertyRef().Property);
Assert.Equal(0.0, cql.AsComparison().AsIsBetweenPredicate().Args[1].AsNumeric().Value);
Assert.Equal(50.0, cql.AsComparison().AsIsBetweenPredicate().Args[2].AsNumeric().Value);

Expand All @@ -256,14 +259,30 @@ public async Task Example11Test()
JObject jObject = JObject.Parse(json);
var cql = JsonConvert.DeserializeObject<BooleanExpression>(jObject["filter"].ToString(), _settings);
Assert.IsType<IsLikePredicate>(cql);
Assert.NotNull(cql.AsComparison().AsIsLike());
Assert.Equal(IsLikePredicateOp.Like, cql.AsComparison().AsIsLike().Op);
Assert.Equal(2, cql.AsComparison().AsIsLike().Args.Count);
Assert.NotNull(cql.AsComparison().AsIsLikePredicate());
Assert.Equal(IsLikePredicateOp.Like, cql.AsComparison().AsIsLikePredicate().Op);
Assert.Equal(2, cql.AsComparison().AsIsLikePredicate().Args.Count);

Assert.Equal("mission", cql.AsComparison().AsIsLike().Args[0].AsPropertyRef().Property);
Assert.IsType<Models.Cql2.String>(cql.AsComparison().AsIsLike().Args[1]);
Assert.Equal("sentinel%", cql.AsComparison().AsIsLike().Args[1].ToString());
Assert.Equal("mission", cql.AsComparison().AsIsLikePredicate().Args[0].AsPropertyRef().Property);
Assert.IsType<Models.Cql2.String>(cql.AsComparison().AsIsLikePredicate().Args[1]);
Assert.Equal("sentinel%", cql.AsComparison().AsIsLikePredicate().Args[1].ToString());

}

[Fact]
public async Task Example12Test()
{
var json = GetJson("CQL2", "Example12");
JObject jObject = JObject.Parse(json);
var cql = JsonConvert.DeserializeObject<BooleanExpression>(jObject["filter"].ToString(), _settings);
Assert.IsType<IsInListPredicate>(cql);
Assert.NotNull(cql.AsComparison().AsIsInListPredicate());
Assert.Equal(IsInListPredicateOp.In, cql.AsComparison().AsIsInListPredicate().Op);
Assert.Equal(2, cql.AsComparison().AsIsInListPredicate().Args.Count);

Assert.Equal("keywords", cql.AsComparison().AsIsInListPredicate().Args[0].AsScalarExpression().AsCharExpression().AsPropertyRef().Property);
Assert.IsType<ScalarExpressionCollection>(cql.AsComparison().AsIsInListPredicate().Args[1]);
Assert.Equal<string>(new List<string>(){ "fire", "forest", "wildfire" }, cql.AsComparison().AsIsInListPredicate().Args[1].AsScalarExpressionCollection().Select(x => x.AsCharExpression().ToString()).ToList());
}
}
}
16 changes: 16 additions & 0 deletions src/Stac.Api.Tests/Resources/CQL2/CQL2Tests_Example12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"filter-lang": "cql2-json",
"filter": {
"op": "in",
"args": [
{
"property": "keywords"
},
[
"fire",
"forest",
"wildfire"
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task<ActionResult<StacFeatureCollection>> PostItemSearchAsync(Filte



private void SetQueryParametersInContext(IStacApiContext stacApiContext, ISearchFilter filter)
private void SetQueryParametersInContext(IStacApiContext stacApiContext, ISearchExpression filter)
{
DefaultQueryParameters queryParameters = new DefaultQueryParameters();
if (filter != null)
Expand Down
Loading

0 comments on commit 1d4893c

Please sign in to comment.