File: test_structs.py

package info (click to toggle)
python-resolvelib 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,504 kB
  • sloc: python: 2,278; javascript: 102; sh: 9; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pytest

from resolvelib.structs import DirectedGraph, build_iter_view


@pytest.fixture()
def graph():
    return DirectedGraph()


def test_graph(graph):
    """Test integrity of a simple graph.

    a -> b -> c
    |         ^
    +---------+
    """
    graph.add("a")
    graph.add("b")
    graph.add("c")
    graph.connect("a", "b")
    graph.connect("b", "c")
    graph.connect("a", "c")
    assert set(graph) == {"a", "b", "c"}
    assert set(graph.iter_edges()) == {("a", "b"), ("a", "c"), ("b", "c")}


def _generate():
    yield 0
    yield 1


@pytest.mark.parametrize("source", [_generate, [0, 1], iter([0, 1])])
def test_iter_view_iterable(source):
    """Iterable protocol for the iterator view."""
    iterator = iter(build_iter_view(source))
    assert next(iterator) == 0
    assert next(iterator) == 1
    with pytest.raises(StopIteration):
        next(iterator)


@pytest.mark.parametrize("source", [_generate, [0, 1], iter([0, 1])])
def test_iter_view_multiple_iterable(source):
    """Iterator view can be independently iter-ed multiple times."""
    view = build_iter_view(source)
    iterator_a = iter(view)
    iterator_b = iter(view)

    next(iterator_a)
    assert next(iterator_b) == 0
    assert next(iterator_a) == 1