Skip to content

Commit

Permalink
Unit tests: MatchScheduler does not generate matches with teams or ve…
Browse files Browse the repository at this point in the history
…nues having overlapping match dates (#134)

MatchScheduler does not generate matches with teams or venues having overlapping match dates
  • Loading branch information
axunonb authored Jan 27, 2024
1 parent 8e67a58 commit 23c33e6
Showing 1 changed file with 48 additions and 0 deletions.
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

0 comments on commit 23c33e6

Please sign in to comment.