2021-03-07 19:35:36 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-03-07 19:35:36 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories;
|
2022-04-26 10:22:37 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
|
2025-03-21 18:02:31 +01:00
|
|
|
internal sealed class CacheInstructionRepositoryTest : UmbracoIntegrationTest
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private const string OriginIdentiy = "Test1";
|
|
|
|
|
private const string Instructions = "{}";
|
|
|
|
|
private readonly DateTime _date = new(2021, 7, 3, 10, 30, 0);
|
|
|
|
|
private const int InstructionCount = 1;
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Count_All()
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
const int Count = 5;
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
|
|
|
|
for (var i = 0; i < Count; i++)
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount));
|
|
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var count = repo.CountAll();
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(count, Is.EqualTo(Count));
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Count_Pending_Instructions()
|
|
|
|
|
{
|
|
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
|
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount));
|
|
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var count = repo.CountPendingInstructions(2);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(count, Is.EqualTo(3));
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Check_Exists()
|
|
|
|
|
{
|
|
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var existsBefore = repo.Exists(1);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount));
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var existsAfter = repo.Exists(1);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(existsBefore, Is.False);
|
|
|
|
|
Assert.That(existsAfter, Is.True);
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Add_Cache_Instruction()
|
|
|
|
|
{
|
|
|
|
|
const string OriginIdentiy = "Test1";
|
|
|
|
|
const string Instructions = "{}";
|
|
|
|
|
var date = new DateTime(2021, 7, 3, 10, 30, 0);
|
|
|
|
|
const int InstructionCount = 1;
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
|
|
|
|
repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount));
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var dtos = ScopeAccessor.AmbientScope.Database.Fetch<CacheInstructionDto>("WHERE id > -1");
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(dtos.Any(), Is.True);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var dto = dtos.First();
|
|
|
|
|
Assert.That(dto.UtcStamp, Is.EqualTo(date));
|
|
|
|
|
Assert.That(dto.Instructions, Is.EqualTo(Instructions));
|
|
|
|
|
Assert.That(dto.OriginIdentity, Is.EqualTo(OriginIdentiy));
|
|
|
|
|
Assert.That(dto.InstructionCount, Is.EqualTo(InstructionCount));
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Get_Pending_Instructions()
|
|
|
|
|
{
|
|
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
|
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount));
|
|
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var instructions = repo.GetPendingInstructions(2, 2);
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(instructions.Count(), Is.EqualTo(2));
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.That(string.Join(",", instructions.Select(x => x.Id)), Is.EqualTo("3,4"));
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Delete_Old_Instructions()
|
|
|
|
|
{
|
|
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var repo = new CacheInstructionRepository((IScopeAccessor)sp);
|
|
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-07 19:35:36 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var date = i == 0 ? DateTime.UtcNow.AddDays(-2) : DateTime.UtcNow;
|
|
|
|
|
repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount));
|
|
|
|
|
}
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
repo.DeleteInstructionsOlderThan(DateTime.UtcNow.AddDays(-1));
|
2021-03-07 19:35:36 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var count = repo.CountAll();
|
|
|
|
|
Assert.That(count, Is.EqualTo(4)); // 5 have been added, 1 is older and deleted.
|
2021-03-07 19:35:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|