Browse Source

Tests added.

pull/283/head
Sebastian 8 years ago
parent
commit
604c0d8f24
  1. 2
      src/Squidex.Domain.Apps.Entities/Apps/AppGrain.cs
  2. 4
      src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsByNameIndexGrain.cs
  3. 2
      src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsByUserIndexGrain.cs
  4. 2
      src/Squidex.Domain.Apps.Entities/Apps/Indexes/IAppsByNameIndex.cs
  5. 2
      src/Squidex.Domain.Apps.Entities/Assets/AssetGrain.cs
  6. 2
      src/Squidex.Domain.Apps.Entities/Backup/BackupGrain.cs
  7. 2
      src/Squidex.Domain.Apps.Entities/Rules/Indexes/RulesByAppIndexGrain.cs
  8. 2
      src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs
  9. 2
      src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasByAppIndexGrain.cs
  10. 2
      src/Squidex.Domain.Apps.Entities/Schemas/SchemaGrain.cs
  11. 4
      src/Squidex.Infrastructure/Commands/CommandContext.cs
  12. 11
      src/Squidex.Infrastructure/Orleans/J.cs
  13. 61
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexCommandMiddlewareTests.cs
  14. 84
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByNameIndexGrainTests.cs
  15. 102
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexCommandMiddlewareTests.cs
  16. 82
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexGrainTests.cs
  17. 75
      tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexCommandMiddlewareTests.cs
  18. 82
      tests/Squidex.Domain.Apps.Entities.Tests/Rules/Indexes/RulesByAppIndexGrainTests.cs
  19. 75
      tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexCommandMiddlewareTests.cs
  20. 83
      tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasByAppIndexGrainTests.cs
  21. 2
      tools/Migrate_01/Migrations/AddPatterns.cs
  22. 2
      tools/Migrate_01/Migrations/PopulateGrainIndexes.cs

2
src/Squidex.Domain.Apps.Entities/Apps/AppGrain.cs

@ -341,7 +341,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
public Task<J<IAppEntity>> GetStateAsync()
{
return Task.FromResult(new J<IAppEntity>(Snapshot));
return J.AsTask<IAppEntity>(Snapshot);
}
}
}

4
src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsByNameIndexGrain.cs

@ -22,7 +22,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
private State state = new State();
[CollectionName("Index_AppsByName")]
private sealed class State
public sealed class State
{
public Dictionary<string, Guid> Apps { get; set; } = new Dictionary<string, Guid>();
}
@ -72,7 +72,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
return Task.FromResult(appId);
}
public Task<List<Guid>> GetAppIdAsync()
public Task<List<Guid>> GetAppIdsAsync()
{
return Task.FromResult(state.Apps.Values.ToList());
}

2
src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsByUserIndexGrain.cs

@ -22,7 +22,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
private State state = new State();
[CollectionName("Index_AppsByUser")]
private sealed class State
public sealed class State
{
public HashSet<Guid> Apps { get; set; } = new HashSet<Guid>();
}

2
src/Squidex.Domain.Apps.Entities/Apps/Indexes/IAppsByNameIndex.cs

@ -22,6 +22,6 @@ namespace Squidex.Domain.Apps.Entities.Apps
Task<Guid> GetAppIdAsync(string name);
Task<List<Guid>> GetAppIdAsync();
Task<List<Guid>> GetAppIdsAsync();
}
}

2
src/Squidex.Domain.Apps.Entities/Assets/AssetGrain.cs

@ -142,7 +142,7 @@ namespace Squidex.Domain.Apps.Entities.Assets
public Task<J<IAssetEntity>> GetStateAsync()
{
return Task.FromResult(new J<IAssetEntity>(Snapshot));
return J.AsTask<IAssetEntity>(Snapshot);
}
}
}

2
src/Squidex.Domain.Apps.Entities/Backup/BackupGrain.cs

@ -275,7 +275,7 @@ namespace Squidex.Domain.Apps.Entities.Backup
public Task<J<List<IBackupJob>>> GetStateAsync()
{
return Task.FromResult(new J<List<IBackupJob>>(state.Jobs.OfType<IBackupJob>().ToList()));
return J.AsTask(state.Jobs.OfType<IBackupJob>().ToList());
}
private bool IsRunning()

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

@ -22,7 +22,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Indexes
private State state = new State();
[CollectionName("Index_RulesByApp")]
private sealed class State
public sealed class State
{
public HashSet<Guid> Rules { get; set; } = new HashSet<Guid>();
}

2
src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs

@ -130,7 +130,7 @@ namespace Squidex.Domain.Apps.Entities.Rules
public Task<J<IRuleEntity>> GetStateAsync()
{
return Task.FromResult(new J<IRuleEntity>(Snapshot));
return J.AsTask<IRuleEntity>(Snapshot);
}
}
}

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

@ -22,7 +22,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
private State state = new State();
[CollectionName("Index_SchemasByApp")]
private sealed class State
public sealed class State
{
public Dictionary<string, Guid> Schemas { get; set; } = new Dictionary<string, Guid>();
}

2
src/Squidex.Domain.Apps.Entities/Schemas/SchemaGrain.cs

@ -305,7 +305,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas
public Task<J<ISchemaEntity>> GetStateAsync()
{
return Task.FromResult(new J<ISchemaEntity>(Snapshot));
return J.AsTask<ISchemaEntity>(Snapshot);
}
}
}

4
src/Squidex.Infrastructure/Commands/CommandContext.cs

@ -45,9 +45,11 @@ namespace Squidex.Infrastructure.Commands
this.commandBus = commandBus;
}
public void Complete(object resultValue = null)
public CommandContext Complete(object resultValue = null)
{
result = Tuple.Create(resultValue);
return this;
}
public T Result<T>()

11
src/Squidex.Infrastructure/Orleans/J.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Threading.Tasks;
using Newtonsoft.Json;
#pragma warning disable SA1401 // Fields must be private
@ -14,5 +15,15 @@ namespace Squidex.Infrastructure.Orleans
public static class J
{
public static JsonSerializer Serializer = new JsonSerializer();
public static J<T> Of<T>(T value)
{
return value;
}
public static Task<J<T>> AsTask<T>(T value)
{
return Task.FromResult<J<T>>(value);
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

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

@ -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();
}
}
}

2
tools/Migrate_01/Migrations/AddPatterns.cs

@ -28,7 +28,7 @@ namespace Migrate_01.Migrations
public async Task UpdateAsync()
{
var ids = await grainFactory.GetGrain<IAppsByNameIndex>(SingleGrain.Id).GetAppIdAsync();
var ids = await grainFactory.GetGrain<IAppsByNameIndex>(SingleGrain.Id).GetAppIdsAsync();
foreach (var id in ids)
{

2
tools/Migrate_01/Migrations/PopulateGrainIndexes.cs

@ -111,7 +111,7 @@ namespace Migrate_01.Migrations
{
if (!schema.IsDeleted)
{
schemasByApp.GetOrAddNew(schema.AppId.Id).Add(schema.Name, schema.Id);
schemasByApp.GetOrAddNew(schema.AppId.Id)[schema.Name] = schema.Id;
}
return TaskHelper.Done;

Loading…
Cancel
Save