Exemplo n.º 1
0
        public void CreateNewRating_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = "user-id-1", BookId = 1234
            };
            var rating2 = new Rating()
            {
                UserId = "user-id-2", BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.Ratings.Add(It.IsAny <Rating>())).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.Ratings.Add(It.IsAny <Rating>()), Times.Once);
        }
Exemplo n.º 2
0
        private RatingsService CreateRatingsService()
        {
            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new RatingsService(userId);

            return(service);
        }
Exemplo n.º 3
0
        public void CreateNewRatingWithCorrectProperties_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = "user-id-1", BookId = 1234
            };
            var rating2 = new Rating()
            {
                UserId = "user-id-2", BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            Rating addedRating = null;

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.Ratings.Add(It.IsAny <Rating>()))
            .Callback((Rating r) => addedRating = r);

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.Ratings.Add(It.IsAny <Rating>()), Times.Once);
            Assert.AreEqual(1, addedRating.BookId);
            Assert.AreEqual("radom-id", addedRating.UserId);
            Assert.AreEqual(5, addedRating.Value);
        }
Exemplo n.º 4
0
        public void ReturnCorrectRating_WhenFound(int bookId, string userId, int expectedValue)
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();

            var rating1Match = new Rating()
            {
                BookId = 3, UserId = "str", Value = 11
            };
            var rating2Match = new Rating()
            {
                BookId = 15, UserId = "user", Value = 2
            };
            var ratingNotMatch = new Rating()
            {
                BookId = 1, UserId = "str"
            };
            var ratings = new List <Rating>()
            {
                rating1Match,
                rating2Match,
                ratingNotMatch
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.SaveChanges()).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            var rating = service.GetRating(bookId, userId);

            // Assert
            Assert.AreEqual(expectedValue, rating);
        }
Exemplo n.º 5
0
        public void ChangeRatingValue_WhenRatingFound()
        {
            // Arrange
            var userId     = "user-id-12424";
            var bookId     = 24147;
            int rateValue  = 5;
            var mockedData = new Mock <IBetterReadsData>();
            var rating1    = new Rating()
            {
                UserId = userId, BookId = bookId
            };
            var rating2 = new Rating()
            {
                UserId = userId, BookId = 2345
            };
            var ratings = new List <Rating>()
            {
                rating1,
                rating2
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(bookId, userId, rateValue);

            // Assert
            Assert.AreEqual(rateValue, rating1.Value);
        }
Exemplo n.º 6
0
        public void CallDataSaveChanges_WhenRatingFound()
        {
            // Arrange
            var userId     = "user-id-12424";
            var bookId     = 24147;
            var mockedData = new Mock <IBetterReadsData>();

            var rating1 = new Rating()
            {
                UserId = userId, BookId = bookId
            };
            var ratings = new List <Rating>()
            {
                rating1
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);
            mockedData.Setup(x => x.SaveChanges()).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(bookId, userId, 5);

            // Assert
            mockedData.Verify(x => x.SaveChanges(), Times.Once);
        }
Exemplo n.º 7
0
        // GET: Workouts
        public ActionResult Index()
        {
            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new RatingsService(userId);
            var model   = service.GetRating();

            return(View(model));
        }
        internal void Initialize()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString());

            this.dbContext          = new ApplicationDbContext(options.Options);
            this.ratingsRepository  = new EfRepository <Rating>(this.dbContext);
            this.projectsRepository = new EfDeletableEntityRepository <Project>(this.dbContext);
            this.service            = new RatingsService(this.ratingsRepository, this.projectsRepository);
        }
        public void Rate_CreatesNewRatingIfItDoesntAlreadyExist()
        {
            // Arrange
            var context       = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var ratingService = new RatingsService(context);

            // Act
            var newRating = ratingService.Rate(1, "4", RatingType.Positive);

            //Assert
            Assert.Equal(1, newRating);
            Assert.Single(context.Ratings);
            Assert.Equal(RatingType.Positive, context.Ratings.First().RatingType);
        }
        public void Rate_SetsToNewRatingIfARatingAlreadyExists()
        {
            // Arrange
            var context       = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var ratingService = new RatingsService(context);

            // Act
            ratingService.Rate(1, "4", RatingType.Positive);
            var newRating = ratingService.Rate(1, "4", RatingType.Negative);

            //Assert
            Assert.Equal(-1, newRating);
            Assert.Equal(RatingType.Negative, context.Ratings.First().RatingType);
        }
        public void Rate_SetsToNeutralIfTheSaveRatingIsGivenTwice()
        {
            // Arrange
            var context       = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var ratingService = new RatingsService(context);

            // Act
            ratingService.Rate(1, "4", RatingType.Positive);
            var newRating = ratingService.Rate(1, "4", RatingType.Positive);

            //Assert
            Assert.Equal(0, newRating);
            Assert.Equal(RatingType.Neutral, context.Ratings.First().RatingType);
        }
