-
Notifications
You must be signed in to change notification settings - Fork 70
/
test_topic.py
70 lines (62 loc) · 2.53 KB
/
test_topic.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import pytest
from aiomqtt import Topic, Wildcard
def test_topic_validation() -> None:
"""Test that Topic raises Exceptions for invalid topics."""
with pytest.raises(TypeError):
Topic(True) # type: ignore[arg-type]
with pytest.raises(TypeError):
Topic(1.0) # type: ignore[arg-type]
with pytest.raises(TypeError):
Topic(None) # type: ignore[arg-type]
with pytest.raises(TypeError):
Topic([]) # type: ignore[arg-type]
with pytest.raises(ValueError, match="Invalid topic: "):
Topic("a/b/#")
with pytest.raises(ValueError, match="Invalid topic: "):
Topic("a/+/c")
with pytest.raises(ValueError, match="Invalid topic: "):
Topic("#")
with pytest.raises(ValueError, match="Invalid topic: "):
Topic("")
with pytest.raises(ValueError, match="Invalid topic: "):
Topic("a" * 65536)
def test_wildcard_validation() -> None:
"""Test that Wildcard raises Exceptions for invalid wildcards."""
with pytest.raises(TypeError):
Wildcard(True) # type: ignore[arg-type]
with pytest.raises(TypeError):
Wildcard(1.0) # type: ignore[arg-type]
with pytest.raises(TypeError):
Wildcard(None) # type: ignore[arg-type]
with pytest.raises(TypeError):
Wildcard([]) # type: ignore[arg-type]
with pytest.raises(ValueError, match="Invalid wildcard: "):
Wildcard("a/#/c")
with pytest.raises(ValueError, match="Invalid wildcard: "):
Wildcard("a/b+/c")
with pytest.raises(ValueError, match="Invalid wildcard: "):
Wildcard("a/b/#c")
with pytest.raises(ValueError, match="Invalid wildcard: "):
Wildcard("")
with pytest.raises(ValueError, match="Invalid wildcard: "):
Wildcard("a" * 65536)
def test_topic_matches() -> None:
"""Test that Topic.matches() does and doesn't match some test wildcards."""
topic = Topic("a/b/c")
assert topic.matches("a/b/c")
assert topic.matches("a/+/c")
assert topic.matches("+/+/+")
assert topic.matches("+/#")
assert topic.matches("#")
assert topic.matches("a/b/c/#")
assert topic.matches("$share/group/a/b/c")
assert topic.matches("$share/group/a/b/+")
assert not topic.matches("abc")
assert not topic.matches("a/b")
assert not topic.matches("a/b/c/d")
assert not topic.matches("a/b/c/d/#")
assert not topic.matches("a/b/z")
assert not topic.matches("a/b/c/+")
assert not topic.matches("$share/a/b/c")
assert not topic.matches("$test/group/a/b/c")