public void StringMaxLength()
        {
            var schema = new SimpleTypeSchema("Currency", typeof(string))
                         .SetStringMaxLength(3);

            schema.GetStringMaxLength() !.MaxLength.Should().Be(3);

            Property <string> currency = new Property <string>("Currency")
                                         .SetSchema(schema);

            var validationRules = ValidationProvider.Instance.GetValidationRules(currency).ToList();

            validationRules.Should().HaveCount(1);

            var messages = new MutablePropertyContainer()
                           .WithValue(currency, "USD")
                           .Validate(validationRules)
                           .ToList();

            messages.Should().BeEmpty();

            messages = new MutablePropertyContainer()
                       .WithValue(currency, "abcd")
                       .Validate(validationRules)
                       .ToList();

            messages[0].FormattedMessage.Should().Be("Value 'abcd' is too long (length: 4, maxLength: 3)");
        }
        public void RenderDouble()
        {
            var propertyDouble         = new Property <double>("propertyDouble");
            var propertyNullableDouble = new Property <double?>("propertyNullableDouble");

            var propertyRendererDouble         = new PropertyRenderer <double>(propertyDouble);
            var propertyRendererNullableDouble = new PropertyRenderer <double?>(propertyNullableDouble);

            var propertyContainer = new MutablePropertyContainer()
                                    .WithValue(propertyDouble, 10.1)
                                    .WithValue(propertyNullableDouble, 10.1);

            CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
            string render1 = propertyRendererDouble.Render(propertyContainer);
            string render2 = propertyRendererNullableDouble.Render(propertyContainer);

            render1.Should().Be(render2);

            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ru-Ru");
            string render3 = propertyRendererDouble.Render(propertyContainer);
            string render4 = propertyRendererNullableDouble.Render(propertyContainer);

            render3.Should().Be(render4);

            render1.Should().Be(render3);

            propertyContainer = new MutablePropertyContainer()
                                .WithValue(propertyNullableDouble, null);
            propertyRendererNullableDouble.Render(propertyContainer).Should().Be(null);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override IPropertyContainer ReadJson(
            JsonReader reader,
            Type objectType,
            IPropertyContainer?existingValue,
            bool hasExistingValue,
            JsonSerializer serializer)
        {
            IPropertySet?schema            = null;
            bool         hasSchemaFromType = false;
            bool         hasSchemaFromJson = false;

            var propertyContainer = new MutablePropertyContainer();

            IPropertySet?knownPropertySet = objectType.GetSchemaByKnownPropertySet();

            if (knownPropertySet != null)
            {
                schema            = knownPropertySet;
                hasSchemaFromType = true;
            }

            ISchemaRepository?schemaRepository = reader.AsMetadataProvider().GetMetadata <ISchemaRepository>();

            if (Options.ReadSchemaFirst)
            {
                JObject jObject = JObject.Load(reader);

                JProperty?jProperty = jObject.Property("$metadata.schema.compact");
                if (jProperty is { First : { } schemaBody })
Esempio n. 4
0
        public void calculate_value_if_base_property_value_provided()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex");

            container.GetValue(Name.Map(name => $"Calculated {name}")).Should().Be("Calculated Alex");
        }
Esempio n. 5
0
        public void calculated_value_should_be_null_if_base_property_was_set_to_null()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, null);

            container.GetValue(Name.Map(name => $"Calculated {name}")).Should().Be(null);
        }
        public void PropertyContainer()
        {
            var propertyContainer = new MutablePropertyContainer();
            var propertyValueA    = propertyContainer.SetValue("PropertyA", "ValueA");
            var propertyValueB    = propertyContainer.SetValue(new Property <int>("PropertyB"), 42);

            propertyValueA.Should().NotBeNull();
            propertyValueB.Should().NotBeNull();

            propertyContainer.GetValue(propertyValueA.Property).Should().Be("ValueA");
            propertyContainer.GetValueUntypedByName("PropertyA").Should().Be("ValueA");

            dynamic dynamicContainer = propertyContainer.AsDynamic();
            object  valueA           = dynamicContainer.PropertyA;

            valueA.Should().Be("ValueA");

            object valueB = dynamicContainer.PropertyB;

            valueB.Should().Be(42);

            object notFoundProperty = dynamicContainer.NotFoundProperty;

            notFoundProperty.Should().BeNull();

            //TODO: override, parent
        }
