Coverage for tests/test_collection.py: 100.00%
41 statements
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-17 17:18 +0200
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-17 17:18 +0200
1"""Tests for the `collection` module."""
3from __future__ import annotations
5import pytest
7from duty.collection import Collection, Duty
8from duty.decorator import duty as decorate
11def none(*args, **kwargs) -> None: # noqa: ANN002, ANN003, D103
12 ... # pragma: no cover
15def test_instantiate_duty() -> None:
16 """Instantiate a duty."""
17 assert Duty("name", "description", none)
18 assert Duty("name", "description", none, pre=["0", "1"], post=["2"])
21def test_dont_get_duty() -> None:
22 """Don't find a duty."""
23 collection = Collection()
24 with pytest.raises(KeyError):
25 collection.get("hello")
28def test_register_aliases() -> None:
29 """Register a duty and its aliases."""
30 duty = decorate(none, name="hello", aliases=["HELLO", "_hello_", ".hello."]) # type: ignore[call-overload]
31 collection = Collection()
32 collection.add(duty)
33 assert collection.get("hello")
34 assert collection.get("HELLO")
35 assert collection.get("_hello_")
36 assert collection.get(".hello.")
39def test_replace_name_and_set_alias() -> None:
40 """Replace underscores by dashes in duties names."""
41 collection = Collection()
42 collection.add(decorate(none, name="snake_case")) # type: ignore[call-overload]
43 assert collection.get("snake_case") is collection.get("snake-case")
46def test_clear_collection() -> None:
47 """Check that duties and their aliases are correctly cleared from a collection."""
48 collection = Collection()
49 collection.add(decorate(none, name="duty_1")) # type: ignore[call-overload]
50 collection.clear()
51 with pytest.raises(KeyError):
52 collection.get("duty-1")
55def test_add_duty_to_multiple_collections() -> None:
56 """Check what happens when adding the same duty to multiple collections."""
57 collection1 = Collection()
58 collection2 = Collection()
60 duty = decorate(none, name="duty") # type: ignore[call-overload]
62 collection1.add(duty)
63 collection2.add(duty)
65 duty1 = collection1.get("duty")
66 duty2 = collection2.get("duty")
68 assert duty1 is not duty2
69 assert duty1.collection is collection1
70 assert duty2.collection is collection2