Skip to content

Commit

Permalink
add some tests + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Jul 19, 2023
1 parent daeedc1 commit aa26a93
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 58 deletions.
131 changes: 73 additions & 58 deletions src/RepoM.Plugin.LuceneQueryParser/LuceneQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public IQuery Parse(string text)
try
{
var result = (SetQuery)_queryParser.Parse(text);
return MapQuery(result);
return ConvertSetQuery(result);
}
catch (ParseException e)
{
Expand All @@ -52,12 +52,12 @@ public IQuery Parse(string text)
}
}

private IQuery MapQuery(SetQuery result)
private static IQuery ConvertSetQuery(SetQuery result)
{
return ConvertWrappedBooleanClause(result.SetBooleanClause);
}

private IQuery ConvertWrappedBooleanClause(WrappedBooleanClause input)
private static IQuery ConvertWrappedBooleanClause(WrappedBooleanClause input)
{
if (input is NotBooleanClause nbc)
{
Expand All @@ -81,79 +81,94 @@ private IQuery ConvertWrappedBooleanClause(WrappedBooleanClause input)
return ConvertQueryToClause(input.Query);
}

private IQuery ConvertQueryToClause(Query query)
private static IQuery ConvertQueryToClause(Query query)
{
if (query is TermQuery tq)
switch (query)
{
if (KEY_FREE_TEXT.Equals(tq.Term.Field, StringComparison.CurrentCulture))
{
return new FreeText(tq.Term.Text);
}

return new SimpleTerm(tq.Term.Field, tq.Term.Text);
case TermQuery tq:
return ConvertTermQuery(tq);
case TermRangeQuery trq:
return ConvertTermRangeQuery(trq);
case WildcardQuery wq:
return ConvertWildcardQuery(wq);
case FuzzyQuery fq:
return ConvertFuzzyQuery(fq);
case SetQuery cq:
return ConvertSetQuery(cq);
case PrefixQuery pq:
return ConvertPrefixQuery(pq);
case PhraseQuery phraseQuery:
return ConvertPhraseQuery(phraseQuery);
default:
{
var fullName = query.GetType().FullName;
throw new NotImplementedException(fullName);
}
}
}

if (query is TermRangeQuery trq)
{
return new TermRange(
trq.Field,
trq.LowerTerm == null ? null : Term.ToString(trq.LowerTerm),
trq.IncludesLower,
trq.UpperTerm == null ? null : Term.ToString(trq.UpperTerm),
trq.IncludesUpper);
}
private static IQuery ConvertPhraseQuery(PhraseQuery phraseQuery)
{
var queryTerms = new HashSet<Term>();
phraseQuery.ExtractTerms(queryTerms);

if (query is WildcardQuery wq)
if (queryTerms.Count < 1)
{
return new WildCardTerm(wq.Term.Field, wq.Term.Text);
throw new NotSupportedException();
}

if (query is FuzzyQuery fq)
{
// do not support fuzzy query,
if (KEY_FREE_TEXT.Equals(fq.Term.Field, StringComparison.CurrentCulture))
{
return new FreeText(fq.Term.Text);
}

return new SimpleTerm(fq.Term.Field, fq.Term.Text);
}
// assume all same field
var field = queryTerms.First().Field;
var s = queryTerms.Aggregate(string.Empty, (current, qt) => current + " " + qt.Text).Trim();

if (query is SetQuery cq)
{
return MapQuery(cq);
}

if (query is PrefixQuery pq)
if (KEY_FREE_TEXT.Equals(field, StringComparison.CurrentCulture))
{
return new StartsWithTerm(pq.Field, pq.Prefix.Text);
return new FreeText(s);
}

if (query is PhraseQuery phraseQuery)
return new SimpleTerm(field, s);

// not supported
}

private static IQuery ConvertPrefixQuery(PrefixQuery pq)
{
return new StartsWithTerm(pq.Field, pq.Prefix.Text);
}

private static IQuery ConvertFuzzyQuery(FuzzyQuery fq)
{
// do not support fuzzy query,
if (KEY_FREE_TEXT.Equals(fq.Term.Field, StringComparison.CurrentCulture))
{
var queryTerms = new HashSet<Term>();
phraseQuery.ExtractTerms(queryTerms);
return new FreeText(fq.Term.Text);
}

if (queryTerms.Count < 1)
{
throw new NotSupportedException();
}
return new SimpleTerm(fq.Term.Field, fq.Term.Text);
}

// assume all same field
var field = queryTerms.First().Field;
var s = queryTerms.Aggregate(string.Empty, (current, qt) => current + " " + qt.Text).Trim();
private static IQuery ConvertWildcardQuery(WildcardQuery wq)
{
return new WildCardTerm(wq.Term.Field, wq.Term.Text);
}

if (KEY_FREE_TEXT.Equals(field, StringComparison.CurrentCulture))
{
return new FreeText(s);
}

return new SimpleTerm(field, s);
private static IQuery ConvertTermRangeQuery(TermRangeQuery trq)
{
return new TermRange(
trq.Field,
trq.LowerTerm == null ? null : Term.ToString(trq.LowerTerm),
trq.IncludesLower,
trq.UpperTerm == null ? null : Term.ToString(trq.UpperTerm),
trq.IncludesUpper);
}

// not supported
private static IQuery ConvertTermQuery(TermQuery tq)
{
if (KEY_FREE_TEXT.Equals(tq.Term.Field, StringComparison.CurrentCulture))
{
return new FreeText(tq.Term.Text);
}

var fullName = query.GetType().FullName;
throw new NotImplementedException(fullName);
return new SimpleTerm(tq.Term.Field, tq.Term.Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace RepoM.Plugin.LuceneQueryParser.Tests;

using System.Threading.Tasks;
using Argon;
using FluentAssertions;
using RepoM.Core.Plugin.RepositoryFiltering.Clause;
using RepoM.Plugin.LuceneQueryParser;
using VerifyTests;
Expand Down Expand Up @@ -29,6 +30,18 @@ public LuceneQueryParserTests()
_settings.DisableRequireUniquePrefix();
}

[Fact]
public void Name_ShouldReturnLucene()
{
// arrange

// act
var result = _sut.Name;

// assert
result.Should().Be("Lucene");
}

[Theory]
[InlineData("tag-only", "tag:abc")]
[InlineData("tag-only", " tag:abc ")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace RepoM.Plugin.SonarCloud.Tests;

using RepoM.Api.IO;
using System;
using System.ComponentModel.DataAnnotations;
using FakeItEasy;
using FluentAssertions;
using RepoM.Api.Common;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Core.Plugin.RepositoryFinder;
using Xunit;
using RepoM.Core.Plugin.Expressions;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;

public class ActionSonarCloudV1MapperTests
{
private readonly ISonarCloudFavoriteService _service;
private readonly IRepositoryExpressionEvaluator _expressionEvaluator;
private readonly ITranslationService _translationService;
private readonly ActionSonarCloudV1Mapper _sut;

public ActionSonarCloudV1MapperTests()
{
_service = A.Fake<ISonarCloudFavoriteService>();
_expressionEvaluator = A.Fake<IRepositoryExpressionEvaluator>();
_translationService = A.Fake<ITranslationService>();
_sut = new ActionSonarCloudV1Mapper(_service, _expressionEvaluator, _translationService);
}

[Fact]
public void Ctor_ShouldThrow_WhenArgumentNull()
{
// arrange
ISonarCloudFavoriteService service = A.Fake<ISonarCloudFavoriteService>();
IRepositoryExpressionEvaluator expressionEvaluator = A.Fake<IRepositoryExpressionEvaluator>();
ITranslationService translationService = A.Fake<ITranslationService>();

// act
Func<ActionSonarCloudV1Mapper> act1 = () => new ActionSonarCloudV1Mapper(service, expressionEvaluator, null!);
Func<ActionSonarCloudV1Mapper> act2 = () => new ActionSonarCloudV1Mapper(service, null!, translationService);
Func<ActionSonarCloudV1Mapper> act3 = () => new ActionSonarCloudV1Mapper(null!, expressionEvaluator, translationService);

// assert
act1.Should().Throw<ArgumentNullException>();
act2.Should().Throw<ArgumentNullException>();
act3.Should().Throw<ArgumentNullException>();
}

[Fact]
public void CanHandleMultipleRepositories_ShouldReturnFalse()
{
// arrange

// act
var result = _sut.CanHandleMultipleRepositories();

// assert
result.Should().BeFalse();
}

[Fact]
public void CanMap_ShouldReturnFalse_WhenWrongType()
{
// arrange

// act
var result = ((IActionToRepositoryActionMapper)_sut).CanMap(new RepositoryAction());

// assert
result.Should().BeFalse();
}

[Fact]
public void CanMap_ShouldReturnFalse_WhenNull()
{
// arrange
RepositoryActionSonarCloudSetFavoriteV1 action = null!;

// act
var result = ((IActionToRepositoryActionMapper)_sut).CanMap(action);

// assert
result.Should().BeFalse();
}

[Fact]
public void CanMap_ShouldReturnTrue_WhenRepositoryActionSonarCloudSetFavoriteV1()
{
// arrange
var action = new RepositoryActionSonarCloudSetFavoriteV1();

// act
var result = ((IActionToRepositoryActionMapper)_sut).CanMap(action);

// assert
result.Should().BeTrue();
}
}

0 comments on commit aa26a93

Please sign in to comment.