Conversation
Updated exception types in `AdminStudentService.cs` to use `KeyNotFoundException` for missing remarks, improving semantic accuracy. Refined absence date handling in `TeacherService.cs` to preserve full `DateTime` values, ensuring proper handling of time components. Removed redundant `GradeType` assignment and switched to `AllAsReadOnly` for fetching absences to improve performance and immutability. Enhanced logging for better debugging and maintainability.
Standardized exception handling by replacing `ArgumentException` and `ArgumentNullException` with `KeyNotFoundException` across test cases. Updated test method names to reflect the new exception type. Enhanced mock setups to ensure compatibility with asynchronous LINQ operations. Removed redundant tests and consolidated similar cases to reduce duplication. Added logger dependencies (`ILogger<T>`) to services and their test setups. Improved test assertions to validate exception messages for better accuracy. Standardized mock data for entities like `Student`, `Course`, `Grade`, and `Remark`. Refactored test cases for improved readability and maintainability. Updated return types in tests where appropriate. Added new test cases to handle missing entity scenarios. Performed general code cleanup, including removing unused imports and ensuring consistent formatting.
Switched the deployment branch for the `student-management-system` service from `master` to `render-deployment
There was a problem hiding this comment.
Pull request overview
This pull request standardizes error handling across the codebase by replacing ArgumentException with KeyNotFoundException for entity not-found scenarios. Additionally, it improves test reliability by ensuring repository mocks properly support EF Core async extensions, updates date handling to preserve full DateTime values, and includes minor code cleanup. A deployment configuration change is also present but not documented in the description.
Key changes:
- Standardized exception types: Replaced
ArgumentExceptionwithKeyNotFoundExceptionacross service methods and updated ~30+ unit tests to expect the correct exception type - Improved test infrastructure: Added
BuildMock()calls to repository mock setups to support EF Core async query extensions - Date handling improvements: Removed unnecessary
.Dateproperty access when specifyingDateTimeKind.Utcto preserve full date-time values
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| render.yaml | Changes deployment branch from master to render-deployment (undocumented change) |
| StudentServiceTests/TeacherServiceTests.cs | Updates 12 test methods to expect KeyNotFoundException instead of ArgumentException/ArgumentNullException; fixes test assertion patterns |
| StudentServiceTests/StudentServiceTests.cs | Updates GetStudentIdAsync test to expect KeyNotFoundException and adds student existence validation |
| StudentServiceTests/Admin/AdminUserManageControllerTests.cs | Adds logger mock and fixes DeleteRole test to expect NotFoundResult instead of BadRequestResult |
| StudentServiceTests/Admin/AdminTeacherServiceTests.cs | Adds logger mock and updates test to expect KeyNotFoundException |
| StudentServiceTests/Admin/AdminStudentServiceTests.cs | Updates 15+ tests to expect KeyNotFoundException; removes obsolete tests; improves test setup with student existence checks |
| StudentServiceTests/Admin/AdminScheduleServiceTests.cs | Adds BuildMock() for async support; removes obsolete deletion test; updates exception expectations |
| StudentServiceTests/Admin/AdminCourseServiceTests.cs | Updates 8 tests to expect KeyNotFoundException; removes redundant test for already-deleted courses; adds BuildMock() support |
| StudentServiceTests/Admin/AdminClassServiceTests.cs | Adds BuildMock() setup for AllAsReadOnly to support async queries |
| StudentManagementSystem.Core/Services/TeacherService.cs | Changes repository method from All to AllAsReadOnly for absence retrieval; removes unnecessary .Date property access |
| StudentManagementSystem.Core/Services/Admin/AdminStudentService.cs | Replaces ArgumentException with KeyNotFoundException for remark not found scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| using MockQueryable; | ||
| using MockQueryable.Moq; | ||
| using Moq; | ||
| using NuGet.Protocol.Core.Types; |
There was a problem hiding this comment.
Unnecessary import added. NuGet.Protocol.Core.Types is imported but not used anywhere in this test file. This should be removed to keep the imports clean.
| using NuGet.Protocol.Core.Types; |
| Assert.That(result, Is.False); | ||
| // Assert & Act | ||
| var ex = Assert.ThrowsAsync<KeyNotFoundException>(() => adminStudentService.ExistAsync(studentId)); | ||
| Assert.That(ex.Message, Is.EqualTo("Student with ID: 1 not found")); |
There was a problem hiding this comment.
Missing '.' at the end of the exception message. For consistency with other error messages in the codebase (e.g., line 186, 314, 361, etc.), this message should end with a period.
| Assert.That(ex.Message, Is.EqualTo("Student with ID: 1 not found")); | |
| Assert.That(ex.Message, Is.EqualTo("Student with ID: 1 not found.")); |
| public async Task<AbsenceFormViewModel> GetAbsenceByIdAsync(int absenceId) | ||
| { | ||
| var absence =await repository.All<Absence>() | ||
| var absence =await repository.AllAsReadOnly<Absence>() |
There was a problem hiding this comment.
Missing space after the equals sign. The code has var absence =await repository.AllAsReadOnly<Absence>() but should be var absence = await repository.AllAsReadOnly<Absence>() for proper spacing.
| var absence =await repository.AllAsReadOnly<Absence>() | |
| var absence = await repository.AllAsReadOnly<Absence>() |
| using MockQueryable; | ||
| using MockQueryable.Moq; | ||
| using Moq; | ||
| using NUnit.Framework; |
There was a problem hiding this comment.
Unused import added. The NUnit.Framework namespace is added but is already available through other test infrastructure in this file. This explicit import is redundant and should be removed unless it's specifically needed for a new feature not visible in this diff.
| using NUnit.Framework; |
| plan: free | ||
| region: frankfurt | ||
| branch: master | ||
| branch: render-deployment |
There was a problem hiding this comment.
This deployment configuration change from master to render-deployment is not mentioned in the PR description. The PR description focuses on error handling standardization, but this change modifies which branch gets deployed. If this change is intentional, it should be documented in the PR description. If this is an accidental inclusion, it should be removed from this PR and handled separately.
| branch: render-deployment | |
| branch: master |
| using Microsoft.Extensions.Logging; | ||
| using MockQueryable; | ||
| using Moq; | ||
| using StudentManagementSystem.Areas.Admin.Controllers; |
There was a problem hiding this comment.
Unused import added. The StudentManagementSystem.Areas.Admin.Controllers import is added but doesn't appear to be used in the visible portions of this test file. This should be removed to keep imports clean.
| using StudentManagementSystem.Areas.Admin.Controllers; |
| using Moq; | ||
| using Microsoft.Extensions.Logging; | ||
| using StudentManagementSystem.Core.Models.Admin.Course; | ||
| using StudentManagementSystem.Core.Services; |
There was a problem hiding this comment.
Unused import added. The StudentManagementSystem.Core.Services import is added but doesn't appear to be used in this test file based on the visible changes. This should be removed unless it's needed for functionality not shown in this diff.
| using StudentManagementSystem.Core.Services; |
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024,12,1), IsDeleted = false }, | ||
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024,12,2), IsDeleted = false } |
There was a problem hiding this comment.
Inconsistent date formatting. This line uses new DateTime(2024,12,1) without spaces after commas, while line 645 uses new DateTime(2024,12,2). For consistency and readability, use spaces after commas: new DateTime(2024, 12, 1).
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024,12,1), IsDeleted = false }, | |
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024,12,2), IsDeleted = false } | |
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024, 12, 1), IsDeleted = false }, | |
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024, 12, 2), IsDeleted = false } |
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024,12,1), IsDeleted = false }, | ||
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024,12,2), IsDeleted = false } |
There was a problem hiding this comment.
Inconsistent date formatting. This line uses new DateTime(2024,12,2) without spaces after commas. For consistency with the rest of the codebase, use spaces after commas: new DateTime(2024, 12, 2).
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024,12,1), IsDeleted = false }, | |
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024,12,2), IsDeleted = false } | |
| new Absence { Id = 1, StudentId = studentId, Course = new Course { Name = "Math" }, Date = new DateTime(2024, 12, 1), IsDeleted = false }, | |
| new Absence { Id = 2, StudentId = studentId, Course = new Course { Name = "Science" }, Date = new DateTime(2024, 12, 2), IsDeleted = false } |
| var absence =await repository.All<Absence>() | ||
| var absence =await repository.AllAsReadOnly<Absence>() | ||
| .Where(a => a.Id == absenceId && !a.IsDeleted) | ||
| .Select(a=> new AbsenceFormViewModel |
There was a problem hiding this comment.
Inconsistent lambda syntax in Select projection. The lambda uses a=> without spaces, while the codebase typically uses spaces around operators. For consistency, change to a => new AbsenceFormViewModel.
| .Select(a=> new AbsenceFormViewModel | |
| .Select(a => new AbsenceFormViewModel |
This pull request focuses on improving error handling consistency across the codebase by standardizing the use of
KeyNotFoundExceptionfor not-found scenarios, updating related unit tests, and making minor improvements to date handling and repository usage. The changes increase clarity, reliability, and maintainability of both the application code and the test suite.Error handling standardization:
Replaced
ArgumentExceptionwithKeyNotFoundExceptionfor entities not found in service methods such as editing or retrieving remarks, courses, students, and course schedules (AdminStudentService.cs,AdminCourseServiceTests.cs,AdminScheduleServiceTests.cs,AdminStudentServiceTests.cs).Updated unit tests to expect
KeyNotFoundExceptioninstead ofArgumentExceptionor generic exceptions, and adjusted test names and assertions accordingly throughout the test files.Repository and data handling improvements:
Ensured that repository mocks for
AllAsReadOnlyreturn correctly configured async queryables in unit tests, improving test reliability for EF Core async extensions (AdminClassServiceTests.cs,AdminCourseServiceTests.cs,AdminScheduleServiceTests.cs).Changed repository usage in absence retrieval to use
AllAsReadOnlyfor consistency and potentially improved performance (TeacherService.cs).Date handling consistency:
Removed unnecessary
.Dateproperty access when specifyingDateTimeKind.Utcfor absence dates, ensuring the full date and time are preserved (TeacherService.cs).Test suite cleanup:
Removed redundant or obsolete test cases, such as those checking for no-ops on already deleted entities or not found scenarios now covered by exception handling (
AdminCourseServiceTests.cs,AdminScheduleServiceTests.cs,AdminStudentServiceTests.cs).Miscellaneous:
Minor code cleanup and organization in test files, including import adjustments and whitespace fixes (
AdminCourseServiceTests.cs,AdminStudentServiceTests.cs).