public void JsonSerializationDeserializationTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Invoice>();
            builder.Entity <Article>().HasOne(p => p.ArticleSettings).WithPrincipal();
            builder.ApplyConventions();

            MetadataModel model = null;

            using (var ms = new MemoryStream())
            {
                var json = new JsonSerializerSettings();
                json.Converters.Add(new LucileJsonInheritanceConverter());

                var serializer = JsonSerializer.Create(json);
                using (var streamWriter = new StreamWriter(ms, Encoding.UTF8, 512, true))
                {
                    serializer.Serialize(streamWriter, builder);
                }

                ms.Seek(0, SeekOrigin.Begin);

                using (var streamReader = new StreamReader(ms))
                    using (var jsonReader = new JsonTextReader(streamReader))
                    {
                        var newBuilder = serializer.Deserialize <MetadataModelBuilder>(jsonReader);
                        model = newBuilder.ToModel();
                    }
            }

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
        public void DefaultStructureConventionTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Invoice>();
            builder.Entity <Article>().HasOne(p => p.ArticleSettings).WithPrincipal();
            builder.ApplyConventions();
            TestModelValidations.ValidateInvoiceArticleDefaultModel(builder);
        }
Beispiel #3
0
        public void MetadataModelBuilderMaxLengthTest()
        {
            var builder       = new MetadataModelBuilder();
            var entityBuilder = builder.Entity <Receipt>();

            builder.ApplyConventions();

            Assert.Equal(100, entityBuilder.Property(c => c.ReceiptNumber).MaxLength);
        }
Beispiel #4
0
        public void TestCustomAccessor_ExpectsNoError()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Article>();
            builder.Entity <ArticleName>();
            builder.Entity <Contact>();

            var model = builder.ApplyConventions().ToModel(new CustomAccessorFactory());

            Assert.NotNull(model);
        }
        public void OneToOneWithConfigDefaultStructureConventionTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Test1>().HasOne(p => p.Test2).WithPrincipal(p => p.Test1);
            builder.ApplyConventions();
            EntityMetadataBuilder entity = builder.Entity <Test1>();

            Assert.Equal(1, entity.PrimaryKey.Count);
            Assert.Equal("Id", entity.PrimaryKey.First());

            entity = builder.Entity <Test2>();

            Assert.Equal(1, entity.PrimaryKey.Count);
            Assert.Equal("Test2ID", entity.PrimaryKey.First());
        }
        public void OneToOneDefaultStructureConventionTest()
        {
            var builder = new MetadataModelBuilder();
            var entity  = builder.Entity <Test1>();

            builder.ApplyConventions();
            var entity2 = builder.Entity <Test2>();

            var nav1 = entity.Navigation("Test2");
            var nav2 = entity2.Navigation("Test1");

            Assert.Equal(NavigationPropertyMultiplicity.ZeroOrOne, nav1.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.Many, nav1.TargetMultiplicity);

            Assert.Equal(NavigationPropertyMultiplicity.ZeroOrOne, nav2.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.Many, nav2.TargetMultiplicity);
        }
        public void DefaultKeyConventionTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Test1>().HasOne(p => p.Test2).WithPrincipal(p => p.Test1);
            builder.ApplyConventions();
            var entity  = builder.Entity <Test1>();
            var entity2 = builder.Entity <Test2>();

            var nav1 = entity.Navigation("Test2");
            var nav2 = entity2.Navigation("Test1");

            Assert.Equal(NavigationPropertyMultiplicity.ZeroOrOne, nav1.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.One, nav1.TargetMultiplicity);

            Assert.Equal(NavigationPropertyMultiplicity.One, nav2.Multiplicity);
            Assert.Equal(NavigationPropertyMultiplicity.ZeroOrOne, nav2.TargetMultiplicity);
        }
        public void DataContractSerializationDeserializationTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Invoice>();
            builder.Entity <Article>().HasOne(p => p.ArticleSettings).WithPrincipal();
            builder.ApplyConventions();

            MetadataModel model = null;

            using (var ms = new MemoryStream())
            {
                var ser = new DataContractSerializer(typeof(MetadataModelBuilder));
                ser.WriteObject(ms, builder);
                ms.Seek(0, SeekOrigin.Begin);
                var newBuilder = (MetadataModelBuilder)ser.ReadObject(ms);
                model = newBuilder.ToModel();
            }

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
Beispiel #9
0
        private static MetadataModel GetModel()
        {
            var metadataModelBuilder = new MetadataModelBuilder();

            metadataModelBuilder.Exclude <EntityBase>();
            metadataModelBuilder.Entity <ArticleName>().PrimaryKey.AddRange(new[] { "ArticleId", "LanguageId" });
            var articleEntity = metadataModelBuilder.Entity <Article>();

            metadataModelBuilder.Entity <ReceiptDetail>();
            metadataModelBuilder.Entity <Invoice>().BaseEntity = metadataModelBuilder.Entity(typeof(Receipt));
            var contactSettings = metadataModelBuilder.Entity <ContactSettings>();

            articleEntity.HasOne(p => p.ArticleSettings).WithPrincipal();
            contactSettings.HasOne(p => p.Contact).WithDependant();

            metadataModelBuilder.ApplyConventions();

            var model = metadataModelBuilder.ToModel();

            return(model);
        }
        public void ProtoBufSerializationDeserializationTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Invoice>();
            builder.Entity <Article>().HasOne(p => p.ArticleSettings).WithPrincipal();
            builder.ApplyConventions();

            MetadataModel model = null;

            using (var ms = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize <MetadataModelBuilder>(ms, builder);

                ms.Seek(0, SeekOrigin.Begin);

                var newBuilder = ProtoBuf.Serializer.Deserialize <MetadataModelBuilder>(ms);
                model = newBuilder.ToModel();
            }

            TestModelValidations.ValidateInvoiceArticleDefaultModel(model);
        }
