Skip to content

Commit

Permalink
#76 Fixed default operator not being applied to right hand side of gr…
Browse files Browse the repository at this point in the history
…oup node in GenerateQueryVisitor (#77)

Co-authored-by: Richard Cockerill <5318261+richardcockerill@users.noreply.github.com>
  • Loading branch information
richardcockerill and richardcockerill authored Oct 12, 2023
1 parent 56e348b commit 3df3d55
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Foundatio.Parsers.LuceneQueries/Nodes/GroupNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public string ToString(GroupOperator defaultOperator) {
builder.Append("(");

if (Left != null)
builder.Append(Left);
builder.Append(Left is GroupNode ? ((GroupNode) Left).ToString(defaultOperator) : Left.ToString());

if (Left != null && Right != null) {
if (op == GroupOperator.And)
Expand All @@ -103,7 +103,7 @@ public string ToString(GroupOperator defaultOperator) {
}

if (Right != null)
builder.Append(Right);
builder.Append(Right is GroupNode ? ((GroupNode)Right).ToString(defaultOperator) : Right.ToString());

if (HasParens)
builder.Append(")");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using Foundatio.Parsers.LuceneQueries.Visitors;
using Xunit;
using Xunit.Abstractions;
using System.Threading.Tasks;
using Foundatio.Parsers.LuceneQueries.Nodes;
using Foundatio.Xunit;
using Pegasus.Common.Tracing;
using Microsoft.Extensions.Logging;

namespace Foundatio.Parsers.LuceneQueries.Tests;

public class GenerateQueryVisitorTests : TestWithLoggingBase {
public GenerateQueryVisitorTests(ITestOutputHelper output) : base(output) {
Log.MinimumLevel = LogLevel.Trace;
}

[Theory]
[InlineData("value1 value2", GroupOperator.Default, "value1 value2")]
[InlineData("value1 value2", GroupOperator.And, "value1 AND value2")]
[InlineData("value1 value2", GroupOperator.Or, "value1 OR value2")]
[InlineData("value1 value2 value3", GroupOperator.Default, "value1 value2 value3")]
[InlineData("value1 value2 value3", GroupOperator.And, "value1 AND value2 AND value3")]
[InlineData("value1 value2 value3", GroupOperator.Or, "value1 OR value2 OR value3")]
[InlineData("value1 value2 value3 value4", GroupOperator.And, "value1 AND value2 AND value3 AND value4")]
[InlineData("(value1 value2) OR (value3 value4)", GroupOperator.And, "(value1 AND value2) OR (value3 AND value4)")]
public Task DefaultOperatorApplied(string query, GroupOperator groupOperator, string expected) {
return GenerateQuery(query, groupOperator, expected);
}

private async Task GenerateQuery(string query, GroupOperator defaultOperator, string expected) {
#if ENABLE_TRACING
var tracer = new LoggingTracer(_logger, reportPerformance: true);
#else
var tracer = NullTracer.Instance;
#endif
var parser = new LuceneQueryParser {
Tracer = tracer
};

IQueryNode parsedQuery = await parser.ParseAsync(query);

var context = new QueryVisitorContext { DefaultOperator = defaultOperator };
string result = GenerateQueryVisitor.Run(parsedQuery, context);
string nodes = await DebugQueryVisitor.RunAsync(parsedQuery);
_logger.LogInformation(nodes);
Assert.Equal(expected, result);
}
}

0 comments on commit 3df3d55

Please sign in to comment.