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 Date range filter #12

Merged
merged 9 commits into from
Mar 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public partial class JsonConvertersTests
new RangeClause()
{
FieldName = "timestamp",
GTEValue = 1581963795598,
LTEValue = 1581964695598,
GTEValue = "1581963795598",
LTEValue = "1581964695598",
Format = "epoch_millis",
},
},
Expand All @@ -156,8 +156,8 @@ public partial class JsonConvertersTests
private static readonly RangeClause LeafResult = new RangeClause()
{
FieldName = "timestamp",
GTEValue = 1581963795598,
LTEValue = 1581964695598,
GTEValue = "1581963795598",
LTEValue = "1581964695598",
Format = "epoch_millis",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public partial class JsonConvertersTests
new RangeClause
{
FieldName = "timestamp",
GTEValue = 0,
LTEValue = 10,
GTEValue = "0",
LTEValue = "10",
Format = "epoch_millis",
};

Expand Down
10 changes: 5 additions & 5 deletions K2Bridge.Tests.UnitTests/JsonConverters/RangeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public class RangeConverterTests
new RangeClause()
{
FieldName = "timestamp",
GTEValue = 0,
LTEValue = 10,
GTEValue = "0",
LTEValue = "10",
Format = "epoch_millis",
},
},
Expand All @@ -97,8 +97,8 @@ public class RangeConverterTests
new RangeClause()
{
FieldName = "TEST_FIELD",
GTEValue = 0,
LTValue = 10,
GTEValue = "0",
LTValue = "10",
},
},
MustNot = new List<IQuery>(),
Expand All @@ -116,7 +116,7 @@ public class RangeConverterTests
new RangeClause()
{
FieldName = "timestamp",
GTEValue = 0,
GTEValue = "0",
Format = "epoch_millis",
},
},
Expand Down
2 changes: 1 addition & 1 deletion K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace UnitTests.K2Bridge.Visitors.LuceneNet
{
using System;
using global::K2Bridge.Models.Request;
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Models.Request.Queries.LuceneNet;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using global::K2Bridge.Visitors.LuceneNet;
using NUnit.Framework;
Expand Down Expand Up @@ -54,7 +56,7 @@ public string Visit_WithValidTermPhraseQuery_ReturnsValidReponse()
var es = phraseQuery.ESQuery;
Assert.NotNull(es);

var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
visitor.Visit((QueryStringClause)es);

return ((QueryStringClause)es).KustoQL;
Expand All @@ -76,7 +78,7 @@ public string Visit_WithValidTermQuery_ReturnsValidReponse()
var es = phraseQuery.ESQuery;
Assert.NotNull(es);

var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
visitor.Visit((QueryStringClause)es);

return ((QueryStringClause)es).KustoQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace UnitTests.K2Bridge.Visitors.LuceneNet
{
using System;
using global::K2Bridge.Models.Request;
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Models.Request.Queries.LuceneNet;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using global::K2Bridge.Visitors.LuceneNet;
using NUnit.Framework;
Expand Down Expand Up @@ -57,7 +59,7 @@ public string Visit_WithValidRangeQuery_ReturnsValidResponse()
var es = rangeQuery.ESQuery;
Assert.NotNull(es);

var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor("days", "long");
visitor.Visit((RangeClause)es);

return ((RangeClause)es).KustoQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace UnitTests.K2Bridge.Visitors.LuceneNet
using System;
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Models.Request.Queries.LuceneNet;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using global::K2Bridge.Visitors.LuceneNet;
using NUnit.Framework;
Expand Down Expand Up @@ -53,7 +54,7 @@ public string Visit_WithValidWildcardTermQuery_ReturnsValidResponse()
var es = termQuery.ESQuery;
Assert.NotNull(es);

var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
visitor.Visit((QueryStringClause)es);

return ((QueryStringClause)es).KustoQL;
Expand Down
25 changes: 13 additions & 12 deletions K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace UnitTests.K2Bridge.Visitors
{
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using Newtonsoft.Json;
using NUnit.Framework;
Expand Down Expand Up @@ -282,7 +283,7 @@ public class ParseElasticToKqlTests
public string TestMatchPhraseQueries(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}
Expand All @@ -294,7 +295,7 @@ public string TestMatchPhraseQueries(string queryString)
public string TestExistsClause(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}
Expand All @@ -309,7 +310,7 @@ public string TestExistsClause(string queryString)
TestName = "QueryAccept_WithBetweenRange_ReturnsExpectedResult")]
public string TestRangeQueries(string queryString)
{
return TestRangeClause(queryString);
return TestRangeClause(queryString, "TEST_FIELD", "long");
}

[TestCase(
Expand All @@ -327,7 +328,7 @@ public void TestRangeQueriesMissingValues(string queryString)
public string TestQueryStringQueries(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}
Expand All @@ -343,31 +344,31 @@ public string TestQueryStringQueries(string queryString)
public string TestCombinedQueries(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}

[TestCase(
QueryWildcardString,
ExpectedResult = "where (* matches regex \"TEST(.)*RESULT\")",
TestName="QueryAccept_WithWildCard_ReturnsExpectedResult")]
TestName = "QueryAccept_WithWildCard_ReturnsExpectedResult")]
public string TestWildcardQuery(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}

[TestCase(
QueryComplexWildcardString,
ExpectedResult = "where (* matches regex \"TEST(.)*RESULT(.)*\")",
TestName="QueryAccept_WithComplexWildCard_ReturnsExpectedResult")]
TestName = "QueryAccept_WithComplexWildCard_ReturnsExpectedResult")]
public string TestComplexWildcardQuery(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
query.Accept(visitor);
return query.KustoQL;
}
Expand All @@ -379,15 +380,15 @@ public string TestComplexWildcardQuery(string queryString)
public string TestPrefixQuery(string queryString)
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of those tests (like this) are now visited twice? was once before...

