Exemplo n.º 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));
            }
        }