Esempio n. 7
0
        public void map_property_with_null_value_allowed_if_base_property_was_set_to_null()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, null);

            container.GetValue(Name.Map(name => $"Name {name ?? "undefined"}", allowMapNull: true)).Should().Be("Name undefined");
        }
        public void map_filled_property()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex");

            container.GetValue(Name.Map(name => $"Name {name}")).Should().Be("Name Alex");
        }
        public void map_property_with_null_value()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, null);

            container.GetValue(Name.Map(name => $"Name {name}")).Should().Be(null);
        }
Esempio n. 10
0
        public void validate_and()
        {
            // Using And
            IEnumerable <IValidationRule> Rules1()
            {
                yield return(Age.NotDefault().And().ShouldBe(a => a > 18).WithMessage("Age should be over 18! but was {value}"));
            }

            // Not using And
            IEnumerable <IValidationRule> Rules2()
            {
                yield return(Age.NotDefault());

                yield return(Age.ShouldBe(a => a > 18).WithMessage("Age should be over 18! but was {value}"));
            }

            Validate(Rules1());
            Validate(Rules2());

            void Validate(IEnumerable <IValidationRule> rules)
            {
                IValidator validator = rules.Cached();

                var messages = new MutablePropertyContainer()
                               .WithValue(Age, 0)
                               .Validate(validator)
                               .ToList();

                messages.Should().HaveCount(2);

                messages[0].FormattedMessage.Should().Be("Age should not have default value 0.");
                messages[1].FormattedMessage.Should().Be("Age should be over 18! but was 0");
            }
        }
Esempio n. 11
0
        public void validate_string_length()
        {
            IProperty <string> Name = new Property <string>("Name");

            IEnumerable <IValidationRule> Rules()
            {
                yield return(new StringMinLengthValidationRule(Name, new StringMinLength(3)));

                yield return(new StringMaxLengthValidationRule(Name, new StringMaxLength(4)));
            }

            var container = new MutablePropertyContainer()
                            .WithValue(Name, "12345678");

            var messages = container.Validate(Rules().Cached()).ToList();

            messages.Should().HaveCount(1);
            messages[0].FormattedMessage.Should().Be("Value '12345678' is too long (length: 8, maxLength: 4)");

            container = new MutablePropertyContainer()
                        .WithValue(Name, "12");

            messages = container.Validate(Rules().Cached()).ToList();
            messages.Should().HaveCount(1);
            messages[0].FormattedMessage.Should().Be("Value '12' is too short (length: 2, minLength: 3)");

            container = new MutablePropertyContainer()
                        .WithValue(Name, "1234");

            messages = container.Validate(Rules().Cached()).ToList();
            messages.Should().HaveCount(0);
        }
Esempio n. 12
0
        public void simple_set_and_get_value()
        {
            IPropertyContainer propertyContainer = new MutablePropertyContainer()
                                                   .WithValue(EntityMeta.CreatedAt, DateTime.Today)
                                                   .WithValue(EntityMeta.Description, "description");

            propertyContainer.GetValue(EntityMeta.CreatedAt).Should().Be(DateTime.Today);
            propertyContainer.GetValue(EntityMeta.Description).Should().Be("description");
        }
        public void CacheResultMetadata()
        {
            IMutablePropertyContainer metadata    = new MutablePropertyContainer().WithValue(CacheResult.DataSource, "source");
            CacheResult <int>         cacheResult = new CacheResult <int>(new CacheSectionDescriptor <int>("int"), "key", 5, metadata: metadata, hitMiss: CacheHitMiss.Hit, error: null, isCached: true);

            cacheResult.GetDataSource().Should().Be("source");

            cacheResult.Metadata.Should().BeSameAs(metadata);
        }
Esempio n. 14
0
        public void get_value_untyped()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex")
                            .WithValue(Age, 42);

            container.GetValueUntyped(Name).Should().Be("Alex");
            container.GetValueUntyped(Age).Should().Be(42);
        }
