Exemple #1
0
        public void Test_EntityRepo_AttachDetach()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);

                #endregion

                repository.Attach(feist, new Relation("author", fb1));
                var rel2 = new Relation("author", fb2);
                var writtenOn = new DateTime(1996, 4, 25);
                rel2.SetData<DateTime>("WrittenOn", writtenOn);
                repository.Attach(feist, rel2);

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());

                var bq = new EntityQuery2("book");
                bq.Include("author", "author");
                var bes = repository.Search(bq);
                foreach (var be in bes)
                {
                    Assert.AreEqual(1, be.RelationsData.Count);
                    Assert.AreEqual(feist.Id, be.GetSingleRelation("author", "author").Entity.Id);
                    if (be.Id == fb2.Id)
                        Assert.AreEqual(writtenOn, be.GetSingleRelation("author", "author").GetData<DateTime>("writtenon"));
                }

                repository.Detach(feist, new Relation("author", fb1));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                repository.Attach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());
                repository.Detach(fb1, new Relation("author", feist));
                e = repository.Read(q);
                Assert.AreEqual(1, e.GetManyRelations("book", "author").Count());

                bool ex = false;
                try { repository.Attach(fb2, new Relation("author", jordan)); }
                catch (Exception) { ex = true; }
                Assert.IsTrue(ex, "Exception not thrown when attaching two authors to single book");
            }
        }
Exemple #2
0
        public void Test_EntityRepo_ReadWithRel()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                EntityQuery2 q = new EntityQuery2("book", fb1.Id);
                q.AddProperties("title", "price");
                q.Include("author", "author");
                var e = repository.Read(q);
                Assert.AreEqual(2, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(fb1.Data[p], e.Data[p]);
                }

                Assert.AreEqual(1, e.RelationsData.Count);
                var authorRel = e.GetSingleRelation("author", "author");
                foreach (var d in feist.Data)
                {
                    Assert.AreEqual(d.Value, authorRel.Entity.Data[d.Key]);
                }

                //repository.Complete();
            }
        }
Exemple #3
0
        public void Test_EntityRepo_Update()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                #endregion

                jordan.LastName += "EDIT";
                jordan.IsAlive = true;
                jordan.Born = jordan.Born.AddDays(2.0);
                jordan.Rating -= 0.5m;

                repository.Update(jordan);
                var q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                var updated = new Author(e);
                Assert.AreEqual(jordan.LastName, updated.LastName);
                Assert.AreEqual(jordan.Born, updated.Born);
                Assert.AreEqual(jordan.IsAlive, updated.IsAlive);
                Assert.AreEqual(jordan.Rating, updated.Rating);
            }
        }
Exemple #4
0
        public void Test_EntityRepo_Read()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                jordan.Id = repository.Create(jordan);
                Assert.IsTrue(jordan.Id > 0);
                feist.Id = repository.Create(feist);
                Assert.IsTrue(feist.Id > 0);

                EntityQuery2 q = new EntityQuery2("author", jordan.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                var e = repository.Read(q);
                Assert.AreEqual(5, e.Data.Count);
                foreach (var p in q.Properties)
                {
                    Assert.AreEqual(jordan.Data[p], e.Data[p]);
                }
            }
        }
Exemple #5
0
        //[TestMethod]
        public void Test_EntityRepo_Perf()
        {
            var r = new Random();
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                for (int i = 0; i < 1000; i++)
                {
                    EntityUpdate update = new EntityUpdate("author");
                    update.Set("firstname", "Robert" + i);
                    update.Set("lastname", "Jordan");
                    update.Set("isalive", false);
                    update.Set("Born", new DateTime(1948, 10, 17));
                    update.Set("Rating", 5.5m + r.Next(4));
                    var id = repository.Create(update.ToEntity());
                    Assert.IsTrue(id > 0);

                    EntityQuery2 q = new EntityQuery2("author", id);
                    q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                    var e = repository.Read(q);
                    foreach (var pu in update.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                    EntityUpdate update2 = new EntityUpdate(e.Name, e.Id);
                    update2.Set("rating", 5.5m + r.Next(4));
                    update2.Set("lastname", e.Data["lastname"] + "_EDIT");

                    repository.Update(update2.ToEntity());
                    e = repository.Read(q);
                    foreach (var pu in update2.PropertyUpdates)
                    {
                        Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }
                    foreach (var pu in update.PropertyUpdates)
                    {
                        if (!pu.Key.Equals("rating", StringComparison.InvariantCultureIgnoreCase) && !pu.Key.Equals("lastname", StringComparison.InvariantCultureIgnoreCase))
                            Assert.AreEqual(pu.Value, e.Data[pu.Key]);
                    }

                }

                ctx.Complete();
            }

            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var qAll = new EntityQuery2("Author");
                var all = repository.Search(qAll);
                Assert.AreEqual(1000, all.Count());
                foreach (var a in all)
                    repository.Delete(a);
                Assert.AreEqual(0, repository.Search(qAll).Count());
            }
        }
