Пример #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));
            }
        }
Пример #2
0
        public void _GenerateDemoData()
        {
            using (GetService <ISecurityService>().BeginSystemContext())
            {
                var repo            = GetService <IEntityRepository>();
                var userGroupLibAll = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Библиотечно-информационни услуги",
                    UserType = Core.Domain.UserTypes.Librarian
                };
                var userGroupLibMyMag = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Моите списания",
                    UserType = Core.Domain.UserTypes.Librarian
                };
                var userGroupStudents = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Студент",
                    UserType = Core.Domain.UserTypes.Customer
                };
                var userGroupEmpl = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Служител",
                    UserType = Core.Domain.UserTypes.Customer
                };
                var userGroupProfs = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Преподавател",
                    UserType = Core.Domain.UserTypes.Customer
                };
                var userGroupOutWithCard = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Външен (с читателска карта)",
                    UserType = Core.Domain.UserTypes.Customer
                };
                var userGroupOutNoCard = new NbuLibrary.Core.Domain.UserGroup()
                {
                    Name     = "Външен (без читателска карта)",
                    UserType = Core.Domain.UserTypes.Customer
                };

                repo.Search(new EntityQuery2("UserGroup")).ToList().ForEach(e => repo.Delete(e, true));

                repo.Create(userGroupLibAll);
                repo.Create(userGroupLibMyMag);
                repo.Create(userGroupStudents);
                repo.Create(userGroupEmpl);
                repo.Create(userGroupProfs);
                repo.Create(userGroupOutWithCard);
                repo.Create(userGroupOutNoCard);

                var userLib1 = new NbuLibrary.Core.Domain.User()
                {
                    UserType  = Core.Domain.UserTypes.Librarian,
                    Email     = "*****@*****.**",
                    Password  = "******",
                    FirstName = "Петър",
                    LastName  = "Димитров",
                    UserGroup = userGroupLibAll
                };

                var userLib2 = new NbuLibrary.Core.Domain.User()
                {
                    UserType  = Core.Domain.UserTypes.Librarian,
                    Email     = "*****@*****.**",
                    Password  = "******",
                    FirstName = "Радост",
                    LastName  = "Иванова",
                    UserGroup = userGroupLibAll
                };

                var userLib3 = new NbuLibrary.Core.Domain.User()
                {
                    UserType  = Core.Domain.UserTypes.Librarian,
                    Email     = "*****@*****.**",
                    Password  = "******",
                    FirstName = "Иван",
                    LastName  = "Георгиев",
                    UserGroup = userGroupLibMyMag
                };
                var qAllUsersWithoutAdmin = new EntityQuery2("User");
                qAllUsersWithoutAdmin.WhereAnyOf("UserType", new object[] { 0, 1 });
                //Assert.AreEqual(0, repo.Search(qAllUsersWithoutAdmin).Count());
                repo.Search(qAllUsersWithoutAdmin).ToList().ForEach(e => repo.Delete(e, true));

                //GetService<IEntityOperationService>().Update(new EntityUpdate(userLib1));
                repo.Create(userLib1);
                repo.Attach(userLib1, new Relation(UserGroup.DEFAULT_ROLE, userLib1.UserGroup));
                repo.Create(userLib2);
                repo.Attach(userLib2, new Relation(UserGroup.DEFAULT_ROLE, userLib2.UserGroup));
                repo.Create(userLib3);
                repo.Attach(userLib3, new Relation(UserGroup.DEFAULT_ROLE, userLib3.UserGroup));

                List <User> customers      = new List <User>();
                int         count          = 100;
                string[]    fnames         = new[] { "Иван", "Петър", "Кристиян", "Йордан", "Георги", "Симеон", "Александър", "Димитър", "Добромир", "Божидар", "Тодор", "Теодор", "Борис", "Борислав", "Цветан" };
                string[]    lnames         = new[] { "Димитров", "Иванов", "Петров", "Йорданов", "Георгиев", "Симеонов", "Александров", "Кирилов", "Тодоров", "Бориславов", "Караиванов", "Ножаров", "Ковачев" };
                UserGroup[] customerGroups = new[] { userGroupOutNoCard, userGroupOutWithCard, userGroupProfs, userGroupStudents, userGroupEmpl };


                Random r = new Random(1);//fixed seed so that we have identical values generated
                for (int i = 0; i < count; i++)
                {
                    string    email    = GenerateEmail(r);
                    string    password = email.Substring(0, email.IndexOf('.'));
                    UserGroup group    = customerGroups[r.Next(customerGroups.Length)];
                    var       user     = new User()
                    {
                        UserType      = UserTypes.Customer,
                        UserGroup     = group,
                        Email         = email,
                        Password      = password,
                        PhoneNumber   = GeneratePhone(r),
                        FirstName     = fnames[r.Next(fnames.Length)],
                        MiddleName    = lnames[r.Next(lnames.Length)],
                        LastName      = lnames[r.Next(lnames.Length)],
                        IsActive      = r.Next(3) < 2,
                        FacultyNumber = GenerateFacultyNumber(r, group),
                        CardNumber    = group == userGroupOutWithCard?GenerateCardNumber(r) : null
                    };
                    customers.Add(user);
                }


                customers.ForEach(e =>
                {
                    repo.Create(e);
                    repo.Attach(e, new Relation(UserGroup.DEFAULT_ROLE, e.UserGroup));
                });
            }
        }