-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Kulyov <[email protected]>
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Unit tests for pytopsort module.""" | ||
|
||
import pytest | ||
|
||
from pytopsort import topsort | ||
|
||
|
||
def test_topsort(): | ||
"""This recipe contains no cycles and can be sorted.""" | ||
|
||
# fmt: off | ||
recipe = [{ | ||
"action": "spread peanut butter on bread", | ||
"after": ["slice bread"], | ||
}, { | ||
"action": "slice bread", | ||
"after": [], | ||
}, { | ||
"action": "eat the delicious breakfast!", | ||
"after": ["spread jam on top", "make a cup of coffee"], | ||
}, { | ||
"action": "make a cup of coffee", | ||
"after": [], | ||
}, { | ||
"action": "spread jam on top", | ||
"after": ["spread peanut butter on bread"], | ||
}] | ||
# fmt: on | ||
|
||
recipe = topsort( | ||
recipe, deptest=lambda self, other: self["action"] in other["after"], | ||
) | ||
|
||
# fmt: off | ||
assert list(recipe) == [{ | ||
"action": "make a cup of coffee", | ||
"after": [], | ||
}, { | ||
"action": "slice bread", | ||
"after": [], | ||
}, { | ||
"action": "spread peanut butter on bread", | ||
"after": ["slice bread"], | ||
}, { | ||
"action": "spread jam on top", | ||
"after": ["spread peanut butter on bread"], | ||
}, { | ||
"action": "eat the delicious breakfast!", | ||
"after": ["spread jam on top", "make a cup of coffee"], | ||
}] | ||
# fmt: on | ||
|
||
|
||
def test_cycles(): | ||
"""This recipe contains a cycle (two actions depend on each other).""" | ||
|
||
# fmt: off | ||
recipe = [{ | ||
"action": "spread peanut butter on bread", | ||
"after": ["slice bread"], | ||
}, { | ||
"action": "slice bread", | ||
"after": ["spread peanut butter on bread"], | ||
}, { | ||
"action": "eat the delicious breakfast!", | ||
"after": ["spread jam on top", "make a cup of coffee"], | ||
}, { | ||
"action": "make a cup of coffee", | ||
"after": [], | ||
}, { | ||
"action": "spread jam on top", | ||
"after": ["spread peanut butter on bread"], | ||
}] | ||
# fmt: on | ||
|
||
with pytest.raises(ValueError) as exc: | ||
recipe = topsort( | ||
recipe, deptest=lambda self, other: self["action"] in other["after"], | ||
) | ||
|
||
assert str(exc.value) == "Dependency graph has a cycle" |