Skip to content

Commit

Permalink
fixed testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingBird95 committed Mar 29, 2018
1 parent 108c64b commit 604b346
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions flask_monitoringdashboard/database/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ def get_average(date_from=None):
return result


def get_data_between(time_from, time_to):
def get_data_between(time_from, time_to=None):
"""
Returns all data in the FunctionCall table, for the export data option.
This function returns all data after the time_from date.
"""
with session_scope() as db_session:
result = db_session.query(FunctionCall).filter(FunctionCall.time >= time_from)\
.filter(FunctionCall.time <= time_to).all()
result = db_session.query(FunctionCall).filter(FunctionCall.time >= time_from)
if time_to:
result = result.filter(FunctionCall.time <= time_to)
result = result.all()
db_session.expunge_all()
return result

Expand All @@ -87,7 +89,7 @@ def get_data():
Equivalent function to get_data_from, but returns all data.
:return: all data from the database in the Endpoint-table.
"""
return get_data_between(datetime.date(1970, 1, 1), datetime.datetime.now())
return get_data_between(datetime.date(1970, 1, 1), datetime.datetime.utcnow())


def get_data_per_version(version):
Expand Down
14 changes: 8 additions & 6 deletions flask_monitoringdashboard/routings/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,25 @@ def submit_test_results():
return '', 204


@blueprint.route('/get_json_data', defaults={'time_from': 0,
'time_to': time.mktime(datetime.datetime.utcnow().timetuple())})
@blueprint.route('/get_json_data/<time_from>', defaults={
'time_to': time.mktime(datetime.datetime.utcnow().timetuple())})
@blueprint.route('/get_json_data', defaults={'time_from': 0})
@blueprint.route('/get_json_data/<time_from>')
@blueprint.route('/get_json_data/<time_from>/<time_to>')
def get_json_data_from(time_from, time_to):
def get_json_data_from(time_from, time_to=None):
"""
The returned data is the data that is encrypted using a security token. This security token is set in the
configuration.
:param time_from: (optional) if specified, only the data-values after this date are returned.
input must be an timestamp value (utc) (= integer)
:param time_to: (optional) if specified, only the data-values before this date are returned.
input must be an timestamp value (utc) (= integer)
:return: all entries from the database. (endpoint-table)
"""
data = []
try:
time1 = datetime.datetime.utcfromtimestamp(int(time_from))
time2 = datetime.datetime.utcfromtimestamp(int(time_to))
time2 = None
if time_to:
time2 = datetime.datetime.utcfromtimestamp(int(time_to))
for entry in get_data_between(time1, time2):
# nice conversion to json-object
data.append({
Expand Down
4 changes: 2 additions & 2 deletions flask_monitoringdashboard/test/db/test_function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def test_get_data_from(self):
"""
Test whether the function returns the right values.
"""
from flask_monitoringdashboard.database.function_calls import get_data_from
result = get_data_from(TIMES[len(TIMES)-1])
from flask_monitoringdashboard.database.function_calls import get_data_between
result = get_data_between(TIMES[len(TIMES)-1], TIMES[len(TIMES)-1])
self.assertEqual(len(result), 1)
self.assertEqual(result[0].endpoint, NAME)
self.assertEqual(result[0].execution_time, EXECUTION_TIMES[len(TIMES)-1])
Expand Down
7 changes: 5 additions & 2 deletions flask_monitoringdashboard/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
Some useful functions for setting up the testing environment, adding data, etc..
"""
import datetime
import time

from flask import Flask

NAME = 'main'
IP = '127.0.0.1'
GROUP_BY = '1'
EXECUTION_TIMES = [1000, 2000, 3000, 4000, 50000]
TIMES = [datetime.datetime.now()] * 5
TIMES = [datetime.datetime.utcnow()] * 5
for i in range(len(TIMES)):
TIMES[i] += datetime.timedelta(seconds=i)
TEST_NAMES = ['test_name1', 'test_name2']
print('Sleeping 10 seconds before executing tests')
time.sleep(10)


def set_test_environment():
Expand Down Expand Up @@ -43,7 +46,7 @@ def add_fake_data():

# Add MonitorRule
with session_scope() as db_session:
db_session.add(MonitorRule(endpoint=NAME, monitor=True, time_added=datetime.datetime.now(),
db_session.add(MonitorRule(endpoint=NAME, monitor=True, time_added=datetime.datetime.utcnow(),
version_added=config.version, last_accessed=TIMES[0]))

# Add Tests
Expand Down

0 comments on commit 604b346

Please sign in to comment.