Ejemplo n.º 1
0
        public void CreateVoteShouldWorkCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);


            var repository = new EfRepository <Vote>(dbContext);

            var service = new VotesService(repository);

            var upVote = new Vote
            {
                NewsFeedPostId = 1,
                UserId         = "1",
                IsUpVote       = true,
            };
            var downVote = new Vote
            {
                NewsFeedPostId = 1,
                UserId         = "2",
                IsUpVote       = false,
            };

            service.VoteAsync(upVote.NewsFeedPostId, upVote.UserId, upVote.IsUpVote);

            service.VoteAsync(downVote.NewsFeedPostId, downVote.UserId, downVote.IsUpVote);

            dbContext.SaveChangesAsync();
            var resultUpVote   = service.GetUpVotes(1);
            var resultDownVote = service.GetDownVotes(1);

            Assert.Equal(1, resultUpVote);
            Assert.Equal(1, resultDownVote);
        }
        public async Task UpVoteAndDownVoteFromSamePersonShouldNegativeOne()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));
            var service        = new VotesService(mockRepository);

            await service.VoteAsync("d3946347-0005-45e0-8a02-ad8179d2ece6", "0a9ec75c-0560-4e5b-94d4-c44429bb7379", true);

            await service.VoteAsync("d3946347-0005-45e0-8a02-ad8179d2ece6", "0a9ec75c-0560-4e5b-94d4-c44429bb7379", false);

            var votes = service.GetVotes("d3946347-0005-45e0-8a02-ad8179d2ece6");

            Assert.Equal(-1, votes);
        }
        public async Task TwoDownVoteAndOneUpVoteFromTwoUsersShouldBeZero()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));
            var service        = new VotesService(mockRepository);

            await service.VoteAsync("d3946347-0005-45e0-8a02-ad8179d2ece6", "0a9ec75c-0560-4e5b-94d4-c44429bb7379", false);

            await service.VoteAsync("d3946347-0005-45e0-8a02-ad8179d2ece6", "175a2793-b1c8-4489-829e-549925137c57", false);

            await service.VoteAsync("d3946347-0005-45e0-8a02-ad8179d2ece6", "0a9ec75c-0560-4e5b-94d4-c44429bb7379", true);

            var votes = service.GetVotes("d3946347-0005-45e0-8a02-ad8179d2ece6");

            Assert.Equal(0, votes);
        }
Ejemplo n.º 4
0
        public async Task TwoDownVotesShouldCountOnce()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("testVotes");

            var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));

            var service = new VotesService(repository);

            await service.VoteAsync(1, "1", false);

            await service.VoteAsync(1, "1", false);

            var votes = service.GetVotes(1);

            Assert.Equal(-1, votes);
        }
Ejemplo n.º 5
0
        public void WhenUserVoteTwiceWithFalseForSamePostShouldCountOnlyOnce()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);


            var repository = new EfRepository <Vote>(dbContext);

            var service = new VotesService(repository);

            service.VoteAsync(1, "1", false);
            service.VoteAsync(1, "1", false);
            dbContext.SaveChangesAsync();
            var result = service.GetDownVotes(1);

            Assert.Equal(1, result);
        }
Ejemplo n.º 6
0
        public async Task DifferentVotesFromUserShouldChanheVote()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));
            var service    = new VotesService(repository);

            await service.VoteAsync(1, "1", true);

            await service.VoteAsync(1, "1", false);

            var votes = service.GetVotes(1);

            Assert.Equal(-1, votes);

            var votesInDiseaseForUser = await repository.All().CountAsync();

            Assert.Equal(1, votesInDiseaseForUser);
        }
Ejemplo n.º 7
0
        public async Task TwoDownVotesShouldCountOnce()
        {
            var options    = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));
            var service    = new VotesService(repository);

            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(1, "1", false);
            }

            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(1, "2", false);
            }

            var votes = service.GetVotes(1);

            Assert.Equal(-2, votes);
        }
Ejemplo n.º 8
0
        public async Task WhenTwoUsersGiveUpVoteSumShouldbeEqualToTwo()
        {
            var votes = new List <Vote>();

            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(x => x.All()).Returns(votes.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => votes.Add(vote));

            var service = new VotesService(mockRepo.Object);

            await service.VoteAsync("1", "1", true);

            await service.VoteAsync("1", "2", true);

            var sum = service.GetVotes("1");

            var expectedResult = 2;

            Assert.Equal(expectedResult, sum);
        }
