File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from flask import Blueprint , jsonify
2+ from flask_restful import Resource , Api
3+
4+ from project .api .models import Score
25
36
47scores_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' ])
Original file line number Diff line number Diff 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
1213class DevelopmentConfig (BaseConfig ):
Original file line number Diff line number Diff line change 11import json
22
33from project .tests .base import BaseTestCase
4+ from project .tests .utils import add_score
45
56
67class 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' )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments