mirror of https://github.com/Squidex/squidex.git
22 changed files with 671 additions and 14 deletions
@ -0,0 +1,61 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Orleans; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|||
{ |
|||
public sealed class AppsByNameIndexCommandMiddlewareTests |
|||
{ |
|||
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>(); |
|||
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
|||
private readonly IAppsByNameIndex index = A.Fake<IAppsByNameIndex>(); |
|||
private readonly Guid appId = Guid.NewGuid(); |
|||
private readonly AppsByNameIndexCommandMiddleware sut; |
|||
|
|||
public AppsByNameIndexCommandMiddlewareTests() |
|||
{ |
|||
A.CallTo(() => grainFactory.GetGrain<IAppsByNameIndex>(SingleGrain.Id, null)) |
|||
.Returns(index); |
|||
|
|||
sut = new AppsByNameIndexCommandMiddleware(grainFactory); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_to_index_on_create() |
|||
{ |
|||
var context = |
|||
new CommandContext(new CreateApp { AppId = appId, Name = "my-app" }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.AddAppAsync(appId, "my-app")) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_app_from_index_on_archive() |
|||
{ |
|||
var context = |
|||
new CommandContext(new ArchiveApp { AppId = appId }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.RemoveAppAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,84 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Infrastructure.Orleans; |
|||
using Squidex.Infrastructure.States; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|||
{ |
|||
public sealed class AppsByNameIndexGrainTests |
|||
{ |
|||
private readonly IStore<string> store = A.Fake<IStore<string>>(); |
|||
private readonly IPersistence<AppsByNameIndexGrain.State> persistence = A.Fake<IPersistence<AppsByNameIndexGrain.State>>(); |
|||
private readonly Guid appId1 = Guid.NewGuid(); |
|||
private readonly Guid appId2 = Guid.NewGuid(); |
|||
private readonly string appName1 = "my-app1"; |
|||
private readonly string appName2 = "my-app2"; |
|||
private readonly AppsByNameIndexGrain sut; |
|||
|
|||
public AppsByNameIndexGrainTests() |
|||
{ |
|||
A.CallTo(() => store.WithSnapshots(A<Type>.Ignored, A<string>.Ignored, A<Func<AppsByNameIndexGrain.State, Task>>.Ignored)) |
|||
.Returns(persistence); |
|||
|
|||
sut = new AppsByNameIndexGrain(store); |
|||
sut.OnActivateAsync(SingleGrain.Id).Wait(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_id_to_index() |
|||
{ |
|||
await sut.AddAppAsync(appId1, appName1); |
|||
|
|||
var result = await sut.GetAppIdAsync(appName1); |
|||
|
|||
Assert.Equal(appId1, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByNameIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_app_id_from_index() |
|||
{ |
|||
await sut.AddAppAsync(appId1, appName1); |
|||
await sut.RemoveAppAsync(appId1); |
|||
|
|||
var result = await sut.GetAppIdAsync(appName1); |
|||
|
|||
Assert.Equal(Guid.Empty, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByNameIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceExactly(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_replace_app_ids_on_rebuild() |
|||
{ |
|||
var state = new Dictionary<string, Guid> |
|||
{ |
|||
[appName1] = appId1, |
|||
[appName2] = appId2 |
|||
}; |
|||
|
|||
await sut.RebuildAsync(state); |
|||
|
|||
Assert.Equal(appId1, await sut.GetAppIdAsync(appName1)); |
|||
Assert.Equal(appId2, await sut.GetAppIdAsync(appName2)); |
|||
|
|||
Assert.Equal(new List<Guid> { appId1, appId2 }, await sut.GetAppIdsAsync()); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByNameIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,102 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Domain.Apps.Core.Apps; |
|||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Orleans; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|||
{ |
|||
public sealed class AppsByUserIndexCommandMiddlewareTests |
|||
{ |
|||
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>(); |
|||
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
|||
private readonly IAppsByUserIndex index = A.Fake<IAppsByUserIndex>(); |
|||
private readonly Guid appId = Guid.NewGuid(); |
|||
private readonly string userId = "123"; |
|||
private readonly AppsByUserIndexCommandMiddleware sut; |
|||
|
|||
public AppsByUserIndexCommandMiddlewareTests() |
|||
{ |
|||
A.CallTo(() => grainFactory.GetGrain<IAppsByUserIndex>(userId, null)) |
|||
.Returns(index); |
|||
|
|||
sut = new AppsByUserIndexCommandMiddleware(grainFactory); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_to_index_on_create() |
|||
{ |
|||
var context = |
|||
new CommandContext(new CreateApp { AppId = appId, Actor = new RefToken("user", userId) }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.AddAppAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_to_index_on_assign_of_contributor() |
|||
{ |
|||
var context = |
|||
new CommandContext(new AssignContributor { AppId = appId }, commandBus) |
|||
.Complete(EntityCreatedResult.Create(userId, 1)); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.AddAppAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_to_index_on_remove_of_contributor() |
|||
{ |
|||
var context = |
|||
new CommandContext(new RemoveContributor { AppId = appId, ContributorId = userId }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.RemoveAppAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_app_from_index_on_archive() |
|||
{ |
|||
var appGrain = A.Fake<IAppGrain>(); |
|||
var appState = A.Fake<IAppEntity>(); |
|||
|
|||
A.CallTo(() => grainFactory.GetGrain<IAppGrain>(appId, null)) |
|||
.Returns(appGrain); |
|||
|
|||
A.CallTo(() => appGrain.GetStateAsync()) |
|||
.Returns(J.AsTask(appState)); |
|||
|
|||
A.CallTo(() => appState.Contributors) |
|||
.Returns(AppContributors.Empty.Assign(userId, AppContributorPermission.Owner)); |
|||
|
|||
var context = |
|||
new CommandContext(new ArchiveApp { AppId = appId }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.RemoveAppAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,82 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Infrastructure.States; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|||
{ |
|||
public sealed class AppsByUserIndexGrainTests |
|||
{ |
|||
private readonly IStore<string> store = A.Fake<IStore<string>>(); |
|||
private readonly IPersistence<AppsByUserIndexGrain.State> persistence = A.Fake<IPersistence<AppsByUserIndexGrain.State>>(); |
|||
private readonly Guid appId1 = Guid.NewGuid(); |
|||
private readonly Guid appId2 = Guid.NewGuid(); |
|||
private readonly AppsByUserIndexGrain sut; |
|||
|
|||
public AppsByUserIndexGrainTests() |
|||
{ |
|||
A.CallTo(() => store.WithSnapshots(A<Type>.Ignored, A<string>.Ignored, A<Func<AppsByUserIndexGrain.State, Task>>.Ignored)) |
|||
.Returns(persistence); |
|||
|
|||
sut = new AppsByUserIndexGrain(store); |
|||
sut.OnActivateAsync("user").Wait(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_app_id_to_index() |
|||
{ |
|||
await sut.AddAppAsync(appId1); |
|||
await sut.AddAppAsync(appId2); |
|||
|
|||
var result = await sut.GetAppIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { appId1, appId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByUserIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceExactly(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_app_id_from_index() |
|||
{ |
|||
await sut.AddAppAsync(appId1); |
|||
await sut.AddAppAsync(appId2); |
|||
await sut.RemoveAppAsync(appId1); |
|||
|
|||
var result = await sut.GetAppIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { appId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByUserIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceOrMore(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_replace_app_ids_on_rebuild() |
|||
{ |
|||
var state = new HashSet<Guid> |
|||
{ |
|||
appId1, |
|||
appId2 |
|||
}; |
|||
|
|||
await sut.RebuildAsync(state); |
|||
|
|||
var result = await sut.GetAppIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { appId1, appId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<AppsByUserIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Domain.Apps.Entities.Rules.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Orleans; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Rules.Indexes |
|||
{ |
|||
public sealed class RulesByAppIndexCommandMiddlewareTests |
|||
{ |
|||
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>(); |
|||
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
|||
private readonly IRulesByAppIndex index = A.Fake<IRulesByAppIndex>(); |
|||
private readonly Guid appId = Guid.NewGuid(); |
|||
private readonly Guid ruleId = Guid.NewGuid(); |
|||
private readonly RulesByAppIndexCommandMiddleware sut; |
|||
|
|||
public RulesByAppIndexCommandMiddlewareTests() |
|||
{ |
|||
A.CallTo(() => grainFactory.GetGrain<IRulesByAppIndex>(appId, null)) |
|||
.Returns(index); |
|||
|
|||
sut = new RulesByAppIndexCommandMiddleware(grainFactory); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_rule_to_index_on_create() |
|||
{ |
|||
var context = |
|||
new CommandContext(new CreateRule { RuleId = appId, AppId = new NamedId<Guid>(appId, "my-app") }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.AddRuleAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_rule_from_index_on_delete() |
|||
{ |
|||
var ruleGrain = A.Fake<IRuleGrain>(); |
|||
var ruleState = A.Fake<IRuleEntity>(); |
|||
|
|||
A.CallTo(() => grainFactory.GetGrain<IRuleGrain>(appId, null)) |
|||
.Returns(ruleGrain); |
|||
|
|||
A.CallTo(() => ruleGrain.GetStateAsync()) |
|||
.Returns(J.AsTask(ruleState)); |
|||
|
|||
A.CallTo(() => ruleState.AppId) |
|||
.Returns(new NamedId<Guid>(appId, "my-app")); |
|||
|
|||
var context = |
|||
new CommandContext(new DeleteRule { RuleId = appId }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.RemoveRuleAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,82 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Infrastructure.States; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Rules.Indexes |
|||
{ |
|||
public sealed class RulesByAppIndexGrainTests |
|||
{ |
|||
private readonly IStore<Guid> store = A.Fake<IStore<Guid>>(); |
|||
private readonly IPersistence<RulesByAppIndexGrain.State> persistence = A.Fake<IPersistence<RulesByAppIndexGrain.State>>(); |
|||
private readonly Guid ruleId1 = Guid.NewGuid(); |
|||
private readonly Guid ruleId2 = Guid.NewGuid(); |
|||
private readonly RulesByAppIndexGrain sut; |
|||
|
|||
public RulesByAppIndexGrainTests() |
|||
{ |
|||
A.CallTo(() => store.WithSnapshots(A<Type>.Ignored, A<Guid>.Ignored, A<Func<RulesByAppIndexGrain.State, Task>>.Ignored)) |
|||
.Returns(persistence); |
|||
|
|||
sut = new RulesByAppIndexGrain(store); |
|||
sut.OnActivateAsync(Guid.NewGuid()).Wait(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_rule_id_to_index() |
|||
{ |
|||
await sut.AddRuleAsync(ruleId1); |
|||
await sut.AddRuleAsync(ruleId2); |
|||
|
|||
var result = await sut.GetRuleIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { ruleId1, ruleId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<RulesByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceExactly(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_rule_id_from_index() |
|||
{ |
|||
await sut.AddRuleAsync(ruleId1); |
|||
await sut.AddRuleAsync(ruleId2); |
|||
await sut.RemoveRuleAsync(ruleId1); |
|||
|
|||
var result = await sut.GetRuleIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { ruleId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<RulesByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceOrMore(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_replace_rule_ids_on_rebuild() |
|||
{ |
|||
var state = new HashSet<Guid> |
|||
{ |
|||
ruleId1, |
|||
ruleId2 |
|||
}; |
|||
|
|||
await sut.RebuildAsync(state); |
|||
|
|||
var result = await sut.GetRuleIdsAsync(); |
|||
|
|||
Assert.Equal(new List<Guid> { ruleId1, ruleId2 }, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<RulesByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Domain.Apps.Entities.Schemas.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Orleans; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes |
|||
{ |
|||
public sealed class SchemasByAppIndexCommandMiddlewareTests |
|||
{ |
|||
private readonly IGrainFactory grainFactory = A.Fake<IGrainFactory>(); |
|||
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
|||
private readonly ISchemasByAppIndex index = A.Fake<ISchemasByAppIndex>(); |
|||
private readonly Guid appId = Guid.NewGuid(); |
|||
private readonly Guid schemaId = Guid.NewGuid(); |
|||
private readonly SchemasByAppIndexCommandMiddleware sut; |
|||
|
|||
public SchemasByAppIndexCommandMiddlewareTests() |
|||
{ |
|||
A.CallTo(() => grainFactory.GetGrain<ISchemasByAppIndex>(appId, null)) |
|||
.Returns(index); |
|||
|
|||
sut = new SchemasByAppIndexCommandMiddleware(grainFactory); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_schema_to_index_on_create() |
|||
{ |
|||
var context = |
|||
new CommandContext(new CreateSchema { SchemaId = appId, Name = "my-schema", AppId = new NamedId<Guid>(appId, "my-app") }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.AddSchemaAsync(appId, "my-schema")) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_schema_from_index_on_delete() |
|||
{ |
|||
var schemaGrain = A.Fake<ISchemaGrain>(); |
|||
var schemaState = A.Fake<ISchemaEntity>(); |
|||
|
|||
A.CallTo(() => grainFactory.GetGrain<ISchemaGrain>(appId, null)) |
|||
.Returns(schemaGrain); |
|||
|
|||
A.CallTo(() => schemaGrain.GetStateAsync()) |
|||
.Returns(J.AsTask(schemaState)); |
|||
|
|||
A.CallTo(() => schemaState.AppId) |
|||
.Returns(new NamedId<Guid>(appId, "my-app")); |
|||
|
|||
var context = |
|||
new CommandContext(new DeleteSchema { SchemaId = appId }, commandBus) |
|||
.Complete(); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => index.RemoveSchemaAsync(appId)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
// ==========================================================================
|
|||
// 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 Squidex.Infrastructure.States; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Schemas.Indexes |
|||
{ |
|||
public sealed class SchemasByAppIndexGrainTests |
|||
{ |
|||
private readonly IStore<Guid> store = A.Fake<IStore<Guid>>(); |
|||
private readonly IPersistence<SchemasByAppIndexGrain.State> persistence = A.Fake<IPersistence<SchemasByAppIndexGrain.State>>(); |
|||
private readonly Guid schemaId1 = Guid.NewGuid(); |
|||
private readonly Guid schemaId2 = Guid.NewGuid(); |
|||
private readonly string schemaName1 = "my-schema1"; |
|||
private readonly string schemaName2 = "my-schema2"; |
|||
private readonly SchemasByAppIndexGrain sut; |
|||
|
|||
public SchemasByAppIndexGrainTests() |
|||
{ |
|||
A.CallTo(() => store.WithSnapshots(A<Type>.Ignored, A<Guid>.Ignored, A<Func<SchemasByAppIndexGrain.State, Task>>.Ignored)) |
|||
.Returns(persistence); |
|||
|
|||
sut = new SchemasByAppIndexGrain(store); |
|||
sut.OnActivateAsync(Guid.NewGuid()).Wait(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_schema_id_to_index() |
|||
{ |
|||
await sut.AddSchemaAsync(schemaId1, schemaName1); |
|||
|
|||
var result = await sut.GetSchemaIdAsync(schemaName1); |
|||
|
|||
Assert.Equal(schemaId1, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<SchemasByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_remove_schema_id_from_index() |
|||
{ |
|||
await sut.AddSchemaAsync(schemaId1, schemaName1); |
|||
await sut.RemoveSchemaAsync(schemaId1); |
|||
|
|||
var result = await sut.GetSchemaIdAsync(schemaName1); |
|||
|
|||
Assert.Equal(Guid.Empty, result); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<SchemasByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappenedTwiceExactly(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_replace_schema_ids_on_rebuild() |
|||
{ |
|||
var state = new Dictionary<string, Guid> |
|||
{ |
|||
[schemaName1] = schemaId1, |
|||
[schemaName2] = schemaId2 |
|||
}; |
|||
|
|||
await sut.RebuildAsync(state); |
|||
|
|||
Assert.Equal(schemaId1, await sut.GetSchemaIdAsync(schemaName1)); |
|||
Assert.Equal(schemaId2, await sut.GetSchemaIdAsync(schemaName2)); |
|||
|
|||
Assert.Equal(new List<Guid> { schemaId1, schemaId2 }, await sut.GetSchemaIdsAsync()); |
|||
|
|||
A.CallTo(() => persistence.WriteSnapshotAsync(A<SchemasByAppIndexGrain.State>.Ignored)) |
|||
.MustHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue