Headless CMS and Content Managment Hub
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.
 
 
 
 
 

82 lines
2.7 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.Threading.Tasks;
using FakeItEasy;
using NodaTime;
using Squidex.Domain.Apps.Entities.Rules.Repositories;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Caching;
using Xunit;
namespace Squidex.Domain.Apps.Entities.Rules.Queries
{
public class RuleEnricherTests
{
private readonly IRuleEventRepository ruleEventRepository = A.Fake<IRuleEventRepository>();
private readonly IRequestCache requestCache = A.Fake<IRequestCache>();
private readonly NamedId<Guid> appId = NamedId.Of(Guid.NewGuid(), "my-app");
private readonly Context requestContext = Context.Anonymous();
private readonly RuleEnricher sut;
public RuleEnricherTests()
{
sut = new RuleEnricher(ruleEventRepository, requestCache);
}
[Fact]
public async Task Should_not_enrich_if_statistics_not_found()
{
var source = CreateRule();
var result = await sut.EnrichAsync(source, requestContext);
Assert.Equal(0, result.NumFailed);
Assert.Equal(0, result.NumSucceeded);
Assert.Null(result.LastExecuted);
A.CallTo(() => requestCache.AddDependency(source.Id, source.Version))
.MustHaveHappened();
A.CallTo(() => requestCache.AddDependency(null))
.MustNotHaveHappened();
}
[Fact]
public async Task Should_enrich_rules_with_found_statistics()
{
var source = CreateRule();
var stats = new RuleStatistics
{
RuleId = source.Id,
NumFailed = 12,
NumSucceeded = 17,
LastExecuted = SystemClock.Instance.GetCurrentInstant()
};
A.CallTo(() => ruleEventRepository.QueryStatisticsByAppAsync(appId.Id))
.Returns(new List<RuleStatistics> { stats });
await sut.EnrichAsync(source, requestContext);
A.CallTo(() => requestCache.AddDependency(source.Id, source.Version))
.MustHaveHappened();
A.CallTo(() => requestCache.AddDependency(stats.LastExecuted))
.MustHaveHappened();
}
private RuleEntity CreateRule()
{
return new RuleEntity { AppId = appId, Id = Guid.NewGuid(), Version = 13 };
}
}
}