public void GetFilteredElements_SpecificKOrder_Test()
        {
            //Arrange
            IQueryableUnitOfWork        unitOfWork   = new UnitOfWorkStub();
            ITraceManager               traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();
            ExtendedRepository <Entity> target       = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            target.GetFilteredElements <SubEntity>(e => e.Id == 1);
        }
        public void EnlistItem()
        {
            object obj = new object();

            using (UnitOfWorkStub uow = new UnitOfWorkStub())
            {
                uow.Enlist(obj);
                Assert.IsTrue(uow.IsEnlisted(obj));
            }
        }
        public void GetFilteredElements_FilterNullThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            target.GetFilteredElements(null);
        }
        public void GetFilteredElements_SpecificKFilterAndSOrder_ORderNullThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            target.GetFilteredElements <SubEntity, int>(t => t.Id == 3, null, false);
        }
        public void GetPagedElementsSubType_InvalidOrderExpressionArgumentException_Test()
        {
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <SubEntity> target = new ExtendedRepository <SubEntity>(unitOfWork, traceManager);
            int pageIndex = 1;
            int pageCount = 1;

            target.GetPagedElements <SubEntity, int>(pageIndex, pageCount, null, false);
        }
        public void GetBySpecWithSubType_NullSpecThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            IEnumerable <Entity> result = target.GetBySpec <SubEntity>((ISpecification <SubEntity>)null);
        }
        public void GetFilteredElements_SpecificKOrderFullSignature_NullFilterThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            target.GetFilteredElements <int>(null, 1, 1, t => t.Id, true);
        }
        public void ApplyChanges_NullEntityCollectionThrowNewArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork actual       = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            //Act
            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(actual, traceManager);

            //Assert
            target.Modify((Collection <Entity>)null);
        }
        public void GetBySpec_NullSpecThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            ISpecification <Entity>     spec   = new DirectSpecification <Entity>(t => t.Id == 1);

            //Act
            target.GetBySpec((ISpecification <Entity>)null);
        }
        public void Attach_NullItem_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            Entity entity = null;

            //Act
            target.RegisterItem(entity);
        }
        public void Add_NullItemThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            Entity entity = null;

            //Act
            target.Add(entity);
        }
        public void CurrentReturnsCorrectly()
        {
            Assert.IsNull(UnitOfWorkStub.Current);

            using (UnitOfWorkStub uow = new UnitOfWorkStub())
            {
                Assert.IsNotNull(UnitOfWorkStub.Current);
                Assert.IsTrue(object.ReferenceEquals(uow, UnitOfWorkStub.Current));
            }

            Assert.IsNull(UnitOfWorkStub.Current);
        }
        public void ApplyChanges_Test()
        {
            //Arrange
            IQueryableUnitOfWork actual       = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            //Act
            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(actual, traceManager);
            Entity item = target.GetAll().First();

            //Assert
            target.Modify(item);
        }
        public void GetFilteredAndOrderedAndPagedElements_InvalidPageCountThrowArgumentException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            int pageIndex = 0;
            int pageCount = 0;

            //Act
            IEnumerable <Entity> result = target.GetFilteredElements <int>(e => e.Id == 1, pageIndex, pageCount, null, false);
        }
        public void GetAll_ForSubtypes_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            IEnumerable <SubEntity> result = target.GetAll <SubEntity>();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() == 1);
        }
        public void GetFilteredAndOrderedElements_InvalidOrderByExpressionThrowArgumentNullException_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            IEnumerable <Entity> result = target.GetFilteredElements <int>(e => e.Id == 1, null, false);

            //Assert
            Assert.IsTrue(result != null);
            Assert.IsTrue(result.Count() == 1);
        }
        public void GetBySpec_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            ISpecification <Entity>     spec   = new DirectSpecification <Entity>(t => t.Id == 1);

            //Act
            IEnumerable <Entity> result = target.GetBySpec(spec);

            //Assert
            Assert.IsTrue(result.Count() == 1);
        }
        public void GetPagedElements_AscendingOrder_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            int pageIndex = 0;
            int pageCount = 1;

            //Act
            IEnumerable <Entity> result = target.GetPagedElements(pageIndex, pageCount, e => e.Id > 0, true);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Count(), pageCount);
        }
        public void GetFiltered_WithAscendingOrderedAndPagedElements_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            int pageIndex = 0;
            int pageCount = 1;

            //Act
            IEnumerable <Entity> result = target.GetFilteredElements <int>(e => e.Id == 1, pageIndex, pageCount, e => e.Id, true);

            //Assert
            Assert.IsTrue(result != null);
            Assert.IsTrue(result.Count() == 1);
        }
        /// <summary>
        ///A test for Container
        ///</summary>
        public void unitOfWorkTestHelper <T>()
            where T : class, IObjectWithChangeTracker, new()
        {
            //Arrange
            IQueryableUnitOfWork actual       = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            //Act
            ExtendedRepository <T> target = new ExtendedRepository <T>(actual, traceManager);

            //Assert
            IUnitOfWork expected;

            expected = target.UnitOfWork as IUnitOfWork;

            Assert.AreEqual(expected, actual);
        }
        public void Attach_Test()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            Entity entity = new Entity()
            {
                Id = 5, Field = "test"
            };

            //Act
            target.RegisterItem(entity);

            //Assert
            Assert.IsTrue(target.GetFilteredElements(t => t.Id == 5).Count() == 1);
        }
        public void DeleteTest()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);

            //Act
            IEnumerable <Entity> result = target.GetAll();

            Entity firstEntity = result.First();

            target.Remove(firstEntity);

            IEnumerable <Entity> postResult = target.GetAll();

            //Assert
            Assert.IsNotNull(postResult);
            Assert.IsFalse(postResult.Contains(firstEntity));
        }
        public void AddTest()
        {
            //Arrange
            IQueryableUnitOfWork unitOfWork   = new UnitOfWorkStub();
            ITraceManager        traceManager = IoCFactory.Instance.CurrentContainer.Resolve <ITraceManager>();

            ExtendedRepository <Entity> target = new ExtendedRepository <Entity>(unitOfWork, traceManager);
            Entity entity = new Entity()
            {
                Id    = 4,
                Field = "field 4"
            };


            //Act
            target.Add(entity);
            IEnumerable <Entity> result = target.GetAll();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() == 4);
            Assert.IsTrue(result.Contains(entity));
        }
        public void EnlistIEnumerable_WhenAlreadyDisposed_ThrowsObjectDisposedException()
        {
            UnitOfWorkStub uow = new UnitOfWorkStub();
            uow.Dispose();

            IEnumerable<object> items = new[] {new object()};

            Assert.Throws<ObjectDisposedException>(() => uow.Enlist(items));
        }
        public void EnlistParams_WhenAlreadyDisposed_ThrowsObjectDisposedException()
        {
            UnitOfWorkStub uow = new UnitOfWorkStub();
            uow.Dispose();

            Assert.Throws<ObjectDisposedException>(() => uow.Enlist(new object()));
        }
        public void IsEnlisted_WhenAlreadyDisposed_ThrowsObjectDisposedException()
        {
            UnitOfWorkStub uow = new UnitOfWorkStub();
            uow.Dispose();

            Assert.Throws<ObjectDisposedException>(() => uow.IsEnlisted(null));
        }
        public void Complete_WhenAlreadyDisposed_ThrowsObjectDisposedException()
        {
            UnitOfWorkStub uow = new UnitOfWorkStub();
            uow.Dispose();

            Assert.Throws<ObjectDisposedException>(uow.Complete);
        }
 public void CompletingTwiceIsNotPermitted()
 {
     using (UnitOfWorkStub uow = new UnitOfWorkStub())
     {
         uow.Complete();
         Assert.Throws<InvalidOperationException>(uow.Complete);
     }
 }
        public void ProcessCalled()
        {
            bool processCalled = false;

            object obj = new object();
            Action<object> process = item =>
            {
                Assert.IsTrue(object.ReferenceEquals(obj, item));
                processCalled = true;
            };

            using (UnitOfWorkStub uow = new UnitOfWorkStub(process))
            {
                uow.Enlist(obj);
                uow.Complete();
            }

            Assert.IsTrue(processCalled);
        }
        public void EnlistItemWhenNested()
        {
            object obj = new object();

            using (UnitOfWorkStub uow = new UnitOfWorkStub())
            {
                using (UnitOfWorkStub uow2 = new UnitOfWorkStub())
                {
                    uow2.Enlist(obj);
                    Assert.IsFalse(uow.IsEnlisted(obj));
                    Assert.IsTrue(uow2.IsEnlisted(obj));
                }

                Assert.IsFalse(uow.IsEnlisted(obj));
            }
        }