From 542ace7a8788ff9e6c53109c670ee580c18f5105 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 5 Mar 2021 17:45:51 +0100 Subject: [PATCH] Fix command result. --- .../Comments/DomainObject/CommentsGrain.cs | 2 +- .../Commands/CommandResult.cs | 4 +-- backend/src/Squidex.Infrastructure/None.cs | 2 +- .../Model/Apps/AppContributorsJsonTests.cs | 2 +- .../CommentsCommandMiddlewareTests.cs | 2 +- .../DomainObject/CommentsGrainTests.cs | 6 ++-- .../Contents/GraphQL/GraphQLMutationTests.cs | 2 +- .../Commands/CommandRequestTests.cs | 11 ++++++ .../Commands/CommandResultTests.cs | 35 +++++++++++++++++++ .../Commands/DomainObjectTests.cs | 12 +++---- .../ETagCommandMiddlewareTests.cs | 2 +- 11 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 backend/tests/Squidex.Infrastructure.Tests/Commands/CommandResultTests.cs diff --git a/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsGrain.cs b/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsGrain.cs index 1d2190e0c..c98a80613 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsGrain.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Comments/DomainObject/CommentsGrain.cs @@ -126,7 +126,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject events.AddRange(uncommittedEvents); - return new CommandResult(DomainId.Create(Key), Version, previousVersion); + return CommandResult.Empty(DomainId.Create(Key), Version, previousVersion); } catch { diff --git a/backend/src/Squidex.Infrastructure/Commands/CommandResult.cs b/backend/src/Squidex.Infrastructure/Commands/CommandResult.cs index 8ff5f951a..4eb3973a2 100644 --- a/backend/src/Squidex.Infrastructure/Commands/CommandResult.cs +++ b/backend/src/Squidex.Infrastructure/Commands/CommandResult.cs @@ -15,9 +15,9 @@ namespace Squidex.Infrastructure.Commands public bool IsChanged => OldVersion != NewVersion; - public CommandResult(DomainId id, long newVersion, long oldVersion) - : this(id, newVersion, oldVersion, None.Value) + public static CommandResult Empty(DomainId id, long newVersion, long oldVersion) { + return new CommandResult(id, newVersion, oldVersion, None.Value); } } } diff --git a/backend/src/Squidex.Infrastructure/None.cs b/backend/src/Squidex.Infrastructure/None.cs index 5f1564823..ce9781f25 100644 --- a/backend/src/Squidex.Infrastructure/None.cs +++ b/backend/src/Squidex.Infrastructure/None.cs @@ -9,7 +9,7 @@ using System; namespace Squidex.Infrastructure { - public sealed class None + public sealed record None { public static readonly Type Type = typeof(None); diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Apps/AppContributorsJsonTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Apps/AppContributorsJsonTests.cs index abc73e7ec..28e888d90 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Apps/AppContributorsJsonTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Apps/AppContributorsJsonTests.cs @@ -15,7 +15,7 @@ namespace Squidex.Domain.Apps.Core.Model.Apps public class AppContributorsJsonTests { [Fact] - public void Should_serialize_and_deserialize() + public void #() { var contributors = AppContributors.Empty; diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs index 31d67437d..29cf5fe4f 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsCommandMiddlewareTests.cs @@ -48,7 +48,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject .Returns(grain); A.CallTo(() => grain.ExecuteAsync(A>.That.Matches(x => x.Value == command))) - .Returns(new CommandResult(commentsId, 0, 0).AsJ()); + .Returns(CommandResult.Empty(commentsId, 0, 0).AsJ()); var isNextCalled = false; diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsGrainTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsGrainTests.cs index d9b8ec6eb..a7f64deb2 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsGrainTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Comments/DomainObject/CommentsGrainTests.cs @@ -55,7 +55,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject var result = await sut.ExecuteAsync(CreateCommentsCommand(command)); - result.Value.ShouldBeEquivalent(new CommandResult(commentsId, 0, EtagVersion.Empty)); + result.Value.ShouldBeEquivalent(CommandResult.Empty(commentsId, 0, EtagVersion.Empty)); sut.GetCommentsAsync(0).Result.Should().BeEquivalentTo(new CommentsResult { @@ -86,7 +86,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject var result = await sut.ExecuteAsync(CreateCommentsCommand(updateCommand)); - result.Value.ShouldBeEquivalent(new CommandResult(commentsId, 1, 0)); + result.Value.ShouldBeEquivalent(CommandResult.Empty(commentsId, 1, 0)); sut.GetCommentsAsync(-1).Result.Should().BeEquivalentTo(new CommentsResult { @@ -122,7 +122,7 @@ namespace Squidex.Domain.Apps.Entities.Comments.DomainObject var result = await sut.ExecuteAsync(CreateCommentsCommand(deleteCommand)); - result.Value.ShouldBeEquivalent(new CommandResult(commentsId, 2, 1)); + result.Value.ShouldBeEquivalent(CommandResult.Empty(commentsId, 2, 1)); sut.GetCommentsAsync(-1).Result.Should().BeEquivalentTo(new CommentsResult { diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs index a3c7db0cb..382da3780 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs @@ -696,7 +696,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL } }"); - commandContext.Complete(new CommandResult(contentId, 13, 12)); + commandContext.Complete(CommandResult.Empty(contentId, 13, 12)); var result = await ExecuteAsync( new ExecutionOptions { Query = query }, Permissions.AppContentsDeleteOwn); diff --git a/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs index ba2bc2a82..1b4924411 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs @@ -9,6 +9,7 @@ using System.Globalization; using System.Threading.Tasks; using Orleans; using Orleans.TestingHost; +using Squidex.Infrastructure.TestHelpers; using Xunit; #pragma warning disable SA1133 // Do not combine attributes @@ -56,6 +57,16 @@ namespace Squidex.Infrastructure.Commands Assert.Equal(cultureUI.Name, sut.CultureUI); } + [Fact] + public void Should_serialize_and_deserialize() + { + var sut = CommandRequest.Create(null!); + + var serialized = sut.SerializeAndDeserialize(); + + Assert.Equal(sut, serialized); + } + [Fact, Trait("Category", "Dependencies")] public async Task Should_communicate_with_orleans() { diff --git a/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandResultTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandResultTests.cs new file mode 100644 index 000000000..029b9af8a --- /dev/null +++ b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandResultTests.cs @@ -0,0 +1,35 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Squidex.Infrastructure.TestHelpers; +using Xunit; + +namespace Squidex.Infrastructure.Commands +{ + public class CommandResultTests + { + [Fact] + public void Should_serialize_and_deserialize() + { + var sut = new CommandResult(DomainId.NewGuid(), 3, 2, "result"); + + var serialized = sut.SerializeAndDeserialize(); + + Assert.Equal(sut, serialized); + } + + [Fact] + public void Should_serialize_and_deserialize_empty() + { + var sut = CommandResult.Empty(DomainId.NewGuid(), 3, 2); + + var serialized = sut.SerializeAndDeserialize(); + + Assert.Equal(sut, serialized); + } + } +} diff --git a/backend/tests/Squidex.Infrastructure.Tests/Commands/DomainObjectTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Commands/DomainObjectTests.cs index 8543e697d..b6eb77eff 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Commands/DomainObjectTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Commands/DomainObjectTests.cs @@ -50,7 +50,7 @@ namespace Squidex.Infrastructure.Commands A.CallTo(() => persistence.ReadAsync(A._)) .MustNotHaveHappened(); - Assert.Equal(new CommandResult(id, 0, EtagVersion.Empty), result); + Assert.Equal(CommandResult.Empty(id, 0, EtagVersion.Empty), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 4, 0); @@ -74,7 +74,7 @@ namespace Squidex.Infrastructure.Commands A.CallTo(() => persistence.ReadAsync(A._)) .MustHaveHappened(); - Assert.Equal(new CommandResult(id, 2, 1), result); + Assert.Equal(CommandResult.Empty(id, 2, 1), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 4, 2); @@ -112,7 +112,7 @@ namespace Squidex.Infrastructure.Commands A.CallTo(() => persistence.ReadAsync(A._)) .MustHaveHappened(); - Assert.Equal(new CommandResult(id, 2, 1), result); + Assert.Equal(CommandResult.Empty(id, 2, 1), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 4, 2); @@ -148,7 +148,7 @@ namespace Squidex.Infrastructure.Commands A.CallTo(() => persistence.ReadAsync(A._)) .MustNotHaveHappened(); - Assert.Equal(new CommandResult(id, 1, 0), result); + Assert.Equal(CommandResult.Empty(id, 1, 0), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 8, 1); @@ -168,7 +168,7 @@ namespace Squidex.Infrastructure.Commands A.CallTo(() => persistence.ReadAsync(A._)) .MustHaveHappenedOnceExactly(); - Assert.Equal(new CommandResult(id, 1, 0), result); + Assert.Equal(CommandResult.Empty(id, 1, 0), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 8, 1); @@ -299,7 +299,7 @@ namespace Squidex.Infrastructure.Commands var result = await sut.ExecuteAsync(new UpdateAuto { Value = MyDomainState.Unchanged }); - Assert.Equal(new CommandResult(id, 0, 0), result); + Assert.Equal(CommandResult.Empty(id, 0, 0), result); Assert.Empty(sut.GetUncomittedEvents()); AssertSnapshot(sut.Snapshot, 4, 0); diff --git a/backend/tests/Squidex.Web.Tests/CommandMiddlewares/ETagCommandMiddlewareTests.cs b/backend/tests/Squidex.Web.Tests/CommandMiddlewares/ETagCommandMiddlewareTests.cs index c1f4e0eac..095f834a4 100644 --- a/backend/tests/Squidex.Web.Tests/CommandMiddlewares/ETagCommandMiddlewareTests.cs +++ b/backend/tests/Squidex.Web.Tests/CommandMiddlewares/ETagCommandMiddlewareTests.cs @@ -74,7 +74,7 @@ namespace Squidex.Web.CommandMiddlewares [Fact] public async Task Should_add_version_from_result_as_etag_to_response() { - var result = new CommandResult(DomainId.Empty, 17, 16); + var result = CommandResult.Empty(DomainId.Empty, 17, 16); await HandleAsync(new CreateContent(), result);