From a079a4a74135365cd38df8e398748b1b5b079a73 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 5 Jun 2018 18:41:13 +0200 Subject: [PATCH] API: display user statistics in users routes --- mpwo_api/mpwo_api/tests/test_users_api.py | 54 +++++++++++++++++++++-- mpwo_api/mpwo_api/users/users.py | 17 +------ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/mpwo_api/mpwo_api/tests/test_users_api.py b/mpwo_api/mpwo_api/tests/test_users_api.py index 5ff51293..5897a712 100644 --- a/mpwo_api/mpwo_api/tests/test_users_api.py +++ b/mpwo_api/mpwo_api/tests/test_users_api.py @@ -21,11 +21,49 @@ def test_single_user(app, user_1): data = json.loads(response.data.decode()) assert response.status_code == 200 - assert 'success' in data['status'] + assert data['status'] == 'success' + assert data['data'] is not None + assert data['data']['username'] == 'test' + assert data['data']['email'] == 'test@test.com' + assert data['data']['created_at'] + assert not data['data']['admin'] + assert data['data']['first_name'] is None + assert data['data']['last_name'] is None + assert data['data']['birth_date'] is None + assert data['data']['bio'] is None + assert data['data']['location'] is None + assert data['data']['nb_activities'] == 0 + assert data['data']['nb_sports'] == 0 + assert data['data']['total_distance'] == 0 + assert data['data']['total_duration'] == '0:00:00' - assert 'created_at' in data['data'] - assert 'test' in data['data']['username'] - assert 'test@test.com' in data['data']['email'] + +def test_single_user_with_activities( + app, user_1, sport_1_cycling, sport_2_running, + activity_cycling_user_1, activity_running_user_1 +): + """=> Get single user details""" + client = app.test_client() + + response = client.get(f'/api/users/{user_1.id}') + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert data['status'] == 'success' + assert data['data'] is not None + assert data['data']['username'] == 'test' + assert data['data']['email'] == 'test@test.com' + assert data['data']['created_at'] + assert not data['data']['admin'] + assert data['data']['first_name'] is None + assert data['data']['last_name'] is None + assert data['data']['birth_date'] is None + assert data['data']['bio'] is None + assert data['data']['location'] is None + assert data['data']['nb_activities'] == 2 + assert data['data']['nb_sports'] == 2 + assert data['data']['total_distance'] == 22 + assert data['data']['total_duration'] == '1:57:04' def test_single_user_no_id(app): @@ -67,6 +105,14 @@ def test_users_list(app, user_1, user_2): assert 'toto' in data['data']['users'][1]['username'] assert 'test@test.com' in data['data']['users'][0]['email'] assert 'toto@toto.com' in data['data']['users'][1]['email'] + assert data['data']['users'][0]['nb_activities'] == 0 + assert data['data']['users'][0]['nb_sports'] == 0 + assert data['data']['users'][0]['total_distance'] == 0 + assert data['data']['users'][0]['total_duration'] == '0:00:00' + assert data['data']['users'][1]['nb_activities'] == 0 + assert data['data']['users'][1]['nb_sports'] == 0 + assert data['data']['users'][1]['total_distance'] == 0 + assert data['data']['users'][1]['total_duration'] == '0:00:00' def test_encode_auth_token(app, user_1): diff --git a/mpwo_api/mpwo_api/users/users.py b/mpwo_api/mpwo_api/users/users.py index 2d70dae4..4c4ff0e2 100644 --- a/mpwo_api/mpwo_api/users/users.py +++ b/mpwo_api/mpwo_api/users/users.py @@ -9,19 +9,10 @@ users_blueprint = Blueprint('users', __name__) def get_users(): """Get all users""" users = User.query.all() - users_list = [] - for user in users: - user_object = { - 'id': user.id, - 'username': user.username, - 'email': user.email, - 'created_at': user.created_at - } - users_list.append(user_object) response_object = { 'status': 'success', 'data': { - 'users': users_list + 'users': [user.serialize() for user in users] } } return jsonify(response_object), 200 @@ -41,11 +32,7 @@ def get_single_user(user_id): else: response_object = { 'status': 'success', - 'data': { - 'username': user.username, - 'email': user.email, - 'created_at': user.created_at - } + 'data': user.serialize() } return jsonify(response_object), 200 except ValueError: