API - fix order by for PGSQL15 support and update tests

This commit is contained in:
Sam 2022-10-26 18:05:22 +02:00
parent ef1c96d1f9
commit b86a1b93f9
5 changed files with 54 additions and 69 deletions

View File

@ -154,7 +154,9 @@ def seven_workouts_user_1() -> List[Workout]:
workout_4 = Workout(
user_id=1,
sport_id=1,
workout_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'),
workout_date=datetime.datetime.strptime(
'23/02/2018 10:00', '%d/%m/%Y %H:%M'
),
distance=1,
duration=datetime.timedelta(seconds=600),
)

View File

@ -370,7 +370,7 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
seven_workouts_user_1[5].serialize()
)
assert data['data']['workouts'][2] == jsonify_dict(
seven_workouts_user_1[4].serialize()
seven_workouts_user_1[3].serialize()
)
assert data['pagination'] == {
'has_next': True,
@ -443,7 +443,7 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
== data['data']['workouts'][0]['workout_date']
)
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
'Fri, 23 Feb 2018 10:00:00 GMT'
== data['data']['workouts'][4]['workout_date']
)
assert data['pagination'] == {
@ -642,11 +642,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
assert len(data['data']['workouts']) == 2
assert 'creation_date' in data['data']['workouts'][0]
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
'Fri, 23 Feb 2018 10:00:00 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert '0:16:40' == data['data']['workouts'][0]['duration']
assert 'creation_date' in data['data']['workouts'][1]
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date']
@ -849,7 +847,7 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
assert 'success' in data['status']
assert len(data['data']['workouts']) == 1
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
'Fri, 23 Feb 2018 10:00:00 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert data['pagination'] == {

View File

@ -1019,8 +1019,43 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
assert response.status_code == 201
assert 'created' in data['status']
assert len(data['data']['workouts']) == 3
assert 'just a workout' == data['data']['workouts'][0]['title']
assert_workout_data_with_gpx(data)
assert 'creation_date' in data['data']['workouts'][0]
assert (
'Tue, 13 Mar 2018 12:44:45 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert 'test' == data['data']['workouts'][0]['user']
assert 1 == data['data']['workouts'][0]['sport_id']
assert '0:04:10' == data['data']['workouts'][0]['duration']
assert data['data']['workouts'][0]['ascent'] == 0.4
assert data['data']['workouts'][0]['ave_speed'] == 4.61
assert data['data']['workouts'][0]['descent'] == 23.4
assert data['data']['workouts'][0]['distance'] == 0.32
assert data['data']['workouts'][0]['max_alt'] == 998.0
assert data['data']['workouts'][0]['max_speed'] == 5.12
assert data['data']['workouts'][0]['min_alt'] == 975.0
assert data['data']['workouts'][0]['moving'] == '0:04:10'
assert data['data']['workouts'][0]['pauses'] is None
assert data['data']['workouts'][0]['with_gpx'] is True
assert data['data']['workouts'][0]['map'] is not None
assert data['data']['workouts'][0]['weather_start'] is None
assert data['data']['workouts'][0]['weather_end'] is None
assert data['data']['workouts'][0]['notes'] is None
assert len(data['data']['workouts'][0]['segments']) == 1
segment = data['data']['workouts'][0]['segments'][0]
assert segment['workout_id'] == data['data']['workouts'][0]['id']
assert segment['segment_id'] == 0
assert segment['duration'] == '0:04:10'
assert segment['ascent'] == 0.4
assert segment['ave_speed'] == 4.61
assert segment['descent'] == 23.4
assert segment['distance'] == 0.32
assert segment['max_alt'] == 998.0
assert segment['max_speed'] == 5.12
assert segment['min_alt'] == 975.0
assert segment['moving'] == '0:04:10'
assert segment['pauses'] is None
def test_it_returns_400_if_folder_is_present_in_zip_archive(
self, app: Flask, user_1: User, sport_1_cycling: Sport

View File

@ -3,7 +3,7 @@ import shutil
from typing import Any, Dict, Tuple, Union
from flask import Blueprint, current_app, request, send_file
from sqlalchemy import exc
from sqlalchemy import asc, desc, exc
from fittrackee import db, limiter
from fittrackee.emails.tasks import (
@ -191,45 +191,14 @@ def get_users(auth_user: User) -> Dict:
per_page = int(params.get('per_page', USER_PER_PAGE))
if per_page > 50:
per_page = 50
order_by = params.get('order_by')
user_column = getattr(User, params.get('order_by', 'username'))
order = params.get('order', 'asc')
query = params.get('q')
users_pagination = (
User.query.filter(
User.username.ilike('%' + query + '%') if query else True,
)
.order_by(
User.workouts_count.asc() # type: ignore
if order_by == 'workouts_count' and order == 'asc'
else True,
User.workouts_count.desc() # type: ignore
if order_by == 'workouts_count' and order == 'desc'
else True,
User.username.asc()
if order_by == 'username' and order == 'asc'
else True,
User.username.desc()
if order_by == 'username' and order == 'desc'
else True,
User.created_at.asc()
if order_by == 'created_at' and order == 'asc'
else True,
User.created_at.desc()
if order_by == 'created_at' and order == 'desc'
else True,
User.admin.asc()
if order_by == 'admin' and order == 'asc'
else True,
User.admin.desc()
if order_by == 'admin' and order == 'desc'
else True,
User.is_active.asc()
if order_by == 'is_active' and order == 'asc'
else True,
User.is_active.desc()
if order_by == 'is_active' and order == 'desc'
else True,
)
.order_by(asc(user_column) if order == 'asc' else desc(user_column))
.paginate(page, per_page, False)
)
users = users_pagination.items

View File

@ -12,7 +12,7 @@ from flask import (
request,
send_from_directory,
)
from sqlalchemy import exc
from sqlalchemy import asc, desc, exc
from werkzeug.exceptions import NotFound, RequestEntityTooLarge
from werkzeug.utils import secure_filename
@ -221,7 +221,9 @@ def get_workouts(auth_user: User) -> Union[Dict, HttpResponse]:
ave_speed_to = params.get('ave_speed_to')
max_speed_from = params.get('max_speed_from')
max_speed_to = params.get('max_speed_to')
order_by = params.get('order_by', 'workout_date')
workout_column = getattr(
Workout, params.get('order_by', 'workout_date')
)
order = params.get('order', 'desc')
sport_id = params.get('sport_id')
per_page = int(params.get('per_page', DEFAULT_WORKOUTS_PER_PAGE))
@ -261,30 +263,9 @@ def get_workouts(auth_user: User) -> Union[Dict, HttpResponse]:
else True,
)
.order_by(
Workout.ave_speed.asc()
if order_by == 'ave_speed' and order == 'asc'
else True,
Workout.ave_speed.desc()
if order_by == 'ave_speed' and order == 'desc'
else True,
Workout.distance.asc()
if order_by == 'distance' and order == 'asc'
else True,
Workout.distance.desc()
if order_by == 'distance' and order == 'desc'
else True,
Workout.moving.asc()
if order_by == 'duration' and order == 'asc'
else True,
Workout.moving.desc()
if order_by == 'duration' and order == 'desc'
else True,
Workout.workout_date.asc()
if order_by == 'workout_date' and order == 'asc'
else True,
Workout.workout_date.desc()
if order_by == 'workout_date' and order == 'desc'
else True,
asc(workout_column)
if order == 'asc'
else desc(workout_column),
)
.paginate(page, per_page, False)
)