Skip to content

Commit f6e917c

Browse files
committed
Some small fixes
1 parent 195a357 commit f6e917c

File tree

6 files changed

+23
-39
lines changed

6 files changed

+23
-39
lines changed

flask_monitoringdashboard/database/request.py

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ def get_latencies_sample(session, endpoint_id, interval, sample_size=500):
1616
.filter(Request.endpoint_id == endpoint_id, *criterion)
1717
.limit(sample_size)
1818
)
19+
# return random rows: See https://stackoverflow.com/a/60815
20+
dialect = session.bind.dialect.name
21+
22+
if dialect == 'sqlite':
23+
query = query.order_by(func.random())
24+
elif dialect == 'mysql':
25+
query = query.order_by(func.rand())
1926

2027
return [item.duration for item in query.all()]
2128

migration/migrate_v1_to_v2.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""
2-
Use this file for migrating the Database from v1.X.X to v2.X.X
3-
Before running the script, make sure to change the OLD_DB_URL and NEW_DB_URL on lines 9 and 10.
4-
Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html on how to configure this.
1+
"""Use this file for migrating the Database from v1.X.X to v2.X.X
2+
Before running the script, make sure to change the OLD_DB_URL and NEW_DB_URL on lines 9 and 10.
3+
Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html on how to configure this.
54
"""
65
import datetime
76
from contextlib import contextmanager
@@ -47,10 +46,9 @@ def get_connection(db_url):
4746

