Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kulyov <[email protected]>
  • Loading branch information
pkulev committed Jul 23, 2020
1 parent 462b943 commit 48606f6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Empty file added tests/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions tests/test_topsort.py
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"

0 comments on commit 48606f6

Please sign in to comment.