|
|
|
@ -12,18 +12,21 @@ using FluentAssertions; |
|
|
|
using Squidex.Core.Schemas; |
|
|
|
using Squidex.Events.Schemas; |
|
|
|
using Squidex.Infrastructure; |
|
|
|
using Squidex.Infrastructure.CQRS; |
|
|
|
using Squidex.Infrastructure.CQRS.Events; |
|
|
|
using Squidex.Write.Schemas; |
|
|
|
using Squidex.Write.Schemas.Commands; |
|
|
|
using Xunit; |
|
|
|
// ReSharper disable ConvertToConstant.Local
|
|
|
|
|
|
|
|
namespace Squidex.Write.Tests.Schemas |
|
|
|
{ |
|
|
|
[Collection("Schema")] |
|
|
|
public class SchemaDomainObjectTests |
|
|
|
{ |
|
|
|
private const string TestName = "schema"; |
|
|
|
private readonly Guid appId = Guid.NewGuid(); |
|
|
|
private readonly string fieldName = "age"; |
|
|
|
private readonly string appName = "schema"; |
|
|
|
private readonly FieldRegistry registry = new FieldRegistry(); |
|
|
|
private readonly SchemaDomainObject sut; |
|
|
|
|
|
|
|
@ -35,23 +38,23 @@ namespace Squidex.Write.Tests.Schemas |
|
|
|
[Fact] |
|
|
|
public void Create_should_throw_if_created() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = TestName }); |
|
|
|
sut.Create(new CreateSchema { Name = appName }); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.Create(new CreateSchema { Name = TestName })); |
|
|
|
Assert.Throws<DomainException>(() => sut.Create(new CreateSchema { Name = appName })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Create_should_throw_if_command_is_invalid() |
|
|
|
public void Create_should_throw_if_command_is_not_valid() |
|
|
|
{ |
|
|
|
Assert.Throws<ValidationException>(() => sut.Create(new CreateSchema())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Create_should_create_schema() |
|
|
|
public void Create_should_create_schema_and_create_events() |
|
|
|
{ |
|
|
|
var properties = new SchemaProperties(); |
|
|
|
|
|
|
|
sut.Create(new CreateSchema { Name = TestName, AppId = appId, Properties = properties }); |
|
|
|
sut.Create(new CreateSchema { Name = appName, AppId = appId, Properties = properties }); |
|
|
|
|
|
|
|
Assert.Equal("schema", sut.Schema.Name); |
|
|
|
|
|
|
|
@ -59,7 +62,7 @@ namespace Squidex.Write.Tests.Schemas |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new SchemaCreated { Name = TestName, AppId = appId, Properties = properties } |
|
|
|
new SchemaCreated { Name = appName, AppId = appId, Properties = properties } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@ -72,31 +75,32 @@ namespace Squidex.Write.Tests.Schemas |
|
|
|
[Fact] |
|
|
|
public void Update_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = TestName }); |
|
|
|
sut.Delete(); |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<ValidationException>(() => sut.Update(new UpdateSchema())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Update_should_throw_if_command_is_invalid() |
|
|
|
public void Update_should_throw_if_command_is_not_valid() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = TestName }); |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<ValidationException>(() => sut.Update(new UpdateSchema())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Update_should_refresh_properties() |
|
|
|
public void Update_should_refresh_properties_and_create_events() |
|
|
|
{ |
|
|
|
var properties = new SchemaProperties(); |
|
|
|
|
|
|
|
sut.Create(new CreateSchema { Name = TestName, AppId = appId }); |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
sut.Update(new UpdateSchema { Properties = properties }); |
|
|
|
|
|
|
|
Assert.Equal(properties, sut.Schema.Properties); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).Skip(1).ToArray() |
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
@ -107,32 +111,342 @@ namespace Squidex.Write.Tests.Schemas |
|
|
|
[Fact] |
|
|
|
public void Delete_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.Delete()); |
|
|
|
Assert.Throws<DomainException>(() => sut.Delete(new DeleteSchema())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Delete_should_throw_if_already_deleted() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = TestName }); |
|
|
|
sut.Delete(); |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.Delete()); |
|
|
|
Assert.Throws<DomainException>(() => sut.Delete(new DeleteSchema())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Delete_should_refresh_properties() |
|
|
|
public void Delete_should_refresh_properties_and_create_events() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = TestName, AppId = appId }); |
|
|
|
sut.Delete(); |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
sut.Delete(new DeleteSchema()); |
|
|
|
|
|
|
|
Assert.True(sut.IsDeleted); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).Skip(1).ToArray() |
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new SchemaDeleted() |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void AddField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.AddField(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void AddField_should_throw_if_command_is_not_valid() |
|
|
|
{ |
|
|
|
Assert.Throws<ValidationException>(() => sut.AddField(new AddField())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void AddField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.AddField(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void AddField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
var properties = new NumberFieldProperties(); |
|
|
|
|
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
sut.AddField(new AddField { Name = fieldName, Properties = properties }); |
|
|
|
|
|
|
|
Assert.Equal(properties, sut.Schema.Fields[1].RawProperties); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldAdded { Name = fieldName, FieldId = 1, Properties = properties } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void UpdateField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.UpdateField(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void UpdateField_should_throw_if_command_is_not_valid() |
|
|
|
{ |
|
|
|
Assert.Throws<ValidationException>(() => sut.UpdateField(new UpdateField())); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void UpdateField_should_throw_if_field_is_not_found() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => sut.UpdateField(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void UpdateField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.UpdateField(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void UpdateField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
var properties = new NumberFieldProperties(); |
|
|
|
|
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.UpdateField(new UpdateField { FieldId = 1, Properties = properties }); |
|
|
|
|
|
|
|
Assert.Equal(properties, sut.Schema.Fields[1].RawProperties); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldUpdated { FieldId = 1, Properties = properties } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void HideField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.HideField(new HideField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void HideField_should_throw_if_field_is_not_found() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => sut.HideField(new HideField { FieldId = 2 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void HideField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.HideField(new HideField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void HideField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.HideField(new HideField { FieldId = 1 }); |
|
|
|
|
|
|
|
Assert.True(sut.Schema.Fields[1].IsHidden); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldHidden { FieldId = 1 } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ShowField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.ShowField(new ShowField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ShowField_should_throw_if_field_is_not_found() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => sut.ShowField(new ShowField { FieldId = 2 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ShowField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.ShowField(new ShowField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ShowField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.HideField(new HideField { FieldId = 1 }); |
|
|
|
sut.ShowField(new ShowField { FieldId = 1 }); |
|
|
|
|
|
|
|
Assert.False(sut.Schema.Fields[1].IsHidden); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).Skip(1).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldShown { FieldId = 1 } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DisableField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.DisableField(new DisableField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DisableField_should_throw_if_field_is_not_found() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => sut.DisableField(new DisableField { FieldId = 2 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DisableField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.DisableField(new DisableField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DisableField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.DisableField(new DisableField { FieldId = 1 }); |
|
|
|
|
|
|
|
Assert.True(sut.Schema.Fields[1].IsDisabled); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldDisabled { FieldId = 1 } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EnableField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.EnableField(new EnableField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EnableField_should_throw_if_field_is_not_found() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => sut.EnableField(new EnableField { FieldId = 2 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EnableField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.EnableField(new EnableField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void EnableField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.DisableField(new DisableField { FieldId = 1 }); |
|
|
|
sut.EnableField(new EnableField { FieldId = 1 }); |
|
|
|
|
|
|
|
Assert.False(sut.Schema.Fields[1].IsDisabled); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).Skip(1).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldEnabled { FieldId = 1 } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DeleteField_should_throw_if_not_created() |
|
|
|
{ |
|
|
|
Assert.Throws<DomainException>(() => sut.DeleteField(new DeleteField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DeleteField_should_throw_if_schema_is_deleted() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
DeleteSchema(); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => sut.DeleteField(new DeleteField { FieldId = 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void DeleteField_should_update_schema_and_create_events() |
|
|
|
{ |
|
|
|
CreateSchema(); |
|
|
|
CreateField(); |
|
|
|
|
|
|
|
sut.DeleteField(new DeleteField { FieldId = 1 }); |
|
|
|
|
|
|
|
Assert.False(sut.Schema.Fields.ContainsKey(1)); |
|
|
|
|
|
|
|
sut.GetUncomittedEvents().Select(x => x.Payload).ToArray() |
|
|
|
.ShouldBeEquivalentTo( |
|
|
|
new IEvent[] |
|
|
|
{ |
|
|
|
new FieldDeleted { FieldId = 1 } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void CreateField() |
|
|
|
{ |
|
|
|
sut.AddField(new AddField { Name = fieldName, Properties = new NumberFieldProperties() }); |
|
|
|
|
|
|
|
((IAggregate)sut).ClearUncommittedEvents(); |
|
|
|
} |
|
|
|
|
|
|
|
private void CreateSchema() |
|
|
|
{ |
|
|
|
sut.Create(new CreateSchema { Name = appName, AppId = appId }); |
|
|
|
|
|
|
|
((IAggregate)sut).ClearUncommittedEvents(); |
|
|
|
} |
|
|
|
|
|
|
|
private void DeleteSchema() |
|
|
|
{ |
|
|
|
sut.Delete(new DeleteSchema()); |
|
|
|
|
|
|
|
((IAggregate)sut).ClearUncommittedEvents(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|