Beispiel #1
0
        public void Save_ModifiedValidItem_SavesChangesToRepository()
        {
            // arrange
            PopulateRepositoryWithTestData();
            var savedEntity = LookupRepositoryInstance.Items[2];
            var saveThis    = SystemUnderTest.GetById(savedEntity.Id);

            Assert.IsNotNull(saveThis, "Item to modify is null");
            Assert.AreNotEqual <int>(0, saveThis.Id,
                                     "Item to modify has an Id of 0 indicating that it was not saved.");
            var expectedId = saveThis.Id;

            ValidatorStrategy.IsValidReturnValue = true;

            LookupTestUtility.ModifyModel(saveThis);

            LookupRepositoryInstance.ResetMethodCallTrackers();

            // act
            SystemUnderTest.Save(saveThis);

            // assert
            Assert.AreEqual <int>(expectedId, saveThis.Id,
                                  "Id should not change when modified.");
            Assert.IsTrue(LookupRepositoryInstance.WasSaveCalled, "Save was not called.");

            var entity = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNotNull(entity, "Entity wasn't saved to repository.");
            LookupTestUtility.AssertAreEqual(saveThis, entity);
        }
Beispiel #2
0
        private void PopulateRepositoryWithTestData()
        {
            var items = LookupTestUtility.CreateEntities();

            foreach (var item in items)
            {
                LookupRepositoryInstance.Save(item);
            }
        }
Beispiel #3
0
        public void Save_ModifiedValidItem_CreatedAndLastUpdatedFieldsArePopulated()
        {
            // arrange
            PopulateRepositoryWithTestData();
            var savedEntity = LookupRepositoryInstance.Items[2];
            var saveThis    = SystemUnderTest.GetById(savedEntity.Id);

            Assert.IsNotNull(saveThis, "Item to modify is null");
            Assert.AreNotEqual <int>(0, saveThis.Id,
                                     "Item to modify has an Id of 0 indicating that it was not saved.");
            var expectedId = saveThis.Id;

            ValidatorStrategy.IsValidReturnValue = true;

            LookupTestUtility.ModifyModel(saveThis);

            LookupRepositoryInstance.ResetMethodCallTrackers();

            UsernameProvider.GetUsernameReturnValue = UnitTestConstants.FakeUsername2;

            var originalCreatedBy      = saveThis.CreatedBy;
            var originalLastModifiedBy = saveThis.LastModifiedBy;

            var originalCreatedDate  = saveThis.CreatedDate;
            var originalLastModified = saveThis.LastModifiedDate;

            Thread.Sleep(UnitTestConstants.NumberOfMillisecondsForRecentDateTimeAssert * 2);

            // act
            SystemUnderTest.Save(saveThis);

            // assert
            Assert.AreEqual <string>(originalCreatedBy,
                                     saveThis.CreatedBy,
                                     "CreatedBy was wrong.");

            Assert.AreEqual <DateTime>(originalCreatedDate, saveThis.CreatedDate,
                                       "CreatedDate was wrong.");

            Assert.AreEqual <string>(UnitTestConstants.FakeUsername2,
                                     saveThis.LastModifiedBy,
                                     "LastModifiedBy was wrong.");

            UnitTestUtility.AssertDateTimeIsRecent(
                saveThis.LastModifiedDate, "LastModifiedDate");

            var entity = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNotNull(entity, "Entity wasn't saved to repository.");
            LookupTestUtility.AssertAreEqual(saveThis, entity);
        }
Beispiel #4
0
        public void Save_NewValidItem_IdIsPopulatedAndIsInRepository()
        {
            // arrange
            var saveThis = LookupTestUtility.CreateModel(true);

            ValidatorStrategy.IsValidReturnValue = true;

            // act
            SystemUnderTest.Save(saveThis);

            // assert
            Assert.AreNotEqual <int>(0, saveThis.Id, "Id should not be zero after save.");
            Assert.IsTrue(LookupRepositoryInstance.WasSaveCalled, "Save was not called.");
            var actual = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNotNull(actual, "Item wasn't saved to repository.");

            var entity = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNotNull(entity, "Entity wasn't saved to repository.");
            LookupTestUtility.AssertAreEqual(saveThis, entity);
        }
Beispiel #5
0
        public void Save_ModifiedInvalidItem_DoesNotGetSavedAndThrowsValidationException()
        {
            // arrange
            PopulateRepositoryWithTestData();
            var savedEntity = LookupRepositoryInstance.Items[2];
            var saveThis    = SystemUnderTest.GetById(savedEntity.Id);

            Assert.IsNotNull(saveThis, "Item to modify is null");
            Assert.AreNotEqual <int>(0, saveThis.Id,
                                     "Item to modify has an Id of 0 indicating that it was not saved.");
            var expectedId = saveThis.Id;

            ValidatorStrategy.IsValidReturnValue = false;
            bool gotException = false;

            LookupTestUtility.ModifyModel(saveThis);

            LookupRepositoryInstance.ResetMethodCallTrackers();

            // act
            try
            {
                SystemUnderTest.Save(saveThis);
            }
            catch (InvalidObjectException)
            {
                gotException = true;
            }
            catch (Exception ex)
            {
                Assert.Fail($"Got wrong kind of exception. {ex}");
            }

            // assert
            Assert.IsTrue(gotException, "Did not get an invalid object exception.");
            Assert.AreNotEqual <int>(0, saveThis.Id, "Id should not be zero after save.");
            Assert.IsFalse(LookupRepositoryInstance.WasSaveCalled, "Save should not be called.");
        }
Beispiel #6
0
        public void Delete_RemovesItemFromRepository()
        {
            // arrange

            PopulateRepositoryWithTestData();
            var savedEntity = LookupRepositoryInstance.Items[2];
            var saveThis    = SystemUnderTest.GetById(savedEntity.Id);

            Assert.IsNotNull(saveThis, "Item to modify is null");
            Assert.AreNotEqual <int>(0, saveThis.Id,
                                     "Item to modify has an Id of 0 indicating that it was not saved.");
            var expectedId = saveThis.Id;

            // act
            SystemUnderTest.DeleteById(saveThis.Id);

            // assert
            Assert.IsTrue(LookupRepositoryInstance.WasDeleteByIdCalled, "DeleteById was not called.");

            var entity = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNull(entity, "Entity should have been deleted from repository.");
        }
Beispiel #7
0
        public void Save_NewValidItem_CreatedAndLastUpdatedFieldsArePopulated()
        {
            // arrange
            var saveThis = LookupTestUtility.CreateModel(true);

            ValidatorStrategy.IsValidReturnValue = true;
            Assert.IsFalse(
                String.IsNullOrWhiteSpace(UsernameProvider.GetUsernameReturnValue),
                "Username provider was not initialized propertly.");

            // act
            SystemUnderTest.Save(saveThis);

            // assert
            UnitTestUtility.AssertDomainModelBaseAuditFieldsArePopulated(
                saveThis, UsernameProvider.GetUsernameReturnValue, "saveThis");



            var entity = LookupRepositoryInstance.GetById(saveThis.Id);

            Assert.IsNotNull(entity, "Entity wasn't saved to repository.");
            LookupTestUtility.AssertAreEqual(saveThis, entity);
        }