Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: MatchScheduler does not generate matches with teams or venues having overlapping match dates #134

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,57 @@ public void Generate_Schedule_Should_Succeed()
Assert.That(matches.All(m => m.LegSequenceNo == 1), Is.True);
Assert.That(matches.All(m => m.RoundId == 1), Is.True);
Assert.That(matches.All(m => m.VenueId == participants.First(p => p.Id == m.HomeTeamId).VenueId), Is.True);

// Check that venue is not double booked
Assert.That(matches.DistinctBy(m => m.VenueId).Select(m => m.VenueId!)
.All(venueId => !VenueHasOverlappingMatches(venueId, matches)), Is.True);

// Check that teams are not double booked
Assert.That(matches.Select(m => m.HomeTeamId)
.Union(matches.Select(m => m.GuestTeamId))
.Distinct()
.All(teamId => !TeamHasOverlappingMatches(teamId, matches)), Is.True);
});
}

private static bool TeamHasOverlappingMatches(long teamId, EntityCollection<MatchEntity> matches)
{
var matchesOfTeam = matches.Where(m => m.HomeTeamId == teamId || m.GuestTeamId == teamId);
var dateTimePeriodsForMatchesOfTeam = matchesOfTeam.Select(m => new DateTimePeriod(m.PlannedStart!.Value, m.PlannedEnd!.Value)).ToList();
for (var i = 0; i < dateTimePeriodsForMatchesOfTeam.Count; i++)
{
for (var j = i + 1; j < dateTimePeriodsForMatchesOfTeam.Count; j++)
{
if (dateTimePeriodsForMatchesOfTeam[i].Overlaps(dateTimePeriodsForMatchesOfTeam[j]))
{
return true;
}
}
}

return false;
}

private static bool VenueHasOverlappingMatches(long? venueId, EntityCollection<MatchEntity> matches)
{
if (venueId == null) return false;

var matchesAtVenue = matches.Where(m => m.VenueId == venueId);
var dateTimePeriodsForMatchesAtVenue = matchesAtVenue.Select(m => new DateTimePeriod(m.PlannedStart!.Value, m.PlannedEnd!.Value)).ToList();
for (var i = 0; i < dateTimePeriodsForMatchesAtVenue.Count; i++)
{
for (var j = i + 1; j < dateTimePeriodsForMatchesAtVenue.Count; j++)
{
if (dateTimePeriodsForMatchesAtVenue[i].Overlaps(dateTimePeriodsForMatchesAtVenue[j]))
{
return true;
}
}
}

return false;
}

private MatchScheduler GetMatchSchedulerInstance()
{
var tournamentMatches = new EntityCollection<MatchEntity>();
Expand Down
Loading