Exemple #6
0
        public void Test_EntityRepo_Delete()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            bool ex = false;
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    IsAlive = true,
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m
                };

                //var jb1 = new Book()
                //{
                //    Title = "The Wheel of Time",
                //    Price = 21.0m
                //};

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                //repository.Create(jb1);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                //repository.Attach(jordan, new Relation("author", jb1));

                #endregion

                var r1 = new EntityQuery2("author", jordan.Id);
                Assert.IsNotNull(repository.Read(r1));
                repository.Delete(jordan);
                Assert.IsNull(repository.Read(r1));

                var r2 = new EntityQuery2("author", feist.Id);
                Assert.IsNotNull(repository.Read(r2));

                try { repository.Delete(feist); }
                catch (Exception) { ex = true; }
            }
            Assert.IsTrue(ex, "Exception not thrown when deleting author with attached relations");
        }
Exemple #7
0
        public void Test_EntityRepo_CreateSequences()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m,
                    Genre = Genre.Fantasy
                };

                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));

                var order = new Entity("Order");
                order.SetData<decimal>("total", fb1.Price + fb2.Price);
                order.SetData<DateTime>("createdon", DateTime.Now);
                repository.Create(order);
                repository.Attach(order, new Relation("ordered", fb1));
                repository.Attach(order, new Relation("ordered", fb2));

                var q = new EntityQuery2("order", order.Id);
                q.AllProperties = true;
                var saved = repository.Read(q);
                Assert.AreEqual(string.Format("BO-1-{0}", DateTime.Now.Year), saved.GetData<string>("number"));

                var order2 = new Entity("Order");
                order2.SetData<decimal>("total", fb1.Price);
                order2.SetData<DateTime>("createdon", DateTime.Now);
                repository.Create(order2);
                repository.Attach(order2, new Relation("ordered", fb1));

                q = new EntityQuery2("order", order2.Id);
                q.AllProperties = true;
                saved = repository.Read(q);
                Assert.AreEqual(string.Format("BO-2-{0}", DateTime.Now.Year), saved.GetData<string>("number"));
            }
        }
Exemple #8
0
        public void Test_EntityRepo_Create()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data
                var jordan = new Author()
                {
                    FirstName = "Robert",
                    LastName = "Jordan",
                    IsAlive = false,
                    Born = new DateTime(1948, 10, 17),
                    Rating = 10.0m
                };

                var feist = new Author()
                {
                    FirstName = "Raymond",
                    LastName = "Feist",
                    Born = new DateTime(1963, 2, 14),
                    Rating = 6.7m
                };

                var fb1 = new Book()
                {
                    Title = "The Apprentice",
                    Price = 19.90m
                };

                var fb2 = new Book()
                {
                    Title = "The Magician",
                    Price = 17.10m,
                    Genre = Genre.Fantasy
                };
                #endregion

                repository.Create(jordan);
                repository.Create(feist);
                repository.Create(fb1);
                repository.Create(fb2);
                repository.Attach(feist, new Relation("author", fb1));
                repository.Attach(feist, new Relation("author", fb2));
                fb1.Genre = Genre.Mistery; //default value
                feist.IsAlive = true;//default value

                var q = new EntityQuery2("author", feist.Id);
                q.AddProperties("FirstName", "lastname", "isalive", "born", "rating");
                q.Include("book", "author");
                var e = repository.Read(q);
                var created = new Author(e);
                Assert.AreEqual(feist.FirstName, created.FirstName);
                Assert.AreEqual(feist.LastName, created.LastName);
                Assert.AreEqual(feist.Born, created.Born);
                Assert.AreEqual(feist.IsAlive, created.IsAlive);
                Assert.AreEqual(feist.Rating, created.Rating);
                Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());//repository.Detach(feist, new Relation("author", fb1));
                var eb1 = e.GetManyRelations("book", "author").First();
                Book b = new Book(eb1.Entity);
                Assert.AreEqual(fb1.Genre, b.Genre);
            }
        }