示例#1
0
        public async Task EditPost_WhenIdEqualsTeamSeasonIdAndModelStateIsNotValid_ShouldReturnTeamSeasonEditView()
        {
            // Arrange
            var teamSeasonsIndexViewModel   = A.Fake <ITeamSeasonsIndexViewModel>();
            var teamSeasonsDetailsViewModel = A.Fake <ITeamSeasonsDetailsViewModel>();
            var teamSeasonRepository        = A.Fake <ITeamSeasonRepository>();
            var sharedRepository            = A.Fake <ISharedRepository>();
            var testController = new TeamSeasonsAdminController(teamSeasonsIndexViewModel, teamSeasonsDetailsViewModel,
                                                                teamSeasonRepository, sharedRepository);

            int id         = 1;
            var teamSeason = new TeamSeason
            {
                ID = 1
            };

            testController.ModelState.AddModelError("LongName", "Please enter a long name.");

            // Act
            var result = await testController.Edit(id, teamSeason);

            // Assert
            A.CallTo(() => teamSeasonRepository.Update(A <TeamSeason> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustNotHaveHappened();
            result.ShouldBeOfType <ViewResult>();
            ((ViewResult)result).Model.ShouldBe(teamSeason);
        }
        public void PredictGameScore_ShouldReturnCorrectlyCalculatedPredictedGameScores()
        {
            // Arrange
            var testObject = new GamePredictorService();

            var guestSeason = new TeamSeason
            {
                OffensiveAverage = 7.00d,
                OffensiveFactor  = 0.500d,
                DefensiveAverage = 14.00d,
                DefensiveFactor  = 1.500d
            };
            var hostSeason = new TeamSeason
            {
                OffensiveAverage = 28.00d,
                OffensiveFactor  = 2.000d,
                DefensiveAverage = 21.00d,
                DefensiveFactor  = 1.000d
            };

            // Act
            var(predictedGuestScore, predictedHostScore) =
                testObject.PredictGameScore(guestSeason, hostSeason);

            // Assert
            predictedGuestScore.ShouldBe((guestSeason.OffensiveFactor * hostSeason.DefensiveAverage +
                                          hostSeason.DefensiveFactor * guestSeason.OffensiveAverage) / 2d);
            predictedHostScore.ShouldBe((hostSeason.OffensiveFactor * guestSeason.DefensiveAverage +
                                         guestSeason.DefensiveFactor * hostSeason.OffensiveAverage) / 2d);
        }
        public async Task FindEntityAsync_ObjectNotFoundException_MethodAborts()
        {
            // Arrange
            var service = new SharedService(_dataMapper, _seasonRepository, _teamSeasonRepository);

            var teamName  = "Team";
            var seasonID  = 2017;
            var dbContext = A.Fake <ProFootballEntities>();

            var teamSeason = new TeamSeason();

            A.CallTo(() => _teamSeasonRepository.FindEntityAsync(dbContext, A <string> .Ignored, A <int> .Ignored))
            .Throws <ObjectNotFoundException>();

            var teamSeasonViewModel = new TeamSeasonViewModel();

            A.CallTo(() => _dataMapper.MapToTeamSeasonViewModel(A <TeamSeason> .Ignored)).Returns(teamSeasonViewModel);

            // Act
            var result = await service.FindEntityAsync(teamName, seasonID, dbContext);

            // Assert
            A.CallTo(() => _dataMapper.MapToTeamSeasonViewModel(teamSeason)).MustNotHaveHappened();
            Assert.IsNull(result);
        }
示例#4
0
        public async Task Details_WhenIdIsNotNullAndTeamSeasonIsFound_ShouldReturnTeamSeasonDetailsView()
        {
            // Arrange
            var teamSeasonsIndexViewModel   = A.Fake <ITeamSeasonsIndexViewModel>();
            var teamSeasonsDetailsViewModel = A.Fake <ITeamSeasonsDetailsViewModel>();

            var        teamSeasonRepository = A.Fake <ITeamSeasonRepository>();
            TeamSeason?teamSeason           = new TeamSeason();

            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(A <int> .Ignored)).Returns(teamSeason);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new TeamSeasonsAdminController(teamSeasonsIndexViewModel, teamSeasonsDetailsViewModel,
                                                                  teamSeasonRepository, sharedRepository);

            int?id = 1;

            // Act
            var result = await testController.Details(id);

            // Assert
            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(id.Value)).MustHaveHappenedOnceExactly();
            teamSeasonsDetailsViewModel.TeamSeason.ShouldBe(teamSeason);
            result.ShouldBeOfType <ViewResult>();
            ((ViewResult)result).Model.ShouldBe(teamSeasonsDetailsViewModel);
        }
        public async Task GetTeamSeason_WhenExceptionIsCaught_ShouldReturnInternalServerError()
        {
            // Arrange
            var        teamSeasonRepository = A.Fake <ITeamSeasonRepository>();
            TeamSeason?teamSeason           = new TeamSeason();

            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(A <int> .Ignored)).Throws <Exception>();

            var sharedRepository = A.Fake <ISharedRepository>();

            var             mapper          = A.Fake <IMapper>();
            TeamSeasonModel?teamSeasonModel = new TeamSeasonModel();

            A.CallTo(() => mapper.Map <TeamSeasonModel>(A <TeamSeason> .Ignored)).Returns(teamSeasonModel);

            var linkGenerator = A.Fake <LinkGenerator>();

            var testController = new TeamSeasonsController(teamSeasonRepository, sharedRepository, mapper, linkGenerator);

            int id = 1;

            // Act
            var result = await testController.GetTeamSeason(id);

            // Assert
            result.Result.ShouldBeOfType <ObjectResult>();
            ((ObjectResult)result.Result).StatusCode.ShouldBe(StatusCodes.Status500InternalServerError);
            ((ObjectResult)result.Result).Value.ShouldBe("Database failure");
        }
        public void FindEntityAsync_EntityNotFoundInDataStore_ThrowsObjectNotFoundException()
        {
            // Arrange
            var repository = new TeamSeasonRepository();

            var dbContext = A.Fake <ProFootballEntities>();
            var teamName  = "Team";
            var seasonID  = 2017;

            TeamSeason teamSeason = null;

            A.CallTo(() => dbContext.TeamSeasons.FindAsync(A <string> .Ignored, A <int> .Ignored)).Returns(teamSeason);

            // Act
            TeamSeason result = null;

            Assert.ThrowsAsync <ObjectNotFoundException>(async() =>
            {
                result = await repository.FindEntityAsync(dbContext, teamName, seasonID);
            });

            // Assert
            A.CallTo(() => dbContext.TeamSeasons.FindAsync(teamName, seasonID)).MustHaveHappenedOnceExactly();
            Assert.IsNull(result);
        }
 public TeamSeasonViewModel MapToTeamSeasonViewModel(TeamSeason teamSeason)
 {
     return(new TeamSeasonViewModel
     {
         TeamName = teamSeason.TeamName,
         SeasonID = teamSeason.SeasonID,
         LeagueName = teamSeason.LeagueName,
         ConferenceName = teamSeason.ConferenceName,
         DivisionName = teamSeason.DivisionName,
         Games = teamSeason.Games,
         Wins = teamSeason.Wins,
         Losses = teamSeason.Losses,
         Ties = teamSeason.Ties,
         WinningPercentage = teamSeason.WinningPercentage,
         PointsFor = teamSeason.PointsFor,
         PointsAgainst = teamSeason.PointsAgainst,
         PythagoreanWins = teamSeason.PythagoreanWins,
         PythagoreanLosses = teamSeason.PythagoreanLosses,
         OffensiveAverage = teamSeason.OffensiveAverage,
         OffensiveFactor = teamSeason.OffensiveFactor,
         OffensiveIndex = teamSeason.OffensiveIndex,
         DefensiveAverage = teamSeason.DefensiveAverage,
         DefensiveFactor = teamSeason.DefensiveFactor,
         DefensiveIndex = teamSeason.DefensiveIndex,
         FinalPythagoreanWinningPercentage = teamSeason.FinalPythagoreanWinningPercentage
     });
 }
        public async Task DeleteTeamSeason_WhenTeamSeasonIsFoundAndNotDeleted_ShouldReturnBadRequest()
        {
            // Arrange
            var        teamSeasonRepository = A.Fake <ITeamSeasonRepository>();
            TeamSeason?teamSeason           = new TeamSeason();

            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(A <int> .Ignored)).Returns(teamSeason);

            var sharedRepository = A.Fake <ISharedRepository>();

            A.CallTo(() => sharedRepository.SaveChangesAsync()).Returns(0);

            var mapper        = A.Fake <IMapper>();
            var linkGenerator = A.Fake <LinkGenerator>();

            var testController = new TeamSeasonsController(teamSeasonRepository, sharedRepository, mapper, linkGenerator);

            int id = 1;

            // Act
            var result = await testController.DeleteTeamSeason(id);

            // Assert
            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(id)).MustHaveHappenedOnceExactly();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly();
            result.Result.ShouldBeOfType <BadRequestResult>();
        }
        public void AddTeamSeasons()
        {
            // Arrange
            var repository = new TeamSeasonRepository();

            var dbContext   = A.Fake <ProFootballEntities>();
            var teamSeasons = new List <TeamSeason>();

            for (int i = 1; i <= 3; i++)
            {
                var teamSeason = new TeamSeason
                {
                    TeamName = "Team " + i,
                    SeasonID = 2017
                };
                teamSeasons.Add(teamSeason);
            }
            A.CallTo(() => dbContext.TeamSeasons.AddRange(A <IEnumerable <TeamSeason> > .Ignored)).Returns(teamSeasons);

            // Act
            var result = repository.AddEntities(dbContext, teamSeasons);

            // Assert
            A.CallTo(() => dbContext.TeamSeasons.AddRange(teamSeasons)).MustHaveHappenedOnceExactly();
            Assert.AreSame(teamSeasons, result);
        }
        public async Task GetTeamSeason_WhenTeamSeasonIsNotNull_ShouldReturnTeamSeasonModelOfDesiredTeamSeason()
        {
            // Arrange
            var        teamSeasonRepository = A.Fake <ITeamSeasonRepository>();
            TeamSeason?teamSeason           = new TeamSeason();

            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(A <int> .Ignored)).Returns(teamSeason);

            var sharedRepository = A.Fake <ISharedRepository>();

            var             mapper          = A.Fake <IMapper>();
            TeamSeasonModel?teamSeasonModel = new TeamSeasonModel();

            A.CallTo(() => mapper.Map <TeamSeasonModel>(A <TeamSeason> .Ignored)).Returns(teamSeasonModel);

            var linkGenerator = A.Fake <LinkGenerator>();

            var testController = new TeamSeasonsController(teamSeasonRepository, sharedRepository, mapper, linkGenerator);

            int id = 1;

            // Act
            var result = await testController.GetTeamSeason(id);

            // Assert
            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(id)).MustHaveHappenedOnceExactly();
            A.CallTo(() => mapper.Map <TeamSeasonModel>(teamSeason)).MustHaveHappenedOnceExactly();
            result.Value.ShouldBeOfType <TeamSeasonModel>();
        }
        public async Task PutTeamSeason_WhenTeamSeasonIsFoundAndSaved_ShouldReturnModelOfTeamSeason()
        {
            // Arrange
            var        teamSeasonRepository = A.Fake <ITeamSeasonRepository>();
            TeamSeason?teamSeason           = new TeamSeason();

            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(A <int> .Ignored)).Returns(teamSeason);

            var sharedRepository = A.Fake <ISharedRepository>();

            A.CallTo(() => sharedRepository.SaveChangesAsync()).Returns(1);

            var mapper      = A.Fake <IMapper>();
            var returnModel = new TeamSeasonModel();

            A.CallTo(() => mapper.Map <TeamSeasonModel>(teamSeason)).Returns(returnModel);

            var linkGenerator = A.Fake <LinkGenerator>();

            var testController = new TeamSeasonsController(teamSeasonRepository, sharedRepository, mapper, linkGenerator);

            int id    = 1;
            var model = new TeamSeasonModel();

            // Act
            var result = await testController.PutTeamSeason(id, model);

            // Assert
            A.CallTo(() => teamSeasonRepository.GetTeamSeasonAsync(id)).MustHaveHappenedOnceExactly();
            A.CallTo(() => mapper.Map(model, teamSeason)).MustHaveHappenedOnceExactly();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly();
            A.CallTo(() => mapper.Map <TeamSeasonModel>(teamSeason)).MustHaveHappenedOnceExactly();
            result.Value.ShouldBe(returnModel);
        }
