|
|
|
@ -15,17 +15,20 @@ using Newtonsoft.Json.Linq; |
|
|
|
using Squidex.Domain.Apps.Core.Schemas; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
#pragma warning disable SA1310 // Field names must not contain underscore
|
|
|
|
|
|
|
|
namespace Squidex.Domain.Apps.Core.Model.Schemas |
|
|
|
{ |
|
|
|
public class SchemaTests |
|
|
|
{ |
|
|
|
private readonly JsonSerializer serializer = TestData.DefaultSerializer(); |
|
|
|
private readonly Schema sut = new Schema("my-schema"); |
|
|
|
private readonly Schema schema_0 = new Schema("my-schema"); |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_instantiate_schema() |
|
|
|
{ |
|
|
|
Assert.Equal("my-schema", sut.Name); |
|
|
|
Assert.True(schema_0.Properties.IsFrozen); |
|
|
|
Assert.Equal("my-schema", schema_0.Name); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
@ -39,108 +42,242 @@ namespace Squidex.Domain.Apps.Core.Model.Schemas |
|
|
|
{ |
|
|
|
var properties = new SchemaProperties { Hints = "my-hint", Label = "my-label" }; |
|
|
|
|
|
|
|
sut.Update(properties); |
|
|
|
var schema_1 = schema_0.Update(properties); |
|
|
|
|
|
|
|
Assert.Equal(properties, sut.Properties); |
|
|
|
Assert.NotSame(properties, schema_0.Properties); |
|
|
|
Assert.Same(properties, schema_1.Properties); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_add_field() |
|
|
|
{ |
|
|
|
var field = AddNumberField(1); |
|
|
|
var field = CreateField(1); |
|
|
|
|
|
|
|
var schema_1 = schema_0.AddField(field); |
|
|
|
|
|
|
|
Assert.Equal(field, sut.FieldsById[1]); |
|
|
|
Assert.Empty(schema_0.Fields); |
|
|
|
Assert.Equal(field, schema_1.FieldsById[1]); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_throw_exception_if_adding_field_with_name_that_already_exists() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => sut.AddField(new NumberField(2, "my-field-1", Partitioning.Invariant))); |
|
|
|
Assert.Throws<ArgumentException>(() => schema_1.AddField(new NumberField(2, "my-field-1", Partitioning.Invariant))); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_throw_exception_if_adding_field_with_id_that_already_exists() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => sut.AddField(new NumberField(1, "my-field-2", Partitioning.Invariant))); |
|
|
|
Assert.Throws<ArgumentException>(() => schema_1.AddField(new NumberField(1, "my-field-2", Partitioning.Invariant))); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_throw_exception_if_updating_with_invalid_properties_type() |
|
|
|
public void Should_hide_field() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
var schema_2 = schema_1.HideField(1); |
|
|
|
var schema_3 = schema_2.HideField(1); |
|
|
|
|
|
|
|
Assert.False(schema_1.FieldsById[1].IsHidden); |
|
|
|
Assert.True(schema_3.FieldsById[1].IsHidden); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_hide_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.HideField(1); |
|
|
|
|
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_show_field() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
var schema_2 = schema_1.HideField(1); |
|
|
|
var schema_3 = schema_2.ShowField(1); |
|
|
|
var schema_4 = schema_3.ShowField(1); |
|
|
|
|
|
|
|
Assert.True(schema_2.FieldsById[1].IsHidden); |
|
|
|
Assert.False(schema_4.FieldsById[1].IsHidden); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_show_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.ShowField(1); |
|
|
|
|
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_disable_field() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
var schema_2 = schema_1.DisableField(1); |
|
|
|
var schema_3 = schema_2.DisableField(1); |
|
|
|
|
|
|
|
Assert.False(schema_1.FieldsById[1].IsDisabled); |
|
|
|
Assert.True(schema_3.FieldsById[1].IsDisabled); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_disable_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.DisableField(1); |
|
|
|
|
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_enable_field() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => sut.FieldsById[1].Update(new StringFieldProperties())); |
|
|
|
var schema_2 = schema_1.DisableField(1); |
|
|
|
var schema_3 = schema_2.EnableField(1); |
|
|
|
var schema_4 = schema_3.EnableField(1); |
|
|
|
|
|
|
|
Assert.True(schema_2.FieldsById[1].IsDisabled); |
|
|
|
Assert.False(schema_4.FieldsById[1].IsDisabled); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_enable_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.EnableField(1); |
|
|
|
|
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_lock_field() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
var schema_2 = schema_1.LockField(1); |
|
|
|
var schema_3 = schema_2.LockField(1); |
|
|
|
|
|
|
|
Assert.False(schema_1.FieldsById[1].IsLocked); |
|
|
|
Assert.True(schema_3.FieldsById[1].IsLocked); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_lock_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.LockField(1); |
|
|
|
|
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_update_field() |
|
|
|
{ |
|
|
|
var properties = new NumberFieldProperties(); |
|
|
|
|
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
var schema_2 = schema_1.UpdateField(1, properties); |
|
|
|
|
|
|
|
Assert.NotSame(properties, schema_1.FieldsById[1].RawProperties); |
|
|
|
Assert.Same(properties, schema_2.FieldsById[1].RawProperties); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_do_nothing_if_field_to_delete_not_found() |
|
|
|
public void Should_throw_exception_if_updating_with_invalid_properties_type() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => schema_1.UpdateField(1, new StringFieldProperties())); |
|
|
|
} |
|
|
|
|
|
|
|
sut.DeleteField(2); |
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_update_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.UpdateField(1, new StringFieldProperties()); |
|
|
|
|
|
|
|
Assert.Equal(1, sut.FieldsById.Count); |
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_delete_field() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
var schema_1 = schema_0.AddField(CreateField(1)); |
|
|
|
var schema_2 = schema_1.DeleteField(1); |
|
|
|
|
|
|
|
Assert.Empty(schema_2.FieldsById); |
|
|
|
} |
|
|
|
|
|
|
|
sut.DeleteField(1); |
|
|
|
[Fact] |
|
|
|
public void Should_return_same_schema_if_field_to_delete_does_not_exist() |
|
|
|
{ |
|
|
|
var schema_1 = schema_0.DeleteField(1); |
|
|
|
|
|
|
|
Assert.Empty(sut.FieldsById); |
|
|
|
Assert.Same(schema_0, schema_1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_publish_schema() |
|
|
|
{ |
|
|
|
sut.Publish(); |
|
|
|
var schema_1 = schema_0.Publish(); |
|
|
|
|
|
|
|
Assert.True(sut.IsPublished); |
|
|
|
Assert.False(schema_0.IsPublished); |
|
|
|
Assert.True(schema_1.IsPublished); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_unpublish_schema() |
|
|
|
{ |
|
|
|
sut.Publish(); |
|
|
|
sut.Unpublish(); |
|
|
|
var schema_1 = schema_0.Publish(); |
|
|
|
var schema_2 = schema_1.Unpublish(); |
|
|
|
|
|
|
|
Assert.False(sut.IsPublished); |
|
|
|
Assert.True(schema_1.IsPublished); |
|
|
|
Assert.False(schema_2.IsPublished); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_reorder_fields() |
|
|
|
{ |
|
|
|
var field1 = AddNumberField(1); |
|
|
|
var field2 = AddNumberField(2); |
|
|
|
var field3 = AddNumberField(3); |
|
|
|
var field1 = CreateField(1); |
|
|
|
var field2 = CreateField(2); |
|
|
|
var field3 = CreateField(3); |
|
|
|
|
|
|
|
sut.ReorderFields(new List<long> { 3, 2, 1 }); |
|
|
|
var schema_1 = schema_0.AddField(field1); |
|
|
|
var schema_2 = schema_1.AddField(field2); |
|
|
|
var schema_3 = schema_2.AddField(field3); |
|
|
|
var schema_4 = schema_3.ReorderFields(new List<long> { 3, 2, 1 }); |
|
|
|
|
|
|
|
Assert.Equal(new List<Field> { field3, field2, field1 }, sut.Fields.ToList()); |
|
|
|
Assert.Equal(new List<Field> { field3, field2, field1 }, schema_4.Fields.ToList()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_throw_exception_if_not_all_fields_are_covered_for_reordering() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
AddNumberField(2); |
|
|
|
var field1 = CreateField(1); |
|
|
|
var field2 = CreateField(2); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => sut.ReorderFields(new List<long> { 1 })); |
|
|
|
var schema_1 = schema_0.AddField(field1); |
|
|
|
var schema_2 = schema_1.AddField(field2); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => schema_2.ReorderFields(new List<long> { 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Should_throw_exception_if_field_to_reorder_does_not_exist() |
|
|
|
{ |
|
|
|
AddNumberField(1); |
|
|
|
AddNumberField(2); |
|
|
|
var field1 = CreateField(1); |
|
|
|
var field2 = CreateField(2); |
|
|
|
|
|
|
|
var schema_1 = schema_0.AddField(field1); |
|
|
|
var schema_2 = schema_1.AddField(field2); |
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => sut.ReorderFields(new List<long> { 1, 4 })); |
|
|
|
Assert.Throws<ArgumentException>(() => schema_2.ReorderFields(new List<long> { 1, 4 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
@ -152,13 +289,9 @@ namespace Squidex.Domain.Apps.Core.Model.Schemas |
|
|
|
schemaTarget.ShouldBeEquivalentTo(schemaSource); |
|
|
|
} |
|
|
|
|
|
|
|
private NumberField AddNumberField(int id) |
|
|
|
private static NumberField CreateField(int id) |
|
|
|
{ |
|
|
|
var field = new NumberField(id, $"my-field-{id}", Partitioning.Invariant); |
|
|
|
|
|
|
|
sut.AddField(field); |
|
|
|
|
|
|
|
return field; |
|
|
|
return new NumberField(id, $"my-field-{id}", Partitioning.Invariant); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|