From 5d6a0953481684d1f7de42af68d3c26b5df9e4f0 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 12 May 2018 19:13:59 +0200 Subject: [PATCH] API: db models tests --- mpwo_api/mpwo_api/activities/models.py | 2 +- ..._0_get.py => test_activities_api_0_get.py} | 0 ..._post.py => test_activities_api_1_post.py} | 0 ...atch.py => test_activities_api_2_patch.py} | 0 ...ete.py => test_activities_api_3_delete.py} | 0 .../mpwo_api/tests/test_activities_model.py | 23 +++++++++++++++++ .../tests/{test_auth.py => test_auth_api.py} | 0 .../{test_records.py => test_records_api.py} | 0 mpwo_api/mpwo_api/tests/test_records_model.py | 25 +++++++++++++++++++ .../{test_sports.py => test_sports_api.py} | 0 mpwo_api/mpwo_api/tests/test_sports_model.py | 10 ++++++++ .../{test_users.py => test_users_api.py} | 0 mpwo_api/mpwo_api/tests/test_users_model.py | 7 ++++++ 13 files changed, 66 insertions(+), 1 deletion(-) rename mpwo_api/mpwo_api/tests/{test_activities_0_get.py => test_activities_api_0_get.py} (100%) rename mpwo_api/mpwo_api/tests/{test_activities_1_post.py => test_activities_api_1_post.py} (100%) rename mpwo_api/mpwo_api/tests/{test_activities_2_patch.py => test_activities_api_2_patch.py} (100%) rename mpwo_api/mpwo_api/tests/{test_activities_3_delete.py => test_activities_api_3_delete.py} (100%) create mode 100644 mpwo_api/mpwo_api/tests/test_activities_model.py rename mpwo_api/mpwo_api/tests/{test_auth.py => test_auth_api.py} (100%) rename mpwo_api/mpwo_api/tests/{test_records.py => test_records_api.py} (100%) create mode 100644 mpwo_api/mpwo_api/tests/test_records_model.py rename mpwo_api/mpwo_api/tests/{test_sports.py => test_sports_api.py} (100%) create mode 100644 mpwo_api/mpwo_api/tests/test_sports_model.py rename mpwo_api/mpwo_api/tests/{test_users.py => test_users_api.py} (100%) create mode 100644 mpwo_api/mpwo_api/tests/test_users_model.py diff --git a/mpwo_api/mpwo_api/activities/models.py b/mpwo_api/mpwo_api/activities/models.py index 670b3949..1c198896 100644 --- a/mpwo_api/mpwo_api/activities/models.py +++ b/mpwo_api/mpwo_api/activities/models.py @@ -72,7 +72,7 @@ class Activity(db.Model): def __str__(self): return str(self.sports.label) + \ - " - " + self.activity_date.strftime('%Y-%m-%d') + " - " + self.activity_date.strftime('%Y-%m-%d %H:%M:%S') def __init__(self, user_id, sport_id, activity_date, distance, duration): self.user_id = user_id diff --git a/mpwo_api/mpwo_api/tests/test_activities_0_get.py b/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_activities_0_get.py rename to mpwo_api/mpwo_api/tests/test_activities_api_0_get.py diff --git a/mpwo_api/mpwo_api/tests/test_activities_1_post.py b/mpwo_api/mpwo_api/tests/test_activities_api_1_post.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_activities_1_post.py rename to mpwo_api/mpwo_api/tests/test_activities_api_1_post.py diff --git a/mpwo_api/mpwo_api/tests/test_activities_2_patch.py b/mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_activities_2_patch.py rename to mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py diff --git a/mpwo_api/mpwo_api/tests/test_activities_3_delete.py b/mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_activities_3_delete.py rename to mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py diff --git a/mpwo_api/mpwo_api/tests/test_activities_model.py b/mpwo_api/mpwo_api/tests/test_activities_model.py new file mode 100644 index 00000000..511bf2a9 --- /dev/null +++ b/mpwo_api/mpwo_api/tests/test_activities_model.py @@ -0,0 +1,23 @@ +import datetime + +from mpwo_api.tests.utils import add_activity, add_sport, add_user + + +def test_add_activity(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + activity = add_activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa + distance=10, + duration=datetime.timedelta(seconds=1024) + ) + + assert 1 == activity.id + assert 1 == activity.user_id + assert 1 == activity.sport_id + assert '2018-01-01 13:36:00' == str(activity.activity_date) + assert 10.0 == float(activity.distance) + assert '0:17:04' == str(activity.duration) + assert 'cycling - 2018-01-01 13:36:00' == str(activity) diff --git a/mpwo_api/mpwo_api/tests/test_auth.py b/mpwo_api/mpwo_api/tests/test_auth_api.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_auth.py rename to mpwo_api/mpwo_api/tests/test_auth_api.py diff --git a/mpwo_api/mpwo_api/tests/test_records.py b/mpwo_api/mpwo_api/tests/test_records_api.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_records.py rename to mpwo_api/mpwo_api/tests/test_records_api.py diff --git a/mpwo_api/mpwo_api/tests/test_records_model.py b/mpwo_api/mpwo_api/tests/test_records_model.py new file mode 100644 index 00000000..3ff07098 --- /dev/null +++ b/mpwo_api/mpwo_api/tests/test_records_model.py @@ -0,0 +1,25 @@ +import datetime + +from mpwo_api.tests.utils import add_activity, add_record, add_sport, add_user + + +def test_add_record(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + + activity = add_activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa + distance=10, + duration=datetime.timedelta(seconds=1024) + ) + record = add_record(1, 1, activity, 'LD') + + assert 1 == record.id + assert 1 == record.user_id + assert 1 == record.sport_id + assert 1 == record.activity_id + assert 'LD' == record.record_type + assert '2018-01-01 13:36:00' == str(record.activity_date) + assert 'cycling - LD - 2018-01-01' == str(record) diff --git a/mpwo_api/mpwo_api/tests/test_sports.py b/mpwo_api/mpwo_api/tests/test_sports_api.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_sports.py rename to mpwo_api/mpwo_api/tests/test_sports_api.py diff --git a/mpwo_api/mpwo_api/tests/test_sports_model.py b/mpwo_api/mpwo_api/tests/test_sports_model.py new file mode 100644 index 00000000..ef1adf18 --- /dev/null +++ b/mpwo_api/mpwo_api/tests/test_sports_model.py @@ -0,0 +1,10 @@ +from mpwo_api.tests.utils import add_sport, add_user + + +def test_add_sport(app): + add_user('test', 'test@test.com', '12345678') + sport = add_sport('cycling') + + assert 1 == sport.id + assert 'cycling' == sport.label + assert 'cycling' == str(sport) diff --git a/mpwo_api/mpwo_api/tests/test_users.py b/mpwo_api/mpwo_api/tests/test_users_api.py similarity index 100% rename from mpwo_api/mpwo_api/tests/test_users.py rename to mpwo_api/mpwo_api/tests/test_users_api.py diff --git a/mpwo_api/mpwo_api/tests/test_users_model.py b/mpwo_api/mpwo_api/tests/test_users_model.py new file mode 100644 index 00000000..d131ffc2 --- /dev/null +++ b/mpwo_api/mpwo_api/tests/test_users_model.py @@ -0,0 +1,7 @@ +from mpwo_api.tests.utils import add_user + + +def test_add_user(app): + user = add_user('test', 'test@test.com', '12345678') + assert '' == str(user) +