fix(documentation): Add test documentation to GradeCalculator and FinalGradeDeterminator

This commit is contained in:
2025-10-07 14:42:44 +02:00
parent e955280865
commit fd07992eee
2 changed files with 126 additions and 1 deletions

View File

@@ -11,7 +11,7 @@ FinalGradeDeterminator()
```
- Initializes ROS2 node with name `final_grade_determinator`
- Declares and retrieves `grade_collection_amount` parameter
- Sets up `DatabaseManager` <!--(uses provided or creates new instance) std::unique_ptr<DatabaseManager> db_manager -->
- Sets up `DatabaseManager`
- Creates publisher for student course management
- Subscribes to exam results topic
- Initializes service client for grade calculation
@@ -28,8 +28,74 @@ FinalGradeDeterminator()
- Sends async request with collected exam grades for the student-course combination
- Uses callback to handle service response
**`void grade_calculator_response(rclcpp::Client<g2_2025_interfaces::srv::Exams>::SharedFuture future, StudentCourse studentCourseCombo)`**
- Verifies database connection
- Publishes final student message to ROS2 topic
- Logs final grade information
- Stores final course result in database
---
## Unit Tests
Unit tests for `FinalGradeDeterminator` are implemented in `src/g2_2025_grade_calculator_pkg/test/FinalGradeDeterminator.test.cpp` using Google Test and ROS2 test utilities. The tests use a mock database manager and a mock grade calculator service to simulate the node's interactions.
### Test Cases
#### 1. ConstructorTest
**Description:** Verifies that the node can be constructed without errors.
- **Input:** Construct a `FinalGradeDeterminator` node.
- **Expected Output:** No exceptions are thrown. The node is created successfully.
#### 2. ExamCollectionTest
**Description:** Sends the required number of exam results and checks that a grade calculation request is triggered, a student message is published, and the results are stored.
- **Input:**
- Student: `"Test Student"`
- Course: `"Test Course"`
- Grades published: `[80, 85, 90, 75, 95]` (5 exam results, which is the default required amount)
- **Expected Output:**
- A grade calculation service request is made with the correct student, course, and grades.
- A student message is published with the correct student and course.
- The final grade is calculated as the average: (80 + 85 + 90 + 75 + 95) / 5 = 85.
#### 3. PartialExamCollectionTest
**Description:** Sends fewer than the required number of exam results and checks that no grade calculation or student message occurs.
- **Input:**
- Student: `"Test Student"`
- Course: `"Test Course"`
- Grades published: `[80, 85, 90]` (only 3 exam results, less than required)
- **Expected Output:**
- No grade calculation service request is made.
- No student message is published.
#### 4. MultipleStudentsTest
**Description:** Simulates multiple students submitting exam results and verifies that each student triggers independent grade calculation and messaging.
- **Input:**
- Students: `"Alice"`, `"Bob"`
- Course: `"Test Course"`
- Each student receives grades: `[80, 85, 90, 75, 95]` (5 exam results per student)
- **Expected Output:**
- Two grade calculation service requests are made, one for each student, with the correct grades.
- Two student messages are published, one for each student.
- The final grade for each student is calculated as the average: (80 + 85 + 90 + 75 + 95) / 5 = 85.
These tests ensure that the node correctly collects exam results, triggers grade calculation at the right time, publishes the appropriate messages, and interacts with the database as expected.

View File

@@ -23,3 +23,62 @@ GradeCalculator()
- Adds a bonus of 10 points if the student name is "wessel"
- Ensures the final grade is clamped between 10 and 100
- Sends the calculated grade back through the service response
---
## Unit Tests
Unit tests for `GradeCalculator` are implemented in `src/g2_2025_grade_calculator_pkg/test/GradeCalculator.test.cpp` using Google Test and ROS2 test utilities. The tests use a client to call the grade calculation service and verify the results.
### Test Cases
#### 1. NormalAverage
**Description:** Verifies that the service returns the average of the provided grades for a normal student name.
- **Input:**
- Student: `"Alice"`
- Grades: `[80, 90, 70]`
- **Expected Output:**
- Result: `80` (average of 80, 90, 70)
#### 2. WesselBonus
**Description:** Checks that the student name "Wessel" (case-insensitive) receives a 10-point bonus, and the result is clamped to 100 if necessary.
- **Input:**
- Student: `"Wessel"`
- Grades: `[80, 90, 70]`
- **Expected Output:**
- Result: `90` (average 80 + 10 bonus)
#### 3. wesselBonus
**Description:** Checks that the bonus logic is case-insensitive for the student name "wessel".
- **Input:**
- Student: `"wessel"`
- Grades: `[80, 90, 70]`
- **Expected Output:**
- Result: `90` (average 80 + 10 bonus)
#### 4. GradeTooHigh
**Description:** Ensures that the grade is clamped to a maximum of 100, even after applying the bonus.
- **Input:**
- Student: `"Wessel"`
- Grades: `[100, 100, 100]`
- **Expected Output:**
- Result: `100` (average 100 + 10 bonus, clamped to 100)
#### 5. GradeTooLow
**Description:** Ensures that the grade is clamped to a minimum of 10.
- **Input:**
- Student: `"Alice"`
- Grades: `[0, 0, 0]`
- **Expected Output:**
- Result: `10` (average 0, clamped to 10)