Skip to content

Commit

Permalink
add comments & correct testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanliang-ms committed Dec 9, 2024
1 parent 57c8f24 commit 5519811
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private static bool IsDurationCompliantWithDaysOfWeek(TimeSpan duration, int int
DateTime firstDayOfNextWeek = firstDayOfThisWeek.AddDays(DaysPerWeek);

DateTime firstOccurrenceInNextWeek = firstDayOfNextWeek.AddDays(
CalculateWeeklyDayOffset(sortedDaysOfWeek.First(), firstDayOfWeek));
CalculateWeeklyDayOffset(sortedDaysOfWeek.First(), firstDayOfWeek)); // firstDayOfWeek may not in the sortedDaysOfWeek

TimeSpan gap = firstOccurrenceInNextWeek - prev;

Expand Down
49 changes: 46 additions & 3 deletions tests/Tests.FeatureManagement/RecurrenceEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public void InvalidTimeWindowAcrossWeeksTest()
{
Type = RecurrencePatternType.Weekly,
Interval = 1,
FirstDayOfWeek = DayOfWeek.Sunday,
DaysOfWeek = new List<DayOfWeek>() { DayOfWeek.Tuesday, DayOfWeek.Saturday } // The time window duration should be shorter than 3 days because the gap between Saturday in the previous week and Tuesday in this week is 3 days.
},
Range = new RecurrenceRange()
Expand All @@ -299,7 +300,7 @@ public void InvalidTimeWindowAcrossWeeksTest()

//
// The settings is valid. No exception should be thrown.
RecurrenceEvaluator.IsMatch(DateTimeOffset.UtcNow, settings);
Assert.True(RecurrenceValidator.TryValidateSettings(settings, out string paramName, out string errorMessage));

settings = new TimeWindowFilterSettings()
{
Expand All @@ -320,7 +321,49 @@ public void InvalidTimeWindowAcrossWeeksTest()

//
// The settings is valid. No exception should be thrown.
RecurrenceEvaluator.IsMatch(DateTimeOffset.UtcNow, settings);
Assert.True(RecurrenceValidator.TryValidateSettings(settings, out paramName, out errorMessage));

settings = new TimeWindowFilterSettings()
{
Start = DateTimeOffset.Parse("2024-1-15T00:00:00+08:00"), // Monday
End = DateTimeOffset.Parse("2024-1-17T00:00:00+08:00"), // Time window duration is 2 days.
Recurrence = new Recurrence()
{
Pattern = new RecurrencePattern()
{
Type = RecurrencePatternType.Weekly,
Interval = 1,
FirstDayOfWeek = DayOfWeek.Sunday,
DaysOfWeek = new List<DayOfWeek>() { DayOfWeek.Monday, DayOfWeek.Saturday }
},
Range = new RecurrenceRange()
}
};

//
// The settings is valid. No exception should be thrown.
Assert.True(RecurrenceValidator.TryValidateSettings(settings, out paramName, out errorMessage));

settings = new TimeWindowFilterSettings()
{
Start = DateTimeOffset.Parse("2024-1-15T00:00:00+08:00"), // Monday
End = DateTimeOffset.Parse("2024-1-17T00:00:01+08:00"), // Time window duration is more than 2 days.
Recurrence = new Recurrence()
{
Pattern = new RecurrencePattern()
{
Type = RecurrencePatternType.Weekly,
Interval = 1,
FirstDayOfWeek = DayOfWeek.Sunday,
DaysOfWeek = new List<DayOfWeek>() { DayOfWeek.Monday, DayOfWeek.Saturday }
},
Range = new RecurrenceRange()
}
};

Assert.False(RecurrenceValidator.TryValidateSettings(settings, out paramName, out errorMessage));
Assert.Equal(ParamName.End, paramName);
Assert.Equal(ErrorMessage.TimeWindowDurationOutOfRange, errorMessage);

settings = new TimeWindowFilterSettings()
{
Expand All @@ -339,7 +382,7 @@ public void InvalidTimeWindowAcrossWeeksTest()
}
};

Assert.False(RecurrenceValidator.TryValidateSettings(settings, out string paramName, out string errorMessage));
Assert.False(RecurrenceValidator.TryValidateSettings(settings, out paramName, out errorMessage));
Assert.Equal(ParamName.End, paramName);
Assert.Equal(ErrorMessage.TimeWindowDurationOutOfRange, errorMessage);
}
Expand Down

0 comments on commit 5519811

Please sign in to comment.