Fix python3 chart (_iter_dates)

This commit is contained in:
redref
2017-02-02 17:53:20 +01:00
parent 2189577f02
commit 2cdf5fea61
2 changed files with 28 additions and 2 deletions

View File

@@ -9,7 +9,7 @@ DATE_FORMAT = "%Y-%m-%d"
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ" DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
def _iter_dates(days_number): def _iter_dates(days_number, reverse=False):
"""Return a list of datetime pairs AB, BC, CD, ... that represent the """Return a list of datetime pairs AB, BC, CD, ... that represent the
24hs time ranges of today (until this midnight) and the 24hs time ranges of today (until this midnight) and the
previous days. previous days.
@@ -18,6 +18,9 @@ def _iter_dates(days_number):
today = datetime.utcnow().replace(hour=0, minute=0, second=0, today = datetime.utcnow().replace(hour=0, minute=0, second=0,
microsecond=0, tzinfo=UTC()) microsecond=0, tzinfo=UTC())
days_list = list(today + one_day * (1 - i) for i in range(days_number + 1)) days_list = list(today + one_day * (1 - i) for i in range(days_number + 1))
if reverse:
days_list.reverse()
return zip(days_list, days_list[1:])
return zip(days_list[1:], days_list) return zip(days_list[1:], days_list)
@@ -65,7 +68,7 @@ def get_daily_reports_chart(db, env, days_number, certname=None):
the database will be considered. the database will be considered.
""" """
result = [] result = []
for start, end in reversed(_iter_dates(days_number)): for start, end in _iter_dates(days_number, reverse=True):
query = _build_query( query = _build_query(
env=env, env=env,
start=start.strftime(DATETIME_FORMAT), start=start.strftime(DATETIME_FORMAT),

View File

@@ -523,3 +523,26 @@ def test_json_report_ok(client, mocker, input_data):
assert 'data' in result_json assert 'data' in result_json
assert len(result_json['data']) == 100 assert len(result_json['data']) == 100
def test_json_daily_reports_chart_ok(client, mocker):
mock_puppetdb_environments(mocker)
mock_puppetdb_default_nodes(mocker)
query_data = {
'reports': [
[{'status': 'changed', 'count': 1}]
for i in range(app.app.config['DAILY_REPORTS_CHART_DAYS'])
]
}
import logging
logging.error(query_data)
dbquery = MockDbQuery(query_data)
mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
rv = client.get('/daily_reports_chart.json')
assert rv.status_code == 200