Skip to content

Commit

Permalink
Merge 87b4891 into 0a451c3
Browse files Browse the repository at this point in the history
  • Loading branch information
dougthor42 committed Mar 4, 2019
2 parents 0a451c3 + 87b4891 commit e541d59
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
+ Migrations are now performed automatically when the flask app is created
(when the first request comes in to WSGI). (#71, #114)
+ Dropped support for Python 3.5 (#119)
+ Added the REST API to get all metrics (#120)


## 0.5.0 (2019-02-28)
Expand Down
6 changes: 6 additions & 0 deletions src/trendlines/error_responses.py
Expand Up @@ -27,6 +27,12 @@ def url(self):

class ErrorResponse(object):

@classmethod
def no_data(cls):
"""No data exists in the table."""
detail = "No data found."
return error_response(404, ErrorResponseType.NO_DATA, detail)

@classmethod
def metric_not_found(cls, name):
detail = "The metric '{}' does not exist".format(name)
Expand Down
10 changes: 9 additions & 1 deletion src/trendlines/routes.py
Expand Up @@ -176,7 +176,15 @@ def api_get_metrics():
"""
Return a list of all metrics in the database.
"""
pass
logger.debug("api: GET all metrics")
raw_data = db.get_metrics()
if len(raw_data) == 0:
# do a thing.
return ErrorResponse.no_data()

data = [model_to_dict(m) for m in raw_data]

return jsonify(data)


@api.route("/api/v1/metric/<metric_name>", methods=["GET"])
Expand Down
8 changes: 6 additions & 2 deletions tests/test_error_responses.py
Expand Up @@ -84,9 +84,13 @@ def test_rfc_error_response_content_type(rfc_object):
(ErrorResponse.metric_already_exists, ("foo", )),
(ErrorResponse.unique_metric_name_required, ("foo", "bar")),
(ErrorResponse.missing_required_key, ("foo", )),
(ErrorResponse.no_data, None),
])
def test_error_response_metric_not_found(app_context, caplog, method, args):
rv = method(*args)
def test_error_response_class_methods(app_context, caplog, method, args):
if args is None:
rv = method()
else:
rv = method(*args)
assert isinstance(rv, tuple)
assert len(rv) == 2
assert rv[0].is_json
18 changes: 18 additions & 0 deletions tests/test_routes.py
Expand Up @@ -339,3 +339,21 @@ def test_api_patch_metric_duplicate_name(client, populated_db):
assert old_name in d['detail']
assert new_name in d['detail']
assert "Unable to change metric name" in d['detail']


def test_api_get_metrics(client, populated_db):
rv = client.get("/api/v1/metric")
assert rv.status_code == 200
assert rv.is_json
d = rv.get_json()
assert len(d) == 6
assert "metric_id" in d[0].keys()
assert d[0]['name'] == "empty_metric"


def test_api_get_metrics_no_data(client):
rv = client.get("/api/v1/metric")
assert rv.status_code == 404
assert rv.is_json
d = rv.get_json()
assert "No data" in d['detail']

0 comments on commit e541d59

Please sign in to comment.