예제 #1
0
        public async Task DictionaryModelBinder_CreatesEmptyCollection_IfIsTopLevelObject()
        {
            // Arrange
            var binder = new DictionaryModelBinder <string, string>(
                new SimpleTypeModelBinder(typeof(string)),
                new SimpleTypeModelBinder(typeof(string)));

            var context = CreateContext();

            context.IsTopLevelObject = true;

            // Lack of prefix and non-empty model name both ignored.
            context.ModelName = "modelName";

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForType(typeof(Dictionary <string, string>));

            context.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);

            Assert.Empty(Assert.IsType <Dictionary <string, string> >(result.Model));
            Assert.Equal("modelName", result.Key);
            Assert.True(result.IsModelSet);
        }
예제 #2
0
        public async Task DictionaryModelBinder_DoesNotCreateCollection_IfNotIsTopLevelObject(string prefix)
        {
            // Arrange
            var binder = new DictionaryModelBinder <int, int>(
                new SimpleTypeModelBinder(typeof(int)),
                new SimpleTypeModelBinder(typeof(int)));

            var context = CreateContext();

            context.ModelName = ModelNames.CreatePropertyModelName(prefix, "ListProperty");

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithDictionaryProperties),
                nameof(ModelWithDictionaryProperties.DictionaryProperty));

            context.ValueProvider = new TestValueProvider(new Dictionary <string, object>());

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.Equal(default(ModelBindingResult), result);
        }
예제 #3
0
        public async Task BindModel_FallsBackToBindingValues_WithValueTypes(IDictionary <long, int> dictionary)
        {
            // Arrange
            var stringDictionary = dictionary.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());

            var binder = new DictionaryModelBinder <long, int>(
                new SimpleTypeModelBinder(typeof(long)),
                new SimpleTypeModelBinder(typeof(int)));

            var context = CreateContext();

            context.ModelName     = "prefix";
            context.ValueProvider = CreateEnumerableValueProvider("prefix[{0}]", stringDictionary);
            context.FieldName     = context.ModelName;

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithDictionaryProperties),
                nameof(ModelWithDictionaryProperties.DictionaryWithValueTypesProperty));

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            Assert.Equal("prefix", result.Key);

            var resultDictionary = Assert.IsAssignableFrom <IDictionary <long, int> >(result.Model);

            Assert.Equal(dictionary, resultDictionary);
        }
예제 #4
0
        public async Task BindModel_FallsBackToBindingValues(
            string modelName,
            string keyFormat,
            IDictionary <string, string> dictionary)
        {
            // Arrange
            var binder = new DictionaryModelBinder <string, string>(
                new SimpleTypeModelBinder(typeof(string)),
                new SimpleTypeModelBinder(typeof(string)));

            var context = CreateContext();

            context.ModelName     = modelName;
            context.ValueProvider = CreateEnumerableValueProvider(keyFormat, dictionary);
            context.FieldName     = modelName;

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithDictionaryProperties),
                nameof(ModelWithDictionaryProperties.DictionaryProperty));

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.True(result.IsModelSet);
            Assert.Equal(modelName, result.Key);

            var resultDictionary = Assert.IsAssignableFrom <IDictionary <string, string> >(result.Model);

            Assert.Equal(dictionary, resultDictionary);
        }
예제 #5
0
        public async Task BindModel_Succeeds(bool isReadOnly)
        {
            // Arrange
            var values = new Dictionary <string, string>()
            {
                { "someName[0].Key", "42" },
                { "someName[0].Value", "forty-two" },
                { "someName[1].Key", "84" },
                { "someName[1].Value", "eighty-four" },
            };

            // Value Provider

            var bindingContext = GetModelBindingContext(isReadOnly, values);

            bindingContext.ValueProvider = CreateEnumerableValueProvider("{0}", values);

            var binder = new DictionaryModelBinder <int, string>(new SimpleTypeModelBinder(), new SimpleTypeModelBinder());

            // Act
            var result = await binder.BindModelResultAsync(bindingContext);

            // Assert
            Assert.True(result.IsModelSet);

            var dictionary = Assert.IsAssignableFrom <IDictionary <int, string> >(result.Model);

            Assert.NotNull(dictionary);
            Assert.Equal(2, dictionary.Count);
            Assert.Equal("forty-two", dictionary[42]);
            Assert.Equal("eighty-four", dictionary[84]);

            // This uses the default IValidationStrategy
            Assert.DoesNotContain(result.Model, bindingContext.ValidationState.Keys);
        }