示例#12
0
        public async Task EditPost_WhenIdEqualsTeamSeasonIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndTeamSeasonWithIdExists_ShouldRethrowException()
        {
            // Arrange
            var teamSeasonsIndexViewModel   = A.Fake <ITeamSeasonsIndexViewModel>();
            var teamSeasonsDetailsViewModel = A.Fake <ITeamSeasonsDetailsViewModel>();

            var teamSeasonRepository = A.Fake <ITeamSeasonRepository>();

            A.CallTo(() => teamSeasonRepository.Update(A <TeamSeason> .Ignored)).Throws <DbUpdateConcurrencyException>();
            A.CallTo(() => teamSeasonRepository.TeamSeasonExists(A <int> .Ignored)).Returns(true);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new TeamSeasonsAdminController(teamSeasonsIndexViewModel, teamSeasonsDetailsViewModel,
                                                                  teamSeasonRepository, sharedRepository);

            int id         = 1;
            var teamSeason = new TeamSeason
            {
                ID = 1
            };

            // Act
            var func = new Func <Task <IActionResult> >(async() => await testController.Edit(id, teamSeason));

            // Assert
            await func.ShouldThrowAsync <DbUpdateConcurrencyException>();
        }
        public async Task FindEntityAsync_HappyPath()
        {
            // Arrange
            var service = new SharedService(_dataMapper, _seasonRepository, _teamSeasonRepository);

            var teamName  = "Team";
            var seasonID  = 2017;
            var dbContext = A.Fake <ProFootballEntities>();

            var teamSeason = new TeamSeason();

            A.CallTo(() => _teamSeasonRepository.FindEntityAsync(dbContext, A <string> .Ignored, A <int> .Ignored))
            .Returns(teamSeason);

            var teamSeasonViewModel = new TeamSeasonViewModel();

            A.CallTo(() => _dataMapper.MapToTeamSeasonViewModel(A <TeamSeason> .Ignored)).Returns(teamSeasonViewModel);

            // Act
            var result = await service.FindEntityAsync(teamName, seasonID, dbContext);

            // Assert
            A.CallTo(() => _teamSeasonRepository.FindEntityAsync(dbContext, teamName, seasonID))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => _dataMapper.MapToTeamSeasonViewModel(teamSeason)).MustHaveHappenedOnceExactly();
            Assert.AreSame(teamSeasonViewModel, result);
        }
