mirror of https://github.com/Squidex/squidex.git
29 changed files with 2107 additions and 24 deletions
@ -0,0 +1,104 @@ |
|||||
|
// ==========================================================================
|
||||
|
// AssetsFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class AssetsFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_items_greater_than_max_items() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { MinItems = 10, MaxItems = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max items must be greater than min items.", "MinItems", "MaxItems") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_width_greater_than_max_width() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { MinWidth = 10, MaxWidth = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max width must be greater than min width.", "MinWidth", "MaxWidth") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_height_greater_than_max_height() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { MinHeight = 10, MaxHeight = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max height must be greater than min height.", "MinHeight", "MaxHeight") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_size_greater_than_max_size() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { MinSize = 10, MaxSize = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max size must be greater than min size.", "MinSize", "MaxSize") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_only_aspect_width_is_defined() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { AspectWidth = 10 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Aspect width and height must be defined.", "AspectWidth", "AspectHeight") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_only_aspect_height_is_defined() |
||||
|
{ |
||||
|
var sut = new AssetsFieldProperties { AspectHeight = 10 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Aspect width and height must be defined.", "AspectWidth", "AspectHeight") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class BooleanFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new BooleanFieldProperties { Editor = (BooleanFieldEditor)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor is not a valid value.", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,126 @@ |
|||||
|
// ==========================================================================
|
||||
|
// DateTimeFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using NodaTime; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class DateTimeFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_not_add_error_if_sut_is_valid() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties |
||||
|
{ |
||||
|
MinValue = FutureDays(10), |
||||
|
MaxValue = FutureDays(20), |
||||
|
DefaultValue = FutureDays(15) |
||||
|
}; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_default_value_is_less_than_min() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { MinValue = FutureDays(10), DefaultValue = FutureDays(5) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Default value must be greater than min value.", "DefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_default_value_is_greater_than_min() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { MaxValue = FutureDays(10), DefaultValue = FutureDays(15) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Default value must be less than max value.", "DefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_greater_than_max() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { MinValue = FutureDays(10), MaxValue = FutureDays(5) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max value must be greater than min value.", "MinValue", "MaxValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { Editor = (DateTimeFieldEditor)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor is not a valid value.", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_calculated_default_value_is_not_valid() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { CalculatedDefaultValue = (DateTimeCalculatedDefaultValue)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Calculated default value is not valid.", "CalculatedDefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_calculated_default_value_default_value_is_defined() |
||||
|
{ |
||||
|
var sut = new DateTimeFieldProperties { CalculatedDefaultValue = DateTimeCalculatedDefaultValue.Now, DefaultValue = FutureDays(10) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Calculated default value and default value cannot be used together.", "CalculatedDefaultValue", "DefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private static Instant FutureDays(int days) |
||||
|
{ |
||||
|
return Instant.FromDateTimeUtc(DateTime.UtcNow.Date.AddDays(days)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GeolocationPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class GeolocationFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new GeolocationFieldProperties { Editor = (GeolocationFieldEditor)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor is not a valid value.", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
// ==========================================================================
|
||||
|
// JsonFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Linq; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class JsonFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new JsonFieldProperties(); |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,134 @@ |
|||||
|
// ==========================================================================
|
||||
|
// NumberFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class NumberFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_not_add_error_if_sut_is_valid() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties |
||||
|
{ |
||||
|
MinValue = 0, |
||||
|
MaxValue = 100, |
||||
|
DefaultValue = 5 |
||||
|
}; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_default_value_is_less_than_min() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { MinValue = 10, DefaultValue = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Default value must be greater than min value.", "DefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_default_value_is_greater_than_min() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { MaxValue = 0, DefaultValue = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Default value must be less than max value.", "DefaultValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_greater_than_max() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { MinValue = 10, MaxValue = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max value must be greater than min value.", "MinValue", "MaxValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_allowed_values_and_max_value_is_specified() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { MaxValue = 10, AllowedValues = ImmutableList.Create(4d) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Either allowed values or min and max value can be defined.", "AllowedValues", "MinValue", "MaxValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_allowed_values_and_min_value_is_specified() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { MinValue = 10, AllowedValues = ImmutableList.Create(4d) }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Either allowed values or min and max value can be defined.", "AllowedValues", "MinValue", "MaxValue") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_radio_button_has_no_allowed_values() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { Editor = NumberFieldEditor.Radio }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Radio buttons or dropdown list need allowed values.", "AllowedValues") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new NumberFieldProperties { Editor = (NumberFieldEditor)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor is not a valid value.", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ReferencesFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class ReferencesFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_greater_than_max() |
||||
|
{ |
||||
|
var sut = new ReferencesFieldProperties { MinItems = 10, MaxItems = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max items must be greater than min items.", "MinItems", "MaxItems") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
// ==========================================================================
|
||||
|
// StringFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class StringFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_greater_than_max() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { MinLength = 10, MaxLength = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max length must be greater than min length.", "MinLength", "MaxLength") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_allowed_values_and_max_value_is_specified() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { MinLength = 10, AllowedValues = ImmutableList.Create("4") }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Either allowed values or min and max length can be defined.", "AllowedValues", "MinLength", "MaxLength") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_allowed_values_and_min_value_is_specified() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { MaxLength = 10, AllowedValues = ImmutableList.Create("4") }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Either allowed values or min and max length can be defined.", "AllowedValues", "MinLength", "MaxLength") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_radio_button_has_no_allowed_values() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { Editor = StringFieldEditor.Radio }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Radio buttons or dropdown list need allowed values.", "AllowedValues") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { Editor = (StringFieldEditor)123 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor is not a valid value.", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_pattern_is_not_valid_regex() |
||||
|
{ |
||||
|
var sut = new StringFieldProperties { Pattern = "[0-9{1}" }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Pattern is not a valid expression.", "Pattern") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// TagsFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards.FieldProperties |
||||
|
{ |
||||
|
public class TagsFieldPropertiesTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_add_error_if_min_greater_than_max() |
||||
|
{ |
||||
|
var sut = new TagsFieldProperties { MinItems = 10, MaxItems = 5 }; |
||||
|
|
||||
|
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Max items must be greater than min items.", "MinItems", "MaxItems") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,247 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GuardSchemaFieldTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Domain.Apps.Core; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
#pragma warning disable SA1310 // Field names must not contain underscore
|
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards |
||||
|
{ |
||||
|
public class GuardSchemaFieldTests |
||||
|
{ |
||||
|
private readonly Schema schema_0; |
||||
|
private readonly StringFieldProperties validProperties = new StringFieldProperties(); |
||||
|
private readonly StringFieldProperties invalidProperties = new StringFieldProperties { MinLength = 10, MaxLength = 5 }; |
||||
|
|
||||
|
public GuardSchemaFieldTests() |
||||
|
{ |
||||
|
schema_0 = |
||||
|
new Schema("my-schema") |
||||
|
.AddField(new StringField(1, "field1", Partitioning.Invariant)) |
||||
|
.AddField(new StringField(2, "field2", Partitioning.Invariant)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanHide_should_throw_exception_if_already_hidden() |
||||
|
{ |
||||
|
var command = new HideField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.HideField(1); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanHide(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanHide_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new HideField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanHide(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanHide_hould_not_throw_exception_if_visible() |
||||
|
{ |
||||
|
var command = new HideField { FieldId = 1 }; |
||||
|
|
||||
|
GuardSchemaField.CanHide(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDisable_should_throw_exception_if_already_disabled() |
||||
|
{ |
||||
|
var command = new DisableField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.DisableField(1); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanDisable(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDisable_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new DisableField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanDisable(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDisable_Should_not_throw_exception_if_enabled() |
||||
|
{ |
||||
|
var command = new DisableField { FieldId = 1 }; |
||||
|
|
||||
|
GuardSchemaField.CanDisable(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanShow_should_throw_exception_if_already_shown() |
||||
|
{ |
||||
|
var command = new ShowField { FieldId = 1 }; |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanShow(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanShow_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new ShowField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanShow(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanShow_should_not_throw_exception_if_hidden() |
||||
|
{ |
||||
|
var command = new ShowField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.HideField(1); |
||||
|
|
||||
|
GuardSchemaField.CanShow(schema_1, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanEnable_should_throw_exception_if_already_enabled() |
||||
|
{ |
||||
|
var command = new EnableField { FieldId = 1 }; |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanEnable(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanEnable_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new EnableField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanEnable(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanEnable_should_not_throw_exception_if_disabled() |
||||
|
{ |
||||
|
var command = new EnableField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.DisableField(1); |
||||
|
|
||||
|
GuardSchemaField.CanEnable(schema_1, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanLock_should_throw_exception_if_already_locked() |
||||
|
{ |
||||
|
var command = new LockField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.LockField(1); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanLock(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void LockField_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new LockField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanLock(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanLock_should_not_throw_exception_if_not_locked() |
||||
|
{ |
||||
|
var command = new LockField { FieldId = 1 }; |
||||
|
|
||||
|
GuardSchemaField.CanLock(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDelete_should_throw_exception_if_not_found() |
||||
|
{ |
||||
|
var command = new DeleteField { FieldId = 3 }; |
||||
|
|
||||
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanDelete(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDelete_should_throw_exception_if_locked() |
||||
|
{ |
||||
|
var command = new DeleteField { FieldId = 1 }; |
||||
|
|
||||
|
var schema_1 = schema_0.LockField(1); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanDelete(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDelete_should_not_throw_exception_if_not_locked() |
||||
|
{ |
||||
|
var command = new DeleteField { FieldId = 1 }; |
||||
|
|
||||
|
GuardSchemaField.CanDelete(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanUpdate_should_throw_exception_if_locked() |
||||
|
{ |
||||
|
var command = new UpdateField { FieldId = 1, Properties = new StringFieldProperties() }; |
||||
|
|
||||
|
var schema_1 = schema_0.LockField(1); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanUpdate(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanUpdate_should_not_throw_exception_if_not_locked() |
||||
|
{ |
||||
|
var command = new UpdateField { FieldId = 1, Properties = new StringFieldProperties() }; |
||||
|
|
||||
|
GuardSchemaField.CanUpdate(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanAdd_should_throw_exception_if_field_already_exists() |
||||
|
{ |
||||
|
var command = new AddField { Name = "field1", Properties = new StringFieldProperties() }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchemaField.CanAdd(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanAdd_should_throw_exception_if_name_not_valid() |
||||
|
{ |
||||
|
var command = new AddField { Name = "INVALID_NAME", Properties = validProperties }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchemaField.CanAdd(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanAdd_should_throw_exception_if_properties_not_valid() |
||||
|
{ |
||||
|
var command = new AddField { Name = "field3", Properties = invalidProperties }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchemaField.CanAdd(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanAdd_should_throw_exception_if_partitioning_not_valid() |
||||
|
{ |
||||
|
var command = new AddField { Name = "field3", Partitioning = "INVALID_PARTITIONING", Properties = validProperties }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchemaField.CanAdd(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanAdd_should_not_throw_exception_if_field_not_exists() |
||||
|
{ |
||||
|
var command = new AddField { Name = "field3", Properties = new StringFieldProperties() }; |
||||
|
|
||||
|
GuardSchemaField.CanAdd(schema_0, command); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,201 @@ |
|||||
|
// ==========================================================================
|
||||
|
// GuardSchemaTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Squidex.Domain.Apps.Core; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
#pragma warning disable SA1310 // Field names must not contain underscore
|
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Guards |
||||
|
{ |
||||
|
public class GuardSchemaTests |
||||
|
{ |
||||
|
private readonly IAppProvider appProvider = A.Fake<IAppProvider>(); |
||||
|
private readonly Schema schema_0; |
||||
|
private readonly NamedId<Guid> appId = new NamedId<Guid>(Guid.NewGuid(), "my-app"); |
||||
|
|
||||
|
public GuardSchemaTests() |
||||
|
{ |
||||
|
schema_0 = |
||||
|
new Schema("my-schema") |
||||
|
.AddField(new StringField(1, "field1", Partitioning.Invariant)) |
||||
|
.AddField(new StringField(2, "field2", Partitioning.Invariant)); |
||||
|
|
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(A<Guid>.Ignored, "new-schema", false)) |
||||
|
.Returns(Task.FromResult<ISchemaEntity>(null)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public Task CanCreate_should_throw_exception_if_name_not_valid() |
||||
|
{ |
||||
|
var command = new CreateSchema { AppId = appId, Name = "INVALID NAME" }; |
||||
|
|
||||
|
return Assert.ThrowsAsync<ValidationException>(() => GuardSchema.CanCreate(command, appProvider)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public Task CanCreate_should_throw_exception_if_name_already_in_use() |
||||
|
{ |
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(A<Guid>.Ignored, "new-schema", false)) |
||||
|
.Returns(Task.FromResult(A.Fake<ISchemaEntity>())); |
||||
|
|
||||
|
var command = new CreateSchema { AppId = appId, Name = "new-schema" }; |
||||
|
|
||||
|
return Assert.ThrowsAsync<ValidationException>(() => GuardSchema.CanCreate(command, appProvider)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public Task CanCreate_should_throw_exception_if_fields_not_valid() |
||||
|
{ |
||||
|
var command = new CreateSchema |
||||
|
{ |
||||
|
AppId = appId, |
||||
|
Fields = new List<CreateSchemaField> |
||||
|
{ |
||||
|
new CreateSchemaField |
||||
|
{ |
||||
|
Name = null, |
||||
|
Properties = null, |
||||
|
Partitioning = "invalid" |
||||
|
}, |
||||
|
new CreateSchemaField |
||||
|
{ |
||||
|
Name = null, |
||||
|
Properties = InvalidProperties(), |
||||
|
Partitioning = "invalid" |
||||
|
} |
||||
|
}, |
||||
|
Name = "new-schema" |
||||
|
}; |
||||
|
|
||||
|
return Assert.ThrowsAsync<ValidationException>(() => GuardSchema.CanCreate(command, appProvider)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public Task CanCreate_should_throw_exception_if_fields_contain_duplicate_names() |
||||
|
{ |
||||
|
var command = new CreateSchema |
||||
|
{ |
||||
|
AppId = appId, |
||||
|
Fields = new List<CreateSchemaField> |
||||
|
{ |
||||
|
new CreateSchemaField |
||||
|
{ |
||||
|
Name = "field1", |
||||
|
Properties = ValidProperties(), |
||||
|
Partitioning = "invariant" |
||||
|
}, |
||||
|
new CreateSchemaField |
||||
|
{ |
||||
|
Name = "field1", |
||||
|
Properties = ValidProperties(), |
||||
|
Partitioning = "invariant" |
||||
|
} |
||||
|
}, |
||||
|
Name = "new-schema" |
||||
|
}; |
||||
|
|
||||
|
return Assert.ThrowsAsync<ValidationException>(() => GuardSchema.CanCreate(command, appProvider)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public Task CanCreate_should_not_throw_exception_if_command_is_valid() |
||||
|
{ |
||||
|
var command = new CreateSchema { AppId = appId, Name = "new-schema" }; |
||||
|
|
||||
|
return GuardSchema.CanCreate(command, appProvider); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanPublish_should_throw_exception_if_already_published() |
||||
|
{ |
||||
|
var command = new PublishSchema(); |
||||
|
|
||||
|
var schema_1 = schema_0.Publish(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchema.CanPublish(schema_1, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanPublish_should_not_throw_exception_if_not_published() |
||||
|
{ |
||||
|
var command = new PublishSchema(); |
||||
|
|
||||
|
GuardSchema.CanPublish(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanUnpublish_should_throw_exception_if_already_unpublished() |
||||
|
{ |
||||
|
var command = new UnpublishSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => GuardSchema.CanUnpublish(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanUnpublish_should_not_throw_exception_if_already_published() |
||||
|
{ |
||||
|
var command = new UnpublishSchema(); |
||||
|
|
||||
|
var schema_1 = schema_0.Publish(); |
||||
|
|
||||
|
GuardSchema.CanUnpublish(schema_1, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanReorder_should_throw_exception_if_field_ids_contains_invalid_id() |
||||
|
{ |
||||
|
var command = new ReorderFields { FieldIds = new List<long> { 1, 3 } }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchema.CanReorder(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanReorder_should_throw_exception_if_field_ids_do_not_covers_all_fields() |
||||
|
{ |
||||
|
var command = new ReorderFields { FieldIds = new List<long> { 1 } }; |
||||
|
|
||||
|
Assert.Throws<ValidationException>(() => GuardSchema.CanReorder(schema_0, command)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanReorder_should_not_throw_exception_if_field_ids_are_valid() |
||||
|
{ |
||||
|
var command = new ReorderFields { FieldIds = new List<long> { 1, 2 } }; |
||||
|
|
||||
|
GuardSchema.CanReorder(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void CanDelete_should_not_throw_exception() |
||||
|
{ |
||||
|
var command = new DeleteSchema(); |
||||
|
|
||||
|
GuardSchema.CanDelete(schema_0, command); |
||||
|
} |
||||
|
|
||||
|
private static StringFieldProperties ValidProperties() |
||||
|
{ |
||||
|
return new StringFieldProperties { MinLength = 10, MaxLength = 20 }; |
||||
|
} |
||||
|
|
||||
|
private static StringFieldProperties InvalidProperties() |
||||
|
{ |
||||
|
return new StringFieldProperties { MinLength = 20, MaxLength = 10 }; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,281 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SchemaCommandMiddlewareTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.TestHelpers; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas |
||||
|
{ |
||||
|
public class SchemaCommandMiddlewareTests : HandlerTestBase<SchemaDomainObject> |
||||
|
{ |
||||
|
private readonly IAppProvider appProvider = A.Fake<IAppProvider>(); |
||||
|
private readonly SchemaCommandMiddleware sut; |
||||
|
private readonly SchemaDomainObject schema; |
||||
|
private readonly FieldRegistry registry = new FieldRegistry(new TypeNameRegistry()); |
||||
|
private readonly string fieldName = "age"; |
||||
|
|
||||
|
protected override Guid Id |
||||
|
{ |
||||
|
get { return SchemaId; } |
||||
|
} |
||||
|
|
||||
|
public SchemaCommandMiddlewareTests() |
||||
|
{ |
||||
|
schema = new SchemaDomainObject(registry); |
||||
|
|
||||
|
sut = new SchemaCommandMiddleware(Handler, appProvider); |
||||
|
|
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName, false)) |
||||
|
.Returns((ISchemaEntity)null); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Create_should_create_schema_domain_object() |
||||
|
{ |
||||
|
var context = CreateContextForCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId }); |
||||
|
|
||||
|
await TestCreate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal(SchemaId, context.Result<EntityCreatedResult<Guid>>().IdOrValue); |
||||
|
|
||||
|
A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName, false)).MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateSchema_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new UpdateSchema { Properties = new SchemaProperties() }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task ReorderSchema_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new ReorderFields { FieldIds = new List<long>() }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task PublishSchema_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new PublishSchema()); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UnpublishSchema_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
PublishSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new UnpublishSchema()); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task ConfigureScripts_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new ConfigureScripts()); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DeleteSchema_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new DeleteSchema()); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Add_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
|
||||
|
Assert.Equal(1, context.Result<EntityCreatedResult<long>>().IdOrValue); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task LockField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new LockField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task HideField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new HideField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task ShowField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
HideField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new ShowField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DisableField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new DisableField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task EnableField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
DisableField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new EnableField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DeleteField_should_update_domain_object() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
var context = CreateContextForCommand(new DeleteField { FieldId = 1 }); |
||||
|
|
||||
|
await TestUpdate(schema, async _ => |
||||
|
{ |
||||
|
await sut.HandleAsync(context); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private void CreateSchema() |
||||
|
{ |
||||
|
schema.Create(CreateCommand(new CreateSchema { Name = SchemaName })); |
||||
|
} |
||||
|
|
||||
|
private void PublishSchema() |
||||
|
{ |
||||
|
schema.Publish(CreateCommand(new PublishSchema())); |
||||
|
} |
||||
|
|
||||
|
private void CreateField() |
||||
|
{ |
||||
|
schema.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); |
||||
|
} |
||||
|
|
||||
|
private void HideField() |
||||
|
{ |
||||
|
schema.HideField(CreateCommand(new HideField { FieldId = 1 })); |
||||
|
} |
||||
|
|
||||
|
private void DisableField() |
||||
|
{ |
||||
|
schema.DisableField(CreateCommand(new DisableField { FieldId = 1 })); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,663 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SchemaDomainObjectTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.TestHelpers; |
||||
|
using Squidex.Domain.Apps.Events.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas |
||||
|
{ |
||||
|
public class SchemaDomainObjectTests : HandlerTestBase<SchemaDomainObject> |
||||
|
{ |
||||
|
private readonly string fieldName = "age"; |
||||
|
private readonly NamedId<long> fieldId; |
||||
|
private readonly SchemaDomainObject sut; |
||||
|
|
||||
|
protected override Guid Id |
||||
|
{ |
||||
|
get { return SchemaId; } |
||||
|
} |
||||
|
|
||||
|
public SchemaDomainObjectTests() |
||||
|
{ |
||||
|
fieldId = new NamedId<long>(1, fieldName); |
||||
|
|
||||
|
var fieldRegistry = new FieldRegistry(new TypeNameRegistry()); |
||||
|
|
||||
|
sut = new SchemaDomainObject(fieldRegistry); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Create_should_throw_exception_if_created() |
||||
|
{ |
||||
|
sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Create_should_create_schema_and_create_events() |
||||
|
{ |
||||
|
var properties = new SchemaProperties(); |
||||
|
|
||||
|
sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties })); |
||||
|
|
||||
|
Assert.Equal(AppId, sut.State.AppId); |
||||
|
|
||||
|
Assert.Equal(SchemaName, sut.State.Name); |
||||
|
Assert.Equal(SchemaName, sut.State.SchemaDef.Name); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaCreated { Name = SchemaName, Properties = properties }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Create_should_create_schema_with_initial_fields() |
||||
|
{ |
||||
|
var properties = new SchemaProperties(); |
||||
|
|
||||
|
var fields = new List<CreateSchemaField> |
||||
|
{ |
||||
|
new CreateSchemaField { Name = "field1", Properties = ValidProperties() }, |
||||
|
new CreateSchemaField { Name = "field2", Properties = ValidProperties() } |
||||
|
}; |
||||
|
|
||||
|
sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, Properties = properties, Fields = fields })); |
||||
|
|
||||
|
var @event = (SchemaCreated)sut.GetUncomittedEvents().Single().Payload; |
||||
|
|
||||
|
Assert.Equal(AppId, sut.State.AppId); |
||||
|
Assert.Equal(SchemaName, sut.State.Name); |
||||
|
Assert.Equal(SchemaName, sut.State.SchemaDef.Name); |
||||
|
|
||||
|
Assert.Equal(2, @event.Fields.Count); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Update_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Update_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Update_should_refresh_properties_and_create_events() |
||||
|
{ |
||||
|
var properties = new SchemaProperties(); |
||||
|
|
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.Update(CreateCommand(new UpdateSchema { Properties = properties })); |
||||
|
|
||||
|
Assert.Equal(properties, sut.State.SchemaDef.Properties); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaUpdated { Properties = properties }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ConfigureScripts_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ConfigureScripts_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ConfigureScripts_should_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.ConfigureScripts(CreateCommand(new ConfigureScripts |
||||
|
{ |
||||
|
ScriptQuery = "<script-query>", |
||||
|
ScriptCreate = "<script-create>", |
||||
|
ScriptUpdate = "<script-update>", |
||||
|
ScriptDelete = "<script-delete>", |
||||
|
ScriptChange = "<script-change>" |
||||
|
})); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new ScriptsConfigured |
||||
|
{ |
||||
|
ScriptQuery = "<script-query>", |
||||
|
ScriptCreate = "<script-create>", |
||||
|
ScriptUpdate = "<script-update>", |
||||
|
ScriptDelete = "<script-delete>", |
||||
|
ScriptChange = "<script-change>" |
||||
|
}) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Reorder_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List<long>() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Reorder_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List<long>() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Reorder_should_refresh_properties_and_create_events() |
||||
|
{ |
||||
|
var fieldIds = new List<long> { 1, 2 }; |
||||
|
|
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.Add(CreateCommand(new AddField { Name = "field1", Properties = ValidProperties() })); |
||||
|
sut.Add(CreateCommand(new AddField { Name = "field2", Properties = ValidProperties() })); |
||||
|
|
||||
|
sut.ClearUncommittedEvents(); |
||||
|
|
||||
|
sut.Reorder(CreateCommand(new ReorderFields { FieldIds = fieldIds })); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaFieldsReordered { FieldIds = fieldIds }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Publish_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Publish(CreateCommand(new PublishSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Publish_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Publish(CreateCommand(new PublishSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Publish_should_refresh_properties_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.Publish(CreateCommand(new PublishSchema())); |
||||
|
|
||||
|
Assert.True(sut.State.SchemaDef.IsPublished); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaPublished()) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Unpublish_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Unpublish(CreateCommand(new UnpublishSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Unpublish_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Unpublish(CreateCommand(new UnpublishSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Unpublish_should_refresh_properties_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
PublishSchema(); |
||||
|
|
||||
|
sut.Unpublish(CreateCommand(new UnpublishSchema())); |
||||
|
|
||||
|
Assert.False(sut.State.SchemaDef.IsPublished); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaUnpublished()) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Delete_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Delete(CreateCommand(new DeleteSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Delete_should_throw_exception_if_already_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Delete(CreateCommand(new DeleteSchema())); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Delete_should_refresh_properties_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.Delete(CreateCommand(new DeleteSchema())); |
||||
|
|
||||
|
Assert.True(sut.State.IsDeleted); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new SchemaDeleted()) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void AddField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = ValidProperties() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void AddField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Add_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
var properties = new NumberFieldProperties(); |
||||
|
|
||||
|
CreateSchema(); |
||||
|
|
||||
|
sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = properties })); |
||||
|
|
||||
|
Assert.Equal(properties, sut.State.SchemaDef.FieldsById[1].RawProperties); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldAdded { Name = fieldName, FieldId = fieldId, Properties = properties }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void UpdateField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void UpdateField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.UpdateField(CreateCommand(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(CreateCommand(new UpdateField { FieldId = 1, Properties = properties })); |
||||
|
|
||||
|
Assert.Equal(properties, sut.State.SchemaDef.FieldsById[1].RawProperties); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldUpdated { FieldId = fieldId, Properties = properties }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void LockField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.LockField(CreateCommand(new LockField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void LockField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.LockField(CreateCommand(new LockField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void LockField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.LockField(CreateCommand(new LockField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.False(sut.State.SchemaDef.FieldsById[1].IsDisabled); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldLocked { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void HideField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.HideField(CreateCommand(new HideField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void HideField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.HideField(CreateCommand(new HideField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void HideField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.HideField(CreateCommand(new HideField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.True(sut.State.SchemaDef.FieldsById[1].IsHidden); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldHidden { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ShowField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ShowField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void ShowField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.HideField(CreateCommand(new HideField { FieldId = 1 })); |
||||
|
sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.False(sut.State.SchemaDef.FieldsById[1].IsHidden); |
||||
|
|
||||
|
sut.GetUncomittedEvents().Skip(1) |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldShown { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DisableField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DisableField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DisableField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.True(sut.State.SchemaDef.FieldsById[1].IsDisabled); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldDisabled { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void EnableField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void EnableField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void EnableField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); |
||||
|
sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.False(sut.State.SchemaDef.FieldsById[1].IsDisabled); |
||||
|
|
||||
|
sut.GetUncomittedEvents().Skip(1) |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldEnabled { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DeleteField_should_throw_exception_if_not_created() |
||||
|
{ |
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DeleteField_should_throw_exception_if_schema_is_deleted() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
DeleteSchema(); |
||||
|
|
||||
|
Assert.Throws<DomainException>(() => |
||||
|
{ |
||||
|
sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DeleteField_should_update_schema_and_create_events() |
||||
|
{ |
||||
|
CreateSchema(); |
||||
|
CreateField(); |
||||
|
|
||||
|
sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); |
||||
|
|
||||
|
Assert.False(sut.State.SchemaDef.FieldsById.ContainsKey(1)); |
||||
|
|
||||
|
sut.GetUncomittedEvents() |
||||
|
.ShouldHaveSameEvents( |
||||
|
CreateEvent(new FieldDeleted { FieldId = fieldId }) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
private void CreateField() |
||||
|
{ |
||||
|
sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); |
||||
|
sut.ClearUncommittedEvents(); |
||||
|
} |
||||
|
|
||||
|
private void CreateSchema() |
||||
|
{ |
||||
|
sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); |
||||
|
sut.ClearUncommittedEvents(); |
||||
|
} |
||||
|
|
||||
|
private void PublishSchema() |
||||
|
{ |
||||
|
sut.Publish(CreateCommand(new PublishSchema())); |
||||
|
sut.ClearUncommittedEvents(); |
||||
|
} |
||||
|
|
||||
|
private void DeleteSchema() |
||||
|
{ |
||||
|
sut.Delete(CreateCommand(new DeleteSchema())); |
||||
|
sut.ClearUncommittedEvents(); |
||||
|
} |
||||
|
|
||||
|
private static StringFieldProperties ValidProperties() |
||||
|
{ |
||||
|
return new StringFieldProperties { MinLength = 10, MaxLength = 20 }; |
||||
|
} |
||||
|
|
||||
|
private static StringFieldProperties InvalidProperties() |
||||
|
{ |
||||
|
return new StringFieldProperties { MinLength = 20, MaxLength = 10 }; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue