Browse Source

Added some cleaners including tests.

pull/309/head
Sebastian 8 years ago
parent
commit
cb9a8ddb35
  1. 13
      src/Squidex.Domain.Apps.Entities/Backup/CleanerGrain.cs
  2. 31
      src/Squidex.Domain.Apps.Entities/Rules/Indexes/RuleIndexCleaner.cs
  3. 2
      src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesByAppIndexGrain.cs
  4. 31
      src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemaIndexCleaner.cs
  5. 2
      src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasByAppIndexGrain.cs
  6. 2
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexCommandMiddlewareTests.cs
  7. 2
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexGrainTests.cs
  8. 2
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexCommandMiddlewareTests.cs
  9. 2
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexGrainTests.cs
  10. 2
      tests/Squidex.Domain.Apps.Entities.Tests/Contents/SingletonCommandMiddlewareTests.cs
  11. 40
      tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RuleIndexCleanerTests.cs
  12. 11
      tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexCommandMiddlewareTests.cs
  13. 17
      tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexGrainTests.cs
  14. 40
      tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemaIndexCleanerTests.cs
  15. 11
      tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexCommandMiddlewareTests.cs
  16. 16
      tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexGrainTests.cs

13
src/Squidex.Domain.Apps.Entities/Backup/CleanerGrain.cs

@ -25,7 +25,7 @@ using Squidex.Infrastructure.States;
namespace Squidex.Domain.Apps.Entities.Backup
{
[Reentrant]
public sealed class CleanerGrain : GrainOfGuid, IRemindable
public sealed class CleanerGrain : GrainOfString, IRemindable, IBackgroundGrain
{
private readonly IGrainFactory grainFactory;
private readonly IStore<Guid> store;
@ -58,11 +58,11 @@ namespace Squidex.Domain.Apps.Entities.Backup
this.eventStore = eventStore;
}
public async override Task OnActivateAsync(Guid key)
public async override Task OnActivateAsync(string key)
{
await RegisterOrUpdateReminder("Default", TimeSpan.Zero, TimeSpan.FromMinutes(10));
persistence = store.WithSnapshots<CleanerGrain, State, Guid>(key, s =>
persistence = store.WithSnapshots<CleanerGrain, State, Guid>(Guid.Empty, s =>
{
state = s;
});
@ -72,6 +72,11 @@ namespace Squidex.Domain.Apps.Entities.Backup
await CleanAsync();
}
public Task ActivateAsync()
{
return CleanAsync();
}
public Task ReceiveReminder(string reminderName, TickStatus status)
{
return CleanAsync();
@ -136,7 +141,7 @@ namespace Squidex.Domain.Apps.Entities.Backup
await storage.ClearAsync(appId);
}
await store.ClearSnapshotAsync<AppState>(appId;
await store.ClearSnapshotAsync<AppState>(appId);
}
private async Task DeleteAsync<TState>(Guid id)

31
src/Squidex.Domain.Apps.Entities/Rules/Indexes/RuleIndexCleaner.cs

@ -0,0 +1,31 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Orleans;
using Squidex.Infrastructure;
namespace Squidex.Domain.Apps.Entities.Rules.Indexes
{
public sealed class RuleIndexCleaner : IAppStorage
{
private readonly IGrainFactory grainFactory;
public RuleIndexCleaner(IGrainFactory grainFactory)
{
Guard.NotNull(grainFactory, nameof(grainFactory));
this.grainFactory = grainFactory;
}
public Task ClearAsync(Guid appId)
{
return grainFactory.GetGrain<IRulesByAppIndex>(appId).ClearAsync();
}
}
}

2
src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesByAppIndexGrain.cs

@ -72,6 +72,8 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
public Task ClearAsync()
{
state = new State();
return persistence.DeleteAsync();
}
}

31
src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemaIndexCleaner.cs

@ -0,0 +1,31 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Orleans;
using Squidex.Infrastructure;
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
{
public sealed class SchemaIndexCleaner : IAppStorage
{
private readonly IGrainFactory grainFactory;
public SchemaIndexCleaner(IGrainFactory grainFactory)
{
Guard.NotNull(grainFactory, nameof(grainFactory));
this.grainFactory = grainFactory;
}
public Task ClearAsync(Guid appId)
{
return grainFactory.GetGrain<ISchemasByAppIndex>(appId).ClearAsync();
}
}
}

2
src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasByAppIndexGrain.cs

@ -79,6 +79,8 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
public Task ClearAsync()
{
state = new State();
return persistence.DeleteAsync();
}
}

