API: records fix

This commit is contained in:
Sam 2018-05-18 15:21:11 +02:00
parent 948e49beef
commit c03d8f2736
3 changed files with 242 additions and 16 deletions

View File

@ -32,16 +32,16 @@ def convert_value_to_integer(record_type, val):
return int(val * 1000) return int(val * 1000)
def update_records(activity, sport_id, connection, session): def update_records(user_id, sport_id, connection, session):
record_table = Record.__table__ record_table = Record.__table__
new_records = Activity.get_user_activity_records( new_records = Activity.get_user_activity_records(
activity.user_id, user_id,
sport_id) sport_id)
for record_type, record_data in new_records.items(): for record_type, record_data in new_records.items():
if record_data['record_value']: if record_data['record_value']:
record = Record.query.filter_by( record = Record.query.filter_by(
user_id=activity.user_id, user_id=user_id,
sport_id=activity.sport_id, sport_id=sport_id,
record_type=record_type, record_type=record_type,
).first() ).first()
if record: if record:
@ -64,7 +64,7 @@ def update_records(activity, sport_id, connection, session):
session.add(new_record) session.add(new_record)
else: else:
connection.execute(record_table.delete().where( connection.execute(record_table.delete().where(
record_table.c.user_id == activity.user_id, record_table.c.user_id == user_id,
).where( ).where(
record_table.c.sport_id == sport_id, record_table.c.sport_id == sport_id,
).where( ).where(
@ -230,7 +230,7 @@ def on_activity_insert(mapper, connection, activity):
@listens_for(db.Session, 'after_flush', once=True) @listens_for(db.Session, 'after_flush', once=True)
def receive_after_flush(session, context): def receive_after_flush(session, context):
update_records(activity, activity.sport_id, connection, session) update_records(activity.user_id, activity.sport_id, connection, session) # noqa
@listens_for(Activity, 'after_update') @listens_for(Activity, 'after_update')
@ -246,7 +246,7 @@ def on_activity_update(mapper, connection, activity):
if rec.sport_id not in sports_list: if rec.sport_id not in sports_list:
sports_list.append(rec.sport_id) sports_list.append(rec.sport_id)
for sport_id in sports_list: for sport_id in sports_list:
update_records(activity, sport_id, connection, session) update_records(activity.user_id, sport_id, connection, session)
class ActivitySegment(db.Model): class ActivitySegment(db.Model):

View File

@ -12,16 +12,13 @@ def get_records(auth_user_id):
"""Get all records for authenticated user""" """Get all records for authenticated user"""
records = Record.query.filter_by(user_id=auth_user_id)\ records = Record.query.filter_by(user_id=auth_user_id)\
.order_by( .order_by(
Record.sport_id, Record.sport_id.asc(),
Record.record_type, Record.record_type.asc(),
).all() ).all()
records_list = []
for record in records:
records_list.append(record.serialize())
response_object = { response_object = {
'status': 'success', 'status': 'success',
'data': { 'data': {
'records': records_list 'records': [record.serialize() for record in records]
} }
} }
return jsonify(response_object), 200 return jsonify(response_object), 200

View File

@ -129,7 +129,7 @@ def test_add_activity_zero_value(
def test_get_records_after_activities_post_and_patch( def test_get_records_after_activities_post_and_patch(
app, user_1, sport_1_cycling, sport_2_running app, user_1, sport_1_cycling
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -374,7 +374,6 @@ def test_get_records_after_activities_post_and_patch(
assert 8.4 == data['data']['records'][3]['value'] assert 8.4 == data['data']['records'][3]['value']
# delete activity 2 => AS and MS record update # delete activity 2 => AS and MS record update
client.delete( client.delete(
'/api/activities/2', '/api/activities/2',
headers=dict( headers=dict(
@ -427,7 +426,6 @@ def test_get_records_after_activities_post_and_patch(
# add an activity with the same data as activity 1 except with a later date # add an activity with the same data as activity 1 except with a later date
# => no change in record # => no change in record
client.post( client.post(
'/api/activities/no_gpx', '/api/activities/no_gpx',
content_type='application/json', content_type='application/json',
@ -594,3 +592,234 @@ def test_get_records_after_activities_post_and_patch(
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['records']) == 0 assert len(data['data']['records']) == 0
def test_get_records_after_sport_change(
app, user_1, sport_1_cycling, sport_2_running
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
title='Activity test 1'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=2,
duration=3600,
activity_date='2018-05-16 16:05',
distance=20,
title='Activity test 2'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
duration=3000,
activity_date='2018-05-17 17:05',
distance=3,
title='Activity test 3'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(dict(
sport_id=2,
duration=3000,
activity_date='2018-05-18 18:05',
distance=10,
title='Activity test 4'
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 8
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 1 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 7.0 == data['data']['records'][0]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 1 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 7.0 == data['data']['records'][1]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 1 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:00:00' == data['data']['records'][2]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 1 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][4]['activity_date'] # noqa
assert 1 == data['data']['records'][4]['user_id']
assert 2 == data['data']['records'][4]['sport_id']
assert 2 == data['data']['records'][4]['activity_id']
assert 'AS' == data['data']['records'][4]['record_type']
assert 20.0 == data['data']['records'][4]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][5]['activity_date'] # noqa
assert 1 == data['data']['records'][5]['user_id']
assert 2 == data['data']['records'][5]['sport_id']
assert 2 == data['data']['records'][5]['activity_id']
assert 'FD' == data['data']['records'][5]['record_type']
assert 20.0 == data['data']['records'][5]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][6]['activity_date'] # noqa
assert 1 == data['data']['records'][6]['user_id']
assert 2 == data['data']['records'][6]['sport_id']
assert 2 == data['data']['records'][6]['activity_id']
assert 'LD' == data['data']['records'][6]['record_type']
assert '1:00:00' == data['data']['records'][6]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][7]['activity_date'] # noqa
assert 1 == data['data']['records'][7]['user_id']
assert 2 == data['data']['records'][7]['sport_id']
assert 2 == data['data']['records'][7]['activity_id']
assert 'MS' == data['data']['records'][7]['record_type']
assert 20.0 == data['data']['records'][7]['value']
client.patch(
'/api/activities/2',
content_type='application/json',
data=json.dumps(dict(
sport_id=1,
)),
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
print(data['data']['records'])
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 8
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][0]['activity_date'] # noqa
assert 1 == data['data']['records'][0]['user_id']
assert 1 == data['data']['records'][0]['sport_id']
assert 2 == data['data']['records'][0]['activity_id']
assert 'AS' == data['data']['records'][0]['record_type']
assert 20.0 == data['data']['records'][0]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][1]['activity_date'] # noqa
assert 1 == data['data']['records'][1]['user_id']
assert 1 == data['data']['records'][1]['sport_id']
assert 2 == data['data']['records'][1]['activity_id']
assert 'FD' == data['data']['records'][1]['record_type']
assert 20.0 == data['data']['records'][1]['value']
assert 'Mon, 14 May 2018 14:05:00 GMT' == data['data']['records'][2]['activity_date'] # noqa
assert 1 == data['data']['records'][2]['user_id']
assert 1 == data['data']['records'][2]['sport_id']
assert 1 == data['data']['records'][2]['activity_id']
assert 'LD' == data['data']['records'][2]['record_type']
assert '1:00:00' == data['data']['records'][2]['value']
assert 'Wed, 16 May 2018 16:05:00 GMT' == data['data']['records'][3]['activity_date'] # noqa
assert 1 == data['data']['records'][3]['user_id']
assert 1 == data['data']['records'][3]['sport_id']
assert 2 == data['data']['records'][3]['activity_id']
assert 'MS' == data['data']['records'][3]['record_type']
assert 20.0 == data['data']['records'][3]['value']
assert 'Fri, 18 May 2018 18:05:00 GMT' == data['data']['records'][4]['activity_date'] # noqa
assert 1 == data['data']['records'][4]['user_id']
assert 2 == data['data']['records'][4]['sport_id']
assert 4 == data['data']['records'][4]['activity_id']
assert 'AS' == data['data']['records'][4]['record_type']
assert 12.0 == data['data']['records'][4]['value']
assert 'Fri, 18 May 2018 18:05:00 GMT' == data['data']['records'][5]['activity_date'] # noqa
assert 1 == data['data']['records'][5]['user_id']
assert 2 == data['data']['records'][5]['sport_id']
assert 4 == data['data']['records'][5]['activity_id']
assert 'FD' == data['data']['records'][5]['record_type']
assert 10.0 == data['data']['records'][5]['value']
assert 'Fri, 18 May 2018 18:05:00 GMT' == data['data']['records'][6]['activity_date'] # noqa
assert 1 == data['data']['records'][6]['user_id']
assert 2 == data['data']['records'][6]['sport_id']
assert 4 == data['data']['records'][6]['activity_id']
assert 'LD' == data['data']['records'][6]['record_type']
assert '0:50:00' == data['data']['records'][6]['value']
assert 'Fri, 18 May 2018 18:05:00 GMT' == data['data']['records'][7]['activity_date'] # noqa
assert 1 == data['data']['records'][7]['user_id']
assert 2 == data['data']['records'][7]['sport_id']
assert 4 == data['data']['records'][7]['activity_id']
assert 'MS' == data['data']['records'][7]['record_type']
assert 12.0 == data['data']['records'][7]['value']