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

1"""Tests for the `collection` module.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6 

7from duty.collection import Collection, Duty 

8from duty.decorator import duty as decorate 

9 

10 

11def none(*args, **kwargs) -> None: # noqa: ANN002, ANN003, D103 

12 ... # pragma: no cover 

13 

14 

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"]) 

19 

20 

21def test_dont_get_duty() -> None: 

22 """Don't find a duty.""" 

23 collection = Collection() 

24 with pytest.raises(KeyError): 

25 collection.get("hello") 

26 

27 

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.") 

37 

38 

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") 

44 

45 

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") 

53 

54 

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() 

59 

60 duty = decorate(none, name="duty") # type: ignore[call-overload] 

61 

62 collection1.add(duty) 

63 collection2.add(duty) 

64 

65 duty1 = collection1.get("duty") 

66 duty2 = collection2.get("duty") 

67 

68 assert duty1 is not duty2 

69 assert duty1.collection is collection1 

70 assert duty2.collection is collection2