public void Invoke_Add_In_UserTweetRepo_When_Tweet_Is_New()
        {
            //Arrange
            var fakeUnit             = new Mock <IUnitOfWork>();
            var fakeDateTimeProvider = Mock.Of <IDateTimeProvider>();
            var fakeTweetService     = new Mock <ITweetService>();
            var sut = new UserTweetService(fakeUnit.Object, fakeTweetService.Object, fakeDateTimeProvider);

            var fakeUserTweetRepo = new Mock <IGenericRepository <UserTweet> >();
            var fakeTweetRepo     = new Mock <IGenericRepository <Tweet> >();

            var tweet = new Tweet {
                TweetId = "456"
            };
            var tweetCollection = new List <Tweet> {
                tweet
            };

            fakeUnit.Setup(u => u.UserTweets).Returns(fakeUserTweetRepo.Object);
            fakeTweetRepo.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            fakeUnit.Setup(u => u.Tweets).Returns(fakeTweetRepo.Object);
            fakeTweetService.Setup(s => s.CreateFromApiById(It.IsAny <string>())).Returns(new Tweet {
                TweetId = "789"
            });

            //Act
            sut.SaveSingleTweetToUserByTweetId("123", "789");

            //Assert
            fakeUserTweetRepo.Verify(v => v.Add(It.IsAny <UserTweet>()), Times.Once());
        }
        public void Invoke_SaveChanges_In_UnitOfWork_When_Tweet_Exist()
        {
            //Arrange
            var fakeUnit         = new Mock <IUnitOfWork>();
            var dateTimeProvider = new TestDateTimeProvider();
            var fakeTweetService = new Mock <ITweetService>();
            var sut = new UserTweetService(fakeUnit.Object, fakeTweetService.Object, dateTimeProvider);

            var fakeUserTweetRepo = new Mock <IGenericRepository <UserTweet> >();
            var fakeTweetRepo     = new Mock <IGenericRepository <Tweet> >();

            var tweet = new Tweet {
                TweetId = "456"
            };
            var tweetCollection = new List <Tweet> {
                tweet
            };

            fakeUnit.Setup(u => u.UserTweets).Returns(fakeUserTweetRepo.Object);
            fakeTweetRepo.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            fakeUnit.Setup(u => u.Tweets).Returns(fakeTweetRepo.Object);
            fakeUnit.Setup(u => u.SaveChanges()).Verifiable();

            //Act
            sut.SaveSingleTweetToUserByTweetId("123", "456");

            //Assert
            fakeUnit.Verify(v => v.SaveChanges(), Times.Once);
        }
        public void Invoke_SaveChanges_When_Tweet_Exists_But_Deleted()
        {
            //Arrange
            var fakeUnit         = new Mock <IUnitOfWork>();
            var dateTimeProvider = new TestDateTimeProvider();
            var fakeTweetService = new Mock <ITweetService>();
            var sut = new UserTweetService(fakeUnit.Object, fakeTweetService.Object, dateTimeProvider);

            var fakeUserTweetRepo = new Mock <IGenericRepository <UserTweet> >();
            var fakeTweetRepo     = new Mock <IGenericRepository <Tweet> >();
            var fakeTweetTagRepo  = new Mock <IGenericRepository <TweetTag> >();

            var tweet = new Tweet {
                TweetId = "456", IsDeleted = true
            };
            var tweetCollection = new List <Tweet> {
                tweet
            };
            var tweetTagCollection = new List <TweetTag>();

            fakeUnit.Setup(u => u.UserTweets).Returns(fakeUserTweetRepo.Object);
            fakeUnit.Setup(u => u.TweetTags).Returns(fakeTweetTagRepo.Object);
            fakeTweetRepo.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            fakeTweetTagRepo.Setup(r => r.AllAndDeleted).Returns(tweetTagCollection.AsQueryable());
            fakeUnit.Setup(u => u.Tweets).Returns(fakeTweetRepo.Object);
            fakeUnit.Setup(s => s.SaveChanges()).Verifiable();

            //Act
            sut.SaveSingleTweetToUserByTweetId("123", "456");

            //Assert
            fakeUnit.Verify(v => v.SaveChanges(), Times.Exactly(2)); //Once for the Modified/Deleted and twice for the UserTweet
        }
        public void Change_DeletedOn_When_Tweet_Exist_But_Deleted()
        {
            //Arrange
            var fakeUnit         = new Mock <IUnitOfWork>();
            var dateTimeProvider = new TestDateTimeProvider();
            var fakeTweetService = new Mock <ITweetService>();
            var sut = new UserTweetService(fakeUnit.Object, fakeTweetService.Object, dateTimeProvider);

            var fakeUserTweetRepo = new Mock <IGenericRepository <UserTweet> >();
            var fakeTweetRepo     = new Mock <IGenericRepository <Tweet> >();
            var fakeTweetTagRepo  = new Mock <IGenericRepository <TweetTag> >();

            var tweet = new Tweet {
                TweetId = "456", IsDeleted = true
            };
            var tweetCollection = new List <Tweet> {
                tweet
            };
            var tweetTagCollection = new List <TweetTag>();

            fakeUnit.Setup(u => u.UserTweets).Returns(fakeUserTweetRepo.Object);
            fakeUnit.Setup(u => u.TweetTags).Returns(fakeTweetTagRepo.Object);
            fakeTweetRepo.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            fakeTweetTagRepo.Setup(r => r.AllAndDeleted).Returns(tweetTagCollection.AsQueryable());
            fakeUnit.Setup(u => u.Tweets).Returns(fakeTweetRepo.Object);

            //Act
            sut.SaveSingleTweetToUserByTweetId("123", "456");

            //Assert
            Assert.AreEqual(dateTimeProvider.Now, tweet.ModifiedOn.Value);
        }
        public void Throw_Argument_Null_Exception_When_TweetId_Is_Null()
        {
            //Arrange
            var fakeTimeProvider = Mock.Of <IDateTimeProvider>();
            var fakeUnit         = Mock.Of <IUnitOfWork>();
            var fakeTweetService = Mock.Of <ITweetService>();
            var sut = new UserTweetService(fakeUnit, fakeTweetService, fakeTimeProvider);

            //Act & Assert
            Assert.ThrowsException <ArgumentNullException>(() => sut.SaveSingleTweetToUserByTweetId("123", null));
        }
        public void Change_Deleted_State_Of_Tag_When_Tweet_Exist_But_Deleted()
        {
            //Arrange
            var fakeUnit         = new Mock <IUnitOfWork>();
            var dateTimeProvider = new TestDateTimeProvider();
            var fakeTweetService = new Mock <ITweetService>();
            var sut = new UserTweetService(fakeUnit.Object, fakeTweetService.Object, dateTimeProvider);

            var fakeUserTweetRepo = new Mock <IGenericRepository <UserTweet> >();
            var fakeTweetRepo     = new Mock <IGenericRepository <Tweet> >();
            var fakeTweetTagRepo  = new Mock <IGenericRepository <TweetTag> >();

            var tweet = new Tweet {
                TweetId = "456", IsDeleted = true, DeletedOn = dateTimeProvider.DeletedOn
            };
            var tweetCollection = new List <Tweet> {
                tweet
            };
            var testTag1 = new Tag {
                Id = 1, IsDeleted = true, DeletedOn = dateTimeProvider.DeletedOn
            };
            var testTag2 = new Tag {
                Id = 2, IsDeleted = true, DeletedOn = dateTimeProvider.DeletedOn
            };
            var testTweetTag1 = new TweetTag {
                TweetId = tweet.TweetId, TagId = testTag1.Id, Tag = testTag1, IsDeleted = true, DeletedOn = dateTimeProvider.DeletedOn
            };
            var testTweetTag2 = new TweetTag {
                TweetId = tweet.TweetId, TagId = testTag2.Id, Tag = testTag2, IsDeleted = true, DeletedOn = dateTimeProvider.DeletedOn
            };
            var tweetTagCollection = new List <TweetTag> {
                testTweetTag1, testTweetTag2
            };

            fakeUnit.Setup(u => u.UserTweets).Returns(fakeUserTweetRepo.Object);
            fakeUnit.Setup(u => u.TweetTags).Returns(fakeTweetTagRepo.Object);
            fakeTweetRepo.Setup(r => r.AllAndDeleted).Returns(tweetCollection.AsQueryable());
            fakeTweetTagRepo.Setup(r => r.AllAndDeleted).Returns(tweetTagCollection.AsQueryable());
            fakeUnit.Setup(u => u.Tweets).Returns(fakeTweetRepo.Object);

            //Act
            sut.SaveSingleTweetToUserByTweetId("123", "456");

            //Assert
            Assert.IsFalse(testTag1.IsDeleted);
            Assert.IsNull(testTag1.DeletedOn);
            Assert.IsFalse(testTag2.IsDeleted);
            Assert.IsNull(testTag2.DeletedOn);
        }