mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
5.5 KiB
152 lines
5.5 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FakeItEasy;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Domain.Apps.Entities.Schemas;
|
|
using Squidex.Domain.Apps.Entities.TestHelpers;
|
|
using Squidex.Infrastructure;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Contents.Queries
|
|
{
|
|
public class ContentEnricherTests
|
|
{
|
|
private readonly IAppProvider appProvider = A.Fake<IAppProvider>();
|
|
private readonly ISchemaEntity schema;
|
|
private readonly Context requestContext;
|
|
private readonly NamedId<DomainId> appId = NamedId.Of(DomainId.NewGuid(), "my-app");
|
|
private readonly NamedId<DomainId> schemaId = NamedId.Of(DomainId.NewGuid(), "my-schema");
|
|
|
|
private sealed class ResolveSchema : IContentEnricherStep
|
|
{
|
|
public ISchemaEntity Schema { get; private set; }
|
|
|
|
public async Task EnrichAsync(Context context, IEnumerable<ContentEntity> contents, ProvideSchema schemas,
|
|
CancellationToken ct)
|
|
{
|
|
foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
|
|
{
|
|
Schema = (await schemas(group.Key)).Schema;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ContentEnricherTests()
|
|
{
|
|
requestContext = new Context(Mocks.ApiUser(), Mocks.App(appId));
|
|
|
|
schema = Mocks.Schema(appId, schemaId);
|
|
|
|
A.CallTo(() => appProvider.GetSchemaAsync(appId.Id, schemaId.Id, false))
|
|
.Returns(schema);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_only_invoke_pre_enrich_for_empty_results()
|
|
{
|
|
var source = Array.Empty<IContentEntity>();
|
|
|
|
var step1 = A.Fake<IContentEnricherStep>();
|
|
var step2 = A.Fake<IContentEnricherStep>();
|
|
|
|
var sut = new ContentEnricher(new[] { step1, step2 }, appProvider);
|
|
|
|
await sut.EnrichAsync(source, requestContext, default);
|
|
|
|
A.CallTo(() => step1.EnrichAsync(requestContext, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
|
|
A.CallTo(() => step2.EnrichAsync(requestContext, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
|
|
A.CallTo(() => step1.EnrichAsync(requestContext, A<IEnumerable<ContentEntity>>._, A<ProvideSchema>._, A<CancellationToken>._))
|
|
.MustNotHaveHappened();
|
|
|
|
A.CallTo(() => step2.EnrichAsync(requestContext, A<IEnumerable<ContentEntity>>._, A<ProvideSchema>._, A<CancellationToken>._))
|
|
.MustNotHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_invoke_steps()
|
|
{
|
|
var source = CreateContent();
|
|
|
|
var step1 = A.Fake<IContentEnricherStep>();
|
|
var step2 = A.Fake<IContentEnricherStep>();
|
|
|
|
var sut = new ContentEnricher(new[] { step1, step2 }, appProvider);
|
|
|
|
await sut.EnrichAsync(source, false, requestContext, default);
|
|
|
|
A.CallTo(() => step1.EnrichAsync(requestContext, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
|
|
A.CallTo(() => step2.EnrichAsync(requestContext, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
|
|
A.CallTo(() => step1.EnrichAsync(requestContext, A<IEnumerable<ContentEntity>>._, A<ProvideSchema>._, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
|
|
A.CallTo(() => step2.EnrichAsync(requestContext, A<IEnumerable<ContentEntity>>._, A<ProvideSchema>._, A<CancellationToken>._))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_provide_and_cache_schema()
|
|
{
|
|
var source = CreateContent();
|
|
|
|
var step1 = new ResolveSchema();
|
|
var step2 = new ResolveSchema();
|
|
|
|
var sut = new ContentEnricher(new[] { step1, step2 }, appProvider);
|
|
|
|
await sut.EnrichAsync(source, false, requestContext, default);
|
|
|
|
Assert.Same(schema, step1.Schema);
|
|
Assert.Same(schema, step1.Schema);
|
|
|
|
A.CallTo(() => appProvider.GetSchemaAsync(appId.Id, schemaId.Id, false))
|
|
.MustHaveHappenedOnceExactly();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_clone_data_if_requested()
|
|
{
|
|
var source = CreateContent(new ContentData());
|
|
|
|
var sut = new ContentEnricher(Enumerable.Empty<IContentEnricherStep>(), appProvider);
|
|
|
|
var result = await sut.EnrichAsync(source, true, requestContext, default);
|
|
|
|
Assert.NotSame(source.Data, result.Data);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_not_clone_data_if_not_requested()
|
|
{
|
|
var source = CreateContent(new ContentData());
|
|
|
|
var sut = new ContentEnricher(Enumerable.Empty<IContentEnricherStep>(), appProvider);
|
|
|
|
var result = await sut.EnrichAsync(source, false, requestContext, default);
|
|
|
|
Assert.Same(source.Data, result.Data);
|
|
}
|
|
|
|
private ContentEntity CreateContent(ContentData? data = null)
|
|
{
|
|
return new ContentEntity { SchemaId = schemaId, Data = data! };
|
|
}
|
|
}
|
|
}
|
|
|