From aa26a932845816745dc6f72a8b72626d3a0e4144 Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Wed, 19 Jul 2023 11:38:05 +0200 Subject: [PATCH] add some tests + refactoring --- .../LuceneQueryParser.cs | 131 ++++++++++-------- .../LuceneQueryParserTests.cs | 13 ++ .../ActionSonarCloudV1MapperTests.cs | 98 +++++++++++++ 3 files changed, 184 insertions(+), 58 deletions(-) create mode 100644 tests/RepoM.Plugin.SonarCloud.Tests/ActionSonarCloudV1MapperTests.cs diff --git a/src/RepoM.Plugin.LuceneQueryParser/LuceneQueryParser.cs b/src/RepoM.Plugin.LuceneQueryParser/LuceneQueryParser.cs index bc970a19..7f535100 100644 --- a/src/RepoM.Plugin.LuceneQueryParser/LuceneQueryParser.cs +++ b/src/RepoM.Plugin.LuceneQueryParser/LuceneQueryParser.cs @@ -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) { @@ -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) { @@ -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(); + 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(); - 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); } } \ No newline at end of file diff --git a/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserTests.cs b/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserTests.cs index 3ba6f30e..d81cd977 100644 --- a/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserTests.cs +++ b/tests/RepoM.Plugin.LuceneQueryParser.Tests/LuceneQueryParserTests.cs @@ -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; @@ -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 ")] diff --git a/tests/RepoM.Plugin.SonarCloud.Tests/ActionSonarCloudV1MapperTests.cs b/tests/RepoM.Plugin.SonarCloud.Tests/ActionSonarCloudV1MapperTests.cs new file mode 100644 index 00000000..79aed73c --- /dev/null +++ b/tests/RepoM.Plugin.SonarCloud.Tests/ActionSonarCloudV1MapperTests.cs @@ -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(); + _expressionEvaluator = A.Fake(); + _translationService = A.Fake(); + _sut = new ActionSonarCloudV1Mapper(_service, _expressionEvaluator, _translationService); + } + + [Fact] + public void Ctor_ShouldThrow_WhenArgumentNull() + { + // arrange + ISonarCloudFavoriteService service = A.Fake(); + IRepositoryExpressionEvaluator expressionEvaluator = A.Fake(); + ITranslationService translationService = A.Fake(); + + // act + Func act1 = () => new ActionSonarCloudV1Mapper(service, expressionEvaluator, null!); + Func act2 = () => new ActionSonarCloudV1Mapper(service, null!, translationService); + Func act3 = () => new ActionSonarCloudV1Mapper(null!, expressionEvaluator, translationService); + + // assert + act1.Should().Throw(); + act2.Should().Throw(); + act3.Should().Throw(); + } + + [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(); + } +} \ No newline at end of file