Beispiel #11
0
        public void AutomaticFixupTest()
        {
            var metadataModelBuilder = new MetadataModelBuilder();

            metadataModelBuilder.Entity <ArticleName>().PrimaryKey.AddRange(new[] { "ArticleId", "LanguageId" });
            var articleEntity = metadataModelBuilder.Entity <Article>();

            metadataModelBuilder.Entity <ReceiptDetail>();
            metadataModelBuilder.Entity <Invoice>().BaseEntity = metadataModelBuilder.Entity(typeof(Receipt));
            var contactSettings = metadataModelBuilder.Entity <ContactSettings>();

            articleEntity.HasOne(p => p.ArticleSettings).WithPrincipal();
            contactSettings.HasOne(p => p.Contact).WithDependant();

            metadataModelBuilder.ApplyConventions();

            var invoice = new Invoice {
                Id = Guid.NewGuid()
            };
            var article = new Article {
                Id = Guid.NewGuid(), ArticleNumber = "12314", SupplierId = Guid.NewGuid(), ArticleDescription = "testchen"
            };

            var detail = new ReceiptDetail {
                Id = Guid.NewGuid(), Receipt = invoice, ReceiptId = invoice.Id, Amount = 123, ArticleId = article.Id
            };

            invoice.Details.Add(detail);

            var name1 = new ArticleName {
                ArticleId = article.Id, LanguageId = "de", TranlatedText = "Text 1"
            };
            var name2 = new ArticleName {
                ArticleId = article.Id, LanguageId = "en", TranlatedText = "Text 1"
            };

            var context = new ModelContext(metadataModelBuilder.ToModel());

            context.Attach(new[] { name1, name2 });
            context.AttachSingle(article);

            Assert.Equal(article, name1.Article);
            Assert.Equal(article, name2.Article);
            Assert.Equal(2, article.Names.Count);
            Assert.Contains(article.Names, p => p == name1);
            Assert.Contains(article.Names, p => p == name2);

            context.AttachSingle(invoice);

            Assert.Equal(article, detail.Article);

            Assert.Null(article.ArticleSettings);

            var articleSettings = new ArticleSettings {
                Id = article.Id, Whatever = "test"
            };

            context.AttachSingle(articleSettings);

            Assert.Equal(article.ArticleSettings, articleSettings);
        }