Skip to content

Commit

Permalink
Merge pull request #22 from ReinoutWW/RW-CourseCompletenessValdiation…
Browse files Browse the repository at this point in the history
…-ExtraComponents
  • Loading branch information
ReinoutWW authored Jan 25, 2025
2 parents f746b17 + d048ca8 commit 43bc399
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
31 changes: 24 additions & 7 deletions HAN.Services/Validation/CourseCompletenessValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace HAN.Services.Validation;

public class CourseCompletenessValidator(CourseComponentService courseComponentService) : ICourseCompletenessValidator
{
private readonly CourseComponentService _courseComponentService = courseComponentService;

public CourseValidationResult Validate(CourseDto courseDto)
{
var result = new CourseValidationResult
Expand All @@ -14,14 +16,20 @@ public CourseValidationResult Validate(CourseDto courseDto)
};

var evlIds = courseDto.Evls.Select(evl => evl.Id).ToList();
var allCourseComponents = courseComponentService.GetAllCourseComponentByEvlIds(evlIds);
var validComponents = _courseComponentService
.GetAllCourseComponentByEvlIds(evlIds);

var scheduledCourseComponentIds = courseDto.Schedule.ScheduleLines
.Select(sl => sl.CourseComponentId)
.ToHashSet();

foreach (var component in allCourseComponents)
foreach (var component in validComponents)
{
if (!courseDto.Schedule.ScheduleLines.Any(sl => sl.CourseComponentId == component.Id))
var hasRemovedComponentFromSchedule = scheduledCourseComponentIds.Remove(component.Id);

if (!hasRemovedComponentFromSchedule)
{
result.IsValid = false;
var evlNames = string.Join(", ", component.Evls.Select(evl => evl.Name));
var evlNames = string.Join(", ", component.Evls.Select(e => e.Name));
result.Errors.Add(new CourseValidationError
{
ErrorCategory = ErrorCategory.Missing,
Expand All @@ -31,9 +39,18 @@ public CourseValidationResult Validate(CourseDto courseDto)
}
}

if (!result.IsValid)
result.Errors.AddRange(from extraId in scheduledCourseComponentIds
select new CourseValidationError
{
ErrorCategory = ErrorCategory.ExtraComponent,
Message = $"Component ID {extraId} is not part of any EVL.",
CourseComponentId = extraId
});

if (result.Errors.Any())
{
result.Message = "Course is incomplete. Missing components.";
result.IsValid = false;
result.Message = "Course completeness validation failed: missing or extra components.";
}

return result;
Expand Down
4 changes: 3 additions & 1 deletion HAN.Services/Validation/CourseValidationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public enum ErrorCategory
[Description("Invalid")]
Invalid,
[Description("Out of order")]
OutOfOrder
OutOfOrder,
[Description("Extra component")]
ExtraComponent
}
37 changes: 36 additions & 1 deletion HAN.Tests/Services/CourseValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,42 @@ public void ValidateSchedule_ShouldBeIncomplete()

Assert.False(complete);
}


[Fact]
public void ValidateSchedule_AddExtraComponent_ShouldBeInvalid()
{
var evls = _persistHelper.SeedEvls();
var course = new CourseDtoBuilder()
.WithName("Course name")
.WithEvls(evls)
.Build();

course = _courseService.CreateCourse(course);

_persistHelper.SeedLessonToEvls(course.Evls);
_persistHelper.SeedExamToEvls(course.Evls);

var evlIds = course.Evls.Select(x => x.Id).ToList();
var courseComponents = _courseComponentService.GetAllCourseComponentByEvlIds(evlIds);

var extraCourseComponent = new CourseComponentDto()
{
Name = "Extra component",
Description = "Extra component description",
};

courseComponents.Add(extraCourseComponent);

course = new CourseDtoBuilder(course)
.WithValidSchedule(courseComponents)
.Build();

var result = _courseValidationService.IsCourseComplete(course);

Assert.Contains(result.Errors, err => err.ErrorCategory == HAN.Services.Validation.ErrorCategory.ExtraComponent);
Assert.False(result.IsValid);
}

private static void ScheduleShouldContainAllCourseComponents(ScheduleDto schedule, List<CourseComponentDto> courseComponents)
{
foreach (var courseComponent in courseComponents)
Expand Down

0 comments on commit 43bc399

Please sign in to comment.