예제 #1
0
        public override void RunAfterAnyTests()
        {
            _pagePermissionRepository.Delete(_deletedPermissions);
            _pageRepository.Delete(_deletedItems);
            _roleRepository.Delete(_deletedRoles);

            base.RunAfterAnyTests();
        }
예제 #2
0
        public static void Test(IKeyedRepository <TKey, TEntity> repo, List <string> protectedPropertyNames, TEntity seed)
        {
            // Get the inital count
            int countBefore = repo.All().Count();

            // Look for the seed.Id item in the DB; if found, refresh up to 5 times then error.
            int loopCount = 0;

            while (true)
            {
                var item = repo.FindBy(seed.Id);
                if (item == null)
                {
                    break;
                }
                if (loopCount > 4)
                {
                    Assert.Fail("Can't find a unique new key for: " + repo.GetType().Name);
                    return;
                }
                seed = Random <TEntity> .UpdateSpecifiedProperties(seed, new List <string> {
                    "Id"
                });

                loopCount += 1;
            }


            // add
            repo.Insert(seed);
            int countAfter = repo.All().Count();

            Assert.IsTrue(countBefore + 1 == countAfter);


            // read
            TEntity entity2 = repo.FindBy(seed.Id);

            //Assert.IsTrue(seed.Equals(entity2));
            Assert.IsTrue(Comparison.PublicInstancePropertiesEqual(seed, entity2, protectedPropertyNames));

            // update
            protectedPropertyNames.Add("Id");             // Make sure Id is now in the list.
            seed = Random <TEntity> .Update(seed, protectedPropertyNames);

            repo.Update(seed);
            TEntity entity3 = repo.FindBy(seed.Id);

            Assert.IsFalse(Comparison.PublicInstancePropertiesEqual(entity2, seed, protectedPropertyNames));
            Assert.IsTrue(Comparison.PublicInstancePropertiesEqual(entity3, seed, protectedPropertyNames));

            // delete
            repo.Delete(seed.Id);
            countAfter = repo.All().Count();
            Assert.IsTrue(countAfter == countBefore);
        }
예제 #3
0
        public void Delete_Returns_True_And_Removes_A_List_Of_Entities_If_Found()
        {
            using (IDbSession dbSession = _dbSessionFactory.Create())
            {
                IKeyedRepository <int, Person> repo = dbSession.CreateKeyedRepository <int, Person>();

                int expected = repo.All().Count() - _persons.Count;
                Assert.IsTrue(repo.Delete(_persons));
                Assert.IsTrue(repo.All().Count() == expected);
            }
        }
예제 #4
0
        public void Delete_Returns_False_If_Does_Not_Exist_And_Does_Not_Modify_List()
        {
            using (IDbSession dbSession = _dbSessionFactory.Create())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                int    countBefore  = repo.All().Count();
                Person doesNotExist = new Person {
                    FirstName = "I do not exist", LastName = "I do not exist"
                };
                Assert.IsFalse(repo.Delete(doesNotExist));
                Assert.IsTrue(repo.All().Count() == countBefore);
            }
        }
        public void Commit_And_Rollback_Work()
        {
            Person person = new Person
            {
                Id        = Guid.NewGuid(),
                FirstName = Guid.NewGuid().ToString(),
                LastName  = Guid.NewGuid().ToString()
            };

            // Rollback
            using (DbSession dbSession = GetSession())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                repo.Add(person);

                dbSession.Rollback();
            }
            using (DbSession dbSession = GetSession())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                Assert.IsNull(repo.FindBy(person.Id));
            }
            // Commit
            using (DbSession dbSession = GetSession())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                repo.Add(person);

                dbSession.Commit();
            }
            using (DbSession dbSession = GetSession())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                Assert.IsNotNull(repo.FindBy(person.Id));
            }

            // Cleanup
            using (DbSession dbSession = GetSession())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                repo.Delete(person);

                dbSession.Commit();
            }
        }
예제 #6
0
        private void CleanUp()
        {
            using (IDbSession dbSession = _dbSessionFactory.Create())
            {
                IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>();

                // delete what we created
                foreach (Person person in _persons)
                {
                    repo.Delete(person);
                }

                dbSession.Commit();
            }
        }
예제 #7
0
        public void Delete_Returns_True_And_Removes_Entity_If_Found()
        {
            using (IDbSession dbSession = _dbSessionFactory.Create())
            {
                IKeyedRepository <int, Person> repo = dbSession.CreateKeyedRepository <int, Person>();

                int expectedCount = repo.All().Count();
                foreach (Person person in _persons)
                {
                    Assert.IsTrue(repo.All().Count() == expectedCount--);
                    Assert.IsTrue(repo.Delete(person));
                    Assert.IsTrue(repo.All().Count() == expectedCount);
                }
            }
        }
예제 #8
0
        // Remove a page and every related entities
        public ActionResult Remove(string slug)
        {
            PageModel model = GetPageModel(slug);

            bool hasRemovePermission = false;

            // check if he is in a role that has delete permissions

            using (TransactionScope ts = new TransactionScope())
            {
                var deleteRoles = (from p in model.Permissions
                                   where p.Delete == true
                                   select p.RoleName).ToList();

                var userRoles = from r in
                                (
                    from u in _userRepository.All()
                    where u.Id == LoggedInUserId
                    select u
                                ).Fetch(u => u.Roles).Single().Roles
                                select r.Name;

                ts.Complete();

                hasRemovePermission = userRoles.Any(r => deleteRoles.Contains(r));
            }

            if (hasRemovePermission)
            {
                try
                {
                    _pageRepository.Delete(_pageRepository.FindById(model.Id));
                }
                catch
                {
                    Alert(AlertType.danger, "Error", "Failed to remove page. Please remove all its children first.");
                    return(RedirectToAction("edit", "page", new { slug = slug }));
                }
            }

            Alert(AlertType.success, "Success", string.Format("Page '{0}' successfully removed.", model.Name));
            return(Redirect("~/"));
        }
예제 #9
0
 [HttpPost] // patch for shared server compatibility, usually would use [HttpDelete]
 public JsonNetResult DeletePageWidget(PageWidgetModel model)
 {
     _pageWidgetRepository.Delete(
         _pageWidgetRepository.FindById(model.Id));
     return(new JsonNetResult(model));
 }