Esempio n. 1
0
        public async Task Read_most_catches_pages_results()
        {
            const int pageSize   = 10;
            var       pageNumber = 1;

            var remaining = _databaseFixture.TestData.Matches
                            .SelectMany(x => x.MatchInnings)
                            .SelectMany(x => x.PlayerInnings)
                            .Where(pi => (pi.DismissalType == DismissalType.Caught && pi.DismissedBy != null) ||
                                   (pi.DismissalType == DismissalType.CaughtAndBowled && pi.Bowler != null))
                            .Select(x => x.DismissalType == DismissalType.Caught ? x.DismissedBy.Player.PlayerId : x.Bowler.Player.PlayerId)
                            .Distinct()
                            .Count();

            while (remaining > 0)
            {
                var filter = new StatisticsFilter {
                    Paging = new Paging {
                        PageNumber = pageNumber, PageSize = pageSize
                    }
                };
                var queryBuilder = new Mock <IStatisticsQueryBuilder>();
                queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((string.Empty, new Dictionary <string, object>()));
                var playerDataSource = new Mock <IPlayerDataSource>();
                playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
                var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);
                var results    = await dataSource.ReadMostCatches(filter).ConfigureAwait(false);

                var expected = pageSize > remaining ? remaining : pageSize;
                Assert.Equal(expected, results.Count());

                pageNumber++;
                remaining -= pageSize;
            }
        }
Esempio n. 2
0
        public async Task Read_most_catches_returns_results_equal_to_max_with_max_results_filter()
        {
            var filter = new StatisticsFilter {
                Paging = new Paging {
                    PageSize = int.MaxValue
                }, MaxResultsAllowingExtraResultsIfValuesAreEqual = 5
            };
            var queryBuilder = new Mock <IStatisticsQueryBuilder>();

            queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((string.Empty, new Dictionary <string, object>()));
            var playerDataSource = new Mock <IPlayerDataSource>();

            playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
            var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);

            var results = (await dataSource.ReadMostCatches(filter).ConfigureAwait(false)).ToList();

            // The test data is altered to ensure the 5th and 6th results are the same
            Assert.True(results.Count > 5);

            var fifthValue = results[4].Result.Total;

            for (var i = 4; i < results.Count; i++)
            {
                Assert.Equal(fifthValue, results[i].Result.Total);
            }
        }
Esempio n. 3
0
        public async Task Read_most_catches_sorts_by_highest_total_first()
        {
            var filter = new StatisticsFilter {
                Paging = new Paging {
                    PageSize = int.MaxValue
                }
            };
            var queryBuilder = new Mock <IStatisticsQueryBuilder>();

            queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((string.Empty, new Dictionary <string, object>()));
            var playerDataSource = new Mock <IPlayerDataSource>();

            playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
            var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);

            var results = await dataSource.ReadMostCatches(filter).ConfigureAwait(false);

            var previousTotal = int.MaxValue;

            foreach (var result in results)
            {
                Assert.True(result.Result.Total <= previousTotal);
                previousTotal = result.Result.Total.Value;
            }
        }
Esempio n. 4
0
        public async Task Read_most_catches_supports_no_filter()
        {
            var queryBuilder = new Mock <IStatisticsQueryBuilder>();

            queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((string.Empty, new Dictionary <string, object>()));
            var playerDataSource = new Mock <IPlayerDataSource>();

            playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
            var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);

            var results = await dataSource.ReadMostCatches(null).ConfigureAwait(false);

            var expected = _databaseFixture.TestData.Players.Where(x => _databaseFixture.TestData.Matches
                                                                   .SelectMany(m => m.MatchInnings)
                                                                   .SelectMany(mi => mi.PlayerInnings)
                                                                   .Where(pi => (pi.DismissalType == DismissalType.Caught && pi.DismissedBy?.Player.PlayerId == x.PlayerId) ||
                                                                          (pi.DismissalType == DismissalType.CaughtAndBowled && pi.Bowler?.Player.PlayerId == x.PlayerId))
                                                                   .Any());

            Assert.Equal(expected.Count(), results.Count());
            foreach (var player in expected)
            {
                Assert.NotNull(results.SingleOrDefault(x => x.Result.Player.PlayerId == player.PlayerId));
            }
        }
