From 8a81ead6119cc940306092a6ebb7de37c795275a Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sat, 29 May 2021 22:50:15 +0200 Subject: [PATCH] Tests fixed. --- .../Schemas/MongoSchemasHash.cs | 41 +++++++------------ .../Schemas/MongoSchemasHashEntity.cs | 5 +-- .../Assets/AssetsJintExtension.cs | 6 +-- .../Contents/GraphQL/CachingGraphQLService.cs | 2 +- .../Contents/ReferencesJintExtension.cs | 2 +- .../Schemas/ISchemasHash.cs | 2 +- .../RuleEventFormatterCompareTests.cs | 7 +++- .../HandleRules/RuleEventFormatterTests.cs | 7 +++- .../Assets/AssetsJintExtensionTests.cs | 7 +++- .../Counter/CounterJintExtensionTests.cs | 3 +- .../Contents/DynamicContentWorkflowTests.cs | 9 +++- .../Contents/ReferencesJintExtensionTests.cs | 7 +++- .../Contents/Text/TextIndexerTests_Mongo.cs | 4 +- .../Schemas/MongoDb/SchemasHashTests.cs | 7 +--- .../EventSourcing/EventStoreTests.cs | 4 +- 15 files changed, 61 insertions(+), 52 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHash.cs b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHash.cs index 08fc5833f..e21c66346 100644 --- a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHash.cs +++ b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHash.cs @@ -39,7 +39,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas public string EventsFilter { - get => "^(app-|schema-)"; + get => "^schema-"; } public MongoSchemasHash(IMongoDatabase database, bool setup = false) @@ -63,30 +63,17 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas continue; } - switch (@event.Payload) + if (@event.Payload is SchemaEvent schemaEvent) { - case SchemaEvent schemaEvent: + writes.Add( + new UpdateOneModel( + Filter.Eq(x => x.AppId, schemaEvent.AppId.Id.ToString()), + Update + .Set($"s.{schemaEvent.SchemaId.Id}", @event.Headers.EventStreamNumber()) + .Set(x => x.Updated, @event.Headers.Timestamp())) { - writes.Add( - new UpdateOneModel( - Filter.Eq(x => x.AppId, schemaEvent.AppId.Id.ToString()), - Update - .Set($"s.{schemaEvent.SchemaId.Id}", @event.Headers.EventStreamNumber()) - .Set(x => x.Updated, @event.Headers.Timestamp()))); - break; - } - - case AppEvent appEvent: - writes.Add( - new UpdateOneModel( - Filter.Eq(x => x.AppId, appEvent.AppId.Id.ToString()), - Update - .Set(x => x.AppVersion, @event.Headers.EventStreamNumber()) - .Set(x => x.Updated, @event.Headers.Timestamp())) - { - IsUpsert = true - }); - break; + IsUpsert = true + }); } } @@ -98,9 +85,11 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas return Collection.BulkWriteAsync(writes); } - public async Task<(Instant Create, string Hash)> GetCurrentHashAsync(DomainId appId) + public async Task<(Instant Create, string Hash)> GetCurrentHashAsync(IAppEntity app) { - var entity = await Collection.Find(x => x.AppId == appId.ToString()).FirstOrDefaultAsync(); + Guard.NotNull(app, nameof(app)); + + var entity = await Collection.Find(x => x.AppId == app.Id.ToString()).FirstOrDefaultAsync(); if (entity == null) { @@ -109,7 +98,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas var ids = entity.SchemaVersions.Select(x => (x.Key, x.Value)) - .Union(Enumerable.Repeat((entity.AppId, entity.AppVersion), 1)); + .Union(Enumerable.Repeat((app.Id.ToString(), app.Version), 1)); var hash = CreateHash(ids); diff --git a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHashEntity.cs b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHashEntity.cs index f8306a0a5..d7f830797 100644 --- a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHashEntity.cs +++ b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemasHashEntity.cs @@ -11,16 +11,13 @@ using NodaTime; namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas { + [BsonIgnoreExtraElements] public sealed class MongoSchemasHashEntity { [BsonId] [BsonElement] public string AppId { get; set; } - [BsonRequired] - [BsonElement("v")] - public long AppVersion { get; set; } - [BsonRequired] [BsonElement("s")] public Dictionary SchemaVersions { get; set; } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetsJintExtension.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetsJintExtension.cs index bbe359ac5..5682f2dcb 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetsJintExtension.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetsJintExtension.cs @@ -44,13 +44,13 @@ namespace Squidex.Domain.Apps.Entities.Assets return; } - var action = new GetAssetsDelegate((references, callback) => GetReferences(context, appId, user, references, callback)); + var action = new GetAssetsDelegate((references, callback) => GetAssets(context, appId, user, references, callback)); context.Engine.SetValue("getAsset", action); context.Engine.SetValue("getAssets", action); } - private void GetReferences(ExecutionContext context, DomainId appId, ClaimsPrincipal user, JsValue references, Action callback) + private void GetAssets(ExecutionContext context, DomainId appId, ClaimsPrincipal user, JsValue references, Action callback) { GetReferencesAsync(context, appId, user, references, callback).Forget(); } @@ -96,7 +96,7 @@ namespace Squidex.Domain.Apps.Entities.Assets var assetQuery = serviceProvider.GetRequiredService(); - var assets = await assetQuery.QueryAsync(requestContext, null, Q.Empty.WithIds(ids)); + var assets = await assetQuery.QueryAsync(requestContext, null, Q.Empty.WithIds(ids), context.CancellationToken); callback(JsValue.FromObject(context.Engine, assets.ToArray())); } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLService.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLService.cs index 633e0e39f..28242adad 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLService.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLService.cs @@ -82,7 +82,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL }, async entry => { - var (created, hash) = await schemasHash.GetCurrentHashAsync(app.Id); + var (created, hash) = await schemasHash.GetCurrentHashAsync(app); return created < entry.Created || string.Equals(hash, entry.Hash, StringComparison.OrdinalIgnoreCase); }); diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/ReferencesJintExtension.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/ReferencesJintExtension.cs index 7eb69f12a..5079373bf 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/ReferencesJintExtension.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/ReferencesJintExtension.cs @@ -98,7 +98,7 @@ namespace Squidex.Domain.Apps.Entities.Contents var contentQuery = serviceProvider.GetRequiredService(); - var contents = await contentQuery.QueryAsync(requestContext, Q.Empty.WithIds(ids)); + var contents = await contentQuery.QueryAsync(requestContext, Q.Empty.WithIds(ids), context.CancellationToken); callback(JsValue.FromObject(context.Engine, contents.ToArray())); } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Schemas/ISchemasHash.cs b/backend/src/Squidex.Domain.Apps.Entities/Schemas/ISchemasHash.cs index 4928ce1b9..659c01d09 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Schemas/ISchemasHash.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Schemas/ISchemasHash.cs @@ -15,7 +15,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas { public interface ISchemasHash { - Task<(Instant Create, string Hash)> GetCurrentHashAsync(DomainId appId); + Task<(Instant Create, string Hash)> GetCurrentHashAsync(IAppEntity app); ValueTask ComputeHashAsync(IAppEntity app, IEnumerable schemas); } diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterCompareTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterCompareTests.cs index f1301abca..da80b0c38 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterCompareTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterCompareTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Collections.Generic; using System.Security.Claims; using System.Threading.Tasks; @@ -110,7 +111,11 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules var cache = new MemoryCache(Options.Create(new MemoryCacheOptions())); - return new JintScriptEngine(cache, extensions); + return new JintScriptEngine(cache, extensions) + { + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) + }; } [Theory] diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs index ef0497ace..9e60502b6 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Collections.Generic; using System.Security.Claims; using System.Threading.Tasks; @@ -100,7 +101,11 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules var cache = new MemoryCache(Options.Create(new MemoryCacheOptions())); - return new JintScriptEngine(cache, extensions); + return new JintScriptEngine(cache, extensions) + { + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) + }; } [Fact] diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetsJintExtensionTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetsJintExtensionTests.cs index 3dbc6fb6e..2fcc0e93d 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetsJintExtensionTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetsJintExtensionTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; @@ -45,7 +46,11 @@ namespace Squidex.Domain.Apps.Entities.Assets A.CallTo(() => appProvider.GetAppAsync(appId.Id, false)) .Returns(Mocks.App(appId)); - sut = new JintScriptEngine(new MemoryCache(Options.Create(new MemoryCacheOptions())), extensions); + sut = new JintScriptEngine(new MemoryCache(Options.Create(new MemoryCacheOptions())), extensions) + { + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) + }; } [Fact] diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Counter/CounterJintExtensionTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Counter/CounterJintExtensionTests.cs index c28527148..e168cbc04 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Counter/CounterJintExtensionTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Counter/CounterJintExtensionTests.cs @@ -33,7 +33,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Counter sut = new JintScriptEngine(cache, extensions) { - TimeoutScript = TimeSpan.FromSeconds(1) + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) }; } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/DynamicContentWorkflowTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/DynamicContentWorkflowTests.cs index 3cdc81b76..68ca6257a 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/DynamicContentWorkflowTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/DynamicContentWorkflowTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Collections.Generic; using System.Threading.Tasks; using FakeItEasy; @@ -92,9 +93,13 @@ namespace Squidex.Domain.Apps.Entities.Contents A.CallTo(() => app.Workflows) .Returns(workflows); - var memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); + var scriptEngine = new JintScriptEngine(new MemoryCache(Options.Create(new MemoryCacheOptions()))) + { + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) + }; - sut = new DynamicContentWorkflow(new JintScriptEngine(memoryCache), appProvider); + sut = new DynamicContentWorkflow(scriptEngine, appProvider); } [Fact] diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ReferencesJintExtensionTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ReferencesJintExtensionTests.cs index 76a500838..f1a8e4867 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ReferencesJintExtensionTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ReferencesJintExtensionTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; @@ -45,7 +46,11 @@ namespace Squidex.Domain.Apps.Entities.Contents A.CallTo(() => appProvider.GetAppAsync(appId.Id, false)) .Returns(Mocks.App(appId)); - sut = new JintScriptEngine(new MemoryCache(Options.Create(new MemoryCacheOptions())), extensions); + sut = new JintScriptEngine(new MemoryCache(Options.Create(new MemoryCacheOptions())), extensions) + { + TimeoutScript = TimeSpan.FromSeconds(2), + TimeoutExecution = TimeSpan.FromSeconds(10) + }; } [Fact] diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs index 827344f92..6cafc0644 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs @@ -49,6 +49,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text { BsonJsonConvention.Register(JsonSerializer.Create(TestUtils.CreateSerializerSettings())); + DomainIdSerializer.Register(); + SupportsQuerySyntax = false; SupportsGeo = true; } @@ -67,4 +69,4 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text ); } } -} \ No newline at end of file +} diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/MongoDb/SchemasHashTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/MongoDb/SchemasHashTests.cs index f4ff53b29..6e4c7b622 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/MongoDb/SchemasHashTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/MongoDb/SchemasHashTests.cs @@ -59,11 +59,6 @@ namespace Squidex.Domain.Apps.Entities.Schemas.MongoDb await _.SchemasHash.On(new[] { - Envelope.Create(new AppCreated - { - AppId = NamedId.Of(app.Id, "my-app") - }).SetEventStreamNumber(app.Version).SetTimestamp(timestamp), - Envelope.Create(new SchemaCreated { AppId = NamedId.Of(app.Id, "my-app"), @@ -77,7 +72,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.MongoDb }).SetEventStreamNumber(schema2.Version).SetTimestamp(timestamp) }); - var (dbTime, dbHash) = await _.SchemasHash.GetCurrentHashAsync(app.Id); + var (dbTime, dbHash) = await _.SchemasHash.GetCurrentHashAsync(app); Assert.Equal(dbHash, computedHash); Assert.Equal(dbTime, timestamp); diff --git a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/EventStoreTests.cs b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/EventStoreTests.cs index 88468d4be..5fc46a495 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/EventStoreTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/EventStoreTests.cs @@ -112,8 +112,8 @@ namespace Squidex.Infrastructure.EventSourcing { new StoredEvent(streamName, "Position", 0, commit1[0]), new StoredEvent(streamName, "Position", 1, commit1[1]), - new StoredEvent(streamName, "Position", 2, commit2[2]), - new StoredEvent(streamName, "Position", 3, commit2[3]) + new StoredEvent(streamName, "Position", 2, commit2[0]), + new StoredEvent(streamName, "Position", 3, commit2[1]) }; ShouldBeEquivalentTo(readEvents1, expected);