From 089603c3f26210c7d175bc86f11aff9178426157 Mon Sep 17 00:00:00 2001 From: Itye Date: Wed, 26 Feb 2020 17:15:25 +0200 Subject: [PATCH 1/5] Support date range filter --- .../JsonConverters/IQueryConverterTests.cs | 8 +-- .../LeafClauseConverterTests.cs | 4 +- .../JsonConverters/RangeConverterTests.cs | 10 ++-- .../K2Bridge.Tests.UnitTests.csproj | 3 + .../LuceneNet/LucenePhraseVisitorTests.cs | 4 +- .../LuceneNet/LuceneRangeVisitorTests.cs | 5 +- .../LuceneNet/LuceneTermVisitorTests.cs | 3 +- .../Visitors/ParseElasticToKqlTests.cs | 22 +++---- .../Visitors/QueryStringClauseVisitorTests.cs | 4 +- .../Visitors/RangeClauseVisitorTests.cs | 50 +++++++++++++--- .../Visitors/RangeVisitorTests.cs | 24 ++++---- .../Visitors/SchemaRetrieverMock.cs | 26 +++------ .../Visitors/VisitorTestsUtils.cs | 39 +++++++++++++ .../JsonConverters/RangeClauseConverter.cs | 8 +-- .../Models/Request/Queries/RangeClause.cs | 8 +-- K2Bridge/Visitors/ClauseFieldType.cs | 32 +++++++++++ K2Bridge/Visitors/ClauseFieldTypeProcessor.cs | 57 +++++++++++++++++++ .../Visitors/LuceneNet/LuceneRangeVisitor.cs | 4 +- K2Bridge/Visitors/QueryStringClauseVisitor.cs | 27 +-------- K2Bridge/Visitors/RangeClauseVisitor.cs | 16 +++++- 20 files changed, 256 insertions(+), 98 deletions(-) create mode 100644 K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs create mode 100644 K2Bridge/Visitors/ClauseFieldType.cs create mode 100644 K2Bridge/Visitors/ClauseFieldTypeProcessor.cs diff --git a/K2Bridge.Tests.UnitTests/JsonConverters/IQueryConverterTests.cs b/K2Bridge.Tests.UnitTests/JsonConverters/IQueryConverterTests.cs index 7780815c..b474ec56 100644 --- a/K2Bridge.Tests.UnitTests/JsonConverters/IQueryConverterTests.cs +++ b/K2Bridge.Tests.UnitTests/JsonConverters/IQueryConverterTests.cs @@ -142,8 +142,8 @@ public partial class JsonConvertersTests new RangeClause() { FieldName = "timestamp", - GTEValue = 1581963795598, - LTEValue = 1581964695598, + GTEValue = "1581963795598", + LTEValue = "1581964695598", Format = "epoch_millis", }, }, @@ -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", }; diff --git a/K2Bridge.Tests.UnitTests/JsonConverters/LeafClauseConverterTests.cs b/K2Bridge.Tests.UnitTests/JsonConverters/LeafClauseConverterTests.cs index 3b966145..ce805e2d 100644 --- a/K2Bridge.Tests.UnitTests/JsonConverters/LeafClauseConverterTests.cs +++ b/K2Bridge.Tests.UnitTests/JsonConverters/LeafClauseConverterTests.cs @@ -58,8 +58,8 @@ public partial class JsonConvertersTests new RangeClause { FieldName = "timestamp", - GTEValue = 0, - LTEValue = 10, + GTEValue = "0", + LTEValue = "10", Format = "epoch_millis", }; diff --git a/K2Bridge.Tests.UnitTests/JsonConverters/RangeConverterTests.cs b/K2Bridge.Tests.UnitTests/JsonConverters/RangeConverterTests.cs index ad865c82..92ff8790 100644 --- a/K2Bridge.Tests.UnitTests/JsonConverters/RangeConverterTests.cs +++ b/K2Bridge.Tests.UnitTests/JsonConverters/RangeConverterTests.cs @@ -77,8 +77,8 @@ public class RangeConverterTests new RangeClause() { FieldName = "timestamp", - GTEValue = 0, - LTEValue = 10, + GTEValue = "0", + LTEValue = "10", Format = "epoch_millis", }, }, @@ -97,8 +97,8 @@ public class RangeConverterTests new RangeClause() { FieldName = "TEST_FIELD", - GTEValue = 0, - LTValue = 10, + GTEValue = "0", + LTValue = "10", }, }, MustNot = new List(), @@ -116,7 +116,7 @@ public class RangeConverterTests new RangeClause() { FieldName = "timestamp", - GTEValue = 0, + GTEValue = "0", Format = "epoch_millis", }, }, diff --git a/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj b/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj index 4ff71b1a..259ef60c 100644 --- a/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj +++ b/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj @@ -68,4 +68,7 @@ PreserveNewest + + + diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs index 2973262c..9a6b1cb3 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs @@ -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; @@ -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.CreateDefaultVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs index 79815f2e..117ce0d9 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs @@ -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; @@ -57,7 +59,8 @@ public string Visit_WithValidRangeQuery_ReturnsValidResponse() var es = rangeQuery.ESQuery; Assert.NotNull(es); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("days")); + VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit((RangeClause)es); return ((RangeClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs index 239c295e..9820c77d 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs @@ -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; @@ -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.CreateDefaultVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs index f79de6e3..be4b84bd 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs @@ -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; @@ -282,7 +283,7 @@ public class ParseElasticToKqlTests public string TestMatchPhraseQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -294,7 +295,7 @@ public string TestMatchPhraseQueries(string queryString) public string TestExistsClause(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -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"); } [TestCase( @@ -327,7 +328,7 @@ public void TestRangeQueriesMissingValues(string queryString) public string TestQueryStringQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -343,7 +344,7 @@ public string TestQueryStringQueries(string queryString) public string TestCombinedQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -355,7 +356,7 @@ public string TestCombinedQueries(string queryString) public string TestWildcardQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -367,7 +368,7 @@ public string TestWildcardQuery(string queryString) public string TestComplexWildcardQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -379,15 +380,16 @@ public string TestComplexWildcardQuery(string queryString) public string TestPrefixQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); query.Accept(visitor); return query.KustoQL; } - private string TestRangeClause(string queryString) + private string TestRangeClause(string queryString, string field = "MyField") { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(field)); + VisitorTestsUtils.AddDefaultDsl(visitor); query.Accept(visitor); return query.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs index d047bd0c..da36a237 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs @@ -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; @@ -92,7 +94,7 @@ public string Visit_WithEmptyQuotePhrase_ReturnsAndContainsResponse() private static string VisitQuery(QueryStringClause queryStringClause) { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = VisitorTestsUtils.CreateDefaultVisitor(); visitor.Visit(queryStringClause); return queryStringClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs index 71fcb011..af667bf7 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs @@ -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; @@ -17,7 +19,7 @@ public class RangeClauseVisitorTests TestName="Visit_WithValidRangeBetweenNumbers_ReturnsValidResponse")] public string TestValidRangeClauseVisitNumberBetweenTwoInts() { - return RangeClauseToKQL(CreateRangeClause(0, 10m)); + return RangeClauseToKQL(CreateRangeClause("0", "10")); } [TestCase( @@ -25,7 +27,7 @@ public string TestValidRangeClauseVisitNumberBetweenTwoInts() TestName="Visit_WithValidRangeBetweenIntAndDecimal_ReturnsValidResponse")] public string TestValidRangeClauseVisitNumberBetweenIntAndDecimal() { - return RangeClauseToKQL(CreateRangeClause(0, 10.10m)); + return RangeClauseToKQL(CreateRangeClause("0", "10.10")); } [TestCase( @@ -33,7 +35,7 @@ public string TestValidRangeClauseVisitNumberBetweenIntAndDecimal() 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 @@ -42,7 +44,7 @@ public string TestValidRangeClauseVisitNumberBetweenTwoDecimalss() TestName="Visit_WithValidRangeBetweenInts_ReturnsValidResponse")] public string TestValidTimeRangeClauseVisitNumberBetweenTwoInts() { - return RangeClauseToKQL(CreateTimeRangeClause(0, 10m)); + return RangeClauseToKQL(CreateTimeRangeClause("0", "10")); } [TestCase( @@ -50,7 +52,7 @@ public string TestValidTimeRangeClauseVisitNumberBetweenTwoInts() TestName="Visit_WithValidTimeRangeBetweenNumbers_ReturnsValidResponse")] public string TestValidTimeRangeClauseVisitNumberBetweenIntAndDecimal() { - return RangeClauseToKQL(CreateTimeRangeClause(0, 10.10m)); + return RangeClauseToKQL(CreateTimeRangeClause("0", "10.10")); } [TestCase( @@ -58,10 +60,18 @@ public string TestValidTimeRangeClauseVisitNumberBetweenIntAndDecimal() 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() { @@ -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() { @@ -87,9 +97,31 @@ 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 = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("MyField")); + VisitorTestsUtils.AddDefaultDsl(visitor); + visitor.Visit(rangeClause); + return rangeClause.KustoQL; + } + + private static string DateRangeClauseToKQL(RangeClause rangeClause) + { + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockDateSchemaRetriever()); + VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit(rangeClause); return rangeClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs index c32e649c..3cc29052 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs @@ -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 NUnit.Framework; @@ -16,9 +17,9 @@ public class RangeVisitorTests TestName = "Visit_WithBasicInput_ReturnsValidResponse")] public string TestBasicRangeVisitor() { - var rangeClause = CreateRangeClause("myField", 3, null, null, 5, "other"); + var rangeClause = CreateRangeClause("myField", "3", null, null, "5", "other"); - return VisitRangeClause(rangeClause); + return VisitRangeClause(rangeClause, "myField"); } [TestCase( @@ -27,7 +28,7 @@ public string TestBasicRangeVisitor() TestName = "Visit_WithEpochInput_ReturnsValidResponse")] public string TestEpochBasicRangeVisitor() { - var rangeClause = CreateRangeClause("myField", 1212121121, null, 2121212121, null, "epoch_millis"); + var rangeClause = CreateRangeClause("myField", "1212121121", null, "2121212121", null, "epoch_millis"); return VisitRangeClause(rangeClause); } @@ -35,7 +36,7 @@ public string TestEpochBasicRangeVisitor() [Test] public void Visit_WithMissingFieldNameInput_ThrowsException() { - var rangeClause = CreateRangeClause(null, 3, null, null, 5, "other"); + var rangeClause = CreateRangeClause(null, "3", null, null, "5", "other"); Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } @@ -43,7 +44,7 @@ public void Visit_WithMissingFieldNameInput_ThrowsException() [Test] public void Visit_WithMissingGTEInput_ThrowsException() { - var rangeClause = CreateRangeClause("myField", null, null, null, 5, "other"); + var rangeClause = CreateRangeClause("myField", null, null, null, "5", "other"); Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } @@ -51,7 +52,7 @@ public void Visit_WithMissingGTEInput_ThrowsException() [Test] public void Visit_WithMissingLTEInput_ThrowsException() { - var rangeClause = CreateRangeClause("myField", 5, null, null, null, "other"); + var rangeClause = CreateRangeClause("myField", "5", null, null, null, "other"); Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } @@ -59,7 +60,7 @@ public void Visit_WithMissingLTEInput_ThrowsException() [Test] public void Visit_WithEpochMissingGTEInput_ThrowsException() { - var rangeClause = CreateRangeClause("myField", null, null, null, 5, "epoch_millis"); + var rangeClause = CreateRangeClause("myField", null, null, null, "5", "epoch_millis"); Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } @@ -67,14 +68,15 @@ public void Visit_WithEpochMissingGTEInput_ThrowsException() [Test] public void Visit_WithEpochMissingLTEInput_ThrowsException() { - var rangeClause = CreateRangeClause("myField", 5, null, null, null, "epoch_millis"); + var rangeClause = CreateRangeClause("myField", "5", null, null, null, "epoch_millis"); Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } - private static string VisitRangeClause(RangeClause clause) + private static string VisitRangeClause(RangeClause clause, string fieldName = "MyField") { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(fieldName)); + VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit(clause); return clause.KustoQL; } @@ -82,7 +84,7 @@ private static string VisitRangeClause(RangeClause clause) private static RangeClause CreateRangeClause( #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. #pragma warning disable SA1114 // Parameter list should follow declaration - string fieldName, decimal? gte, decimal? gt, decimal? lte, decimal? lt, string? format) + string fieldName, string gte, string gt, string lte, string lt, string? format) #pragma warning restore SA1114 // Parameter list should follow declaration #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. { diff --git a/K2Bridge.Tests.UnitTests/Visitors/SchemaRetrieverMock.cs b/K2Bridge.Tests.UnitTests/Visitors/SchemaRetrieverMock.cs index 62cf6eb7..eafcb2f9 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/SchemaRetrieverMock.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/SchemaRetrieverMock.cs @@ -12,14 +12,14 @@ namespace UnitTests.K2Bridge.Visitors public static class SchemaRetrieverMock { - public static ISchemaRetrieverFactory CreateMockSchemaRetriever() + public static ISchemaRetrieverFactory CreateMockSchemaRetriever(string name = "dayOfWeek", string type = "string") { var response = new FieldCapabilityResponse(); response.AddField( new FieldCapabilityElement { - Name = "dayOfWeek", - Type = "string", + Name = name, + Type = type, }); var responseTask = Task.FromResult(response); @@ -30,22 +30,14 @@ public static ISchemaRetrieverFactory CreateMockSchemaRetriever() return new SchemaRetrieverFactory(mockLogger.Object, mockDAL.Object); } - public static ISchemaRetrieverFactory CreateMockNumericSchemaRetriever() + public static ISchemaRetrieverFactory CreateMockNumericSchemaRetriever(string name = "dayOfWeek", string type = "long") { - var response = new FieldCapabilityResponse(); - response.AddField( - new FieldCapabilityElement - { - Name = "dayOfWeek", - Type = "long", - }); - var responseTask = Task.FromResult(response); - - var mockDAL = new Mock(); - mockDAL.Setup(kusto => kusto.GetFieldCapsAsync(It.IsNotNull())).Returns(responseTask); + return CreateMockSchemaRetriever(name, type); + } - var mockLogger = new Mock>(); - return new SchemaRetrieverFactory(mockLogger.Object, mockDAL.Object); + public static ISchemaRetrieverFactory CreateMockDateSchemaRetriever(string name = "MyField", string type = "date") + { + return CreateMockSchemaRetriever(name, type); } } } \ No newline at end of file diff --git a/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs new file mode 100644 index 00000000..c136c569 --- /dev/null +++ b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. +// See LICENSE file in the project root for full license information. + +namespace K2Bridge.Tests.UnitTests.Visitors +{ + using global::UnitTests.K2Bridge.Visitors; + using K2Bridge.Models.Request; + using K2Bridge.Models.Request.Queries; + using K2Bridge.Visitors; + + public static class VisitorTestsUtils + { + /// + /// A helpler method to add the default DSL to visitor in order to + /// Have the ISchemaRetriever initialised by the default visitor. + /// + /// + internal static void AddDefaultDsl(ElasticSearchDSLVisitor visitor) + { + var dsl = new ElasticSearchDSL + { + Query = new Query + { + Bool = new BoolQuery(), + }, + IndexName = "someindex", + }; + visitor.Visit(dsl); + } + + internal static ElasticSearchDSLVisitor CreateDefaultVisitor() + { + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); + AddDefaultDsl(visitor); + return visitor; + } + } +} diff --git a/K2Bridge/JsonConverters/RangeClauseConverter.cs b/K2Bridge/JsonConverters/RangeClauseConverter.cs index 951c4737..e6ab0683 100644 --- a/K2Bridge/JsonConverters/RangeClauseConverter.cs +++ b/K2Bridge/JsonConverters/RangeClauseConverter.cs @@ -27,10 +27,10 @@ public override object ReadJson( var obj = new RangeClause { FieldName = first.Name, - GTEValue = first.First.Value("gte"), - GTValue = first.First.Value("gt"), - LTEValue = first.First.Value("lte"), - LTValue = first.First.Value("lt"), + GTEValue = first.First.Value("gte"), + GTValue = first.First.Value("gt"), + LTEValue = first.First.Value("lte"), + LTValue = first.First.Value("lt"), Format = (string)first.First["format"], }; diff --git a/K2Bridge/Models/Request/Queries/RangeClause.cs b/K2Bridge/Models/Request/Queries/RangeClause.cs index c9696cb9..3a833778 100644 --- a/K2Bridge/Models/Request/Queries/RangeClause.cs +++ b/K2Bridge/Models/Request/Queries/RangeClause.cs @@ -14,14 +14,14 @@ internal class RangeClause : KustoQLBase, ILeafClause, IVisitable { public string FieldName { get; set; } - public decimal? GTEValue { get; set; } + public string GTEValue { get; set; } // isn't created by kibana but kept here for completeness - public decimal? GTValue { get; set; } + public string GTValue { get; set; } - public decimal? LTEValue { get; set; } + public string LTEValue { get; set; } - public decimal? LTValue { get; set; } + public string LTValue { get; set; } public string Format { get; set; } diff --git a/K2Bridge/Visitors/ClauseFieldType.cs b/K2Bridge/Visitors/ClauseFieldType.cs new file mode 100644 index 00000000..d9e3a534 --- /dev/null +++ b/K2Bridge/Visitors/ClauseFieldType.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. +// See LICENSE file in the project root for full license information. + +namespace K2Bridge.Visitors +{ + /// + /// ClauseFieldType. + /// + public enum ClauseFieldType + { + /// + /// Numeric value type + /// + Numeric, + + /// + /// String value type + /// + Text, + + /// + /// Date type + /// + Date, + + /// + /// Unknown type + /// + Unknown, + } +} diff --git a/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs b/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs new file mode 100644 index 00000000..09126ce6 --- /dev/null +++ b/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. +// See LICENSE file in the project root for full license information. + +namespace K2Bridge.Visitors +{ + using System.Threading.Tasks; + using K2Bridge.KustoDAL; + + /// + /// A helper class for processing types. + /// + public static class ClauseFieldTypeProcessor + { + /// + /// Get a clause field type. + /// + /// schemaRetriever. + /// fieldName. + /// A ClauseFieldType. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "await it valid.")] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Resources are not supported yet.")] + public static async Task GetType(ISchemaRetriever schemaRetriever, string fieldName) + { + if (schemaRetriever == null) + { + throw new System.Exception("schemaRetriever cannot be null."); + } + + Ensure.IsNotNullOrEmpty(fieldName, nameof(fieldName)); + + var dic = await schemaRetriever.RetrieveTableSchema(); + + // if we failed to get this field type, treat as Unknown + if (dic.Contains(fieldName) == false) + { + return ClauseFieldType.Unknown; + } + + var fieldType = dic[fieldName]; + switch (fieldType) + { + case "integer": + case "long": + case "float": + case "double": + return ClauseFieldType.Numeric; + case "string": + return ClauseFieldType.Text; + case "date": + return ClauseFieldType.Date; + default: + return ClauseFieldType.Unknown; + } + } + } +} diff --git a/K2Bridge/Visitors/LuceneNet/LuceneRangeVisitor.cs b/K2Bridge/Visitors/LuceneNet/LuceneRangeVisitor.cs index 457379bb..53757995 100644 --- a/K2Bridge/Visitors/LuceneNet/LuceneRangeVisitor.cs +++ b/K2Bridge/Visitors/LuceneNet/LuceneRangeVisitor.cs @@ -23,8 +23,8 @@ public void Visit(LuceneRangeQuery rangeQueryWrapper) var rangeClause = new RangeClause { FieldName = rangeQuery.Field, - GTEValue = decimal.Parse(rangeQuery.LowerTerm, CultureInfo.InvariantCulture), - LTValue = decimal.Parse(rangeQuery.UpperTerm, CultureInfo.InvariantCulture), + GTEValue = rangeQuery.LowerTerm, + LTValue = rangeQuery.UpperTerm, }; rangeQueryWrapper.ESQuery = rangeClause; } diff --git a/K2Bridge/Visitors/QueryStringClauseVisitor.cs b/K2Bridge/Visitors/QueryStringClauseVisitor.cs index 8f063f96..4ed09eeb 100644 --- a/K2Bridge/Visitors/QueryStringClauseVisitor.cs +++ b/K2Bridge/Visitors/QueryStringClauseVisitor.cs @@ -82,30 +82,9 @@ public void Visit(QueryStringClause queryStringClause) private async Task GetIsFieldNumeric(string fieldName) { - // for tests - if (schemaRetriever == null) - { - return false; - } - - var dic = await schemaRetriever.RetrieveTableSchema(); - - // if we failed to get this field type, treat as non numeric (string) - if (dic.Contains(fieldName) == false) - { - return false; - } - - var fieldType = dic[fieldName]; - - return fieldType switch - { - "integer" => true, - "long" => true, - "float" => true, - "double" => true, - _ => false, - }; + Ensure.IsNotNullOrEmpty(fieldName, nameof(fieldName)); + var t = await ClauseFieldTypeProcessor.GetType(schemaRetriever, fieldName); + return t == ClauseFieldType.Numeric; } /// diff --git a/K2Bridge/Visitors/RangeClauseVisitor.cs b/K2Bridge/Visitors/RangeClauseVisitor.cs index 915d41f3..21ecc757 100644 --- a/K2Bridge/Visitors/RangeClauseVisitor.cs +++ b/K2Bridge/Visitors/RangeClauseVisitor.cs @@ -4,6 +4,7 @@ namespace K2Bridge.Visitors { + using System; using K2Bridge.Models.Request.Queries; /// @@ -29,8 +30,19 @@ public void Visit(RangeClause rangeClause) { // general "is between" filter on numeric fields uses a rangeClause query with GTE+LT (not LTE like above) EnsureClause.IsNotNull(rangeClause.LTValue, nameof(rangeClause.LTValue)); - - rangeClause.KustoQL = $"{rangeClause.FieldName} >= {rangeClause.GTEValue} and {rangeClause.FieldName} < {rangeClause.LTValue}"; + var t = ClauseFieldTypeProcessor.GetType(schemaRetriever, rangeClause.FieldName).Result; + switch (t) + { + case ClauseFieldType.Numeric: + case ClauseFieldType.Text: + rangeClause.KustoQL = $"{rangeClause.FieldName} >= {rangeClause.GTEValue} and {rangeClause.FieldName} < {rangeClause.LTValue}"; + break; + case ClauseFieldType.Date: + rangeClause.KustoQL = $"{rangeClause.FieldName} >= todatetime('{rangeClause.GTEValue}') and {rangeClause.FieldName} < todatetime('{rangeClause.LTValue}')"; + break; + case ClauseFieldType.Unknown: + throw new Exception($"Field name {rangeClause.FieldName} has an unknown type."); + } } } } From 163e727efb72a58b2aeddf3c98cc9c1f9ba6a118 Mon Sep 17 00:00:00 2001 From: Itye Date: Tue, 3 Mar 2020 13:13:38 +0200 Subject: [PATCH 2/5] fixed UT --- .../Visitors/LuceneNet/LuceneRangeVisitorTests.cs | 2 +- K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs | 6 +++--- .../Visitors/RangeClauseVisitorTests.cs | 2 +- K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs | 6 +++--- K2Bridge/Visitors/ClauseFieldTypeProcessor.cs | 5 +---- K2Bridge/Visitors/RangeClauseVisitor.cs | 4 +++- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs index 117ce0d9..2d8b2a93 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs @@ -59,7 +59,7 @@ public string Visit_WithValidRangeQuery_ReturnsValidResponse() var es = rangeQuery.ESQuery; Assert.NotNull(es); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("days")); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("days", "long")); VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit((RangeClause)es); diff --git a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs index be4b84bd..4b20785a 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs @@ -310,7 +310,7 @@ public string TestExistsClause(string queryString) TestName = "QueryAccept_WithBetweenRange_ReturnsExpectedResult")] public string TestRangeQueries(string queryString) { - return TestRangeClause(queryString, "TEST_FIELD"); + return TestRangeClause(queryString, "TEST_FIELD", "long"); } [TestCase( @@ -385,10 +385,10 @@ public string TestPrefixQuery(string queryString) return query.KustoQL; } - private string TestRangeClause(string queryString, string field = "MyField") + private string TestRangeClause(string queryString, string field = "MyField", string type = "string") { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(field)); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(field, type)); VisitorTestsUtils.AddDefaultDsl(visitor); query.Accept(visitor); return query.KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs index af667bf7..85eeae8d 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs @@ -112,7 +112,7 @@ private static RangeClause CreateDateRangeClause(string min, string max) private static string RangeClauseToKQL(RangeClause rangeClause) { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("MyField")); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("MyField", "long")); VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit(rangeClause); return rangeClause.KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs index 3cc29052..56ddb5c4 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs @@ -19,7 +19,7 @@ public string TestBasicRangeVisitor() { var rangeClause = CreateRangeClause("myField", "3", null, null, "5", "other"); - return VisitRangeClause(rangeClause, "myField"); + return VisitRangeClause(rangeClause, "myField", "integer"); } [TestCase( @@ -73,9 +73,9 @@ public void Visit_WithEpochMissingLTEInput_ThrowsException() Assert.Throws(typeof(IllegalClauseException), () => VisitRangeClause(rangeClause)); } - private static string VisitRangeClause(RangeClause clause, string fieldName = "MyField") + private static string VisitRangeClause(RangeClause clause, string fieldName = "MyField", string type = "string") { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(fieldName)); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(fieldName, type)); VisitorTestsUtils.AddDefaultDsl(visitor); visitor.Visit(clause); return clause.KustoQL; diff --git a/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs b/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs index 09126ce6..68ec6ca4 100644 --- a/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs +++ b/K2Bridge/Visitors/ClauseFieldTypeProcessor.cs @@ -22,10 +22,7 @@ public static class ClauseFieldTypeProcessor [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Resources are not supported yet.")] public static async Task GetType(ISchemaRetriever schemaRetriever, string fieldName) { - if (schemaRetriever == null) - { - throw new System.Exception("schemaRetriever cannot be null."); - } + Ensure.IsNotNull(schemaRetriever, nameof(schemaRetriever), "schemaRetriever cannot be null."); Ensure.IsNotNullOrEmpty(fieldName, nameof(fieldName)); diff --git a/K2Bridge/Visitors/RangeClauseVisitor.cs b/K2Bridge/Visitors/RangeClauseVisitor.cs index 21ecc757..ec209707 100644 --- a/K2Bridge/Visitors/RangeClauseVisitor.cs +++ b/K2Bridge/Visitors/RangeClauseVisitor.cs @@ -13,6 +13,7 @@ namespace K2Bridge.Visitors internal partial class ElasticSearchDSLVisitor : IVisitor { /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Resource are not available in this version.")] public void Visit(RangeClause rangeClause) { Ensure.IsNotNull(rangeClause, nameof(rangeClause)); @@ -34,12 +35,13 @@ public void Visit(RangeClause rangeClause) switch (t) { case ClauseFieldType.Numeric: - case ClauseFieldType.Text: rangeClause.KustoQL = $"{rangeClause.FieldName} >= {rangeClause.GTEValue} and {rangeClause.FieldName} < {rangeClause.LTValue}"; break; case ClauseFieldType.Date: rangeClause.KustoQL = $"{rangeClause.FieldName} >= todatetime('{rangeClause.GTEValue}') and {rangeClause.FieldName} < todatetime('{rangeClause.LTValue}')"; break; + case ClauseFieldType.Text: + throw new NotSupportedException("Text Range is not supported."); case ClauseFieldType.Unknown: throw new Exception($"Field name {rangeClause.FieldName} has an unknown type."); } From cdb4964d7ae94d1f7d55239de09bff7aa081bc23 Mon Sep 17 00:00:00 2001 From: Itye Date: Tue, 3 Mar 2020 13:35:41 +0200 Subject: [PATCH 3/5] fix --- K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj b/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj index 259ef60c..b8cfafaf 100644 --- a/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj +++ b/K2Bridge.Tests.UnitTests/K2Bridge.Tests.UnitTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -68,7 +68,4 @@ PreserveNewest - - - From 7a44d5bd0b6822aa9f68a29562a8a686dc23f7fa Mon Sep 17 00:00:00 2001 From: Itye Date: Tue, 3 Mar 2020 13:42:02 +0200 Subject: [PATCH 4/5] refactor --- .../LuceneNet/LucenePhraseVisitorTests.cs | 2 +- .../LuceneNet/LuceneRangeVisitorTests.cs | 2 +- .../Visitors/LuceneNet/LuceneTermVisitorTests.cs | 2 +- .../Visitors/ParseElasticToKqlTests.cs | 16 ++++++++-------- .../Visitors/QueryStringClauseVisitorTests.cs | 2 +- .../Visitors/RangeClauseVisitorTests.cs | 4 ++-- .../Visitors/RangeVisitorTests.cs | 2 +- .../Visitors/VisitorTestsUtils.cs | 6 +++--- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs index 9a6b1cb3..73face58 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs @@ -78,7 +78,7 @@ public string Visit_WithValidTermQuery_ReturnsValidReponse() var es = phraseQuery.ESQuery; Assert.NotNull(es); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs index 2d8b2a93..f0824a9f 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs @@ -60,7 +60,7 @@ public string Visit_WithValidRangeQuery_ReturnsValidResponse() Assert.NotNull(es); var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("days", "long")); - VisitorTestsUtils.AddDefaultDsl(visitor); + VisitorTestsUtils.AddRootDsl(visitor); visitor.Visit((RangeClause)es); return ((RangeClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs index 9820c77d..540859a4 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs @@ -54,7 +54,7 @@ public string Visit_WithValidWildcardTermQuery_ReturnsValidResponse() var es = termQuery.ESQuery; Assert.NotNull(es); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs index 4b20785a..45a37cc7 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs @@ -283,7 +283,7 @@ public class ParseElasticToKqlTests public string TestMatchPhraseQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -295,7 +295,7 @@ public string TestMatchPhraseQueries(string queryString) public string TestExistsClause(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -328,7 +328,7 @@ public void TestRangeQueriesMissingValues(string queryString) public string TestQueryStringQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -344,7 +344,7 @@ public string TestQueryStringQueries(string queryString) public string TestCombinedQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -356,7 +356,7 @@ public string TestCombinedQueries(string queryString) public string TestWildcardQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -368,7 +368,7 @@ public string TestWildcardQuery(string queryString) public string TestComplexWildcardQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -380,7 +380,7 @@ public string TestComplexWildcardQuery(string queryString) public string TestPrefixQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -389,7 +389,7 @@ private string TestRangeClause(string queryString, string field = "MyField", str { var query = JsonConvert.DeserializeObject(queryString); var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(field, type)); - VisitorTestsUtils.AddDefaultDsl(visitor); + VisitorTestsUtils.AddRootDsl(visitor); query.Accept(visitor); return query.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs index da36a237..5697ff2c 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs @@ -94,7 +94,7 @@ public string Visit_WithEmptyQuotePhrase_ReturnsAndContainsResponse() private static string VisitQuery(QueryStringClause queryStringClause) { - var visitor = VisitorTestsUtils.CreateDefaultVisitor(); + var visitor = VisitorTestsUtils.CreateRootVisitor(); visitor.Visit(queryStringClause); return queryStringClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs index 85eeae8d..9234e681 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs @@ -113,7 +113,7 @@ private static RangeClause CreateDateRangeClause(string min, string max) private static string RangeClauseToKQL(RangeClause rangeClause) { var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("MyField", "long")); - VisitorTestsUtils.AddDefaultDsl(visitor); + VisitorTestsUtils.AddRootDsl(visitor); visitor.Visit(rangeClause); return rangeClause.KustoQL; } @@ -121,7 +121,7 @@ private static string RangeClauseToKQL(RangeClause rangeClause) private static string DateRangeClauseToKQL(RangeClause rangeClause) { var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockDateSchemaRetriever()); - VisitorTestsUtils.AddDefaultDsl(visitor); + VisitorTestsUtils.AddRootDsl(visitor); visitor.Visit(rangeClause); return rangeClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs index 56ddb5c4..8def96a8 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs @@ -76,7 +76,7 @@ public void Visit_WithEpochMissingLTEInput_ThrowsException() private static string VisitRangeClause(RangeClause clause, string fieldName = "MyField", string type = "string") { var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(fieldName, type)); - VisitorTestsUtils.AddDefaultDsl(visitor); + VisitorTestsUtils.AddRootDsl(visitor); visitor.Visit(clause); return clause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs index c136c569..9fdaef5a 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs @@ -16,7 +16,7 @@ public static class VisitorTestsUtils /// Have the ISchemaRetriever initialised by the default visitor. /// /// - internal static void AddDefaultDsl(ElasticSearchDSLVisitor visitor) + internal static void AddRootDsl(ElasticSearchDSLVisitor visitor) { var dsl = new ElasticSearchDSL { @@ -29,10 +29,10 @@ internal static void AddDefaultDsl(ElasticSearchDSLVisitor visitor) visitor.Visit(dsl); } - internal static ElasticSearchDSLVisitor CreateDefaultVisitor() + internal static ElasticSearchDSLVisitor CreateRootVisitor() { var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); - AddDefaultDsl(visitor); + AddRootDsl(visitor); return visitor; } } From 487761793294f7e69437c577691580289e66957c Mon Sep 17 00:00:00 2001 From: Itye Date: Thu, 5 Mar 2020 12:05:08 +0200 Subject: [PATCH 5/5] fixes --- .../LuceneNet/LucenePhraseVisitorTests.cs | 4 ++-- .../LuceneNet/LuceneRangeVisitorTests.cs | 3 +-- .../LuceneNet/LuceneTermVisitorTests.cs | 2 +- .../Visitors/ParseElasticToKqlTests.cs | 21 +++++++++---------- .../Visitors/QueryStringClauseVisitorTests.cs | 2 +- .../Visitors/RangeClauseVisitorTests.cs | 5 ++--- .../Visitors/RangeVisitorTests.cs | 3 +-- .../Visitors/VisitorTestsUtils.cs | 8 +++---- K2Bridge/Visitors/QueryStringClauseVisitor.cs | 4 ++-- 9 files changed, 24 insertions(+), 28 deletions(-) diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs index 73face58..130fad9e 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LucenePhraseVisitorTests.cs @@ -56,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; @@ -78,7 +78,7 @@ public string Visit_WithValidTermQuery_ReturnsValidReponse() var es = phraseQuery.ESQuery; Assert.NotNull(es); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs index f0824a9f..2aebf289 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneRangeVisitorTests.cs @@ -59,8 +59,7 @@ public string Visit_WithValidRangeQuery_ReturnsValidResponse() var es = rangeQuery.ESQuery; Assert.NotNull(es); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("days", "long")); - VisitorTestsUtils.AddRootDsl(visitor); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor("days", "long"); visitor.Visit((RangeClause)es); return ((RangeClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs index 540859a4..f5c47896 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/LuceneNet/LuceneTermVisitorTests.cs @@ -54,7 +54,7 @@ public string Visit_WithValidWildcardTermQuery_ReturnsValidResponse() var es = termQuery.ESQuery; Assert.NotNull(es); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); visitor.Visit((QueryStringClause)es); return ((QueryStringClause)es).KustoQL; diff --git a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs index 45a37cc7..865fcc86 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/ParseElasticToKqlTests.cs @@ -283,7 +283,7 @@ public class ParseElasticToKqlTests public string TestMatchPhraseQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -295,7 +295,7 @@ public string TestMatchPhraseQueries(string queryString) public string TestExistsClause(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -328,7 +328,7 @@ public void TestRangeQueriesMissingValues(string queryString) public string TestQueryStringQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -344,7 +344,7 @@ public string TestQueryStringQueries(string queryString) public string TestCombinedQueries(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -352,11 +352,11 @@ public string TestCombinedQueries(string queryString) [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(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -364,11 +364,11 @@ public string TestWildcardQuery(string queryString) [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(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -380,7 +380,7 @@ public string TestComplexWildcardQuery(string queryString) public string TestPrefixQuery(string queryString) { var query = JsonConvert.DeserializeObject(queryString); - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); query.Accept(visitor); return query.KustoQL; } @@ -388,8 +388,7 @@ public string TestPrefixQuery(string queryString) private string TestRangeClause(string queryString, string field = "MyField", string type = "string") { var query = JsonConvert.DeserializeObject(queryString); - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(field, type)); - VisitorTestsUtils.AddRootDsl(visitor); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(field, type); query.Accept(visitor); return query.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs index 5697ff2c..186fa41b 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/QueryStringClauseVisitorTests.cs @@ -94,7 +94,7 @@ public string Visit_WithEmptyQuotePhrase_ReturnsAndContainsResponse() private static string VisitQuery(QueryStringClause queryStringClause) { - var visitor = VisitorTestsUtils.CreateRootVisitor(); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(); visitor.Visit(queryStringClause); return queryStringClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs index 9234e681..960f3f10 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeClauseVisitorTests.cs @@ -112,8 +112,7 @@ private static RangeClause CreateDateRangeClause(string min, string max) private static string RangeClauseToKQL(RangeClause rangeClause) { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever("MyField", "long")); - VisitorTestsUtils.AddRootDsl(visitor); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor("MyField", "long"); visitor.Visit(rangeClause); return rangeClause.KustoQL; } @@ -121,7 +120,7 @@ private static string RangeClauseToKQL(RangeClause rangeClause) private static string DateRangeClauseToKQL(RangeClause rangeClause) { var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockDateSchemaRetriever()); - VisitorTestsUtils.AddRootDsl(visitor); + VisitorTestsUtils.VisitRootDsl(visitor); visitor.Visit(rangeClause); return rangeClause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs index 8def96a8..3d6d7f49 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/RangeVisitorTests.cs @@ -75,8 +75,7 @@ public void Visit_WithEpochMissingLTEInput_ThrowsException() private static string VisitRangeClause(RangeClause clause, string fieldName = "MyField", string type = "string") { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(fieldName, type)); - VisitorTestsUtils.AddRootDsl(visitor); + var visitor = VisitorTestsUtils.CreateAndVisitRootVisitor(fieldName, type); visitor.Visit(clause); return clause.KustoQL; } diff --git a/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs index 9fdaef5a..8ddd57a0 100644 --- a/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs +++ b/K2Bridge.Tests.UnitTests/Visitors/VisitorTestsUtils.cs @@ -16,7 +16,7 @@ public static class VisitorTestsUtils /// Have the ISchemaRetriever initialised by the default visitor. /// /// - internal static void AddRootDsl(ElasticSearchDSLVisitor visitor) + internal static void VisitRootDsl(ElasticSearchDSLVisitor visitor) { var dsl = new ElasticSearchDSL { @@ -29,10 +29,10 @@ internal static void AddRootDsl(ElasticSearchDSLVisitor visitor) visitor.Visit(dsl); } - internal static ElasticSearchDSLVisitor CreateRootVisitor() + internal static ElasticSearchDSLVisitor CreateAndVisitRootVisitor(string name = "dayOfWeek", string type = "string") { - var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); - AddRootDsl(visitor); + var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever(name, type)); + VisitRootDsl(visitor); return visitor; } } diff --git a/K2Bridge/Visitors/QueryStringClauseVisitor.cs b/K2Bridge/Visitors/QueryStringClauseVisitor.cs index 7a126b0a..cf51aec2 100644 --- a/K2Bridge/Visitors/QueryStringClauseVisitor.cs +++ b/K2Bridge/Visitors/QueryStringClauseVisitor.cs @@ -92,8 +92,8 @@ public void Visit(QueryStringClause queryStringClause) private async Task GetIsFieldNumeric(string fieldName) { Ensure.IsNotNullOrEmpty(fieldName, nameof(fieldName)); - var t = await ClauseFieldTypeProcessor.GetType(schemaRetriever, fieldName); - return t == ClauseFieldType.Numeric; + var fieldType = await ClauseFieldTypeProcessor.GetType(schemaRetriever, fieldName); + return fieldType == ClauseFieldType.Numeric; } ///