Esempio n. 5
0
        private async Task ActAndAssertStatistics(StatisticsFilter filter, SqlServerBestPlayerTotalStatisticsDataSource dataSource, Func <Stoolball.Matches.Match, bool> matchFilter, Func <MatchInnings, bool> bowlingInningsFilter, Func <MatchInnings, bool> battingInningsFilter)
        {
            var results = await dataSource.ReadMostCatches(filter).ConfigureAwait(false);

            var expected = _databaseFixture.TestData.Players.Select(p => new BestStatistic
            {
                Player       = p,
                TotalMatches = (int)_databaseFixture.TestData.Matches
                               .Where(matchFilter)
                               .Count(m => m.MatchInnings.Where(bowlingInningsFilter).Any(mi =>
                                                                                          mi.OversBowled.Any(o => o.Bowler.Player.PlayerId == p.PlayerId) ||
                                                                                          mi.BowlingFigures.Any(bf => bf.Bowler.Player.PlayerId == p.PlayerId)
                                                                                          ) ||
                                      m.MatchInnings.Where(battingInningsFilter).Any(mi => mi.PlayerInnings.Any(pi => pi.Batter.Player.PlayerId == p.PlayerId || pi.DismissedBy?.Player.PlayerId == p.PlayerId || pi.Bowler?.Player.PlayerId == p.PlayerId)) ||
                                      m.Awards.Any(aw => aw.PlayerIdentity.Player.PlayerId == p.PlayerId)),
                Total = (int)_databaseFixture.TestData.Matches
                        .Where(matchFilter)
                        .SelectMany(m => m.MatchInnings)
                        .Where(bowlingInningsFilter)
                        .SelectMany(mi => mi.PlayerInnings)
                        .Where(pi => (pi.DismissalType == DismissalType.Caught && pi.DismissedBy?.Player.PlayerId == p.PlayerId) ||
                               (pi.DismissalType == DismissalType.CaughtAndBowled && pi.Bowler?.Player.PlayerId == p.PlayerId))
                        .Count(),
            }).Where(x => x.Total > 0);

            foreach (var player in expected)
            {
                var result = results.SingleOrDefault(x => x.Result.Player.PlayerId == player.Player.PlayerId);
                Assert.NotNull(result);

                Assert.Equal(player.TotalMatches, result.Result.TotalMatches);
                Assert.Null(result.Result.TotalInnings);
                Assert.Equal(player.Total, result.Result.Total);
            }
        }
Esempio n. 6
0
        public async Task Read_most_catches_supports_filter_by_team_id()
        {
            var filter = new StatisticsFilter
            {
                Paging = new Paging
                {
                    PageSize = int.MaxValue
                },
                Team = _databaseFixture.TestData.TeamWithFullDetails
            };
            var queryBuilder = new Mock <IStatisticsQueryBuilder>();

            queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((" AND TeamId = @TeamId", new Dictionary <string, object> {
                { "TeamId", _databaseFixture.TestData.TeamWithFullDetails.TeamId }
            }));
            var playerDataSource = new Mock <IPlayerDataSource>();

            playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
            var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);

            var results = await dataSource.ReadMostCatches(filter).ConfigureAwait(false);

            var expected = _databaseFixture.TestData.Players.Where(x => _databaseFixture.TestData.Matches
                                                                   .Where(x => x.Teams.Select(t => t.Team.TeamId).Contains(_databaseFixture.TestData.TeamWithFullDetails.TeamId.Value))
                                                                   .SelectMany(m => m.MatchInnings)
                                                                   .Where(i => i.BowlingTeam.Team.TeamId == _databaseFixture.TestData.TeamWithFullDetails.TeamId.Value)
                                                                   .SelectMany(mi => mi.PlayerInnings)
                                                                   .Where(pi => (pi.DismissalType == DismissalType.Caught && pi.DismissedBy?.Player.PlayerId == x.PlayerId) ||
                                                                          (pi.DismissalType == DismissalType.CaughtAndBowled && pi.Bowler?.Player.PlayerId == x.PlayerId))
                                                                   .Any());

            Assert.Equal(expected.Count(), results.Count());
            foreach (var player in expected)
            {
                Assert.NotNull(results.SingleOrDefault(x => x.Result.Player.PlayerId == player.PlayerId));
            }
        }
Esempio n. 7
0
        public async Task Read_most_catches_returns_teams()
        {
            var filter = new StatisticsFilter {
                Paging = new Paging {
                    PageSize = int.MaxValue
                }
            };
            var queryBuilder = new Mock <IStatisticsQueryBuilder>();

            queryBuilder.Setup(x => x.BuildWhereClause(It.IsAny <StatisticsFilter>())).Returns((string.Empty, new Dictionary <string, object>()));
            var playerDataSource = new Mock <IPlayerDataSource>();

            playerDataSource.Setup(x => x.ReadPlayers(It.IsAny <PlayerFilter>())).Returns(Task.FromResult(_databaseFixture.TestData.Players));
            var dataSource = new SqlServerBestPlayerTotalStatisticsDataSource(_databaseFixture.ConnectionFactory, queryBuilder.Object, playerDataSource.Object);

            var results = await dataSource.ReadMostCatches(filter).ConfigureAwait(false);

            var expected = _databaseFixture.TestData.Players.Where(x => _databaseFixture.TestData.Matches
                                                                   .SelectMany(m => m.MatchInnings)
                                                                   .SelectMany(x => x.PlayerInnings)
                                                                   .Where(pi => (pi.DismissalType == DismissalType.Caught && pi.DismissedBy?.Player.PlayerId == x.PlayerId) ||
                                                                          (pi.DismissalType == DismissalType.CaughtAndBowled && pi.Bowler?.Player.PlayerId == x.PlayerId))
                                                                   .Any());

            foreach (var player in expected)
            {
                var result = results.SingleOrDefault(x => x.Result.Player.PlayerId == player.PlayerId);
                Assert.NotNull(result);

                foreach (var identity in player.PlayerIdentities)
                {
                    var resultIdentity = result.Result.Player.PlayerIdentities.SingleOrDefault(x => x.PlayerIdentityId == identity.PlayerIdentityId);
                    Assert.NotNull(resultIdentity);
                    Assert.Equal(identity.Team.TeamName, resultIdentity.Team.TeamName);
                }
            }
        }