A Python port of the Invisible Internet Project (I2P)
1"""Tests for graphs/stats — TDD: tests before implementation."""
2
3import pytest
4from i2p_apps.console.app import create_app
5from i2p_apps.console.services.stats_collector import StatsCollector
6
7
8@pytest.fixture
9def client():
10 app = create_app({"TESTING": True})
11 return app.test_client()
12
13
14class TestGraphsPage:
15 def test_graphs_renders(self, client):
16 resp = client.get("/graphs")
17 assert resp.status_code == 200
18 assert b"html" in resp.data.lower()
19
20 def test_graphs_has_chart_container(self, client):
21 resp = client.get("/graphs")
22 assert b"plotly" in resp.data.lower() or b"chart" in resp.data.lower()
23
24
25class TestStatsCollector:
26 def test_creation(self):
27 sc = StatsCollector()
28 assert sc is not None
29
30 def test_record_and_get(self):
31 sc = StatsCollector()
32 sc.record("bandwidth.in", 1024)
33 sc.record("bandwidth.in", 2048)
34 values = sc.get("bandwidth.in")
35 assert len(values) == 2
36
37 def test_get_nonexistent(self):
38 sc = StatsCollector()
39 assert sc.get("nonexistent") == []
40
41 def test_average(self):
42 sc = StatsCollector()
43 sc.record("test.stat", 10)
44 sc.record("test.stat", 20)
45 sc.record("test.stat", 30)
46 assert sc.average("test.stat") == 20.0
47
48 def test_average_empty(self):
49 sc = StatsCollector()
50 assert sc.average("empty") == 0.0
51
52 def test_summary(self):
53 sc = StatsCollector()
54 sc.record("stat1", 100)
55 sc.record("stat2", 200)
56 summary = sc.summary()
57 assert "stat1" in summary
58 assert "stat2" in summary
59
60 def test_max_samples(self):
61 sc = StatsCollector(max_samples=3)
62 for i in range(10):
63 sc.record("test", float(i))
64 assert len(sc.get("test")) == 3