Skip to content

Commit

Permalink
Improve code coverage for graph module (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmr committed Sep 7, 2022
1 parent 4772570 commit 0a34aa3
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,23 @@ impl UnitGraph for InMemoryUnitGraph {
if let Some(dependencies) = dependencies {
for dependency_id in dependencies {
let dependents = self.get_dependents(&dependency_id);
let mut missing_dependent = dependents.is_none();
if let Some(dependents) = dependents {
// Verify that the dependency and dependent graphs agree with each other
// by checking that all the dependencies of the current unit list it as
// a dependent.
if !dependents.contains(&current_id) {
return Err(anyhow!(
"unit {} lists unit {} as a dependency but the reverse \
relationship does not exist",
current_id,
dependency_id
));
missing_dependent = true;
}
}
if missing_dependent {
return Err(anyhow!(
"unit {} lists unit {} as a dependency but the dependent \
relationship does not exist",
current_id,
dependency_id
));
}

if path.contains(&dependency_id) {
return Err(anyhow!("cycle in dependency graph detected"));
Expand Down Expand Up @@ -456,7 +460,7 @@ impl UnitGraph for InMemoryUnitGraph {
mod test {
use anyhow::Result;
use indoc::indoc;
use ustr::Ustr;
use ustr::{Ustr, UstrSet};

use crate::data::UnitType;

Expand Down Expand Up @@ -701,4 +705,36 @@ mod test {

Ok(())
}

#[test]
fn update_unit_type_different_types() -> Result<()> {
let mut graph = InMemoryUnitGraph::default();
let unit_id = Ustr::from("unit_id");
graph.update_unit_type(&unit_id, UnitType::Course)?;
assert!(graph.update_unit_type(&unit_id, UnitType::Lesson).is_err());
Ok(())
}

#[test]
fn missing_dependent_relationship() -> Result<()> {
let mut graph = InMemoryUnitGraph::default();
let course_id = Ustr::from("course_id");
let lesson1_id = Ustr::from("lesson1_id");
let lesson2_id = Ustr::from("lesson2_id");
graph.add_course(&course_id).unwrap();
graph.add_lesson(&lesson1_id, &course_id).unwrap();
graph.add_lesson(&lesson2_id, &course_id).unwrap();
graph.add_dependencies(&lesson2_id, UnitType::Lesson, &[lesson1_id.clone()])?;

// Manually remove the dependent relationship to trigger the check and make the cycle
// detection fail.
graph
.dependent_graph
.insert(lesson1_id.clone(), UstrSet::default());
assert!(graph.check_cycles().is_err());
// Also check that the check fails if the dependents value is `None`.
graph.dependency_graph.remove(&lesson1_id);
assert!(graph.check_cycles().is_err());
Ok(())
}
}

0 comments on commit 0a34aa3

Please sign in to comment.