public void Can_get_and_set_configuration_facet()
        {
            var complexType = new EdmComplexType();
            complexType.SetConfiguration(42);

            Assert.Equal(42, complexType.GetConfiguration());
        }
        public void Configure_should_throw_when_property_not_found()
        {
            var complexType = new EdmComplexType { Name = "C" };
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            complexTypeConfiguration.Property(new PropertyPath(new MockPropertyInfo()), () => mockPropertyConfiguration.Object);

            Assert.Equal(Strings.PropertyNotFound(("P"), "C"), Assert.Throws<InvalidOperationException>(() => complexTypeConfiguration.Configure(complexType)).Message);
        }
        public void Configure_should_set_configuration()
        {
            var complexType = new EdmComplexType { Name = "C" };
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));

            complexTypeConfiguration.Configure(complexType);

            Assert.Same(complexTypeConfiguration, complexType.GetConfiguration());
        }
        public void AddPrimitiveProperty_should_create_and_add_to_primitive_properties()
        {
            var complexType = new EdmComplexType();

            var property = complexType.AddPrimitiveProperty("Foo");

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.True(complexType.Properties.Contains(property));
        }
        public void GetPrimitiveProperty_should_return_correct_property()
        {
            var complexType = new EdmComplexType();
            var property = complexType.AddPrimitiveProperty("Foo");

            var foundProperty = complexType.GetPrimitiveProperty("Foo");

            Assert.NotNull(foundProperty);
            Assert.Same(property, foundProperty);
        }
        public void Should_be_able_to_get_and_set_clr_type()
        {
            var complexType = new EdmComplexType();

            Assert.Null(complexType.GetClrType());

            complexType.SetClrType(typeof(object));

            Assert.Equal(typeof(object), complexType.GetClrType());
        }
        public void AddComplexProperty_should_create_and_add_complex_property()
        {
            var complexType = new EdmComplexType();

            var complexTypeProperty = new EdmComplexType();
            var property = complexType.AddComplexProperty("Foo", complexTypeProperty);

            Assert.NotNull(property);
            Assert.Equal("Foo", property.Name);
            Assert.Same(complexTypeProperty, property.PropertyType.ComplexType);
            Assert.True(complexType.Properties.Contains(property));
        }
        public static EdmProperty AsComplex(this EdmProperty property, EdmComplexType complexType)
        {
            //Contract.Requires(property != null);
            //Contract.Requires(complexType != null);

            property.PropertyType = new EdmTypeReference
                {
                    EdmType = complexType,
                    IsNullable = false
                };

            return property;
        }
        public void Configure_should_configure_properties()
        {
            var complexType = new EdmComplexType { Name = "C" };
            var property = complexType.AddPrimitiveProperty("P");
            property.PropertyType.EdmType = EdmPrimitiveType.Int32;
            var complexTypeConfiguration = new ComplexTypeConfiguration(typeof(object));
            var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>();
            var mockPropertyInfo = new MockPropertyInfo();
            complexTypeConfiguration.Property(new PropertyPath(mockPropertyInfo), () => mockPropertyConfiguration.Object);
            property.SetClrPropertyInfo(mockPropertyInfo);

            complexTypeConfiguration.Configure(complexType);

            mockPropertyConfiguration.Verify(p => p.Configure(property));
        }
        public void Map_should_map_complex_type_properties()
        {
            var complexType = new EdmComplexType();
            var mappingContext
                = new MappingContext(new ModelConfiguration(), new ConventionsConfiguration(), new EdmModel().Initialize());

            new PropertyMapper(new TypeMapper(mappingContext))
                .Map(new MockPropertyInfo(typeof(int), "Foo"), complexType, () => new ComplexTypeConfiguration(typeof(object)));

            Assert.Equal(1, complexType.DeclaredProperties.Count);

            var property = complexType.GetPrimitiveProperty("Foo");

            Assert.Equal(EdmPrimitiveType.Int32, property.PropertyType.PrimitiveType);
        }
        public void Map(
            PropertyInfo propertyInfo, EdmComplexType complexType,
            Func<ComplexTypeConfiguration> complexTypeConfiguration)
        {
            //Contract.Requires(propertyInfo != null);
            //Contract.Requires(complexType != null);

            var property = MapPrimitiveOrComplexOrEnumProperty(
                propertyInfo, complexTypeConfiguration, discoverComplexTypes: true);

            if (property != null)
            {
                complexType.DeclaredProperties.Add(property);
            }
        }
        public static EdmProperty AddComplexProperty(
            this EdmComplexType complexType, string name, EdmComplexType targetComplexType)
        {
            //Contract.Requires(complexType != null);
            //Contract.Requires(complexType.Properties != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));
            //Contract.Requires(targetComplexType != null);

            var property = new EdmProperty
                {
                    Name = name
                }.AsComplex(targetComplexType);

            complexType.DeclaredProperties.Add(property);

            return property;
        }
        public void Generate_should_flatten_complex_properties_to_columns()
        {
            var databaseMapping = CreateEmptyModel();
            var entityType = new EdmEntityType { Name = "E" };
            var complexType = new EdmComplexType { Name = "C" };
            complexType.AddPrimitiveProperty("P1").PropertyType.EdmType = EdmPrimitiveType.Int32;
            entityType.AddComplexProperty("C1", complexType);
            entityType.AddPrimitiveProperty("P2").PropertyType.EdmType = EdmPrimitiveType.String;
            var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType);
            entityType.SetClrType(typeof(object));

            new EntityTypeMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(entityType, databaseMapping);

            var entityTypeMappingFragment
                = databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().TypeMappingFragments.Single();

            Assert.Equal(2, entityTypeMappingFragment.PropertyMappings.Count());
            Assert.Equal(2, entityTypeMappingFragment.Table.Columns.Count());
        }
        public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel().Initialize(), new DbDatabaseMetadata());
            var entitySet = new EdmEntitySet();
            var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet);
            var entityTypeMapping = new DbEntityTypeMapping();
            entitySetMapping.EntityTypeMappings.Add(entityTypeMapping);
            var entityTypeMappingFragment = new DbEntityTypeMappingFragment();
            entityTypeMapping.TypeMappingFragments.Add(entityTypeMappingFragment);
            var propertyMapping1 = new DbEdmPropertyMapping();
            var complexType = new EdmComplexType();
            complexType.SetClrType(typeof(object));
            propertyMapping1.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } });
            entityTypeMappingFragment.PropertyMappings.Add(propertyMapping1);
            var propertyMapping2 = new DbEdmPropertyMapping();
            propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference() });
            propertyMapping2.PropertyPath.Add(new EdmProperty { PropertyType = new EdmTypeReference { EdmType = complexType } });
            entityTypeMappingFragment.PropertyMappings.Add(propertyMapping2);

            Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count());
        }
        public void ComplexType_apply_should_set_correct_defaults_for_unconfigured_strings()
        {
            var entityType = new EdmComplexType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.String;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmComplexType>)new PropertyMaxLengthConvention())
                .Apply(entityType, new EdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(true, primitiveTypeFacets.IsUnicode);
            Assert.Equal(false, primitiveTypeFacets.IsFixedLength);
            Assert.Null(primitiveTypeFacets.MaxLength);
            Assert.Equal(true, primitiveTypeFacets.IsMaxLength);
        }
 internal virtual void Configure(EdmComplexType complexType)
 {
     Configure(complexType.Name, complexType.DeclaredProperties, complexType.Annotations);
 }
        public void ComplexType_apply_should_set_correct_defaults_for_unconfigured_binary()
        {
            var entityType = new EdmComplexType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Binary;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmComplexType>)new SqlCePropertyMaxLengthConvention())
                .Apply(entityType, CreateEdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(4000, primitiveTypeFacets.MaxLength);
        }
        public static EdmComplexType AddComplexType(this EdmModel model, string name)
        {
            //Contract.Requires(model != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));
            Contract.Assert(model.Namespaces.Count == 1);

            var complexType = new EdmComplexType
                {
                    Name = name
                };

            model.Namespaces.Single().ComplexTypes.Add(complexType);

            return complexType;
        }
        public void ComplexType_apply_should_set_correct_defaults_for_fixed_length_binary()
        {
            var entityType = new EdmComplexType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Binary;
            property.PropertyType.PrimitiveTypeFacets.IsFixedLength = true;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmComplexType>)new PropertyMaxLengthConvention())
                .Apply(entityType, new EdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Null(primitiveTypeFacets.IsUnicode);
            Assert.Equal(128, primitiveTypeFacets.MaxLength);
            Assert.Equal(null, primitiveTypeFacets.IsMaxLength);
        }
        public void ComplexType_apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings()
        {
            var entityType = new EdmComplexType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.String;
            property.PropertyType.PrimitiveTypeFacets.IsFixedLength = true;
            property.PropertyType.PrimitiveTypeFacets.IsUnicode = false;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmComplexType>)new SqlCePropertyMaxLengthConvention())
                .Apply(entityType, CreateEdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(4000, primitiveTypeFacets.MaxLength);
        }