Skip to content

Commit 0894235

Browse files
committed
Add ping route to Scores API
1 parent c8a09c2 commit 0894235

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

services/scores/project/api/scores.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
from flask import Blueprint, jsonify
2+
from flask_restful import Resource, Api
3+
4+
from project.api.models import Score
25

36

47
scores_blueprint = Blueprint('scores', __name__)
8+
api = Api(scores_blueprint)
9+
10+
11+
class ScoresList(Resource):
12+
13+
def get(self):
14+
"""Get all scores"""
15+
16+
response_object = {
17+
'status': 'success',
18+
'data': {
19+
'scores': [score.to_json() for score in Score.query.all()]
20+
}
21+
}
22+
23+
return response_object, 200
24+
25+
26+
api.add_resource(ScoresList, '/scores')
527

628

729
@scores_blueprint.route('/scores/ping', methods=['GET'])

services/scores/project/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class BaseConfig:
77
DEBUG_TB_INTERCEPT_REDIRECTS = False
88
SECRET_KEY = os.environ.get('SECRET_KEY')
99
SQLALCHEMY_TRACK_MODIFICATIONS = False
10+
USERS_SERVICE_URL = os.environ.get('USERS_SERVICE_URL')
1011

1112

1213
class DevelopmentConfig(BaseConfig):

services/scores/project/tests/test_scores_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import json
22

33
from project.tests.base import BaseTestCase
4+
from project.tests.utils import add_score
45

56

67
class TestScoresService(BaseTestCase):
78

9+
def test_get_all_scores(self):
10+
"""Ensure get all scores behaves correctly."""
11+
add_score(1, 1, True)
12+
add_score(2, 1, False)
13+
14+
with self.client:
15+
response = self.client.get('/scores')
16+
data = json.loads(response.data.decode())
17+
18+
self.assertEqual(response.status_code, 200)
19+
self.assertIn('success', data['status'])
20+
21+
self.assertEqual(len(data['data']['scores']), 2)
22+
23+
self.assertEqual(1, data['data']['scores'][0]['user_id'])
24+
self.assertEqual(1, data['data']['scores'][0]['exercise_id'])
25+
self.assertEqual(True, data['data']['scores'][0]['correct'])
26+
27+
self.assertEqual(2, data['data']['scores'][1]['user_id'])
28+
self.assertEqual(1, data['data']['scores'][1]['exercise_id'])
29+
self.assertEqual(False, data['data']['scores'][1]['correct'])
30+
831
def test_ping(self):
932
"""Ensure the /ping route behaves correctly."""
1033
response = self.client.get('/scores/ping')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from project import db
2+
from project.api.models import Score
3+
4+
5+
def add_score(user_id, exercise_id, correct):
6+
score = Score(
7+
user_id=user_id,
8+
exercise_id=exercise_id,
9+
correct=correct
10+
)
11+
db.session.add(score)
12+
db.session.commit()
13+
14+
return score

0 commit comments

Comments
 (0)