Exemplo n.º 12
0
        public async Task TwoRatesFromDifferentUsersShoiuldCountTwoTimes()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            await service.RateAsync(1, "UserId1", 3);

            await service.RateAsync(1, "UserId2", 3);

            var rates = service.GetAllCount();

            Assert.Equal(2, rates);
        }
Exemplo n.º 13
0
        public async Task IncorrectRateShoiuldReturnZeroCount()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            await service.RateAsync(1, "UserId", 6);

            await service.RateAsync(1, "UserId", 0);

            var rates = service.GetAllCount();

            Assert.Equal(0, rates);
        }
Exemplo n.º 14
0
        public void CallDataSaveChanges_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();

            mockedData.Setup(x => x.Ratings.All).Returns(new List <Rating>().AsQueryable());
            mockedData.Setup(x => x.SaveChanges()).Verifiable();

            var service = new RatingsService(mockedData.Object);

            // Act
            service.RateBook(1, "radom-id", 5);

            // Assert
            mockedData.Verify(x => x.SaveChanges(), Times.Once);
        }
Exemplo n.º 15
0
        public async Task TwoRateFromSameUserShoiuldUpdateLastRate()
        {
            var companyId = 1;

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            await service.RateAsync(companyId, "UserId1", 3);

            await service.RateAsync(companyId, "UserId1", 5);

            var avgRates = await service.GetAvgCompanyRate(companyId);

            Assert.Equal(5.0, avgRates);
        }
        public void GetUsersRatingTypeForAPost_GetsCorrectRatingType()
        {
            // Arrange
            var context = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var user    = new User {
                Id = "4", UserName = "******"
            };

            context.Users.Add(user);
            context.SaveChanges();
            var ratingService = new RatingsService(context);

            // Act
            ratingService.Rate(1, user.Id, RatingType.Positive);
            var ratingType = ratingService.GetUsersRatingTypeForAPost(1, user.UserName);

            //Assert
            Assert.Equal(RatingType.Positive, ratingType);
        }
Exemplo n.º 17
0
        public async Task GetCompanyRatesShoiuldReturnCorrectTotalRatesForCurrentCompany()
        {
            var companyId = 1;

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            await service.RateAsync(companyId, "UserId1", 3);

            await service.RateAsync(companyId, "UserId2", 5);

            await service.RateAsync(companyId, "UserId3", 4);

            var companyRatesCount = service.GetCompanyRates(companyId);

            Assert.Equal(3, companyRatesCount);
        }
Exemplo n.º 18
0
        public async Task UserCannotRateIfUserAlreayRate()
        {
            var companyId = 1;

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            var rateIsAllowed = service.IsRateAllowed(companyId, "UserId1");

            Assert.True(rateIsAllowed);

            await service.RateAsync(companyId, "UserId1", 3);

            var rateAllowed = service.IsRateAllowed(companyId, "UserId1");

            Assert.False(rateAllowed);
        }
Exemplo n.º 19
0
        public async Task ManyRatesFromDifferentUsersShoiuldReturnCorrectAverageRate()
        {
            var companyId = 1;

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var mockRepository = new EfDeletableEntityRepository <Rate>(new ApplicationDbContext(options.Options));
            var service        = new RatingsService(mockRepository);

            await service.RateAsync(companyId, "UserId1", 3);

            await service.RateAsync(companyId, "UserId2", 5);

            await service.RateAsync(companyId, "UserId3", 4);

            var avgRates = await service.GetAvgCompanyRate(companyId);

            Assert.Equal(4.0, avgRates);
        }