Esempio n. 15
0
        public void simple_set_and_get()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex")
                            .WithValue(Age, 42);

            container.GetValue(Name).Should().Be("Alex");
            container.GetValue(Age).Should().Be(42);
        }
Esempio n. 16
0
        public void do_not_calculate_if_value_was_provided()
        {
            IProperty <string> mappedName = Name.Map(name => $"Calculated {name}");

            // Set value. It will not be calculated
            var container = new MutablePropertyContainer()
                            .WithValue(mappedName, "Alex");

            container.GetValue(mappedName).Should().Be("Alex");
        }
        public void map_absent_property_with_chaining_and_search_options()
        {
            var           container     = new MutablePropertyContainer();
            SearchOptions searchOptions = SearchOptions.ExistingOnly.CalculateValue();

            container
            .GetPropertyValue(Name
                              .Map(name => $"Name {name}", searchOptions: searchOptions)
                              .Map(text => text.Length), searchOptions)
            .Should().Be(null);
        }
        public void map_filled_property_with_chaining()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex");

            container
            .GetValue(Name
                      .Map(name => $"Name {name}")
                      .Map(text => text.Length))
            .Should().Be(9);
        }
Esempio n. 19
0
        public void map_absent_property_with_chaining()
        {
            var propertyValue = new MutablePropertyContainer()
                                .GetPropertyValue(Name
                                                  .Map(name => $"Name {name}")
                                                  .Map(text => text.Length));

            propertyValue.Should().NotBeNull();
            propertyValue.Property.Name.Should().Be(Name.Name);
            propertyValue.Value.Should().Be(default(int));
            propertyValue.Source.Should().Be(ValueSource.NotDefined);
        }
Esempio n. 20
0
        public void get_property_value()
        {
            IPropertyContainer propertyContainer = new MutablePropertyContainer()
                                                   .WithValue(EntityMeta.CreatedAt, DateTime.Today)
                                                   .WithValue(EntityMeta.Description, "description");

            IPropertyValue <string>?propertyValue = propertyContainer.GetPropertyValue(EntityMeta.Description);

            propertyValue.Should().NotBeNull();
            propertyValue.Property.Should().BeSameAs(EntityMeta.Description);
            propertyValue.Value.Should().Be("description");
            propertyValue.Source.Should().Be(ValueSource.Defined);
        }
Esempio n. 21
0
        public void validate_and_break_on_first_error()
        {
            IEnumerable <IValidationRule> Rules()
            {
                yield return(Age.NotDefault().And(breakOnFirstError: true).ShouldBe(a => a > 18).WithMessage("Age should be over 18! but was {value}"));
            }

            var messages = new MutablePropertyContainer()
                           .WithValue(Age, 0).Validate(Rules().Cached()).ToList();

            messages.Should().HaveCount(1);

            messages[0].FormattedMessage.Should().Be("Age should not have default value 0.");
        }
Esempio n. 22
0
        public IPropertyContainer CreateTestContainer()
        {
            var initialContainer = new MutablePropertyContainer()
                                   .WithValue(TestMeta.StringProperty, "Text")
                                   .WithValue(TestMeta.IntProperty, 42)
                                   .WithValue(TestMeta.DoubleProperty, 10.2)
                                   .WithValue(TestMeta.NullableIntProperty, null)
                                   .WithValue(TestMeta.BoolProperty, true)
                                   .WithValue(TestMeta.DateProperty, new LocalDate(2020, 12, 26))
                                   .WithValue(TestMeta.StringArray, new[] { "a1", "a2" })
                                   .WithValue(TestMeta.IntArray, new[] { 1, 2 })
            ;

            return(initialContainer);
        }
Esempio n. 23
0
        public void validate_or()
        {
            IEnumerable <IValidationRule> Rules()
            {
                yield return(Sex.ShouldBe(value => value == "Male").Or().ShouldBe(value => value == "Female"));
            }

            var messages = new MutablePropertyContainer()
                           .WithValue(Sex, "Other")
                           .Validate(Rules().Cached())
                           .ToList();

            messages.Should().HaveCount(1);

            messages[0].FormattedMessage.Should().Be("[Sex should match expression: (value == \"Male\") but value is 'Other'.] or [Sex should match expression: (value == \"Female\") but value is 'Other'.]");
        }