示例#14
0
        public async Task EditPost_WhenIdEqualsTeamSeasonIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndTeamSeasonWithIdDoesNotExist_ShouldReturnNotFound()
        {
            // Arrange
            var teamSeasonsIndexViewModel   = A.Fake <ITeamSeasonsIndexViewModel>();
            var teamSeasonsDetailsViewModel = A.Fake <ITeamSeasonsDetailsViewModel>();

            var teamSeasonRepository = A.Fake <ITeamSeasonRepository>();

            A.CallTo(() => teamSeasonRepository.Update(A <TeamSeason> .Ignored)).Throws <DbUpdateConcurrencyException>();
            A.CallTo(() => teamSeasonRepository.TeamSeasonExists(A <int> .Ignored)).Returns(false);

            var sharedRepository = A.Fake <ISharedRepository>();
            var testController   = new TeamSeasonsAdminController(teamSeasonsIndexViewModel, teamSeasonsDetailsViewModel,
                                                                  teamSeasonRepository, sharedRepository);

            int id         = 1;
            var teamSeason = new TeamSeason
            {
                ID = 1
            };

            // Act
            var result = await testController.Edit(id, teamSeason);

            // Assert
            result.ShouldBeOfType <NotFoundResult>();
        }
示例#15
0
        public async Task EditPost_WhenIdEqualsTeamSeasonIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsNotCaught_ShouldUpdateTeamSeasonInDataStoreAndRedirectToIndexView()
        {
            // Arrange
            var teamSeasonsIndexViewModel   = A.Fake <ITeamSeasonsIndexViewModel>();
            var teamSeasonsDetailsViewModel = A.Fake <ITeamSeasonsDetailsViewModel>();
            var teamSeasonRepository        = A.Fake <ITeamSeasonRepository>();
            var sharedRepository            = A.Fake <ISharedRepository>();
            var testController = new TeamSeasonsAdminController(teamSeasonsIndexViewModel, teamSeasonsDetailsViewModel,
                                                                teamSeasonRepository, sharedRepository);

            int id         = 1;
            var teamSeason = new TeamSeason
            {
                ID = 1
            };

            // Act
            var result = await testController.Edit(id, teamSeason);

            // Assert
            A.CallTo(() => teamSeasonRepository.Update(teamSeason)).MustHaveHappenedOnceExactly();
            A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly();
            result.ShouldBeOfType <RedirectToActionResult>();
            ((RedirectToActionResult)result).ActionName.ShouldBe <string>(nameof(testController.Index));
        }