query.Accept(visitor);
return query.KustoQL;
}

private string TestRangeClause(string queryString)
private string TestRangeClause(string queryString, string field = "MyField", string type = "string")
{
var query = JsonConvert.DeserializeObject<Query>(queryString);
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(field, type);
query.Accept(visitor);
return query.KustoQL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace UnitTests.K2Bridge.Visitors
{
using global::K2Bridge.Models.Request;
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using NUnit.Framework;

Expand Down Expand Up @@ -92,7 +94,7 @@ public string Visit_WithEmptyQuotePhrase_ReturnsAndContainsResponse()

private static string VisitQuery(QueryStringClause queryStringClause)
{
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor();
visitor.Visit(queryStringClause);
return queryStringClause.KustoQL;
}
Expand Down
49 changes: 40 additions & 9 deletions K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace UnitTests.K2Bridge.Visitors
{
using global::K2Bridge.Models.Request;
using global::K2Bridge.Models.Request.Queries;
using global::K2Bridge.Tests.UnitTests.Visitors;
using global::K2Bridge.Visitors;
using NUnit.Framework;

Expand All @@ -17,23 +19,23 @@ public class RangeClauseVisitorTests
TestName="Visit_WithValidRangeBetweenNumbers_ReturnsValidResponse")]
public string TestValidRangeClauseVisitNumberBetweenTwoInts()
{
return RangeClauseToKQL(CreateRangeClause(0, 10m));
return RangeClauseToKQL(CreateRangeClause("0", "10"));
}

[TestCase(
ExpectedResult = "MyField >= 0 and MyField < 10.10",
TestName="Visit_WithValidRangeBetweenIntAndDecimal_ReturnsValidResponse")]
public string TestValidRangeClauseVisitNumberBetweenIntAndDecimal()
{
return RangeClauseToKQL(CreateRangeClause(0, 10.10m));
return RangeClauseToKQL(CreateRangeClause("0", "10.10"));
}

[TestCase(
ExpectedResult = "MyField >= 10.10 and MyField < 20.20",
TestName="Visit_WithValidRangeBetweenDecimals_ReturnsValidResponse")]
public string TestValidRangeClauseVisitNumberBetweenTwoDecimalss()
{
return RangeClauseToKQL(CreateRangeClause(10.10m, 20.20m));
return RangeClauseToKQL(CreateRangeClause("10.10", "20.20"));
}

// Time RangeClause Query Tests
Expand All @@ -42,26 +44,34 @@ public string TestValidRangeClauseVisitNumberBetweenTwoDecimalss()
TestName="Visit_WithValidRangeBetweenInts_ReturnsValidResponse")]
public string TestValidTimeRangeClauseVisitNumberBetweenTwoInts()
{
return RangeClauseToKQL(CreateTimeRangeClause(0, 10m));
return RangeClauseToKQL(CreateTimeRangeClause("0", "10"));
}

[TestCase(
ExpectedResult = "MyField >= fromUnixTimeMilli(0) and MyField <= fromUnixTimeMilli(10.10)",
TestName="Visit_WithValidTimeRangeBetweenNumbers_ReturnsValidResponse")]
public string TestValidTimeRangeClauseVisitNumberBetweenIntAndDecimal()
{
return RangeClauseToKQL(CreateTimeRangeClause(0, 10.10m));
return RangeClauseToKQL(CreateTimeRangeClause("0", "10.10"));
}

[TestCase(
ExpectedResult = "MyField >= fromUnixTimeMilli(10.10) and MyField <= fromUnixTimeMilli(20.20)",
TestName="Visit_WithValidTimeRangeBetweenDecimals_ReturnsValidResponse")]
public string TestValidTimeRangeClauseVisitNumberBetweenTwoDecimalss()
{
return RangeClauseToKQL(CreateTimeRangeClause(10.10m, 20.20m));
return RangeClauseToKQL(CreateTimeRangeClause("10.10", "20.20"));
}

private static RangeClause CreateRangeClause(decimal min, decimal max)
[TestCase(
ExpectedResult = "MyField >= todatetime('2020-01-01 00:00') and MyField < todatetime('2020-02-22 10:00')",
TestName = "Visit_ValidDateRange_ReturnsValidResponse")]
public string TestValidDateRange_ReturnsValidResponse()
{
return DateRangeClauseToKQL(CreateDateRangeClause("2020-01-01 00:00", "2020-02-22 10:00"));
}

private static RangeClause CreateRangeClause(string min, string max)
{
return new RangeClause()
{
Expand All @@ -74,7 +84,7 @@ private static RangeClause CreateRangeClause(decimal min, decimal max)
};
}

private static RangeClause CreateTimeRangeClause(decimal min, decimal max)
private static RangeClause CreateTimeRangeClause(string min, string max)
{
return new RangeClause()
{
Expand All @@ -87,9 +97,30 @@ private static RangeClause CreateTimeRangeClause(decimal min, decimal max)
};
}

private static RangeClause CreateDateRangeClause(string min, string max)
{
return new RangeClause()
{
FieldName = "MyField",
GTEValue = min,
GTValue = null,
LTEValue = null,
LTValue = max,
Format = null,
};
}

private static string RangeClauseToKQL(RangeClause rangeClause)
{
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever());
var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor("MyField", "long");
visitor.Visit(rangeClause);
return rangeClause.KustoQL;
}

private static string DateRangeClauseToKQL(RangeClause rangeClause)
{
var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockDateSchemaRetriever());
VisitorTestsUtils.VisitRootDsl(visitor);
visitor.Visit(rangeClause);
return rangeClause.KustoQL;
}
Expand Down
Loading