Strengthen test_add_duplicate_student to detect overwrites in student-grades-tracker#186
Strengthen test_add_duplicate_student to detect overwrites in student-grades-tracker#186HenriTEL wants to merge 1 commit into
test_add_duplicate_student to detect overwrites in student-grades-tracker#186Conversation
WalkthroughThe test now mutates the existing "Alice" entry (sets name to "Original Alice"), calls add_student("Alice") again, and then asserts the map size and key presence are unchanged and that the stored "Alice" entry’s name remains "Original Alice". 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@challenges/student-grades-tracker/tests/tests.rs`:
- Around line 14-19: The test should not rely on pointer identity (which HashMap
does not guarantee); instead, ensure duplicate insertion preserves the student's
observable state: before calling tracker.add_student("Alice") a second time,
mutate Alice (e.g., call tracker.add_grade("Alice", grade) or push into the
Student.grades via tracker.students.get_mut("Alice")), then call
tracker.add_student("Alice") and assert the student's data is unchanged by
comparing the Student record or its grades vector (use
tracker.students.get("Alice").unwrap() and check fields like grades) and also
assert len stayed 1 and contains_key("Alice"); update the test to remove the
pointer comparisons (before/after pointers) and replace them with these
value-based assertions referencing tracker, add_student, Student, and the grades
field or add_grade method.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 426f603b-9c4e-4f92-a0d6-5f90c20b3d61
📒 Files selected for processing (1)
challenges/student-grades-tracker/tests/tests.rs
Fix `test_add_duplicate_student` so that it properly check for overwrites.
A solution like the following should not pass:
```rust
use std::collections::HashMap;
pub struct Student {
pub name: String,
pub grades: Vec<u8>,
}
pub struct StudentGrades {
pub students: HashMap<String, Student>,
}
impl StudentGrades {
pub fn new() -> Self {
Self {
students: HashMap::new(),
}
}
pub fn add_student(&mut self, name: &str) {
self.students.insert(
name.to_string(),
Student {
name: name.to_string(),
grades: vec![]
}
);
}
pub fn add_grade(&mut self, name: &str, grade: u8) {
if let Some(student) = self.students.get_mut(name) {
student.grades.push(grade);
}
}
pub fn get_grades(&self, name: &str) -> &[u8] {
if let Some(student) = self.students.get(name) {
student.grades.as_slice()
} else {
&[]
}
}
}
```
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@challenges/student-grades-tracker/tests/tests.rs`:
- Around line 14-18: The test currently mutates the internal tracker.students
map and Student.name directly; instead, remove any direct access to
tracker.students and Student.name and verify duplicate handling via the public
API: create the student with tracker.add_student("Alice"), use the public method
to add a grade (e.g., tracker.add_grade("Alice", ...)), call
tracker.add_student("Alice") again, then assert the student still exists and
that the previously added grades are preserved by querying through the public
getter (e.g., tracker.get_grades("Alice") or the equivalent public accessor).
Ensure all assertions use add_student, add_grade, and the public grade retrieval
methods rather than inspecting internal fields.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0a0fdabc-56aa-4606-a02e-48e3530beffc
📒 Files selected for processing (1)
challenges/student-grades-tracker/tests/tests.rs
test_add_duplicate_student to detect overwrites in student-grades-tracker
Fix
test_add_duplicate_studentso that it properly checks for overwrites.A solution like the following should not pass: