Browse Source

More unit tests.

pull/315/head
Sebastian Stehle 7 years ago
parent
commit
9d3a778960
  1. 8
      src/Squidex.Domain.Apps.Entities/Assets/AssetQueryService.cs
  2. 50
      src/Squidex.Domain.Apps.Entities/Assets/Queries/QueryTagVisitor.cs
  3. 36
      tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetQueryServiceTests.cs
  4. 58
      tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/QueryTagVisitorTests.cs

8
src/Squidex.Domain.Apps.Entities/Assets/AssetQueryService.cs

@ -12,6 +12,7 @@ using System.Threading.Tasks;
using Microsoft.OData;
using Squidex.Domain.Apps.Core.Tags;
using Squidex.Domain.Apps.Entities.Assets.Edm;
using Squidex.Domain.Apps.Entities.Assets.Queries;
using Squidex.Domain.Apps.Entities.Assets.Repositories;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Queries;
@ -21,7 +22,7 @@ namespace Squidex.Domain.Apps.Entities.Assets
{
public sealed class AssetQueryService : IAssetQueryService
{
private const int MaxResults = 20;
private const int MaxResults = 200;
private readonly ITagService tagService;
private readonly IAssetRepository assetRepository;
@ -86,6 +87,11 @@ namespace Squidex.Domain.Apps.Entities.Assets
{
var result = EdmAssetModel.Edm.ParseQuery(query).ToQuery();
if (result.Filter != null)
{
result.Filter = QueryTagVisitor.Transform(result.Filter, context.App.Id, tagService);
}
if (result.Sort.Count == 0)
{
result.Sort.Add(new SortNode(new List<string> { "lastModified" }, SortOrder.Descending));

50
src/Squidex.Domain.Apps.Entities/Assets/Queries/QueryTagVisitor.cs

@ -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;
}
}
}

36
tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetQueryServiceTests.cs

@ -15,6 +15,7 @@ using Squidex.Domain.Apps.Core.Tags;
using Squidex.Domain.Apps.Entities.Apps;
using Squidex.Domain.Apps.Entities.Assets.Repositories;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Queries;
using Xunit;
namespace Squidex.Domain.Apps.Entities.Assets
@ -87,24 +88,49 @@ namespace Squidex.Domain.Apps.Entities.Assets
Assert.Empty(result[1].Tags);
}
/*
* TODO
[Fact]
public async Task Should_load_assets_with_query_and_resolve_tags()
{
A.CallTo(() => assetRepository.QueryAsync(appId, "my-query"))
A.CallTo(() => assetRepository.QueryAsync(appId, A<Query>.Ignored))
.Returns(ResultList.Create(8,
CreateAsset(Guid.NewGuid(), "id1", "id2"),
CreateAsset(Guid.NewGuid(), "id2", "id3")));
var result = await sut.QueryAsync(context, Q.Empty.WithODataQuery("my-query"));
var result = await sut.QueryAsync(context, Q.Empty);
Assert.Equal(8, result.Total);
Assert.Equal(2, result.Count);
Assert.Equal(HashSet.Of("name1", "name2"), result[0].Tags);
Assert.Equal(HashSet.Of("name2", "name3"), result[1].Tags);
} */
}
[Fact]
public async Task Should_transform_odata_query()
{
await sut.QueryAsync(context, Q.Empty.WithODataQuery("$top=100&$orderby=fileName asc&$search=Hello World"));
A.CallTo(() => assetRepository.QueryAsync(appId, A<Query>.That.Matches(x => x.ToString() == "FullText: Hello World; Take: 100; Sort: fileName Ascending")))
.MustHaveHappened();
}
[Fact]
public async Task Should_transform_odata_query_and_enrich_with_defaults()
{
await sut.QueryAsync(context, Q.Empty.WithODataQuery("$filter=fileName eq 'ABC'"));
A.CallTo(() => assetRepository.QueryAsync(appId, A<Query>.That.Matches(x => x.ToString() == "Filter: fileName == ABC; Take: 200; Sort: lastModified Descending")))
.MustHaveHappened();
}
[Fact]
public async Task Should_limit_number_of_assets()
{
await sut.QueryAsync(context, Q.Empty.WithODataQuery("$top=300&$skip=20"));
A.CallTo(() => assetRepository.QueryAsync(appId, A<Query>.That.Matches(x => x.ToString() == "Skip: 20; Take: 200; Sort: lastModified Descending")))
.MustHaveHappened();
}
private IAssetEntity CreateAsset(Guid id, params string[] tags)
{

58
tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/QueryTagVisitorTests.cs

@ -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…
Cancel
Save