Inheritance: DomainObject
 /// <summary>
 /// Creates the valid unit association. Is this correct?
 /// </summary>
 /// <returns></returns>
 private UnitAssociation CreateValidUnitAssociation()
 {
     var unitAssociation = new UnitAssociation
                               {
                                   Inactive = false,
                                   User = _userRepository.GetNullableById(2),
                                   Unit = _unitRepository.GetNullableById(2)
                               };
     return unitAssociation;
 }
Exemplo n.º 2
0
        public void CanGetUserWithSavedAssociations()
        {
            #region Create a User
            var user = CreateValidUser();
            user.LoginID = "XXX123HBFE"; //Just a random login id. Used lower in code to check that we get it.

            Assert.IsTrue(user.IsTransient());

            NHibernateSessionManager.Instance.BeginTransaction();
            _repository.EnsurePersistent(user);
            NHibernateSessionManager.Instance.CommitTransaction();

            Assert.IsFalse(user.IsTransient()); //Make sure the user is saved

            NHibernateSessionManager.Instance.GetSession().Evict(user);//get the user out of the local cache

            //Now get the user back out
            var userId = user.Id;

            var retrievedUser = _repository.GetNullableById(userId);

            Assert.IsNotNull(retrievedUser);
            Assert.AreEqual("XXX123HBFE", retrievedUser.LoginID); //Make sure it is the correct user
            Assert.AreEqual(11, _repository.GetAll().Count);
            #endregion Create a User

            #region Create UnitAssociations
            NHibernateSessionManager.Instance.BeginTransaction();
            var allUnits = _unitRepository.GetAll();
            foreach (var unit in allUnits)
            {
                var userAssociation = new UnitAssociation
                                          {
                                              Inactive = false,
                                              User = _repository.GetNullableById(userId),
                                              Unit = unit
                                          };
                _unitAssociationRepository.EnsurePersistent(userAssociation);

            }
            NHibernateSessionManager.Instance.CommitTransaction();
            #endregion Create UnitAssociations

            var allUnitAssociations = _unitAssociationRepository.GetAll();
            var filteredUnitAssociations = new List<UnitAssociation>();
            foreach (var unitAssociation in allUnitAssociations)
            {
                if(unitAssociation.User.Id == userId)
                {
                    filteredUnitAssociations.Add(unitAssociation);
                }
            }

            #region Modify user
            User modifyUser = _repository.GetNullableById(userId);

            Assert.AreEqual("XXX123HBFE", modifyUser.LoginID);

            Assert.IsNotNull(modifyUser.UnitAssociations);
            Assert.AreEqual(5, modifyUser.UnitAssociations.Count);
            foreach (var list in filteredUnitAssociations)
            {
                Assert.IsTrue(modifyUser.UnitAssociations.Contains(list));
            }

            //NHibernateSessionManager.Instance.BeginTransaction();
            //modifyUser.UnitAssociations = filteredUnitAssociations;
            //_repository.EnsurePersistent(modifyUser);
            //NHibernateSessionManager.Instance.CommitTransaction();

            //NHibernateSessionManager.Instance.GetSession().Evict(modifyUser); //Evict the user from the cache so we can retrieve it

            //modifyUser = _repository.GetById(userId); //Get the user back out

            //Assert.AreEqual("XXX123HBFE", modifyUser.FirstName);

            //Assert.IsNotNull(modifyUser.UnitAssociations);
            //Assert.AreEqual(5, modifyUser.UnitAssociations.Count);
            //foreach (var list in filteredUnitAssociations)
            //{
            //    Assert.IsTrue(modifyUser.UnitAssociations.Contains(list));
            //}
            #endregion Modify user
        }