2
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexCommandMiddlewareTests.cs

@ -16,7 +16,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{
public sealed class AppsByNameIndexCommandMiddlewareTests
public class AppsByNameIndexCommandMiddlewareTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();

2
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexGrainTests.cs

@ -15,7 +15,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{
public sealed class AppsByNameIndexGrainTests
public class AppsByNameIndexGrainTests
{
private readonly IStore<string> store = A.Fake<IStore<string>>();
private readonly IPersistence<AppsByNameIndexGrain.State> persistence = A.Fake<IPersistence<AppsByNameIndexGrain.State>>();

2
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexCommandMiddlewareTests.cs

@ -18,7 +18,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{
public sealed class AppsByUserIndexCommandMiddlewareTests
public class AppsByUserIndexCommandMiddlewareTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();

2
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexGrainTests.cs

@ -15,7 +15,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{
public sealed class AppsByUserIndexGrainTests
public class AppsByUserIndexGrainTests
{
private readonly IStore<string> store = A.Fake<IStore<string>>();
private readonly IPersistence<AppsByUserIndexGrain.State> persistence = A.Fake<IPersistence<AppsByUserIndexGrain.State>>();

2
tests/Squidex.Domain.Apps.Entities.Tests/Contents/SingletonCommandMiddlewareTests.cs

@ -14,7 +14,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Contents
{
public sealed class SingletonCommandMiddlewareTests
public class SingletonCommandMiddlewareTests
{
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();
private readonly SingletonCommandMiddleware sut = new SingletonCommandMiddleware();

40
tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RuleIndexCleanerTests.cs

@ -0,0 +1,40 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using FakeItEasy;
using Orleans;
using Xunit;
namespace Squidex.Domain.Apps.Entities.Rules.Indexes
{
public class RuleIndexCleanerTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly IRulesByAppIndex index = A.Fake<IRulesByAppIndex>();
private readonly Guid appId = Guid.NewGuid();
private readonly RuleIndexCleaner sut;
public RuleIndexCleanerTests()
{
A.CallTo(() => grainFactory.GetGrain<IRulesByAppIndex>(appId, null))
.Returns(index);
sut = new RuleIndexCleaner(grainFactory);
}
[Fact]
public async Task Should_forward_to_index()
{
await sut.ClearAsync(appId);
A.CallTo(() => index.ClearAsync())
.MustHaveHappened();
}
}
}

11
tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexCommandMiddlewareTests.cs

@ -17,7 +17,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Rules.Indexes
{
public sealed class RulesByAppIndexCommandMiddlewareTests
public class RulesByAppIndexCommandMiddlewareTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();
@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
public async Task Should_add_rule_to_index_on_create()
{
var context =
new CommandContext(new CreateRule { RuleId = appId, AppId = NamedId.Of(appId, "my-app") }, commandBus)
new CommandContext(new CreateRule { RuleId = appId, AppId = BuildAppId() }, commandBus)
.Complete();
await sut.HandleAsync(context);
@ -60,7 +60,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
.Returns(J.AsTask(ruleState));
A.CallTo(() => ruleState.AppId)
.Returns(NamedId.Of(appId, "my-app"));
.Returns(BuildAppId());
var context =
new CommandContext(new DeleteRule { RuleId = appId }, commandBus)
@ -71,5 +71,10 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
A.CallTo(() => index.RemoveRuleAsync(appId))
.MustHaveHappened();
}
private NamedId<Guid> BuildAppId()
{
return NamedId.Of(appId, "my-app");
}
}
}

17
tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexGrainTests.cs

@ -14,7 +14,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Rules.Indexes
{
public sealed class RulesByAppIndexGrainTests
public class RulesByAppIndexGrainTests
{
private readonly IStore<Guid> store = A.Fake<IStore<Guid>>();
private readonly IPersistence<RulesByAppIndexGrain.State> persistence = A.Fake<IPersistence<RulesByAppIndexGrain.State>>();
@ -45,6 +45,21 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
.MustHaveHappenedTwiceExactly();
}
[Fact]
public async Task Should_delete_and_reset_state_when_cleaning()
{
await sut.AddRuleAsync(ruleId1);
await sut.AddRuleAsync(ruleId2);
await sut.ClearAsync();
var ids = await sut.GetRuleIdsAsync();
Assert.Empty(ids);
A.CallTo(() => persistence.DeleteAsync())
.MustHaveHappened();
}
[Fact]
public async Task Should_remove_rule_id_from_index()
{

40
tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemaIndexCleanerTests.cs

@ -0,0 +1,40 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using FakeItEasy;
using Orleans;
using Xunit;
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
{
public class SchemaIndexCleanerTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly ISchemasByAppIndex index = A.Fake<ISchemasByAppIndex>();
private readonly Guid appId = Guid.NewGuid();
private readonly SchemaIndexCleaner sut;
public SchemaIndexCleanerTests()
{
A.CallTo(() => grainFactory.GetGrain<ISchemasByAppIndex>(appId, null))
.Returns(index);
sut = new SchemaIndexCleaner(grainFactory);
}
[Fact]
public async Task Should_forward_to_index()
{
await sut.ClearAsync(appId);
A.CallTo(() => index.ClearAsync())
.MustHaveHappened();
}
}
}

11
tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexCommandMiddlewareTests.cs

@ -17,7 +17,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
{
public sealed class SchemasByAppIndexCommandMiddlewareTests
public class SchemasByAppIndexCommandMiddlewareTests
{
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>();
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();
@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
public async Task Should_add_schema_to_index_on_create()
{
var context =
new CommandContext(new CreateSchema { SchemaId = appId, Name = "my-schema", AppId = NamedId.Of(appId, "my-app") }, commandBus)
new CommandContext(new CreateSchema { SchemaId = appId, Name = "my-schema", AppId = BuildAppId() }, commandBus)
.Complete();
await sut.HandleAsync(context);
@ -60,7 +60,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
.Returns(J.AsTask(schemaState));
A.CallTo(() => schemaState.AppId)
.Returns(NamedId.Of(appId, "my-app"));
.Returns(BuildAppId());
var context =
new CommandContext(new DeleteSchema { SchemaId = appId }, commandBus)
@ -71,5 +71,10 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
A.CallTo(() => index.RemoveSchemaAsync(appId))
.MustHaveHappened();
}
private NamedId<Guid> BuildAppId()
{
return NamedId.Of(appId, "my-app");
}
}
}

16
tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexGrainTests.cs

@ -14,7 +14,7 @@ using Xunit;
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
{
public sealed class SchemasByAppIndexGrainTests
public class SchemasByAppIndexGrainTests
{
private readonly IStore<Guid> store = A.Fake<IStore<Guid>>();
private readonly IPersistence<SchemasByAppIndexGrain.State> persistence = A.Fake<IPersistence<SchemasByAppIndexGrain.State>>();
@ -46,6 +46,20 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
.MustHaveHappened();
}
[Fact]
public async Task Should_delete_and_reset_state_when_cleaning()
{
await sut.AddSchemaAsync(schemaId1, schemaName1);
await sut.ClearAsync();
var id = await sut.GetSchemaIdAsync(schemaName1);
Assert.Equal(id, Guid.Empty);
A.CallTo(() => persistence.DeleteAsync())
.MustHaveHappened();
}
[Fact]
public async Task Should_remove_schema_id_from_index()
{

Loading…
Cancel
Save