4847
@contextmanager
4948
def session_scope():
50-
"""
51-
When accessing the database, use the following syntax:
52-
with session_scope() as session:
53-
session.query(...)
49+
"""When accessing the database, use the following syntax:
50+
>>> with session_scope() as session:
51+
>>> session.query(...)
5452
5553
:return: the session for accessing the database
5654
"""

tests/unit/core/config/test_config.py

-6
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,3 @@ def test_parser():
4141
assert parse_literal(parser, section, 'literal', 'default') == ['a', 'b', 'c']
4242
assert parse_literal(parser, section, 'literal2', 'default') == 1.23
4343

44-
45-
def test_environment_vars():
46-
"""Test whether environment variables can be read."""
47-
48-
os.environ['ENVIRONMENT_VAR'] = 'abc'
49-
assert get_environment_var('ENVIRONMENT_VAR') == 'abc'

tests/unit/database/test_endpoint.py

-4
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,25 @@
1313

1414

1515
def test_get_endpoint(session, endpoint):
16-
"""Test whether the function returns the right values."""
1716
endpoint2 = get_endpoint_by_name(session, endpoint.name)
1817
assert endpoint.name == endpoint2.name
1918
assert endpoint.id == endpoint2.id
2019

2120

2221
@pytest.mark.parametrize('endpoint__monitor_level', [1])
2322
def test_update_endpoint(session, endpoint):
24-
"""Test whether the function returns the right values."""
2523
update_endpoint(session, endpoint.name, 2)
2624
assert get_endpoint_by_name(session, endpoint.name).monitor_level == 2
2725

2826

2927
@pytest.mark.parametrize('timestamp', [datetime(2020, 2, 2), datetime(2020, 3, 3)])
3028
def test_update_last_accessed(session, endpoint, timestamp):
31-
"""Test whether the function returns the right values."""
3229
update_last_requested(session, endpoint.name, timestamp=timestamp)
3330
result = get_value(get_last_requested(session), endpoint.name)
3431
assert result == timestamp
3532

3633

3734
def test_endpoints(session, endpoint):
38-
"""Test whether the function returns the right values."""
3935
endpoints = get_endpoints(session)
4036
assert endpoints.count() == session.query(Endpoint).count()
4137
assert [endpoint.id == e.id for e in endpoints] # check that the endpoint is included.

tests/unit/database/test_outlier.py

-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
def test_add_outlier(session, request_1):
14-
"""Test whether the function returns the right values."""
1514
assert not request_1.outlier
1615

1716
add_outlier(
@@ -27,22 +26,19 @@ def test_add_outlier(session, request_1):
2726

2827

2928
def test_get_outliers(session, outlier_1, endpoint):
30-
"""Test whether the function returns the right values."""
3129
outliers = get_outliers_sorted(session, endpoint_id=endpoint.id, offset=0, per_page=10)
3230
assert len(outliers) == 1
3331
assert outliers[0].id == outlier_1.id
3432

3533

3634
@pytest.mark.usefixtures('outlier_1', 'outlier_2')
3735
def test_count_outliers(session, endpoint):
38-
"""Test whether the function returns the right values."""
3936
assert count_outliers(session, endpoint.id) == 2
4037

4138

4239
@pytest.mark.usefixtures('outlier_1', 'outlier_2')
4340
@pytest.mark.parametrize('outlier_1__cpu_percent', ['[0, 1, 2, 3]'])
4441
@pytest.mark.parametrize('outlier_2__cpu_percent', ['[1, 2, 3, 4]'])
4542
def test_get_outliers_cpus(session, endpoint):
46-
"""Test whether the function returns the right values."""
4743
expected_cpus = ['[{0}, {1}, {2}, {3}]'.format(i, i + 1, i + 2, i + 3) for i in range(2)]
4844
assert get_outliers_cpus(session, endpoint.id) == expected_cpus

tests/unit/database/test_request.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
from flask_monitoringdashboard.core.date_interval import DateInterval
13-
from flask_monitoringdashboard.database import session_scope
1413
from flask_monitoringdashboard.database.count import count_requests
1514
from flask_monitoringdashboard.database.endpoint import get_avg_duration, get_endpoints
1615
from flask_monitoringdashboard.database.request import add_request, get_date_of_first_request, get_latencies_sample
@@ -23,25 +22,21 @@ def test_get_latencies_sample(session, request_1, endpoint):
2322
assert data == [request_1.duration]
2423

2524

26-
def test_add_request(endpoint):
27-
"""Test whether the function returns the right values."""
28-
# For some reason, using the session-fixture here doesn't work.
25+
def test_add_request(endpoint, session):
2926
num_requests = len(endpoint.requests)
30-
with session_scope() as session:
31-
add_request(
32-
session,
33-
duration=200,
34-
endpoint_id=endpoint.id,
35-
ip='127.0.0.1',
36-
group_by=None,
37-
status_code=200,
38-
)
39-
assert count_requests(session, endpoint.id) == num_requests + 1
27+
add_request(
28+
session,
29+
duration=200,
30+
endpoint_id=endpoint.id,
31+
ip='127.0.0.1',
32+
group_by=None,
33+
status_code=200,
34+
)
35+
assert count_requests(session, endpoint.id) == num_requests + 1
4036

4137

4238
@pytest.mark.parametrize('request_1__time_requested', [datetime(2020, 2, 3)])
4339
def test_get_versions(session, request_1):
44-
"""Test whether the function returns the right values."""
4540
for version, first_request in get_versions(session):
4641
if version == request_1.version_requested:
4742
assert first_request == request_1.time_requested
@@ -50,14 +45,12 @@ def test_get_versions(session, request_1):
5045

5146

5247
def test_get_endpoints(session, endpoint):
53-
"""Test whether the function returns the right values."""
5448
endpoints = get_endpoints(session)
5549
assert endpoint.name in [endpoint.name for endpoint in endpoints]
5650

5751

5852
@pytest.mark.parametrize('request_1__time_requested', [datetime(1970, 1, 1)])
5953
def test_get_date_of_first_request(session, request_1):
60-
"""Test whether the function returns the right values."""
6154
total_seconds = int(time.mktime(request_1.time_requested.timetuple()))
6255
assert get_date_of_first_request(session) == total_seconds
6356

0 commit comments

Comments
 (0)