Exemplo n.º 20
0
        public async Task When2UsersRateForTheSameQuestionTheAverageVoteShouldBeCorrect()
        {
            var list     = new List <Rating>();
            var mockRepo = new Mock <IRepository <Rating> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Rating>())).Callback(
                (Rating rating) => list.Add(rating));
            var service = new RatingsService(mockRepo.Object);

            await service.SetRatingAsync(2, "Niki", 5);

            await service.SetRatingAsync(2, "Pesho", 1);

            await service.SetRatingAsync(2, "Niki", 2);

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

            Assert.Equal(2, list.Count);
            Assert.Equal(1.5, service.GetAverageRatings(2));
        }
Exemplo n.º 21
0
        public void ReturnZero_WhenRatingNotFound()
        {
            // Arrange
            var mockedData = new Mock <IBetterReadsData>();

            var ratings = new List <Rating>()
            {
                new Mock <Rating>().Object,
                new Mock <Rating>().Object,
                new Mock <Rating>().Object
            }.AsQueryable();

            mockedData.Setup(x => x.Ratings.All).Returns(ratings);

            var service = new RatingsService(mockedData.Object);

            // Act
            var rating = service.GetRating(1, "str");

            // Assert
            Assert.AreEqual(0, rating);
        }
        public void GetUsersRatings_ReturnsCorrectRatings()
        {
            // Arrange
            var context = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var user1   = new User {
                Id = "4", UserName = "******"
            };
            var user2 = new User {
                Id = "2", UserName = "******"
            };

            context.Users.AddRange(user1, user2);
            context.SaveChanges();
            var ratingService = new RatingsService(context);

            // Act
            ratingService.Rate(1, user1.Id, RatingType.Positive);
            ratingService.Rate(1, user2.Id, RatingType.Negative);
            var ratings = ratingService.GetUsersRatings(user1.UserName);

            //Assert
            Assert.Equal(RatingType.Positive, ratings.First().RatingType);
        }
Exemplo n.º 23
0
        public async Task WhenUserRate2TimesOnly1RateeShouldBeCounted()
        {
            var list     = new List <Rating>();
            var mockRepo = new Mock <IRepository <Rating> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <Rating>())).Callback(
                (Rating rating) => list.Add(rating));
            var service = new RatingsService(mockRepo.Object);

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

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

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

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

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

            Assert.Equal(1, list.Count);
            Assert.Equal(5, list.First().Value);
        }
Exemplo n.º 24
0
        public NFLResult PredictGame(NFLGame game, IStorePredictions persistor, DateTime predictionDate)
        {
            const int avgScore = 21;

            var homeRating = RatingsService.GetNibbleRatingFor(game.HomeNflTeam, predictionDate);
            var awayRating = RatingsService.GetNibbleRatingFor(game.AwayNflTeam, predictionDate);

            var homeOff = homeRating.Offence;
            var homeDef = homeRating.Defence;
            var awayOff = awayRating.Offence;
            var awayDef = awayRating.Defence;

            var homeScore = avgScore + ((homeOff + awayDef) / 2) + 3;              //  3pts home field advantage
            var awayScore = avgScore + ((awayOff + homeDef) / 2);

            homeScore = Utility.PickAScore(homeScore);
            awayScore = Utility.PickAScore(awayScore);

            if (homeScore == awayScore)
            {
                homeScore++;                                      //  no ties
            }
            var res = new NFLResult(game.HomeTeam, homeScore, game.AwayTeam, awayScore);

            //TODO:  Nibble predictor does not predict Tdp or Tdr

            if (AuditTrail)
            {
                AuditPrediction(game, awayDef, awayOff, homeOff, res, homeDef);
                if (StorePrediction)
                {
                    StorePredictedResult(game, res);
                }
            }
            return(res);
        }
Exemplo n.º 25
0
 public RatingsController(IKTMData data)
     : base(data)
 {
     this.service = new RatingsService();
 }