mirror of https://github.com/Squidex/squidex.git
4 changed files with 146 additions and 6 deletions
@ -0,0 +1,50 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Domain.Apps.Core.Tags; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Queries; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets.Queries |
|||
{ |
|||
public sealed class QueryTagVisitor : TransformVisitor |
|||
{ |
|||
private readonly ITagService tagService; |
|||
private readonly Guid appId; |
|||
|
|||
private QueryTagVisitor(Guid appId, ITagService tagService) |
|||
{ |
|||
this.appId = appId; |
|||
|
|||
this.tagService = tagService; |
|||
} |
|||
|
|||
public static FilterNode Transform(FilterNode nodeIn, Guid appId, ITagService tagService) |
|||
{ |
|||
Guard.NotNull(tagService, nameof(tagService)); |
|||
|
|||
return nodeIn.Accept(new QueryTagVisitor(appId, tagService)); |
|||
} |
|||
|
|||
public override FilterNode Visit(FilterComparison nodeIn) |
|||
{ |
|||
if (string.Equals(nodeIn.Path[0], nameof(IAssetEntity.Tags), StringComparison.OrdinalIgnoreCase) && nodeIn.Value is string stringValue) |
|||
{ |
|||
var tagNames = Task.Run(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, HashSet.Of(stringValue))).Result; |
|||
|
|||
if (tagNames.TryGetValue(stringValue, out var normalized)) |
|||
{ |
|||
return new FilterComparison(nodeIn.Path, nodeIn.Operator, normalized, FilterValueType.String); |
|||
} |
|||
} |
|||
|
|||
return nodeIn; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using FakeItEasy; |
|||
using Squidex.Domain.Apps.Core.Tags; |
|||
using Squidex.Infrastructure.Queries; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets.Queries |
|||
{ |
|||
public class QueryTagVisitorTests |
|||
{ |
|||
private readonly ITagService tagService = A.Fake<ITagService>(); |
|||
private readonly Guid appId = Guid.NewGuid(); |
|||
|
|||
[Fact] |
|||
public void Should_normalize_tags() |
|||
{ |
|||
A.CallTo(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, A<HashSet<string>>.That.Contains("tag1"))) |
|||
.Returns(new Dictionary<string, string> { ["tag1"] = "normalized1" }); |
|||
|
|||
var source = FilterBuilder.Eq("tags", "tag1"); |
|||
var result = QueryTagVisitor.Transform(source, appId, tagService); |
|||
|
|||
Assert.Equal("tags == normalized1", result.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_not_not_fail_when_not_found() |
|||
{ |
|||
A.CallTo(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, A<HashSet<string>>.That.Contains("tag1"))) |
|||
.Returns(new Dictionary<string, string>()); |
|||
|
|||
var source = FilterBuilder.Eq("tags", "tag1"); |
|||
var result = QueryTagVisitor.Transform(source, appId, tagService); |
|||
|
|||
Assert.Equal("tags == tag1", result.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_not_normalize_other_field() |
|||
{ |
|||
var source = FilterBuilder.Eq("other", "value"); |
|||
var result = QueryTagVisitor.Transform(source, appId, tagService); |
|||
|
|||
Assert.Equal("other == value", result.ToString()); |
|||
|
|||
A.CallTo(() => tagService.GetTagIdsAsync(appId, A<string>.Ignored, A<HashSet<string>>.Ignored)) |
|||
.MustNotHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue