FitTrackee/fittrackee/tests/workouts/test_sports_model.py

135 lines
4.8 KiB
Python
Raw Normal View History

2021-01-02 19:28:03 +01:00
from typing import Dict, Optional
2021-01-20 16:47:00 +01:00
from flask import Flask
from fittrackee import db
from fittrackee.users.models import User, UserSportPreference
from fittrackee.workouts.models import Sport, Workout
2021-01-02 19:28:03 +01:00
2020-05-10 15:55:56 +02:00
class TestSportModel:
@staticmethod
2021-01-02 19:28:03 +01:00
def assert_sport_model(
sport: Sport, is_admin: Optional[bool] = False
) -> Dict:
2020-05-10 15:55:56 +02:00
assert 1 == sport.id
assert 'Cycling' == sport.label
assert '<Sport \'Cycling\'>' == str(sport)
2018-05-12 19:13:59 +02:00
serialized_sport = sport.serialize(is_admin=is_admin)
2020-05-10 15:55:56 +02:00
assert 1 == serialized_sport['id']
assert 'Cycling' == serialized_sport['label']
assert serialized_sport['is_active'] is True
assert serialized_sport['is_active_for_user'] is True
assert serialized_sport['color'] is None
assert serialized_sport['stopped_speed_threshold'] == 1
2020-05-10 15:55:56 +02:00
return serialized_sport
2021-01-02 19:28:03 +01:00
def test_sport_model(self, app: Flask, sport_1_cycling: Sport) -> None:
2020-05-10 15:55:56 +02:00
serialized_sport = self.assert_sport_model(sport_1_cycling)
assert 'has_workouts' not in serialized_sport
def test_sport_model_with_workout(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
sport_1_cycling: Sport,
user_1: User,
workout_cycling_user_1: Workout,
2021-01-02 19:28:03 +01:00
) -> None:
2020-05-10 15:55:56 +02:00
serialized_sport = self.assert_sport_model(sport_1_cycling)
assert 'has_workouts' not in serialized_sport
2018-05-12 19:13:59 +02:00
def test_sport_model_with_workout_as_admin(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
sport_1_cycling: Sport,
user_1: User,
workout_cycling_user_1: Workout,
2021-01-02 19:28:03 +01:00
) -> None:
2020-05-10 15:55:56 +02:00
serialized_sport = self.assert_sport_model(sport_1_cycling, True)
assert serialized_sport['has_workouts'] is True
class TestSportModelWithPreferences:
def test_sport_model_with_color_preference(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference,
) -> None:
user_sport_1_preference.color = '#00000'
serialized_sport = sport_1_cycling.serialize(
sport_preferences=user_sport_1_preference.serialize()
)
assert serialized_sport['id'] == 1
assert serialized_sport['label'] == 'Cycling'
assert serialized_sport['is_active'] is True
assert serialized_sport['is_active_for_user'] is True
assert serialized_sport['color'] == '#00000'
assert serialized_sport['stopped_speed_threshold'] == 1
assert 'has_workouts' not in serialized_sport
def test_sport_model_with_is_active_preference(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference,
) -> None:
user_sport_1_preference.is_active = False
serialized_sport = sport_1_cycling.serialize(
sport_preferences=user_sport_1_preference.serialize()
)
assert serialized_sport['id'] == 1
assert serialized_sport['label'] == 'Cycling'
assert serialized_sport['is_active'] is True
assert serialized_sport['is_active_for_user'] is False
assert serialized_sport['color'] is None
assert serialized_sport['stopped_speed_threshold'] == 1
assert 'has_workouts' not in serialized_sport
def test_inactive_sport_model_with_is_active_preference(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference,
) -> None:
sport_1_cycling.is_active = False
user_sport_1_preference.is_active = True
serialized_sport = sport_1_cycling.serialize(
sport_preferences=user_sport_1_preference.serialize()
)
assert serialized_sport['id'] == 1
assert serialized_sport['label'] == 'Cycling'
assert serialized_sport['is_active'] is False
assert serialized_sport['is_active_for_user'] is False
assert serialized_sport['color'] is None
assert serialized_sport['stopped_speed_threshold'] == 1
assert 'has_workouts' not in serialized_sport
def test_sport_model_with_threshold_preference(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference,
) -> None:
user_sport_1_preference.stopped_speed_threshold = 0.5
db.session.commit()
serialized_sport = sport_1_cycling.serialize(
sport_preferences=user_sport_1_preference.serialize()
)
assert serialized_sport['id'] == 1
assert serialized_sport['label'] == 'Cycling'
assert serialized_sport['is_active'] is True
assert serialized_sport['is_active_for_user'] is True
assert serialized_sport['color'] is None
assert serialized_sport['stopped_speed_threshold'] == 0.5
assert 'has_workouts' not in serialized_sport