예제 #6
0
        public async Task BindModel_DoesNotFallBack_WithoutEnumerableValueProvider()
        {
            // Arrange
            var dictionary = new Dictionary <string, string>(StringComparer.Ordinal)
            {
                { "one", "one" },
                { "two", "two" },
                { "three", "three" },
            };

            var binder = new DictionaryModelBinder <string, string>(
                new SimpleTypeModelBinder(typeof(string)),
                new SimpleTypeModelBinder(typeof(string)));

            var context = CreateContext();

            context.ModelName     = "prefix";
            context.ValueProvider = CreateTestValueProvider("prefix[{0}]", dictionary);
            context.FieldName     = context.ModelName;

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithDictionaryProperties),
                nameof(ModelWithDictionaryProperties.DictionaryProperty));

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            Assert.Equal("prefix", result.Key);

            var resultDictionary = Assert.IsAssignableFrom <IDictionary <string, string> >(result.Model);

            Assert.Empty(resultDictionary);
        }
예제 #7
0
        public async Task BindModel_FallsBackToBindingValues_WithComplexValues()
        {
            // Arrange
            var dictionary = new Dictionary <int, ModelWithProperties>
            {
                { 23, new ModelWithProperties {
                      Id = 43, Name = "Wilma"
                  } },
                { 27, new ModelWithProperties {
                      Id = 98, Name = "Fred"
                  } },
            };
            var stringDictionary = new Dictionary <string, string>
            {
                { "prefix[23].Id", "43" },
                { "prefix[23].Name", "Wilma" },
                { "prefix[27].Id", "98" },
                { "prefix[27].Name", "Fred" },
            };

            var context = CreateContext();

            context.ModelName     = "prefix";
            context.ValueProvider = CreateEnumerableValueProvider("{0}", stringDictionary);
            context.FieldName     = context.ModelName;

            var metadataProvider = new TestModelMetadataProvider();

            context.ModelMetadata = metadataProvider.GetMetadataForProperty(
                typeof(ModelWithDictionaryProperties),
                nameof(ModelWithDictionaryProperties.DictionaryWithComplexValuesProperty));

            var valueMetadata = metadataProvider.GetMetadataForType(typeof(ModelWithProperties));

            var binder = new DictionaryModelBinder <int, ModelWithProperties>(
                new SimpleTypeModelBinder(typeof(int)),
                new ComplexTypeModelBinder(new Dictionary <ModelMetadata, IModelBinder>()
            {
                { valueMetadata.Properties["Id"], new SimpleTypeModelBinder(typeof(int)) },
                { valueMetadata.Properties["Name"], new SimpleTypeModelBinder(typeof(string)) },
            }));

            // Act
            var result = await binder.BindModelResultAsync(context);

            // Assert
            Assert.NotEqual(default(ModelBindingResult), result);
            Assert.True(result.IsModelSet);
            Assert.Equal("prefix", result.Key);

            var resultDictionary = Assert.IsAssignableFrom <IDictionary <int, ModelWithProperties> >(result.Model);

            Assert.Equal(dictionary, resultDictionary);

            // This requires a non-default IValidationStrategy
            Assert.Contains(result.Model, context.ValidationState.Keys);
            var entry    = context.ValidationState[result.Model];
            var strategy = Assert.IsType <ShortFormDictionaryValidationStrategy <int, ModelWithProperties> >(entry.Strategy);

            Assert.Equal(
                new KeyValuePair <string, int>[]
            {
                new KeyValuePair <string, int>("23", 23),
                new KeyValuePair <string, int>("27", 27),
            }.OrderBy(kvp => kvp.Key),
                strategy.KeyMappings.OrderBy(kvp => kvp.Key));
        }