Пример #1
0
        public void Serialize_with_schemaNamespace_returns_false_if_error_in_model()
        {
            var model      = new EdmModel(DataSpace.SSpace);
            var mockWriter = new Mock <XmlWriter>();

            // add EntityType with no properties which will cause error
            var et = new EntityType("TestEntity", "TestNamespace", DataSpace.SSpace);

            model.AddItem(et);

            var validationErrors = new List <DataModelErrorEventArgs>();
            var serializer       = new SsdlSerializer();

            serializer.OnError += (_, e) => validationErrors.Add(e);
            Assert.False(serializer.Serialize(
                             model,
                             "MyNamespace",
                             ProviderRegistry.Sql2008_ProviderInfo.ProviderInvariantName,
                             ProviderRegistry.Sql2008_ProviderInfo.ProviderManifestToken,
                             mockWriter.Object,
                             false));
            Assert.Equal(1, validationErrors.Count());
            Assert.Equal(
                Strings.EdmModel_Validator_Semantic_KeyMissingOnEntityType("TestEntity"),
                validationErrors[0].ErrorMessage);
            mockWriter.Verify(m => m.WriteStartDocument(), Times.Never());
        }
Пример #2
0
        public void Serialize_without_schemaNamespace_returns_false_if_multiple_Containers()
        {
            var mockModel = new Mock <EdmModel>(DataSpace.SSpace, XmlConstants.SchemaVersionLatest);

            mockModel.Setup(m => m.Containers).Returns(
                new EntityContainer[] {
                new EntityContainer("Container1", DataSpace.SSpace),
                new EntityContainer("Container2", DataSpace.SSpace)
            });
            var mockWriter = new Mock <XmlWriter>();

            var validationErrors = new List <DataModelErrorEventArgs>();
            var serializer       = new SsdlSerializer();

            serializer.OnError += (_, e) => validationErrors.Add(e);
            Assert.False(serializer.Serialize(
                             mockModel.Object,
                             ProviderRegistry.Sql2008_ProviderInfo.ProviderInvariantName,
                             ProviderRegistry.Sql2008_ProviderInfo.ProviderManifestToken,
                             mockWriter.Object,
                             false));
            Assert.Equal(1, validationErrors.Count());
            Assert.Equal(Strings.Serializer_OneNamespaceAndOneContainer, validationErrors[0].ErrorMessage);
            mockWriter.Verify(m => m.WriteStartDocument(), Times.Never());
        }
 public virtual bool Serialize(
     EdmModel dbDatabase,
     string provider,
     string providerManifestToken,
     XmlWriter xmlWriter,
     bool serializeDefaultNullability = true)
 {
     Check.NotNull <EdmModel>(dbDatabase, nameof(dbDatabase));
     Check.NotEmpty(provider, nameof(provider));
     Check.NotEmpty(providerManifestToken, nameof(providerManifestToken));
     Check.NotNull <XmlWriter>(xmlWriter, nameof(xmlWriter));
     if (!this.ValidateModel(dbDatabase))
     {
         return(false);
     }
     SsdlSerializer.CreateVisitor(xmlWriter, dbDatabase, serializeDefaultNullability).Visit(dbDatabase, provider, providerManifestToken);
     return(true);
 }
        public void Serialize_without_schemaNamespace_returns_false_if_multiple_NamespaceNames()
        {
            var mockModel = new Mock<EdmModel>(DataSpace.SSpace, XmlConstants.SchemaVersionLatest);
            mockModel.Setup(m => m.NamespaceNames).Returns(new string[2]);
            var mockWriter = new Mock<XmlWriter>();

            var validationErrors = new List<DataModelErrorEventArgs>();
            var serializer = new SsdlSerializer();
            serializer.OnError += (_, e) => validationErrors.Add(e);
            Assert.False(serializer.Serialize(
                mockModel.Object,
                ProviderRegistry.Sql2008_ProviderInfo.ProviderInvariantName,
                ProviderRegistry.Sql2008_ProviderInfo.ProviderManifestToken,
                mockWriter.Object,
                false));
            Assert.Equal(1, validationErrors.Count());
            Assert.Equal(Strings.Serializer_OneNamespaceAndOneContainer, validationErrors[0].ErrorMessage);
            mockWriter.Verify(m => m.WriteStartDocument(), Times.Never());
        }
Пример #5
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));
            }
        }
        private static void ValidateModel(DbModel model, List<EdmSchemaError> errors)
        {
            var settings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment };
            using (var writer = XmlWriter.Create(new StringBuilder(), settings))
            {
                var ssdlSerializer = new SsdlSerializer();
                ssdlSerializer.OnError +=
                    CreateOnErrorEventHandler(errors, ErrorCodes.GenerateModelFromDbReverseEngineerStoreModelFailed);

                ssdlSerializer.Serialize(
                    model.StoreModel,
                    model.ProviderInfo.ProviderInvariantName,
                    model.ProviderInfo.ProviderManifestToken,
                    writer);

                var csdlSerializer = new CsdlSerializer();
                csdlSerializer.OnError +=
                    CreateOnErrorEventHandler(errors, ErrorCodes.GenerateModelFromDbInvalidConceptualModel);

                csdlSerializer.Serialize(model.ConceptualModel, writer);

                new MslSerializerWrapper().Serialize(model, writer);
            }
        }
Пример #7
0
        private static List<DataModelErrorEventArgs> SerializeAndValidateSsdl(this EdmModel model, XmlWriter writer)
        {
            var providerInfoProperty = typeof(EdmModel).GetProperty("ProviderInfo", BindingFlags.Instance | BindingFlags.NonPublic);
            var providerInfo = (DbProviderInfo)providerInfoProperty.GetValue(model);

            var validationErrors = new List<DataModelErrorEventArgs>();

            var serializer = new SsdlSerializer();
            serializer.OnError += (_, e) => validationErrors.Add(e);
            serializer.Serialize(model, providerInfo.ProviderInvariantName, providerInfo.ProviderManifestToken, writer, true);

            return validationErrors;
        }
        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));
            }
        }
        public void Serialize_with_schemaNamespace_returns_false_if_error_in_model()
        {
            var model = new EdmModel(DataSpace.SSpace);
            var mockWriter = new Mock<XmlWriter>();

            // add EntityType with no properties which will cause error
            var et = new EntityType("TestEntity", "TestNamespace", DataSpace.SSpace);
            model.AddItem(et);

            var validationErrors = new List<DataModelErrorEventArgs>();
            var serializer = new SsdlSerializer();
            serializer.OnError += (_, e) => validationErrors.Add(e);
            Assert.False(serializer.Serialize(
                model,
                "MyNamespace",
                ProviderRegistry.Sql2008_ProviderInfo.ProviderInvariantName,
                ProviderRegistry.Sql2008_ProviderInfo.ProviderManifestToken,
                mockWriter.Object,
                false));
            Assert.Equal(1, validationErrors.Count());
            Assert.Equal(
                Strings.EdmModel_Validator_Semantic_KeyMissingOnEntityType("TestEntity"),
                validationErrors[0].ErrorMessage);
            mockWriter.Verify(m => m.WriteStartDocument(), Times.Never());
        }