Example #1
0
        public void Test_EntityRepo_Count()
        {
            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
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

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

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                Assert.AreEqual(2, repository.Count(query));

                //greater then
                EntityQuery2 q = new EntityQuery2("book");
                q.WhereGreaterThen("price", 19.0m);
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereLessThen("price", 20.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //is boolean
                q = new EntityQuery2("author");
                q.WhereIs("isalive", false);
                var r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //is string (ignore case)
                q = new EntityQuery2("author");
                q.WhereIs("lastname", "jordan");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //starts with
                q = new EntityQuery2("author");
                q.WhereStartsWith("firstname", "ra");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //ends with
                q = new EntityQuery2("book");
                q.WhereEndsWith("title", "world");
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                //less then
                q = new EntityQuery2("book");
                q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id });
                Assert.AreEqual(3, repository.Search(q).Count());
                Assert.AreEqual(3, repository.Count(q));

                //between decimal
                q = new EntityQuery2("book");
                q.WhereBetween("price", 19.0m, 22.0m);
                Assert.AreEqual(2, repository.Search(q).Count());
                Assert.AreEqual(2, repository.Count(q));

                //between datetime
                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                r = repository.Search(q);
                Assert.AreEqual(1, r.Count());
                Assert.AreEqual(1, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1));
                q.WhereIs("isalive", true);
                Assert.AreEqual(0, repository.Search(q).Count());
                Assert.AreEqual(0, repository.Count(q));

                q = new EntityQuery2("author");
                q.WhereBetween("born", new DateTime(1960, 1, 1), new DateTime(1970, 1, 1));
                q.WhereIs("isalive", true);
                q.WhereStartsWith("firstname", "ra");
                Assert.AreEqual(1, repository.Search(q).Count());
                Assert.AreEqual(1, repository.Count(q));
            }
        }
Example #2
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);
            }
        }
Example #3
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");
            }
        }
Example #4
0
        public void Test_EntityRepo_SearchRelated()
        {
            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
                };

                var jb1 = new Book()
                {
                    Title = "The Shadow is Rising",
                    Price = 21.15m
                };
                var jb2 = new Book()
                {
                    Title = "The Eye of the World",
                    Price = 25.80m
                };

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

                EntityQuery2 query = new EntityQuery2("book");
                query.AllProperties = true;
                var fq = new RelationQuery("author", "author", feist.Id);
                query.RelatedTo.Add(fq);
                var res = repository.Search(query);
                Assert.AreEqual(2, res.Count());
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title));
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title));

                query.Include("author", "author");
                res = repository.Search(query);
                Assert.AreEqual(2, res.Count());
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title));
                Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title));
            }
        }
Example #5
0
        public void Test_EntityRepo_SearchWithRels()
        {
            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));

                var query = new EntityQuery2("author");
                query.AddProperties("firstname", "lastname", "born");
                query.Include("book", "author");
                var res = repository.Search(query);

                Assert.AreEqual(2, res.Count());
                var rf = res.Single(e => e.Id == feist.Id);
                var rj = res.Single(e => e.Id == jordan.Id);

                Assert.AreEqual(1, rf.RelationsData.Count);
                var books = rf.GetManyRelations("book", "author");
                Assert.AreEqual(2, books.Count());
                foreach (var r in books)
                {
                    var orig = r.Entity.Id == fb1.Id ? fb1 : fb2;
                    foreach (var pm in dms.Domain.Entities["book"].Properties)
                    {
                        if (orig.Data.ContainsKey(pm.Name))
                            Assert.AreEqual(orig.Data[pm.Name], r.Entity.Data[pm.Name]);
                    }
                }

                Assert.AreEqual(0, rj.RelationsData.Count);
                Assert.AreEqual(0, rj.GetManyRelations("book", "author").Count());

                //TODO: rules!

                //repository.Complete();
            }
        }
Example #6
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();
            }
        }
Example #7
0
        public void Test_EntityRepo_Paging()
        {
            var dbService = new TestDatabaseService();
            var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService));
            using (var ctx = dbService.GetDatabaseContext(true))
            {
                #region prepare data

                int aCnt = 20;
                int bCnt = 3;
                for (int i = 0; i < aCnt; i++)
                {
                    var a = new Author()
                    {
                        FirstName = "Fname" + i,
                        LastName = "Lname" + i,
                        Born = DateTime.Now.AddYears(-20).AddDays(i),
                        NumberOfAwards = i / 3
                    };
                    repository.Create(a);
                    for (int j = 0; j < bCnt; j++)
                    {
                        var b = new Book()
                        {
                            Title = string.Format("Book_{0}_{1}", i, j),
                            Genre = Genre.SciFi,
                            Price = 10.0m + j,
                            ISBN = string.Format("{0}_{1}", a.LastName, j)
                        };
                        repository.Create(b);
                        repository.Attach(b, new Relation("author", a));
                    }
                }

                Assert.AreEqual(aCnt, repository.Search(new EntityQuery2("author")).Count());
                Assert.AreEqual(aCnt * bCnt, repository.Search(new EntityQuery2("book")).Count());

                #endregion

                var query = new EntityQuery2("Author");
                query.AddProperties("FirstName", "LastName");
                query.Include("book", "author");
                query.Paging = new Paging(1, 10);
                var res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                int idx = 0;
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }

                //assert second page
                query.Paging.Page++;
                res = repository.Search(query);
                Assert.AreEqual(10, res.Count());
                foreach (var r in res)
                {
                    Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname"));
                    var books = r.GetManyRelations("book", "author");
                    int bidx = 0;
                    foreach (var b in books)
                    {
                        Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title"));
                    }
                    idx++;
                }
            }
        }
Example #8
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");
        }
Example #9
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"));
            }
        }
Example #10
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);
            }
        }