API: delete a sport - update

This commit is contained in:
Sam 2018-05-09 19:21:46 +02:00
parent 58b69878c4
commit ff45b5982e
2 changed files with 47 additions and 2 deletions

View File

@ -157,7 +157,15 @@ def delete_sport(auth_user_id, sport_id):
} }
} }
code = 404 code = 404
except (exc.IntegrityError, exc.OperationalError, ValueError) as e: except exc.IntegrityError as e:
db.session.rollback()
appLog.error(e)
response_object = {
'status': 'error',
'message': 'Error. Associated activities exist.'
}
code = 500
except (exc.OperationalError, ValueError) as e:
db.session.rollback() db.session.rollback()
appLog.error(e) appLog.error(e)
response_object = { response_object = {

View File

@ -1,6 +1,7 @@
import datetime
import json import json
from mpwo_api.tests.utils import add_admin, add_sport, add_user from mpwo_api.tests.utils import add_activity, add_admin, add_sport, add_user
def test_get_all_sports(app): def test_get_all_sports(app):
@ -249,3 +250,39 @@ def test_delete_a_sport_not_admin(app):
assert response.status_code == 401 assert response.status_code == 401
assert 'error' in data['status'] assert 'error' in data['status']
assert 'You do not have permissions.' in data['message'] assert 'You do not have permissions.' in data['message']
def test_delete_a_sport_with_an_activity(app):
add_admin()
add_sport('cycling')
add_activity(
1,
1,
datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'),
datetime.timedelta(seconds=1024)
)
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='admin@example.com',
password='12345678'
)),
content_type='application/json'
)
response = client.delete(
'/api/sports/1',
content_type='application/json',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error. Associated activities exist.' in data['message']