docs: Add unit tests

- DatabaseManager: Added unit test documentation
- ConfigManager: Added unit test documentation
- ExamResultGenerator: Added unit test documentation
- FinalGradeDeterminator: Fix spacing
This commit is contained in:
2025-10-07 19:49:31 +02:00
parent 62995c13c2
commit 130b495030
4 changed files with 213 additions and 10 deletions

View File

@@ -130,5 +130,86 @@ ssl = false
[database.pool]
min_connections = 1
max_connections = 10
```
---
## Unit Tests
Unit tests for `ConfigManager` are implemented in `src/g2_2025_grade_calculator_pkg/test/ConfigManager.test.cpp` using Google Test and ROS2 test utilities. The tests use temporary TOML files to validate configuration loading and parsing functionality.
### Test Cases
#### 1. ConstructorTest
**Description:** Verifies that ConfigManager can be created with a ROS2 logger without crashing.
- **Test Action:** Create ConfigManager instance with ROS2 logger
- **Expected Result:** Instance created successfully without exceptions
#### 2. LoadValidConfigTest
**Description:** Tests loading of valid TOML configuration files with proper parsing.
- **Test Action:**
- Create temporary TOML file with valid configuration
- Call `load_config()` with file path
- **Expected Result:**
- Returns `true`
- `is_loaded()` returns `true`
#### 3. LoadInvalidFileTest
**Description:** Tests error handling when attempting to load non-existent configuration files.
- **Test Action:** Call `load_config()` with non-existent file path
- **Expected Result:**
- Returns `false`
- `is_loaded()` returns `false`
#### 4. DatabaseConfigParsingTest
**Description:** Tests complete database configuration parsing with all parameters.
- **Test Configuration:**
```toml
[database]
host = "test_host"
port = 1234
dbname = "test_db"
user = "test_user"
password = "test_password"
timeout = 60
ssl = true
[database.pool]
min_connections = 2
max_connections = 20
```
- **Expected Result:** All configuration values parsed correctly with proper types
#### 5. DatabaseConfigWithoutPoolTest
**Description:** Tests default values when optional pool section is missing from configuration.
- **Test Configuration:**
```toml
[database]
host = "localhost"
port = 5432
dbname = "grades"
user = "postgres"
password = "postgres"
```
- **Expected Result:**
- Main database config parsed correctly
- Default pool values: `min_connections = 1`, `max_connections = 10`
#### 6. GetConfigWithoutLoadingTest
**Description:** Tests behavior when attempting to access configuration before loading any file.
- **Test Action:** Call `get_database_config()` without loading configuration
- **Expected Result:**
- Returns `std::nullopt`
- `is_loaded()` returns `false`

View File

@@ -123,3 +123,69 @@ if (db_manager.is_connected()) {
}
}
```
---
## Unit Tests
Unit tests for `DatabaseManager` are implemented in `src/g2_2025_grade_calculator_pkg/test/DatabaseManager.test.cpp` using Google Test and ROS2 test utilities. The tests are designed to work reliably whether a database connection is available or not, focusing on error handling and method behavior validation.
### Test Cases
#### 1. ConstructorTest
**Description:** Verifies that DatabaseManager can be created without crashing and proper initialization occurs.
- **Test Action:** Create DatabaseManager instance with ROS2 logger
- **Expected Result:** Instance created successfully without exceptions
#### 2. ConnectionStatusTest
**Description:** Tests that the `is_connected()` method returns a valid boolean value.
- **Test Action:** Call `is_connected()` method
- **Expected Result:** Returns either `true` or `false` (no crashes or invalid states)
#### 3. QueuePendingCombinationsTest
**Description:** Verifies retrieval of pending student-course combinations that need exam results.
- **Test Action:** Call `queue_pending_combinations()`
- **Expected Result:** Returns valid vector (empty if no database connection, populated if connected)
#### 4. StoreExamResultTest
**Description:** Tests exam result storage with graceful handling of connection states.
- **Test Action:**
- Student: `"TestStudent"`
- Course: `"TestCourse"`
- Grade: `85`
- **Expected Result:** Returns `false` if no connection, `true` if connected and successful
#### 5. EnrollStudentTest
**Description:** Tests student enrollment into courses.
- **Test Action:**
- StudentCourse object with test data
- **Expected Result:** Returns `false` if no connection, `true` if connected and successful
#### 6. GetFinalGradeTest
**Description:** Tests final grade retrieval for non-existent student-course combinations.
- **Test Action:**
- Student: `"NonExistentStudent"`
- Course: `"NonExistentCourse"`
- **Expected Result:** Returns `-1` (no results found or no connection)
#### 7. StoreFinalResultTest
**Description:** Tests storing calculated final course results.
- **Test Action:**
- StudentCourse object
- Exam count: `3`
- Final grade: `75`
- **Expected Result:** Returns `false` if no connection, `true` if connected and successful

