Exemple #1
0
        public void Test_ModelBuilder_BuildModel_Relations()
        {
            ModelBuilder ba = new ModelBuilder("Author");
            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddString("Title", 256);

            bb.AddRelationTo(ba.EntityModel, RelationType.ManyToOne, "Author");

            var author = ba.EntityModel;
            var book = bb.EntityModel;

            Assert.IsNotNull(author);
            Assert.AreEqual(1, author.Relations.Count);
            var rel = book.Relations["author", "book", "author"];
            Assert.AreEqual(book.Name, rel.Right.Name);
            Assert.AreEqual(RelationType.OneToMany, rel.Type);
            Assert.AreEqual(RelationType.ManyToOne, rel.TypeFor(book.Name));

            rel = author.GetRelation("Book", "AUTHOR");
            Assert.AreEqual(book.Name, rel.Right.Name);

            bb.AddRelationTo(new NomenclatureModel("Genre"), RelationType.OneToOne, "BookGenre");

            var nom = book.GetRelation("genre", "BookGenre");
            Assert.AreEqual("Genre", nom.Right.Name);
        }
Exemple #2
0
        private static void CreateDomain()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre");

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");
            bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered");
            dm.Entities.Add(bo.EntityModel);

            dms.Save(dm);
        }
Exemple #3
0
        public void Test_DomainModelSerializer()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ba.EntityModel.Rules.Add(new FutureOrPastDateRuleModel((DateTimePropertyModel)ba.EntityModel.Properties["born"], TimeSpan.Zero, false));

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre");
            bb.AddUri("BookUri", "books");

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);
            dm.Relations.Add(ba.EntityModel.Relations.Single());
            ModelBuilder mbRel = new ModelBuilder(ba.EntityModel.Relations.Single());
            mbRel.AddIdentity("id");
            mbRel.AddIdentity("rid");
            mbRel.AddIdentity("lid");
            mbRel.Rules.AddRequired("lid");
            mbRel.Rules.AddRequired("rid");

            DomainModelSerializer ser = new DomainModelSerializer();
            XmlDocument xmlDoc = ser.Serialize(dm);
            var dm2 = ser.Deserialize(xmlDoc);

            Assert.AreEqual(dm.Entities.Count, dm2.Entities.Count);
            Assert.AreEqual(dm.Relations.Count, dm2.Relations.Count);
            var en1 = dm.Entities.GetEnumerator();
            var en2 = dm2.Entities.GetEnumerator();
            while (en1.MoveNext() && en2.MoveNext())
            {
                Assert.AreEqual(en1.Current.Name, en2.Current.Name);
                Assert.AreEqual(en1.Current.Properties.Count, en2.Current.Properties.Count);
                Assert.AreEqual(en1.Current.Relations.Count, en2.Current.Relations.Count);
                Assert.AreEqual(en1.Current.Rules.Count, en2.Current.Rules.Count);
                var p1 = en1.Current.Properties.GetEnumerator();
                var p2 = en2.Current.Properties.GetEnumerator();
                while (p1.MoveNext() && p2.MoveNext())
                {
                    Assert.AreEqual(p1.Current.Name, p2.Current.Name);
                    Assert.AreEqual(p1.Current.Type, p2.Current.Type);
                    Assert.AreEqual(p1.Current.DefaultValue, p2.Current.DefaultValue);
                    Assert.AreEqual(p1.Current.GetType(), p2.Current.GetType());
                    if (p1.Current.Type == PropertyType.Enum)
                        Assert.AreEqual(((EnumPropertyModel)p1.Current).EnumType, ((EnumPropertyModel)p2.Current).EnumType);
                    else if (p1.Current.Type == PropertyType.Computed)
                        Assert.AreEqual(((ComputedPropertyModel)p1.Current).Formula, ((ComputedPropertyModel)p2.Current).Formula);
                    else if (p1.Current.Type == PropertyType.Number)
                        Assert.AreEqual(((NumberPropertyModel)p1.Current).IsInteger, ((NumberPropertyModel)p2.Current).IsInteger);
                    else if (p1.Current.Type == PropertyType.Sequence)
                    {
                        Assert.AreEqual(((SequencePropertyModel)p1.Current).SequenceType, ((SequencePropertyModel)p2.Current).SequenceType);
                        Assert.AreEqual(((SequencePropertyModel)p1.Current).SequenceId, ((SequencePropertyModel)p2.Current).SequenceId);
                    }
                }

                var r1 = en1.Current.Rules.GetEnumerator();
                var r2 = en2.Current.Rules.GetEnumerator();
                while (r1.MoveNext() && r2.MoveNext())
                {
                    Assert.AreEqual(r1.Current.GetType(), r2.Current.GetType());
                    if (r1.Current is RequiredRuleModel)
                        Assert.AreEqual((r1.Current as RequiredRuleModel).Property.Name, (r2.Current as RequiredRuleModel).Property.Name);
                    else if (r1.Current is UniqueRuleModel)
                    {
                        Assert.AreEqual((r1.Current as UniqueRuleModel).Properties.Count(), (r2.Current as UniqueRuleModel).Properties.Count());
                        var pren1 = (r1.Current as UniqueRuleModel).Properties.GetEnumerator();
                        var pren2 = (r2.Current as UniqueRuleModel).Properties.GetEnumerator();
                        while (pren1.MoveNext() && pren2.MoveNext())
                        {
                            Assert.AreEqual(pren1.Current.Name, pren2.Current.Name);
                        }
                    }
                    else if (r1.Current is FutureOrPastDateRuleModel)
                    {
                        var er1 = r1.Current as FutureOrPastDateRuleModel;
                        var er2 = r2.Current as FutureOrPastDateRuleModel;
                        Assert.AreEqual(er1.Future, er2.Future);
                        Assert.AreEqual(er1.Offset, er2.Offset);
                        Assert.AreEqual(er1.Property.Name, er2.Property.Name);
                    }
                }
            }

            var rn1 = dm.Relations.GetEnumerator();
            var rn2 = dm2.Relations.GetEnumerator();
            while (rn1.MoveNext() && rn2.MoveNext())
            {
                Assert.AreEqual(rn1.Current.Name, rn2.Current.Name);
                Assert.AreEqual(rn1.Current.Properties.Count, rn2.Current.Properties.Count);
                Assert.AreEqual(rn1.Current.Relations.Count, rn2.Current.Relations.Count);
                Assert.AreEqual(rn1.Current.Rules.Count, rn2.Current.Rules.Count);
                var p1 = rn1.Current.Properties.GetEnumerator();
                var p2 = rn2.Current.Properties.GetEnumerator();
                while (p1.MoveNext() && p2.MoveNext())
                {
                    Assert.AreEqual(p1.Current.Name, p2.Current.Name);
                    Assert.AreEqual(p1.Current.Type, p2.Current.Type);
                    Assert.AreEqual(p1.Current.DefaultValue, p2.Current.DefaultValue);
                    Assert.AreEqual(p1.Current.GetType(), p2.Current.GetType());
                }
            }
        }
