-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_metrics.py
More file actions
57 lines (43 loc) · 1.85 KB
/
Copy pathtest_metrics.py
File metadata and controls
57 lines (43 loc) · 1.85 KB
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
"""Behavioral tests for the metrics mini-project."""
from __future__ import annotations
from collections.abc import Iterator
import pytest
from patterns.python.prebound_method.examples.metrics import api
from patterns.python.prebound_method.examples.metrics.collector import MetricsCollector
from patterns.python.prebound_method.examples.metrics.main import main
from patterns.python.prebound_method.pattern import shares_instance
@pytest.fixture(autouse=True)
def clean_shared_collector() -> Iterator[None]:
api.reset()
yield
api.reset()
class TestModuleApi:
def test_all_prebound_names_share_the_hidden_collector(self) -> None:
assert shares_instance(api.increment, api.timing, api.snapshot, api.reset)
def test_modules_that_never_met_report_into_one_place(self) -> None:
api.increment("orders")
api.increment("orders", by=2)
api.timing("checkout_ms", 10.0)
api.timing("checkout_ms", 20.0)
snap = api.snapshot()
assert snap["counts"] == {"orders": 3}
assert snap["timing_avg_ms"] == {"checkout_ms": 15.0}
def test_reset_is_the_test_seam(self) -> None:
api.increment("orders")
api.reset()
assert api.snapshot() == {"counts": {}, "timing_avg_ms": {}}
class TestIsolation:
def test_an_isolated_collector_leaves_the_shared_one_alone(self) -> None:
own = MetricsCollector()
own.increment("private")
assert api.snapshot() == {"counts": {}, "timing_avg_ms": {}}
assert own.snapshot()["counts"] == {"private": 1}
class TestDemo:
def test_main_reports_orders_and_the_shared_instance(
self, capsys: pytest.CaptureFixture[str]
) -> None:
main()
out = capsys.readouterr().out
assert "one hidden instance: True" in out
assert "'orders': 3" in out
assert "'payment_errors': 1" in out