-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_prometheus_querybuilder.py
75 lines (52 loc) · 2.26 KB
/
test_prometheus_querybuilder.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
71
72
73
74
75
from prometheus_query_builder.query import Query
import pytest
def test_query():
""" Test query with only metric name """
query = Query("http_requests_total")
assert str(query) == "http_requests_total"
def test_query_with_label():
query = Query("http_requests_total")
query.add_label("environment", "production")
assert str(query) == 'http_requests_total{environment="production"}'
def test_query_with_label_operators():
query = Query("http_requests_total")
query.add_label("environment", "production", "!=")
assert str(query) == 'http_requests_total{environment!="production"}'
def test_query_with_unsupported_operator():
query = Query("http_requests_total")
with pytest.raises(ValueError):
query.add_label("environment", "production", "!===")
def test_query_with_labels():
query = Query("http_requests_total")
query.add_label("environment", "production")
query.add_label("method", "GET")
assert str(query) == 'http_requests_total{environment="production",method="GET"}'
def test_query_with_label_update():
query = Query("http_requests_total")
query.add_label("environment", "production")
query.add_label("environment", "stage")
assert str(query) == 'http_requests_total{environment="stage"}'
def test_query_remove_label():
query = Query("http_requests_total")
query.add_label("environment", "production")
assert len(query.labels) == 1
query.remove_label("environment")
assert len(query.labels) == 0
def test_query_with_time_duration():
query = Query("http_requests_total")
query.add_label("environment", "production")
query.add_time_duration("5m")
assert str(query) == 'http_requests_total{environment="production"}[5m]'
def test_query_with_offset():
query = Query("http_requests_total")
query.add_offset("5m")
assert str(query) == 'http_requests_total offset 5m'
def test_query_with_at_modifier():
query = Query("http_requests_total")
query.add_at_modifier("1609746000")
assert str(query) == 'http_requests_total @ 1609746000'
def test_query_with_time_modifiers():
query = Query("http_requests_total")
query.add_offset("5m")
query.add_at_modifier("1609746000")
assert str(query) == 'http_requests_total offset 5m @ 1609746000'