View File

@@ -36,3 +36,68 @@ ExamResultGenerator()
**`void add_student_course_combination(const StudentCourse& sc)`**
- Enrolls student into course via database
- Adds combination to processing queue
---
## Unit Tests
Unit tests for `ExamResultGenerator` are implemented in `src/g2_2025_grade_calculator_pkg/test/ExamResultGenerator.test.cpp` using Google Test and ROS2 test utilities. The tests use a test subscriber node to capture published messages and validate node behavior.
### Test Cases
#### 1. ConstructorTest
**Description:** Verifies that ExamResultGenerator can be created without crashing and proper initialization occurs.
- **Test Action:** Create ExamResultGenerator node instance
- **Expected Result:**
- Node created successfully without exceptions
- Node name set to `"exam_result_generator"`
#### 2. PublisherCreationTest
**Description:** Verifies that the `exam_results` topic publisher is properly configured.
- **Test Action:** Check topic names and types after node creation
- **Expected Result:**
- `/exam_results` topic is published
- Topic uses `g2_2025_interfaces/msg/Exam` message type
#### 3. SubscriberCreationTest
**Description:** Tests that the node subscribes to the `student_course_management` topic with correct message type.
- **Test Action:** Check subscription topics and types
- **Expected Result:**
- `/student_course_management` topic is subscribed
- Subscription uses `g2_2025_interfaces/msg/Student` message type
#### 4. StudentManagementMessageHandlingTest
**Description:** Tests the node's ability to handle incoming student management messages without crashing.
- **Test Action:**
- Publish student management message:
- Student: `"Test Student"`
- Course: `"Test Course"`
- **Expected Result:** Message processed without crashes
#### 5. MultipleStudentMessagesTest
**Description:** Validates handling of multiple rapid student management messages for robustness testing.
- **Test Action:**
- Send multiple messages with different student-course combinations
- Students: `["Alice", "Bob", "Charlie"]`
- Courses: `["Math", "Physics", "Chemistry"]`
- **Expected Result:** All messages processed without crashes or memory issues
#### 6. ExamMessageValidationTest
**Description:** Captures and validates published exam result messages for correct content and format.
- **Test Action:** Listen for published exam result messages over 6 seconds
- **Expected Result:**
- Published grades are within range `[10, 100]`
- Course names are not empty
- Message format is correct

View File

@@ -46,11 +46,8 @@ FinalGradeDeterminator()
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.
@@ -58,8 +55,6 @@ Unit tests for `FinalGradeDeterminator` are implemented in `src/g2_2025_grade_ca
- **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.
@@ -73,8 +68,6 @@ Unit tests for `FinalGradeDeterminator` are implemented in `src/g2_2025_grade_ca
- 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.
@@ -87,8 +80,6 @@ Unit tests for `FinalGradeDeterminator` are implemented in `src/g2_2025_grade_ca
- 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.