Activities: database and routes update + unit tests
This commit is contained in:
parent
3f0b3844d4
commit
5f53d2ae13
@ -1,14 +1,35 @@
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from ..users.utils import authenticate
|
||||
from .models import Activity
|
||||
from .models import Activity, Sport
|
||||
|
||||
activities_blueprint = Blueprint('activities', __name__)
|
||||
|
||||
|
||||
@activities_blueprint.route('/sports', methods=['GET'])
|
||||
@authenticate
|
||||
def get_sports(auth_user_id):
|
||||
"""Get all sports"""
|
||||
sports = Sport.query.all()
|
||||
sports_list = []
|
||||
for sport in sports:
|
||||
sport_object = {
|
||||
'id': sport.id,
|
||||
'label': sport.label
|
||||
}
|
||||
sports_list.append(sport_object)
|
||||
response_object = {
|
||||
'status': 'success',
|
||||
'data': {
|
||||
'sports': sports_list
|
||||
}
|
||||
}
|
||||
return jsonify(response_object), 200
|
||||
|
||||
|
||||
@activities_blueprint.route('/activities', methods=['GET'])
|
||||
@authenticate
|
||||
def get_activities(user_id):
|
||||
def get_activities(auth_user_id):
|
||||
"""Get all activities"""
|
||||
activities = Activity.query.all()
|
||||
activities_list = []
|
||||
@ -17,7 +38,9 @@ def get_activities(user_id):
|
||||
'id': activity.id,
|
||||
'user_id': activity.user_id,
|
||||
'sport_id': activity.sport_id,
|
||||
'creation_date': activity.creation_date
|
||||
'creation_date': activity.creation_date,
|
||||
'activity_date': activity.activity_date,
|
||||
'duration': activity.duration.seconds
|
||||
}
|
||||
activities_list.append(activity_object)
|
||||
response_object = {
|
||||
|
@ -51,7 +51,8 @@ class Activity(db.Model):
|
||||
return self.sport.label + \
|
||||
" - " + self.activity_date.strftime('%Y-%m-%d')
|
||||
|
||||
def __init__(self, user_id, sport_id, activity_date):
|
||||
def __init__(self, user_id, sport_id, activity_date, duration):
|
||||
self.user_id = user_id
|
||||
self.sport_id = sport_id
|
||||
self.activity_date = activity_date
|
||||
self.duration = duration
|
||||
|
94
mpwo_api/mpwo_api/tests/test_activities.py
Normal file
94
mpwo_api/mpwo_api/tests/test_activities.py
Normal file
@ -0,0 +1,94 @@
|
||||
import datetime
|
||||
import json
|
||||
|
||||
from mpwo_api.tests.base import BaseTestCase
|
||||
from mpwo_api.tests.utils import add_activity, add_sport, add_user
|
||||
|
||||
|
||||
class TestActivitiesService(BaseTestCase):
|
||||
"""Tests for Activities."""
|
||||
|
||||
def test_get_all_sports(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
add_sport('cycling')
|
||||
add_sport('running')
|
||||
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.get(
|
||||
'/api/sports',
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('success', data['status'])
|
||||
|
||||
self.assertEqual(len(data['data']['sports']), 2)
|
||||
self.assertIn('cycling', data['data']['sports'][0]['label'])
|
||||
self.assertIn('running', data['data']['sports'][1]['label'])
|
||||
|
||||
def test_get_all_activities(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
add_user('toto', 'toto@toto.com', '12345678')
|
||||
add_sport('cycling')
|
||||
add_sport('running')
|
||||
add_activity(
|
||||
1,
|
||||
2,
|
||||
datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'),
|
||||
datetime.timedelta(seconds=1024))
|
||||
add_activity(
|
||||
2,
|
||||
1,
|
||||
datetime.datetime.strptime('23/01/2018', '%d/%m/%Y'),
|
||||
datetime.timedelta(seconds=3600))
|
||||
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.get(
|
||||
'/api/activities',
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('success', data['status'])
|
||||
self.assertEqual(len(data['data']['activities']), 2)
|
||||
self.assertTrue('creation_date' in data['data']['activities'][0])
|
||||
self.assertTrue('creation_date' in data['data']['activities'][1])
|
||||
self.assertEqual('Tue, 23 Jan 2018 00:00:00 GMT',
|
||||
data['data']['activities'][0][
|
||||
'activity_date'])
|
||||
self.assertEqual('Mon, 01 Jan 2018 00:00:00 GMT',
|
||||
data['data']['activities'][1][
|
||||
'activity_date'])
|
||||
self.assertTrue('creation_date' in data['data']['activities'][1])
|
||||
self.assertEqual(2, data['data']['activities'][0]['user_id'])
|
||||
self.assertEqual(1, data['data']['activities'][1]['user_id'])
|
||||
self.assertEqual(1, data['data']['activities'][0]['sport_id'])
|
||||
self.assertEqual(2, data['data']['activities'][1]['sport_id'])
|
||||
self.assertEqual(3600, data['data']['activities'][0]['duration'])
|
||||
self.assertEqual(1024, data['data']['activities'][1]['duration'])
|
@ -1,6 +1,7 @@
|
||||
import datetime
|
||||
|
||||
from mpwo_api import db
|
||||
from mpwo_api.activities.models import Activity, Sport
|
||||
from mpwo_api.users.models import User
|
||||
|
||||
|
||||
@ -21,3 +22,21 @@ def add_user_full(username, email, password):
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
def add_sport(label):
|
||||
sport = Sport(label=label)
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
return sport
|
||||
|
||||
|
||||
def add_activity(user_id, sport_id, activity_date, duration):
|
||||
activity = Activity(
|
||||
user_id=user_id,
|
||||
sport_id=sport_id,
|
||||
activity_date=activity_date,
|
||||
duration=duration)
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
return activity
|
||||
|
@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
|
||||
from mpwo_api import app, db
|
||||
from mpwo_api.activities.models import Sport
|
||||
from mpwo_api.users.models import User
|
||||
|
||||
|
||||
@ -20,8 +21,14 @@ def init_data():
|
||||
password='mpwoadmin')
|
||||
admin.admin = True
|
||||
db.session.add(admin)
|
||||
db.session.add(Sport(label='Cycling (Sport)'))
|
||||
db.session.add(Sport(label='Cycling (Transport)'))
|
||||
db.session.add(Sport(label='Hiking'))
|
||||
db.session.add(Sport(label='Mountain Biking'))
|
||||
db.session.add(Sport(label='Running'))
|
||||
db.session.add(Sport(label='Walking'))
|
||||
db.session.commit()
|
||||
print('Admin created.')
|
||||
print('Initial data stored in database.')
|
||||
|
||||
|
||||
def run_test(test_path='mpwo_api/tests'):
|
||||
|
Loading…
Reference in New Issue
Block a user