public IList<GameOptionPick> GetGameOptionPicksForSeason(int season)
        {
            var gameOptionPicks = new List<GameOptionPick>();
            var games = _gameRepository.GetForSeason(season, null);
            var options = _optionRepository.GetAllForSeason(season);
            var picks = _pickRepository.GetForSeasonAllUsers(season);

            foreach (var game in games)
            {
                var gameOptionPick = new GameOptionPick
                    {
                        GameId = game.Id,
                        Season = game.Season,
                        SeasonType = game.SeasonType,
                        Week = game.Week
                    };
                var g = game;

                foreach (var option in options.Where(option => option.GameId == g.Id && option.Outcome.HasValue))
                {
                    var userPickOption = new UserPickOption
                        {
                            OptionId = option.Id,
                            Outcome = option.Outcome.Value,
                            UserPicks = picks.Where(pick => pick.OptionId == option.Id).ToList()
                        };

                    gameOptionPick.Options.Add(userPickOption);
                }

                gameOptionPicks.Add(gameOptionPick);
            }

            return gameOptionPicks;
        }
        public void GetRecordsToUpdate_CalculatesWeeklyRecordsCorrectly()
        {
            var preSeasonWeeks = 4;
            var regularSeasonWeeks = 17;
            var postSeasonWeeks = 5;
            var season = 2013;
            var userId = 1;

            var games = CreateGameOptionPicks(season, preSeasonWeeks, regularSeasonWeeks, postSeasonWeeks);

            foreach (var game in games)
            {
                var option = new UserPickOption
                    {
                        Outcome = OptionOutcome.Won
                    };

                if (game.Season == season && game.SeasonType == SeasonType.PRE)
                {
                    if (game.Week == 1)
                    {
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 4)
                    {
                        option.Outcome = OptionOutcome.Lost;
                        AddUserPick(option, userId);
                    }
                }

                if (game.Season == season && game.SeasonType == SeasonType.REG)
                {
                    if (game.Week == 3)
                    {
                        option.Outcome = OptionOutcome.Tie;
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 5)
                    {
                        var anotherOption = new UserPickOption
                            {
                                Outcome = OptionOutcome.Won
                            };

                        game.Options.Add(anotherOption);
                        AddUserPick(option, userId);
                        AddUserPick(anotherOption, userId);
                    }
                    else if (game.Week == 10)
                    {
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 13)
                    {
                        option.Outcome = OptionOutcome.Lost;
                        AddUserPick(option, userId);
                    }
                }

                if (game.Season == season && game.SeasonType == SeasonType.POST)
                {
                    if (game.Week == 4)
                    {
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 1)
                    {
                        option.Outcome = OptionOutcome.Lost;
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 2)
                    {
                        option.Outcome = OptionOutcome.Lost;
                        AddUserPick(option, userId);
                    }
                    else if (game.Week == 3)
                    {
                        option.Outcome = OptionOutcome.Lost;
                        AddUserPick(option, userId);
                    }
                }

                game.Options.Add(option);
            }

            _recordsRepository
                .Setup(repository => repository.GetGameOptionPicksForSeason(season))
                .Returns(games);

            var records = _recordsManager.CalculateRecordsForSeason(season);

            // Weekly asserts
            AssertWeeklyRecords(season, SeasonType.PRE, 1, 1, 0, 0, records);
            AssertWeeklyRecords(season, SeasonType.PRE, 4, 0, 1, 0, records);

            AssertWeeklyRecords(season, SeasonType.REG, 3, 0, 0, 1, records);
            AssertWeeklyRecords(season, SeasonType.REG, 5, 2, 0, 0, records);
            AssertWeeklyRecords(season, SeasonType.REG, 10, 1, 0, 0, records);
            AssertWeeklyRecords(season, SeasonType.REG, 13, 0, 1, 0, records);

            AssertWeeklyRecords(season, SeasonType.POST, 2, 0, 1, 0, records);
            AssertWeeklyRecords(season, SeasonType.POST, 3, 0, 1, 0, records);
            AssertWeeklyRecords(season, SeasonType.POST, 4, 1, 0, 0, records);

            // Season type asserts
            AssertSeasonTypeRecords(season, SeasonType.PRE, 1, 1, 0, records);
            AssertSeasonTypeRecords(season, SeasonType.REG, 3, 1, 1, records);
            AssertSeasonTypeRecords(season, SeasonType.POST, 1, 3, 0, records);

            // Season asserts
            AssertSeasonRecords(season, 5, 5, 1, records);
        }
 private static void AddUserPick(UserPickOption option, int userId)
 {
     option.UserPicks.Add(new UserPick
         {
             UserId = userId
         });
 }
        public void GetRecordsToUpdate_CreatesCorrectUserRecords()
        {
            var user1 = 1;
            var user2 = 2;
            var season = 2013;
            var preSeasonWeeks = 4;
            var regularSeasonWeeks = 17;
            var postSeasonWeeks = 5;
            var gameOptionPicks = CreateGameOptionPicks(season, preSeasonWeeks, regularSeasonWeeks, postSeasonWeeks);

            foreach (var game in gameOptionPicks)
            {
                var option = new UserPickOption
                    {
                        Outcome = OptionOutcome.Won
                    };

                game.Options.Add(option);

                foreach (var o in game.Options)
                {
                    o.UserPicks.Add(new UserPick
                        {
                            UserId = user1
                        });

                    // Don't add any preseason picks for user 2
                    if (game.SeasonType == SeasonType.PRE)
                    {
                        continue;
                    }

                    // Don't add regular season week 10 pick for user 2
                    if (game.SeasonType == SeasonType.REG && game.Week == 10)
                    {
                        continue;
                    }

                    // Don't add post season week 1 pick for user 2
                    if (game.SeasonType == SeasonType.POST && game.Week == 1)
                    {
                        continue;
                    }

                    // Don't add post season week 1 pick for user 3
                    if (game.SeasonType == SeasonType.POST && game.Week == 3)
                    {
                        continue;
                    }

                    o.UserPicks.Add(new UserPick
                        {
                            UserId = user2
                        });
                }
            }

            _recordsRepository
                .Setup(repository => repository.GetGameOptionPicksForSeason(season))
                .Returns(gameOptionPicks);

            var records = _recordsManager.CalculateRecordsForSeason(season);

            var user1Records = records.Where(record => record.UserId == user1);
            var user2Records = records.Where(record => record.UserId == user2);

            // Assert that records are created for both users
            Assert.True(user1Records.Any());
            Assert.True(user2Records.Any());

            // Assert that user 1 has record for lifetime
            Assert.Equal(1, user1Records.Count(record => record.RecordType == RecordType.Lifetime));
            // Assert that user 1 has record for season
            Assert.Equal(1, user1Records.Count(record => record.RecordType == RecordType.Season));
            // Assert that user 1 has 3 records for each season type
            Assert.Equal(3, user1Records.Count(record => record.RecordType == RecordType.SeasonType));
            // Assert that user 1 has 4 records for each week of the preseason
            Assert.Equal(preSeasonWeeks, user1Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.PRE));
            // Assert that user 1 has 17 records for each week of the regular season
            Assert.Equal(regularSeasonWeeks, user1Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.REG));
            // Assert that user 1 has 5 records for each week of the post season
            Assert.Equal(postSeasonWeeks, user1Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.POST));

            // Assert that user 2 has record for lifetime
            Assert.Equal(1, user2Records.Count(record => record.RecordType == RecordType.Lifetime));
            // Assert that user 2 has record for season
            Assert.Equal(1, user2Records.Count(record => record.RecordType == RecordType.Season));
            // Assert that user 2 has 3 records for each season type
            Assert.Equal(2, user2Records.Count(record => record.RecordType == RecordType.SeasonType));
            // Assert that user 2 does not have any preseason records
            Assert.Equal(0, user2Records.Count(record => record.RecordType == RecordType.SeasonType && record.SeasonType == SeasonType.PRE));
            // Assert that user 2 has 0 records for each week of the preseason
            Assert.Equal(0, user2Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.PRE));
            // Assert that user 2 has 16 records for each week of the regular season
            Assert.Equal(regularSeasonWeeks - 1, user2Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.REG));
            // Assert that user 2 does not have a regular season week 10 record
            Assert.Null(user2Records.FirstOrDefault(record => record.SeasonType == SeasonType.REG && record.Week == 10));
            // Assert that user 2 has 4 records for each week of the post season
            Assert.Equal(postSeasonWeeks - 2, user2Records.Count(record => record.RecordType == RecordType.Week && record.SeasonType == SeasonType.POST));
            // Assert that user 2 does not have a post season week 1 record
            Assert.Null(user2Records.FirstOrDefault(record => record.SeasonType == SeasonType.POST && record.Week == 1));
            // Assert that user 2 does not have a post season week 3 record
            Assert.Null(user2Records.FirstOrDefault(record => record.SeasonType == SeasonType.POST && record.Week == 3));
        }