Esempio n. 24
0
        public void validate_not_exist()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex");

            IEnumerable <IValidationRule> Rules()
            {
                yield return(Name.Exists());

                yield return(Age.Exists());
            }

            var messages = container.Validate(Rules().Cached()).ToList();

            messages.Should().HaveCount(1);
            messages[0].FormattedMessage.Should().Be("Age is not exists.");
        }
        /// <inheritdoc />
        public IOperationManager <TSessionState, TOperationState> AddOperationManager(IOperationManager <TSessionState, TOperationState> operationManager)
        {
            if (operationManager.SessionManager != this)
            {
                throw new ArgumentException("OperationManager.SessionManager should be the same as target SessionManager", nameof(operationManager));
            }

            // Add SessionManager metadata to OperationManager
            var mergedMetadata = new MutablePropertyContainer(_metadata);

            mergedMetadata.SetValues(operationManager.Metadata);
            operationManager.UpdateSession(context => context.NewMetadata = mergedMetadata);

            SessionStorage.Set(operationManager.Session.Id.Value, operationManager);

            return(operationManager);
        }
Esempio n. 26
0
        public void validate_or_for_different_properties()
        {
            IEnumerable <IValidationRule> Rules()
            {
                yield return(Name.NotNull().Or(Age.NotDefault()));
            }

            var messages = new MutablePropertyContainer()
                           .WithValue(Name, null)
                           .WithValue(Age, 0)
                           .Validate(Rules().Cached())
                           .ToList();

            messages.Should().HaveCount(1);

            messages[0].FormattedMessage.Should().Be("[Name should not be null.] or [Age should not have default value 0.]");
        }
Esempio n. 27
0
        public void validate()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex Jr")
                            .WithValue(Age, 9);

            IEnumerable <IValidationRule> Rules()
            {
                yield return(Name.NotNull());

                yield return(Age.NotDefault().And().ShouldBe(a => a > 18).WithMessage("Age should be over 18! but was {value}"));
            }

            var messages = container.Validate(Rules().Cached()).ToList();

            messages.Should().HaveCount(1);
            messages[0].FormattedMessage.Should().Be("Age should be over 18! but was 9");
        }
        public object Create(object request, ISpecimenContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (request is Type type && type.IsAssignableTo <IPropertyContainer>())
            {
                IPropertySet knownPropertySet = type.GetSchemaByKnownPropertySet();

                if (knownPropertySet != null)
                {
                    var propertyContainer = new MutablePropertyContainer();
                    foreach (IProperty property in knownPropertySet.GetProperties())
                    {
                        object value = null;

                        if (property.GetAllowedValuesUntyped() is { ValuesUntyped: { } allowedValues })
        public void DynamicContainer()
        {
            var propertyContainer = new MutablePropertyContainer();

            propertyContainer.SetValue("PropertyA", "ValueA");
            propertyContainer.SetValue(new Property <int>("PropertyB"), 42);

            dynamic dynamicContainer = propertyContainer.AsDynamic();
            object  valueA           = dynamicContainer.PropertyA;

            valueA.Should().Be("ValueA");

            object valueB = dynamicContainer.PropertyB;

            valueB.Should().Be(42);

            object notFoundProperty = dynamicContainer.NotFoundProperty;

            notFoundProperty.Should().BeNull();
        }
Esempio n. 30
0
        public void validate()
        {
            var container = new MutablePropertyContainer()
                            .WithValue(Name, "Alex Jr")
                            .WithValue(Age, 9)
                            .WithValue(Sex, "Undefined");

            IEnumerable <IValidationRule> Rules()
            {
                yield return(Name.NotNull());

                yield return(Age.NotDefault().And().ShouldBe(a => a > 18).WithMessage("Age should be over 18! but was {value}"));

                yield return(Sex.OnlyAllowedValues().And().ShouldMatchNullability());
            }

            var messages = container.Validate(Rules().Cached()).ToList();

            messages.Should().HaveCount(2);
            messages[0].FormattedMessage.Should().Be("Age should be over 18! but was 9");
            messages[1].FormattedMessage.Should().Be("Sex can not be 'Undefined' because it is not in allowed values list.");
        }