Example #1
0
        public void Create_factory_method_sets_properties_and_seals_the_type()
        {
            var entity =
                EntityType.Create(
                    "Customer",
                    "MyModel",
                    DataSpace.CSpace,
                    new[] { "Id" },
                    new[]
            {
                EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
            },
                    new[]
            {
                new MetadataProperty(
                    "TestProperty",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                    "value"),
            });

            Assert.Equal("MyModel.Customer", entity.FullName);
            Assert.Equal(DataSpace.CSpace, entity.DataSpace);
            Assert.True(new [] { "Id" }.SequenceEqual(entity.KeyMemberNames));
            Assert.True(new[] { "Id", "Name" }.SequenceEqual(entity.Members.Select(m => m.Name)));
            Assert.True(entity.IsReadOnly);

            var metadataProperty = entity.MetadataProperties.SingleOrDefault(p => p.Name == "TestProperty");

            Assert.NotNull(metadataProperty);
            Assert.Equal("value", metadataProperty.Value);
        }
Example #2
0
        public void Create_entity_type_throws_if_key_members_defined_on_both_base_and_derived_types()
        {
            var baseType =
                EntityType.Create(
                    "Base",
                    "MyModel",
                    DataSpace.CSpace,
                    new[] { "Id" },
                    new[]
            {
                EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
            },
                    null);

            Assert.Equal(
                Strings.CannotDefineKeysOnBothBaseAndDerivedTypes,
                Assert.Throws <ArgumentException>(
                    () =>
                    EntityType.Create(
                        "Derived",
                        "MyModel",
                        DataSpace.CSpace,
                        baseType,
                        new[] { "Id" },
                        new[]
            {
                EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
            },
                        null))
                .Message);
        }
Example #3
0
        public void Declared_members_should_be_accessible_by_name()
        {
            var entityType =
                EntityType.Create(
                    "Blog",
                    "BlogModel",
                    DataSpace.CSpace,
                    new[] { "Id", "Title" },
                    new[] {
                EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                EdmProperty.CreatePrimitive("Title", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
            },
                    null);

            Assert.NotNull(entityType.DeclaredMembers["Id"]);
            Assert.NotNull(entityType.DeclaredMembers["Title"]);
        }
Example #4
0
        public void Create_entity_type_having_base_type_with_key_members()
        {
            var baseType =
                EntityType.Create(
                    "Base",
                    "MyModel",
                    DataSpace.CSpace,
                    new[] { "Id" },
                    new[]
            {
                EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
            },
                    null);

            var entity =
                EntityType.Create(
                    "Derived",
                    "MyModel",
                    DataSpace.CSpace,
                    baseType,
                    null,
                    new[]
            {
                EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
            },
                    new[]
            {
                new MetadataProperty(
                    "TestProperty",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                    "value")
            });

            Assert.Same(baseType, entity.BaseType);
            Assert.Equal("MyModel.Derived", entity.FullName);
            Assert.Equal(DataSpace.CSpace, entity.DataSpace);
            Assert.True(new[] { "Id" }.SequenceEqual(entity.KeyMemberNames));
            Assert.True(new[] { "Id", "Name" }.SequenceEqual(entity.Members.Select(m => m.Name)));
            Assert.True(entity.IsReadOnly);

            var metadataProperty = entity.MetadataProperties.SingleOrDefault(p => p.Name == "TestProperty");

            Assert.NotNull(metadataProperty);
            Assert.Equal("value", metadataProperty.Value);
        }
        private static AssociationType CreateCSpaceAssociationType(int index)
        {
            var sourceProperty = new EdmProperty("SourceProperty");
            var targetProperty = new EdmProperty("TargetProperty");

            var sourceEntityType = EntityType.Create(
                "SourceEntityType" + index,
                "Namespace",
                DataSpace.CSpace,
                new [] { "SourceProperty" },
                new[] { sourceProperty },
                Enumerable.Empty <MetadataProperty>());
            var targetEntityType = EntityType.Create(
                "TargetEntityType" + index,
                "Namespace",
                DataSpace.CSpace,
                new[] { "TargetProperty" },
                new[] { targetProperty },
                Enumerable.Empty <MetadataProperty>());

            var sourceEnd = new AssociationEndMember("SourceEnd" + index, sourceEntityType);
            var targetEnd = new AssociationEndMember("TargetEnd" + index, targetEntityType);

            var constraint =
                new ReferentialConstraint(
                    sourceEnd,
                    targetEnd,
                    new[] { sourceProperty },
                    new[] { targetProperty });

            var associationType =
                AssociationType.Create(
                    "AssociationType" + index,
                    "Namespace",
                    true,
                    DataSpace.CSpace,
                    sourceEnd,
                    targetEnd,
                    constraint,
                    Enumerable.Empty <MetadataProperty>());

            return(associationType);
        }
Example #6
0
        public void Validation_error_not_reported_for_types_marked_as_invalid()
        {
            var invalidAttribute =
                MetadataProperty.Create(
                    "EdmSchemaInvalid",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                    true);

            var model = new EdmModel(DataSpace.SSpace);

            model.AddItem(
                EntityType.Create("E", "N", DataSpace.SSpace, new string[0], new EdmMember[0], new[] { invalidAttribute }));

            using (var writer = XmlWriter.Create(new StringBuilder()))
            {
                var ssdlSerializer = new SsdlSerializer();
                ssdlSerializer.OnError += (_, e) => { throw new Exception("Should not be invoked."); };

                Assert.True(ssdlSerializer.Serialize(model, "N", "invName", "42", writer));
            }
        }