Exemple #4
0
        public void Test_ModelBuilder_BuildModel_General()
        {
            ModelBuilder b = new ModelBuilder("Author");
            b.AddString("FirstName", 128);
            b.AddString("LastName", 128);
            b.AddBoolean("IsAlive", true);
            b.AddInteger("NumberOfAwards");
            b.AddDateTime("Born");
            b.AddDecimal("Rating");

            var em = b.EntityModel;
            Assert.IsNotNull(em);

            //string prop
            var fname = em.Properties["firstNAME"];
            Assert.IsTrue(fname is StringPropertyModel);
            Assert.AreEqual(128, (fname as StringPropertyModel).Length);

            //boolean prop
            var isalive = em.Properties["isalive"];
            Assert.IsTrue(isalive is BooleanPropertyModel);
            Assert.AreEqual(true, isalive.DefaultValue);

            //integer prop
            var noa = em.Properties["numberofawards"];
            Assert.IsTrue(noa is NumberPropertyModel);
            Assert.AreEqual(true, (noa as NumberPropertyModel).IsInteger);

            //datetime prop
            var born = em.Properties["born"];
            Assert.IsTrue(born is DateTimePropertyModel);
            //TODO: more testing datetime prop building

            //decimal prop
            var rating = em.Properties["rating"];
            Assert.IsTrue(rating is NumberPropertyModel);
            Assert.AreEqual(false, (rating as NumberPropertyModel).IsInteger);
        }
Exemple #5
0
        public void Test_DomainModelService_Union()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");

            ModelBuilder bb2 = new ModelBuilder("Book");
            bb2.AddIdentity("Id");
            bb2.AddString("Title", 256);
            bb2.AddString("Barcode", 20);
            bb2.AddDecimal("Price");

            bo.AddRelationTo(bb2.EntityModel, RelationType.ManyToMany, "Ordered");

            var dm2 = new DomainModel();
            dm2.Entities.Add(bo.EntityModel);
            dm2.Entities.Add(bb2.EntityModel);

            var un = svc.Union(new DomainModel[] { dm, dm2 });

            Assert.AreEqual(3, un.Entities.Count);
            var author = un.Entities["author"];
            Assert.AreEqual(8, author.Properties.Count);
            Assert.AreEqual(5, un.Entities["book"].Properties.Count);
            Assert.AreEqual(4, un.Entities["Order"].Properties.Count);
            Assert.AreEqual(2, un.Relations.Count);
        }
