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+
|
| 6 |
+
import streamlit.metrics
|
| 7 |
+
|
| 8 |
+
from mock import call, patch
|
| 9 |
+
|
| 10 |
+
from streamlit import config
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class MetricsTest(unittest.TestCase):
|
| 14 |
+
"""Metrics Unittest class."""
|
| 15 |
+
|
| 16 |
+
def setUp(self):
|
| 17 |
+
"""Make sure Client singleton is always empty before starting tests."""
|
| 18 |
+
streamlit.metrics.Client._singleton = None
|
| 19 |
+
|
| 20 |
+
def tearDown(self):
|
| 21 |
+
"""Cleanup metrics client."""
|
| 22 |
+
config.set_option("global.metrics", False)
|
| 23 |
+
streamlit.metrics.Client._singleton = None
|
| 24 |
+
client = streamlit.metrics.Client.get_current()
|
| 25 |
+
client.toggle_metrics()
|
| 26 |
+
|
| 27 |
+
def test_constructor(self):
|
| 28 |
+
"""Test streamlit.metrics.Client."""
|
| 29 |
+
client = streamlit.metrics.Client()
|
| 30 |
+
self.assertEqual(streamlit.metrics.Client._singleton, client)
|
| 31 |
+
|
| 32 |
+
def test_get_current(self):
|
| 33 |
+
"""Test streamlit.metrics.clientget_current."""
|
| 34 |
+
client = streamlit.metrics.Client.get_current()
|
| 35 |
+
self.assertEqual(streamlit.metrics.Client._singleton, client)
|
| 36 |
+
|
| 37 |
+
def test_not_singleton(self):
|
| 38 |
+
"""Test streamlit.metrics.Client not singleton."""
|
| 39 |
+
client = streamlit.metrics.Client.get_current()
|
| 40 |
+
|
| 41 |
+
with pytest.raises(RuntimeError) as e:
|
| 42 |
+
streamlit.metrics.Client()
|
| 43 |
+
msg = "Client already initialized. Use .get_current() instead"
|
| 44 |
+
self.assertEqual(msg, str(e.value))
|
| 45 |
+
|
| 46 |
+
def test_enabled_metrics_no_prometheus(self):
|
| 47 |
+
"""Test streamlit.metrics.Client.toggle_metrics no prometheus."""
|
| 48 |
+
config.set_option("global.metrics", True)
|
| 49 |
+
|
| 50 |
+
client = streamlit.metrics.Client.get_current()
|
| 51 |
+
if sys.version_info <= (3, 0):
|
| 52 |
+
builtin_import = "__builtin__.__import__"
|
| 53 |
+
else:
|
| 54 |
+
builtin_import = "builtins.__import__"
|
| 55 |
+
|
| 56 |
+
with pytest.raises(ImportError) as e:
|
| 57 |
+
with patch(builtin_import, side_effect=ImportError):
|
| 58 |
+
client.toggle_metrics()
|
| 59 |
+
msg = "prometheus-client is not installed. pip install prometheus-client"
|
| 60 |
+
self.assertEqual(msg, str(e.value))
|
| 61 |
+
|
| 62 |
+
def test_enabled_metrics(self):
|
| 63 |
+
"""Test streamlit.metrics.toggle_metrics enabled."""
|
| 64 |
+
config.set_option("global.metrics", True)
|
| 65 |
+
client = streamlit.metrics.Client.get_current()
|
| 66 |
+
client._metrics = {}
|
| 67 |
+
|
| 68 |
+
# yapf: disable
|
| 69 |
+
client._raw_metrics = [
|
| 70 |
+
('Counter', 'unittest_counter', 'Unittest counter', []),
|
| 71 |
+
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
| 72 |
+
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
| 73 |
+
]
|
| 74 |
+
# yapf: enable
|
| 75 |
+
|
| 76 |
+
client.toggle_metrics()
|
| 77 |
+
|
| 78 |
+
client.get("unittest_counter").inc()
|
| 79 |
+
client.get("unittest_counter_labels").labels("some_label")
|
| 80 |
+
client.get("unittest_gauge").set(42)
|
| 81 |
+
|
| 82 |
+
truth = [
|
| 83 |
+
"unittest_counter_total 1.0",
|
| 84 |
+
'unittest_counter_labels_total{label="some_label"} 0.0',
|
| 85 |
+
"unittest_gauge 42.0",
|
| 86 |
+
]
|
| 87 |
+
lines = client.generate_latest().splitlines()
|
| 88 |
+
metrics = [
|
| 89 |
+
x.decode("utf-8") for x in lines if x.decode("utf-8").startswith("unit")
|
| 90 |
+
]
|
| 91 |
+
metrics = [str(x) for x in metrics if "_created" not in x]
|
| 92 |
+
self.assertEqual(sorted(truth), sorted(metrics))
|
| 93 |
+
|
| 94 |
+
def test_disabled_metrics_check_value(self):
|
| 95 |
+
"""Test streamlit.metrics.Client.toggle_metrics disabled check value."""
|
| 96 |
+
with patch("streamlit.metrics.MockMetric", spec=True) as mock_metric:
|
| 97 |
+
config.set_option("global.metrics", False)
|
| 98 |
+
client = streamlit.metrics.Client.get_current()
|
| 99 |
+
client._metrics = {}
|
| 100 |
+
|
| 101 |
+
# yapf: disable
|
| 102 |
+
client._raw_metrics = [
|
| 103 |
+
('Counter', 'unittest_counter', 'Unittest counter', []),
|
| 104 |
+
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
| 105 |
+
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
| 106 |
+
]
|
| 107 |
+
# yapf: enable
|
| 108 |
+
|
| 109 |
+
client.toggle_metrics()
|
| 110 |
+
|
| 111 |
+
# Test that handler in Server.py will return nothing.
|
| 112 |
+
self.assertEqual(client.generate_latest(), "")
|
| 113 |
+
|
| 114 |
+
client.get("unittest_counter").inc()
|
| 115 |
+
client.get("unittest_counter_labels").labels("some_label")
|
| 116 |
+
client.get("unittest_gauge").set(42)
|
| 117 |
+
client.get("unittest_gauge").dec()
|
| 118 |
+
|
| 119 |
+
calls = [
|
| 120 |
+
call(), # Constructor
|
| 121 |
+
call(), # unittest_counter
|
| 122 |
+
call(), # unittest_counter_labels
|
| 123 |
+
call(), # unittest_gauge
|
| 124 |
+
call().inc(),
|
| 125 |
+
call().labels("some_label"),
|
| 126 |
+
call().set(42),
|
| 127 |
+
call().dec(),
|
| 128 |
+
]
|
| 129 |
+
self.assertEqual(calls, mock_metric.mock_calls)
|
| 130 |
+
|
| 131 |
+
def test_disabled_metrics(self):
|
| 132 |
+
"""Test streamlit.metrics.Client.toggle_metrics disabled."""
|
| 133 |
+
config.set_option("global.metrics", False)
|
| 134 |
+
client = streamlit.metrics.Client.get_current()
|
| 135 |
+
client._metrics = {}
|
| 136 |
+
|
| 137 |
+
# yapf: disable
|
| 138 |
+
client._raw_metrics = [
|
| 139 |
+
('Counter', 'unittest_counter', 'Unittest counter', []),
|
| 140 |
+
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
| 141 |
+
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
| 142 |
+
]
|
| 143 |
+
# yapf: enable
|
| 144 |
+
|
| 145 |
+
client.toggle_metrics()
|
| 146 |
+
|
| 147 |
+
client.get("unittest_counter").inc()
|
| 148 |
+
client.get("unittest_counter_labels").labels("some_label")
|
| 149 |
+
client.get("unittest_gauge").set(42)
|
| 150 |
+
client.get("unittest_gauge").dec()
|