Ejemplo n.º 9
0
        public async Task WhenUserVoteTwoTimesOnlyOneVoteShouldBeCounted()
        {
            var votes = new List <Vote>();

            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(x => x.All()).Returns(votes.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => votes.Add(vote));

            var service = new VotesService(mockRepo.Object);

            await service.VoteAsync("1", "1", false);

            await service.VoteAsync("1", "1", true);

            var expectedResult = 1;
            var expectedOutput = "UpVote";

            Assert.Equal(expectedResult, votes.Count);
            Assert.Equal(expectedOutput, votes.First().Type.ToString());
        }
Ejemplo n.º 10
0
        public async Task When2UsersVotesForSameArticleTheAverageShoultBeCorrect()
        {
            var list     = new List <Vote>();
            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => list.Add(vote));

            var service = new VotesService(mockRepo.Object);

            await service.VoteAsync(2, "Gosho", 5);

            await service.VoteAsync(2, "Tosho", 1);

            await service.VoteAsync(2, "Gosho", 2);

            mockRepo.Verify(x => x.AddAsync(It.IsAny <Vote>()), Times.Exactly(2));

            Assert.Equal(2, list.Count);
            Assert.Equal(1.5, service.GetAverageStarsFromVotes(2));
        }
Ejemplo n.º 11
0
        public async Task TwoDownVotesShouldCountOnce()
        {
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var votesRepository = new EfRepository <Vote>(context);

            var service = new VotesService(votesRepository);

            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(5, "e6335b0d-322e-48c8-9d42-205d3915ed39", false);
            }

            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(5, "d4b831df-ef23-4127-883a-76cd2533e28a", false);
            }

            var votes = service.GetVotes(5);

            Assert.Equal(-2, votes);
        }
Ejemplo n.º 12
0
        public async Task TestVoteAsyncMethod()
        {
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var votesRepository = new EfRepository <Vote>(context);

            var service = new VotesService(votesRepository);

            await service.VoteAsync(5, "e6335b0d-322e-48c8-9d42-205d3915ed39", true);

            var votes = service.GetVotes(5);

            Assert.Equal(1, votes);
        }
Ejemplo n.º 13
0
        public async Task WhenUserVotes2TimesOnly1VoteShoutBeCounted()
        {
            var list     = new List <Vote>();
            var mockRepo = new Mock <IRepository <Vote> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => list.Add(vote));

            var service = new VotesService(mockRepo.Object);

            await service.VoteAsync(1, "1", 1);

            await service.VoteAsync(1, "1", 3);

            await service.VoteAsync(1, "1", 4);

            await service.VoteAsync(1, "1", 5);

            await service.VoteAsync(1, "1", 5);

            Assert.Equal(1, list.Count);
            Assert.Equal(5, list.First().StarsCount);
        }
Ejemplo n.º 14
0
        public async Task TwoDownVotesShouldCountOnce()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "MemoryDb");

            var repository = new EfRepository <Vote>(new ApplicationDbContext(options.Options));
            var service    = new VotesService(repository);

            // voting 100 times with first User
            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(1, "1", false); // down vote
            }

            // voting 100 times with second User
            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(1, "2", false); // down vote
            }

            var votesCount = service.GetVotes(1);

            Assert.Equal(-2, votesCount);
        }
Ejemplo n.º 15
0
        public async Task TestGetVotesMethod()
        {
            var context         = ApplicationDbContextInMemoryFactory.InitializeContext();
            var votesRepository = new EfRepository <Vote>(context);

            var service = new VotesService(votesRepository);

            for (int i = 0; i < 100; i++)
            {
                await service.VoteAsync(5, $"{i}", true);
            }

            var votes = service.GetVotes(5);

            Assert.Equal(100, votes);
        }
        public async Task AnyNumberOfDownVoteShouldCountOnce()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("x").Options;

            var repository = new EfRepository <Vote>(
                new ApplicationDbContext(options));

            var service = new VotesService(repository);

            for (int i = 0; i < 5; i++)
            {
                await service.VoteAsync(1, "1", false);
            }

            var votesCount = service.GetVotes(1);

            Assert.Equal(-1, votesCount);
        }