Exemple #6
0
        public void Test_DomainModelService_Merge()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre", Genre.Fantasy);

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            svc.Save(dm);
            using (var conn = new TestDatabaseService().GetSqlConnection())
            {
                conn.Open();
                DatabaseManager dbm = new DatabaseManager(conn);
                dbm.LoadSchema();
                var tableA = dbm.Tables.Find(t => t.Name == "Author");
                Assert.IsNotNull(tableA);
                var fnCol = tableA.Columns.Find(c => c.Name.Equals("FullName", StringComparison.InvariantCultureIgnoreCase));
                Assert.IsNotNull(fnCol);
            }

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");
            bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered");
            dm.Entities.Add(bo.EntityModel);

            ba.EntityModel.Properties.Remove(ba.EntityModel.Properties["IsAlive"]);
            (ba.EntityModel.Properties["LastName"] as StringPropertyModel).Length = 256;
            (ba.EntityModel.Properties["firstName"] as StringPropertyModel).Length = 256;
            (bb.EntityModel.Properties["genre"] as EnumPropertyModel).DefaultValue = Genre.Horror;
            ba.AddString("Nickname", 256);

            svc.Merge(dm);

            Assert.AreEqual(3, svc.Domain.Entities.Count);
            //foreach (var em1 in dm.Entities)
            //{
            //    var em2 = svc.Domain.Entities[em1.Name];
            //    Assert.AreEqual(em1.Properties.Count, em2.Properties.Count);
            //}
            //TODO-tests:finish domainmodelservice_merge test

            //Computed columns are droped and recreated when upgrading the columns in their computed definition
            using (var conn = new TestDatabaseService().GetSqlConnection())
            {
                conn.Open();
                DatabaseManager dbm = new DatabaseManager(conn);
                dbm.LoadSchema();
                var tableA = dbm.Tables.Find(t => t.Name == "Author");
                Assert.IsNotNull(tableA);
                var fnCol = tableA.Columns.Find(c => c.Name.Equals("FullName", StringComparison.InvariantCultureIgnoreCase));
                Assert.IsNotNull(fnCol);
            }
        }
Exemple #7
0
        public void Test_DomainModelService_Compare()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", false);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");
            ba.AddString("Uri", 20);

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");

            bb.Rules.AddUnique("ISBN");
            bb.Rules.AddRequired("title");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            svc.Save(dm);

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddIdentity("Number");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");
            bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered");
            dm.Entities.Add(bo.EntityModel);

            ba.EntityModel.Properties.Remove(ba.EntityModel.Properties["IsAlive"]);
            (ba.EntityModel.Properties["LastName"] as StringPropertyModel).Length = 256;
            (ba.EntityModel.Properties["firstName"] as StringPropertyModel).Length = 256;
            ba.AddString("Nickname", 256);
            ba.Rules.AddUnique("Uri");
            (ba.EntityModel.Properties["Rating"] as NumberPropertyModel).DefaultValue = 5.0m;

            DomainModelChanges dmc = svc.CompareWithExisting(dm);
            Assert.AreEqual(2, dmc.EntityChanges.Count());
            Assert.AreEqual(1, dmc.EntityChanges.Count(ec => ec.Change == ChangeType.Created));
            var ecs = dmc.EntityChanges.Single(ec => ec.Change == ChangeType.Created);
            Assert.AreEqual(bo.EntityModel.Properties.Count, ecs.PropertyChanges.Count());
            Assert.AreEqual(1, dmc.EntityChanges.Count(ec => ec.Change == ChangeType.Modified));

            Assert.AreEqual(1, dmc.RelationChanges.Count());

            var change = dmc.EntityChanges.Single(e => e.New.Name == ba.EntityModel.Name);
            Assert.AreEqual(5, change.PropertyChanges.Count());
            Assert.AreEqual(3, change.PropertyChanges.Count(pc => pc.Change == ChangeType.Modified));
            Assert.AreEqual(1, change.PropertyChanges.Count(pc => pc.Change == ChangeType.Deleted));
            Assert.AreEqual(1, change.PropertyChanges.Count(pc => pc.Change == ChangeType.Created));
            Assert.AreEqual(1, change.RuleChanges.Count());
        }
Exemple #8
0
        public void Test_DomainModelService_Basic()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ba.EntityModel.Rules.Add(new FutureOrPastDateRuleModel((DateTimePropertyModel)ba.EntityModel.Properties["born"], TimeSpan.Zero, false));

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre");

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");
            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            svc.Save(dm);

            Assert.IsNotNull(svc.Domain);
            Assert.IsNotNull(svc.Domain.Entities["author"]);
            Assert.IsNotNull(svc.Domain.Entities["book"]);
            Assert.IsNotNull(svc.Domain.Relations["book", "author", "author"]);
        }
Exemple #9
0
        private static void CreateDomain()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre", Genre.Mistery);

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            var rel = ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");
            var br = new ModelBuilder(rel);
            br.AddDateTime("WrittenOn");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");
            bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered");
            bo.AddUri("Number", "BO");
            dm.Entities.Add(bo.EntityModel);

            dms.Save(dm);

            using (var conn = new TestDatabaseService().GetSqlConnection())
            {
                conn.Open();
                var dbManager = new DatabaseManager(conn);
                SequenceProvider.Initialize(dbManager);
            }
        }