示例#16
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,TeamName,SeasonYear,LeagueName")] TeamSeason teamSeason)
        {
            if (id != teamSeason.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _teamSeasonRepository.Update(teamSeason);
                    await _sharedRepository.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await _teamSeasonRepository.TeamSeasonExists(teamSeason.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(teamSeason));
        }
        public void SelectedTeamSetter_WhenValueDoesNotEqualSelectedTeam_ShouldAssignValueToSelectedTeam()
        {
            // Arrange
            var teamSeasonRepository = A.Fake <ITeamSeasonRepository>();

            var teamSeasonScheduleRepository = A.Fake <ITeamSeasonScheduleRepository>();
            var teamSeasonOpponentProfiles   = new List <TeamSeasonOpponentProfile>();

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleProfile(A <string> .Ignored,
                                                                                     A <int> .Ignored)).Returns(teamSeasonOpponentProfiles);

            var teamSeasonScheduleTotals = new TeamSeasonScheduleTotals();

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleTotals(A <string> .Ignored,
                                                                                    A <int> .Ignored)).Returns(teamSeasonScheduleTotals);

            var teamSeasonScheduleAverages = new TeamSeasonScheduleAverages();

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleAverages(A <string> .Ignored,
                                                                                      A <int> .Ignored)).Returns(teamSeasonScheduleAverages);

            var testObject = new TeamSeasonsControlViewModel(teamSeasonRepository, teamSeasonScheduleRepository)
            {
                SelectedTeam = null
            };

            var teamSeason = new TeamSeason();

            // Act
            testObject.SelectedTeam = teamSeason;

            // Assert
            testObject.SelectedTeam.ShouldBe(teamSeason);

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleProfile(A <string> .Ignored,
                                                                                     A <int> .Ignored)).MustHaveHappenedOnceExactly();
            testObject.TeamSeasonScheduleProfile.ShouldBeOfType <ReadOnlyCollection <TeamSeasonOpponentProfile> >();
            testObject.TeamSeasonScheduleProfile.ShouldBe(teamSeasonOpponentProfiles.ToList());

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleTotals(A <string> .Ignored,
                                                                                    A <int> .Ignored)).MustHaveHappenedOnceExactly();
            testObject.TeamSeasonScheduleTotals.ShouldBeOfType <ReadOnlyCollection <TeamSeasonScheduleTotals> >();

            var teamSeasonScheduleTotalsList = new List <TeamSeasonScheduleTotals> {
                teamSeasonScheduleTotals
            };

            testObject.TeamSeasonScheduleTotals.ShouldBe(teamSeasonScheduleTotalsList);

            A.CallTo(() => teamSeasonScheduleRepository.GetTeamSeasonScheduleAverages(A <string> .Ignored,
                                                                                      A <int> .Ignored)).MustHaveHappenedOnceExactly();
            testObject.TeamSeasonScheduleAverages.ShouldBeOfType <ReadOnlyCollection <TeamSeasonScheduleAverages> >();

            var teamSeasonScheduleAveragesList = new List <TeamSeasonScheduleAverages> {
                teamSeasonScheduleAverages
            };

            testObject.TeamSeasonScheduleAverages.ShouldBe(teamSeasonScheduleAveragesList);
        }
