mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
505 lines
18 KiB
505 lines
18 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Squidex.Domain.Apps.Core.EventSynchronization;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Domain.Apps.Events.Schemas;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.Json;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Domain.Apps.Core.Operations.EventSynchronization
|
|
{
|
|
public class SchemaSynchronizerTests
|
|
{
|
|
private readonly Func<long> idGenerator;
|
|
private readonly IJsonSerializer jsonSerializer = TestUtils.DefaultSerializer;
|
|
private readonly NamedId<long> stringId = NamedId.Of(13L, "my-value");
|
|
private readonly NamedId<long> nestedId = NamedId.Of(141L, "my-value");
|
|
private readonly NamedId<long> arrayId = NamedId.Of(14L, "11-array");
|
|
private int fields = 50;
|
|
|
|
public SchemaSynchronizerTests()
|
|
{
|
|
idGenerator = () => fields++;
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_schema_deleted()
|
|
{
|
|
var sourceSchema = new Schema("source");
|
|
var targetSchema = (Schema)null;
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaDeleted()
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_category_changed()
|
|
{
|
|
var sourceSchema = new Schema("source");
|
|
var targetSchema = new Schema("target").MoveTo("Category");
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaCategoryChanged { Name = "Category" }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_scripts_configured()
|
|
{
|
|
var scripts = new Dictionary<string, string>
|
|
{
|
|
["Create"] = "Script"
|
|
};
|
|
|
|
var sourceSchema = new Schema("source");
|
|
var targetSchema = new Schema("target").ConfigureScripts(scripts);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaScriptsConfigured { Scripts = scripts }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_preview_urls_configured()
|
|
{
|
|
var previewUrls = new Dictionary<string, string>
|
|
{
|
|
["Web"] = "Url"
|
|
};
|
|
|
|
var sourceSchema = new Schema("source");
|
|
var targetSchema = new Schema("target").ConfigurePreviewUrls(previewUrls);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaPreviewUrlsConfigured { PreviewUrls = previewUrls }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_schema_published()
|
|
{
|
|
var sourceSchema = new Schema("source");
|
|
var targetSchema = new Schema("target").Publish();
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaPublished()
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_schema_unpublished()
|
|
{
|
|
var sourceSchema = new Schema("source").Publish();
|
|
var targetSchema = new Schema("target");
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaUnpublished()
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_deleted()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldDeleted { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_deleted()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target");
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldDeleted { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_updated()
|
|
{
|
|
var properties = new StringFieldProperties { IsRequired = true };
|
|
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name, properties));
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldUpdated { Properties = properties, FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_updated()
|
|
{
|
|
var properties = new StringFieldProperties { IsRequired = true };
|
|
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant, properties);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldUpdated { Properties = properties, FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_locked()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).LockField(nestedId.Id, arrayId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldLocked { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_locked()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).LockField(stringId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldLocked { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_hidden()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).HideField(nestedId.Id, arrayId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldHidden { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_hidden()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).HideField(stringId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldHidden { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_shown()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).HideField(nestedId.Id, arrayId.Id);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldShown { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_shown()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).HideField(stringId.Id);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldShown { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_disabled()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).DisableField(nestedId.Id, arrayId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldDisabled { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_disabled()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).DisableField(stringId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldDisabled { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_enabled()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).DisableField(nestedId.Id, arrayId.Id);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name));
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldEnabled { FieldId = nestedId, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_enabled()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).DisableField(stringId.Id);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldEnabled { FieldId = stringId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_field_created()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source");
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(stringId.Id, stringId.Name, Partitioning.Invariant).HideField(stringId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
var createdId = NamedId.Of(50L, stringId.Name);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldAdded { FieldId = createdId, Name = stringId.Name, Partitioning = Partitioning.Invariant.Key, Properties = new StringFieldProperties() },
|
|
new FieldHidden { FieldId = createdId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_field_created()
|
|
{
|
|
var sourceSchema =
|
|
new Schema("source");
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(nestedId.Id, nestedId.Name)).HideField(nestedId.Id, arrayId.Id);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
var id1 = NamedId.Of(50L, arrayId.Name);
|
|
var id2 = NamedId.Of(51L, stringId.Name);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldAdded { FieldId = id1, Name = arrayId.Name, Partitioning = Partitioning.Invariant.Key, Properties = new ArrayFieldProperties() },
|
|
new FieldAdded { FieldId = id2, Name = stringId.Name, ParentFieldId = id1, Properties = new StringFieldProperties() },
|
|
new FieldHidden { FieldId = id2, ParentFieldId = id1 }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_nested_fields_reordered()
|
|
{
|
|
var id1 = NamedId.Of(1, "f1");
|
|
var id2 = NamedId.Of(2, "f1");
|
|
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(10, "f1")
|
|
.AddString(11, "f2"));
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
|
|
.AddString(20, "f2")
|
|
.AddString(15, "f1"));
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaFieldsReordered { FieldIds = new List<long> { 11, 10 }, ParentFieldId = arrayId }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_fields_reordered()
|
|
{
|
|
var id1 = NamedId.Of(1, "f1");
|
|
var id2 = NamedId.Of(2, "f1");
|
|
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(10, "f1", Partitioning.Invariant)
|
|
.AddString(11, "f2", Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(20, "f2", Partitioning.Invariant)
|
|
.AddString(15, "f1", Partitioning.Invariant);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new SchemaFieldsReordered { FieldIds = new List<long> { 11, 10 } }
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_create_events_if_fields_reordered_after_sync()
|
|
{
|
|
var id1 = NamedId.Of(1, "f1");
|
|
var id2 = NamedId.Of(2, "f1");
|
|
|
|
var sourceSchema =
|
|
new Schema("source")
|
|
.AddString(10, "f1", Partitioning.Invariant)
|
|
.AddString(11, "f2", Partitioning.Invariant);
|
|
|
|
var targetSchema =
|
|
new Schema("target")
|
|
.AddString(25, "f3", Partitioning.Invariant)
|
|
.AddString(20, "f1", Partitioning.Invariant);
|
|
|
|
var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);
|
|
|
|
events.ShouldHaveSameEvents(
|
|
new FieldDeleted { FieldId = NamedId.Of(11L, "f2") },
|
|
new FieldAdded { FieldId = NamedId.Of(50L, "f3"), Name = "f3", Partitioning = Partitioning.Invariant.Key, Properties = new StringFieldProperties() },
|
|
new SchemaFieldsReordered { FieldIds = new List<long> { 50, 10 } }
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|