示例#18
0
 public int Update(TeamSeason teamSeason)
 {
     return(Update(UPDATE, new Dictionary <string, object>
     {
         { "Id", teamSeason.Id },
         { "position", teamSeason.Position }
     }));
 }
示例#19
0
        /// <summary>
        /// Updates the rankings for a specified TeamSeason
        /// </summary>
        /// <param name="teamSeason">The TeamSeason object for which rankings will be updated</param>
        private void UpdateRankingsByTeamSeason(TeamSeason teamSeason)
        {
            try
            {
                lock (_dbLock)
                {
                    // Get needed stored procedure results.
                    var teamSeasonScheduleTotals = _storedProcedureRepository.GetTeamSeasonScheduleTotals(
                        teamSeason.TeamName, teamSeason.SeasonID).FirstOrDefault();

                    var teamSeasonScheduleAverages = _storedProcedureRepository.GetTeamSeasonScheduleAverages(
                        teamSeason.TeamName, teamSeason.SeasonID).FirstOrDefault();

                    // Calculate new rankings.
                    if (teamSeasonScheduleTotals != null && teamSeasonScheduleAverages != null &&
                        teamSeasonScheduleTotals.ScheduleGames != null)
                    {
                        teamSeason.OffensiveAverage = _calculator.Divide(teamSeason.PointsFor, teamSeason.Games);
                        teamSeason.DefensiveAverage = _calculator.Divide(teamSeason.PointsAgainst, teamSeason.Games);

                        teamSeason.OffensiveFactor =
                            _calculator.Divide((double)teamSeason.OffensiveAverage,
                                               (double)teamSeasonScheduleAverages.PointsAgainst);

                        teamSeason.DefensiveFactor =
                            _calculator.Divide((double)teamSeason.DefensiveAverage,
                                               (double)teamSeasonScheduleAverages.PointsFor);

                        var leagueSeason = _leagueSeasonRepository.FindEntity(teamSeason.LeagueName,
                                                                              teamSeason.SeasonID);

                        teamSeason.OffensiveIndex = (teamSeason.OffensiveAverage + teamSeason.OffensiveFactor *
                                                     leagueSeason.AveragePoints) / 2;

                        teamSeason.DefensiveIndex = (teamSeason.DefensiveAverage + teamSeason.DefensiveFactor *
                                                     leagueSeason.AveragePoints) / 2;

                        teamSeason.FinalPythagoreanWinningPercentage =
                            _calculator.CalculatePythagoreanWinningPercentage(teamSeason);
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message == "Nullable object must have a value.")
                {
                    // Ignore exception.
                }
                else
                {
                    _sharedService.ShowExceptionMessage(ex, $"InvalidOperationException: {teamSeason.TeamName}");
                }
            }
            catch (Exception ex)
            {
                _sharedService.ShowExceptionMessage(ex.InnerException, $"Exception: {teamSeason.TeamName}");
            }
        }
        /// <summary>
        /// Calculates a predicted game score.
        /// </summary>
        /// <param name="guestSeason">A <see cref="TeamSeason"/> object representing the guest's season data.</param>
        /// <param name="hostSeason">A <see cref="TeamSeason"/> object representing the host's season data.</param>
        /// <returns></returns>
        public (double?, double?) PredictGameScore(TeamSeason guestSeason, TeamSeason hostSeason)
        {
            var guestScore = (guestSeason.OffensiveFactor * hostSeason.DefensiveAverage +
                              hostSeason.DefensiveFactor * guestSeason.OffensiveAverage) / 2d;
            var hostScore = (hostSeason.OffensiveFactor * guestSeason.DefensiveAverage +
                             guestSeason.DefensiveFactor * hostSeason.OffensiveAverage) / 2d;

            return(guestScore, hostScore);
        }
示例#21
0
        public TeamSeason AwayTeam(ref Match match)
        {
            TeamSeason awayTeam   = new TeamSeason();
            string     teamAway   = match.AwayTeam;
            string     countryA   = match.Country;
            string     season     = match.Season.ToString();
            string     nameSportM = match.NameSport.ToString();
            var        searchAT   = from c in db.TeamSeasons
                                    where
                                    c.FederationSeason.SportFederation.NameSportString == nameSportM &&
                                    c.FederationSeason.SportFederation.Country == countryA &&
                                    c.Team.Name == teamAway &&
                                    c.SeasonString == season
                                    select c;

            if (searchAT.Count() > 0)
            {
                awayTeam = searchAT.First();
            }
            else
            {
                awayTeam = CreateTeamSeason(match, "away");
            }

            if (match.Tour == null || match.Tour.Substring(0, 5) == "Group")
            {
                if (match.AwayTeamResult == Result.Win)
                {
                    awayTeam.Win++;
                    awayTeam.Point += 3;
                }
                else
                if (match.AwayTeamResult == Result.Draw)
                {
                    awayTeam.Draw++;
                    awayTeam.Point += 1;
                }
                else
                if (match.AwayTeamResult == Result.Lose)
                {
                    awayTeam.Lose++;
                }

                awayTeam.Played++;
                awayTeam.Goals += match.AwayTeamGoal;

                awayTeam.GoalAgainst += match.HomeTeamGoal;
            }

            db.Entry(awayTeam).State = EntityState.Modified;
            match.TeamSeasons.Add(awayTeam);

            db.Matches.Add(match);
            db.SaveChanges();

            return(awayTeam);
        }
示例#22
0
        private TeamSeason CreateTeamSeason(Match match, string NameTeam)
        {
            TeamSeason teamSeason = new TeamSeason();
            string     nameTeam   = "";

            if (NameTeam == "home")
            {
                nameTeam = match.HomeTeam;
            }
            else
            {
                nameTeam = match.AwayTeam;
            }

            string nameSport = match.NameSport.ToString();
            string country   = match.Country;
            string name      = nameTeam;
            var    search    = from c in db.Teams
                               where
                               (c.Country == country || country == "UEFA") &&
                               c.NameSportString == nameSport &&
                               c.Name == name
                               select c;

            string season     = match.Season.ToString();
            string tournament = match.Tournament.ToString();
            var    searchFS   = from c in db.FederationSeasons
                                where
                                c.SportFederation.Country == country &&
                                c.SportFederation.NameSportString == nameSport &&
                                c.SeasonString == season &&
                                c.TournamentString == tournament
                                select c;

            if (search.Count() > 0 && searchFS.Count() > 0)
            {
                Team             team      = search.First();
                FederationSeason fedSeason = searchFS.First();

                teamSeason.NameTeam           = nameTeam;
                teamSeason.Season             = match.Season;
                teamSeason.FederationSeasonId = fedSeason.FederationSeasonId;
                teamSeason.FederationSeason   = fedSeason;
                teamSeason.Team   = team;
                teamSeason.TeamId = team.TeamId;

                db.TeamSeasons.Add(teamSeason);
                db.SaveChanges();
            }
            else
            {
                return(null);
            }

            return(teamSeason);
        }
        public void MapToTeamSeasonViewModel()
        {
            // Arrange
            var teamSeason = new TeamSeason
            {
                TeamName          = "Team",
                SeasonID          = 2017,
                LeagueName        = "League",
                ConferenceName    = "Conference",
                DivisionName      = "DivisionName",
                Games             = 3d,
                Wins              = 1d,
                Losses            = 1d,
                Ties              = 1d,
                WinningPercentage = 0.5,
                PointsFor         = 9d,
                PointsAgainst     = 9d,
                PythagoreanWins   = 1.5,
                PythagoreanLosses = 1.5,
                OffensiveAverage  = 3d,
                OffensiveFactor   = 0.5,
                OffensiveIndex    = 3d,
                DefensiveAverage  = 3d,
                DefensiveFactor   = 0.5,
                DefensiveIndex    = 3d,
                FinalPythagoreanWinningPercentage = 0.5
            };

            // Act
            var mapper = new DataMapper();
            var result = mapper.MapToTeamSeasonViewModel(teamSeason);

            // Assert
            Assert.IsInstanceOf(typeof(TeamSeasonViewModel), result);
            Assert.AreEqual(teamSeason.TeamName, result.TeamName);
            Assert.AreEqual(teamSeason.SeasonID, result.SeasonID);
            Assert.AreEqual(teamSeason.LeagueName, result.LeagueName);
            Assert.AreEqual(teamSeason.ConferenceName, result.ConferenceName);
            Assert.AreEqual(teamSeason.DivisionName, result.DivisionName);
            Assert.AreEqual(teamSeason.Games, result.Games);
            Assert.AreEqual(teamSeason.Wins, result.Wins);
            Assert.AreEqual(teamSeason.Losses, result.Losses);
            Assert.AreEqual(teamSeason.Ties, result.Ties);
            Assert.AreEqual(teamSeason.WinningPercentage, result.WinningPercentage);
            Assert.AreEqual(teamSeason.PointsFor, result.PointsFor);
            Assert.AreEqual(teamSeason.PointsAgainst, result.PointsAgainst);
            Assert.AreEqual(teamSeason.PythagoreanWins, result.PythagoreanWins);
            Assert.AreEqual(teamSeason.PythagoreanLosses, result.PythagoreanLosses);
            Assert.AreEqual(teamSeason.OffensiveAverage, result.OffensiveAverage);
            Assert.AreEqual(teamSeason.OffensiveFactor, result.OffensiveFactor);
            Assert.AreEqual(teamSeason.OffensiveIndex, result.OffensiveIndex);
            Assert.AreEqual(teamSeason.DefensiveAverage, result.DefensiveAverage);
            Assert.AreEqual(teamSeason.DefensiveFactor, result.DefensiveFactor);
            Assert.AreEqual(teamSeason.DefensiveIndex, result.DefensiveIndex);
            Assert.AreEqual(teamSeason.FinalPythagoreanWinningPercentage, result.FinalPythagoreanWinningPercentage);
        }
        public void CalculatePredictionCommand_HappyPath()
        {
            // Arrange
            // Instantiate class under test.
            var viewModel = new GamePredictorWindowViewModel(_sharedService, _windowService)
            {
                GuestName = "Guest",
                HostName  = "Host"
            };

            // Set up needed infrastructure of class under test.
            var guestSeason = new TeamSeason
            {
                TeamName         = "Guest",
                SeasonID         = 2017,
                OffensiveFactor  = 1.1,
                OffensiveAverage = 22,
                DefensiveFactor  = 0.9,
                DefensiveAverage = 20
            };

            A.CallTo(() => _sharedService.FindTeamSeason(viewModel.GuestName, viewModel.GuestSelectedSeason))
            .Returns(guestSeason);

            var hostSeason = new TeamSeason
            {
                TeamName         = "Host",
                SeasonID         = 2017,
                OffensiveFactor  = 1.2,
                OffensiveAverage = 23,
                DefensiveFactor  = 0.8,
                DefensiveAverage = 19
            };

            A.CallTo(() => _sharedService.FindTeamSeason(viewModel.HostName, viewModel.HostSelectedSeason))
            .Returns(hostSeason);

            // Act
            viewModel.CalculatePredictionCommand.Execute(null);

            // Assert
            #region ValidateDataEntry()

            A.CallTo(() => _sharedService.FindTeamSeason(viewModel.GuestName, viewModel.GuestSelectedSeason))
            .MustHaveHappenedOnceExactly();
            A.CallTo(() => _sharedService.FindTeamSeason(viewModel.HostName, viewModel.HostSelectedSeason))
            .MustHaveHappenedOnceExactly();

            #endregion ValidateDataEntry()

            Assert.AreEqual((int)((guestSeason.OffensiveFactor * hostSeason.DefensiveAverage +
                                   hostSeason.DefensiveFactor * guestSeason.OffensiveAverage) / 2d), viewModel.GuestScore);
            Assert.AreEqual((int)((hostSeason.OffensiveFactor * guestSeason.DefensiveAverage +
                                   guestSeason.DefensiveFactor * hostSeason.OffensiveAverage) / 2d), viewModel.HostScore);
        }
示例#25
0
 public int Add(TeamSeason teamSeason)
 {
     return(Add(INSERT, new Dictionary <string, object>
     {
         { "divisionId", teamSeason.DivisionId },
         { "gameDetailsId", teamSeason.GameDetailsId },
         { "seasonId", teamSeason.SeasonId },
         { "teamId", teamSeason.TeamId },
         { "position", teamSeason.Position }
     }));
 }
示例#26
0
        public async Task <IActionResult> Create([Bind("TeamName,SeasonYear,LeagueName")] TeamSeason teamSeason)
        {
            if (ModelState.IsValid)
            {
                await _teamSeasonRepository.AddAsync(teamSeason);

                await _sharedRepository.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(teamSeason));
        }
        public void EditTeamSeason_ExceptionCaught_LogsAndRethrowsException()
        {
            // Arrange
            var dbContext  = A.Fake <ProFootballEntities>();
            var repository = new TeamSeasonRepository(dbContext);

            var teamSeason = new TeamSeason();

            A.CallTo(() => dbContext.SetModified(A <TeamSeason> .Ignored)).Throws <Exception>();

            // Act & Assert
            Assert.Throws <Exception>(() => repository.EditEntity(teamSeason));
        }
示例#28
0
        /// <summary>
        /// Updates a <see cref="TeamSeason"/> entity in the data store.
        /// </summary>
        /// <param name="teamSeason">The <see cref="TeamSeason"/> to update.</param>
        /// <returns>The updated <see cref="TeamSeason"/> entity.</returns>
        public TeamSeason Update(TeamSeason teamSeason)
        {
            if (_dbContext.TeamSeasons is null)
            {
                return(teamSeason);
            }

            var entity = _dbContext.TeamSeasons.Attach(teamSeason);

            entity.State = EntityState.Modified;

            return(teamSeason);
        }
        public void EditTeamSeason_HappyPath()
        {
            // Arrange
            var dbContext  = A.Fake <ProFootballEntities>();
            var repository = new TeamSeasonRepository(dbContext);

            var teamSeason = new TeamSeason();

            // Act
            repository.EditEntity(teamSeason);

            // Assert
            A.CallTo(() => dbContext.SetModified(teamSeason)).MustHaveHappenedOnceExactly();
        }
示例#30
0
 public ActionResult DeleteConfirmed(int id)
 {
     try
     {
         TeamSeason teamSeason = db.TeamSeasons.Find(id);
         db.TeamSeasons.Remove(teamSeason);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch (Exception e)
     {
         ViewBag.Error = e.Message;
         return(View("Error"));
     }
 }
        public void AddOrEditTeam( int id, int seasonid, string name, string color, int captainid )
        {
            var team = new Team { Id = id, Name = name, Color = color, CaptainId = captainid };
            var teamSeason = new TeamSeason { SeasonId = seasonid, TeamId = team.Id };

            var returnId = 0;
            if ( team.Id <= 0 )
                returnId = int.Parse( teamRepository.Add( team ).ToString() );
            else
                returnId = teamRepository.Update( team );

            if ( returnId > 0 )
            {
                var currentTeamSeasonId = ( from ts in db.TeamSeasons
                                            where ts.SeasonId == teamSeason.SeasonId &&
                                                  ts.TeamId == team.Id
                                            select team.Id ).SingleOrDefault<int>();

                if ( currentTeamSeasonId <= 0 )
                {
                    teamSeason.TeamId = returnId;
                    teamSeasonRepository.Add( teamSeason );
                }
            }

        }