|
| 1 | +/* |
| 2 | + * This file is part of wger Workout Manager <https://github.com/wger-project>. |
| 3 | + * Copyright (C) 2020, 2021 wger Team |
| 4 | + * |
| 5 | + * wger Workout Manager is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * wger Workout Manager is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +import 'dart:convert'; |
| 20 | +import 'package:flutter_test/flutter_test.dart'; |
| 21 | +import 'package:http/http.dart' as http; |
| 22 | +import 'package:mockito/mockito.dart'; |
| 23 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 24 | +import 'package:wger/models/workouts/repetition_unit.dart'; |
| 25 | +import 'package:wger/models/workouts/weight_unit.dart'; |
| 26 | +import 'package:wger/models/workouts/workout_plan.dart'; |
| 27 | +import 'package:wger/providers/exercises.dart'; |
| 28 | +import 'package:wger/providers/workout_plans.dart'; |
| 29 | +import '../measurements/measurement_provider_test.mocks.dart'; |
| 30 | +import '../other/base_provider_test.mocks.dart'; |
| 31 | +import '../utils.dart'; |
| 32 | + |
| 33 | +void main() { |
| 34 | + var repetitionUnitsResponse = '{"count":7,"next":null,"previous":null,' |
| 35 | + '"results":[' |
| 36 | + '{"id":6,"name":"Kilometers"},' |
| 37 | + '{"id":7,"name":"MaxReps"},' |
| 38 | + '{"id":5,"name":"Miles"},' |
| 39 | + '{"id":4,"name":"Minutes"},' |
| 40 | + '{"id":1,"name":"Repetitions"},' |
| 41 | + '{"id":3,"name":"Seconds"},' |
| 42 | + '{"id":2,"name":"UntilFailure"}]}'; |
| 43 | + |
| 44 | + var weightUnitsResponse = '{"count":6,"next":null,"previous":null,' |
| 45 | + '"results":[' |
| 46 | + '{"id":3,"name":"BodyWeight"},' |
| 47 | + '{"id":1,"name":"kg"},' |
| 48 | + '{"id":5,"name":"KilometersPerHour"},' |
| 49 | + '{"id":2,"name":"lb"},' |
| 50 | + '{"id":6,"name":"MilesPerHour"},' |
| 51 | + '{"id":4,"name":"Plates"}]}'; |
| 52 | + |
| 53 | + group('test workout plans provider', () { |
| 54 | + test('Test that fetch and set plans', () async { |
| 55 | + final client = MockClient(); |
| 56 | + |
| 57 | + final mockBaseProvider = MockWgerBaseProvider(); |
| 58 | + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); |
| 59 | + |
| 60 | + when(client.get( |
| 61 | + Uri.https('localhost', 'api/v2/workout/325397/'), |
| 62 | + headers: anyNamed('headers'), |
| 63 | + )).thenAnswer((_) async => http.Response( |
| 64 | + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', |
| 65 | + 200)); |
| 66 | + |
| 67 | + // Load the entries |
| 68 | + final WorkoutPlansProvider provider = |
| 69 | + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); |
| 70 | + |
| 71 | + final plan = await provider.fetchAndSetPlanSparse(325397); |
| 72 | + final plans = provider.getPlans(); |
| 73 | + |
| 74 | + // Check that everything is ok |
| 75 | + expect(plan, isA<WorkoutPlan>()); |
| 76 | + expect(plan.id, 325397); |
| 77 | + expect(plans.length, 1); |
| 78 | + }); |
| 79 | + |
| 80 | + test('Test that deletes workout plan', () async { |
| 81 | + final client = MockClient(); |
| 82 | + |
| 83 | + final mockBaseProvider = MockWgerBaseProvider(); |
| 84 | + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); |
| 85 | + |
| 86 | + when(client.get( |
| 87 | + Uri.https('localhost', 'api/v2/workout/325397/'), |
| 88 | + headers: anyNamed('headers'), |
| 89 | + )).thenAnswer((_) async => http.Response( |
| 90 | + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', |
| 91 | + 200)); |
| 92 | + |
| 93 | + when(client.delete( |
| 94 | + Uri.https('localhost', 'api/v2/workout/325397/'), |
| 95 | + headers: anyNamed('headers'), |
| 96 | + )).thenAnswer((_) async => http.Response( |
| 97 | + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', |
| 98 | + 200)); |
| 99 | + |
| 100 | + // Load the entries |
| 101 | + final WorkoutPlansProvider provider = |
| 102 | + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); |
| 103 | + |
| 104 | + await provider.fetchAndSetPlanSparse(325397); |
| 105 | + await provider.deleteWorkout(325397); |
| 106 | + final plans = provider.getPlans(); |
| 107 | + expect(plans.length, 0); |
| 108 | + }); |
| 109 | + |
| 110 | + test('Test that fetch and set repetition units for workout', () async { |
| 111 | + final client = MockClient(); |
| 112 | + |
| 113 | + final mockBaseProvider = MockWgerBaseProvider(); |
| 114 | + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); |
| 115 | + |
| 116 | + when(client.get( |
| 117 | + Uri.https('localhost', 'api/v2/setting-repetitionunit/'), |
| 118 | + headers: anyNamed('headers'), |
| 119 | + )).thenAnswer((_) async => http.Response(repetitionUnitsResponse, 200)); |
| 120 | + |
| 121 | + // Load the entries |
| 122 | + final WorkoutPlansProvider provider = |
| 123 | + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); |
| 124 | + |
| 125 | + await provider.fetchAndSetRepetitionUnits(); |
| 126 | + |
| 127 | + List<RepetitionUnit> repetitionUnits = provider.repetitionUnits; |
| 128 | + |
| 129 | + expect(repetitionUnits, isA<List<RepetitionUnit>>()); |
| 130 | + expect(repetitionUnits.length, 7); |
| 131 | + }); |
| 132 | + test('Test that fetch and set weight units for workout', () async { |
| 133 | + final client = MockClient(); |
| 134 | + |
| 135 | + final mockBaseProvider = MockWgerBaseProvider(); |
| 136 | + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); |
| 137 | + |
| 138 | + when(client.get( |
| 139 | + Uri.https('localhost', 'api/v2/setting-weightunit/'), |
| 140 | + headers: anyNamed('headers'), |
| 141 | + )).thenAnswer((_) async => http.Response(weightUnitsResponse, 200)); |
| 142 | + |
| 143 | + // Load the entries |
| 144 | + final WorkoutPlansProvider provider = |
| 145 | + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); |
| 146 | + |
| 147 | + await provider.fetchAndSetWeightUnits(); |
| 148 | + |
| 149 | + List<WeightUnit> weightUnits = provider.weightUnits; |
| 150 | + |
| 151 | + expect(weightUnits, isA<List<WeightUnit>>()); |
| 152 | + expect(weightUnits.length, 6); |
| 153 | + }); |
| 154 | + |
| 155 | + test('Test that fetch and set both type of units', () async { |
| 156 | + final client = MockClient(); |
| 157 | + |
| 158 | + final mockBaseProvider = MockWgerBaseProvider(); |
| 159 | + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); |
| 160 | + final prefs = await SharedPreferences.getInstance(); |
| 161 | + |
| 162 | + when(client.get( |
| 163 | + Uri.https('localhost', 'api/v2/setting-repetitionunit/'), |
| 164 | + headers: anyNamed('headers'), |
| 165 | + )).thenAnswer((_) async => http.Response(repetitionUnitsResponse, 200)); |
| 166 | + when(client.get( |
| 167 | + Uri.https('localhost', 'api/v2/setting-weightunit/'), |
| 168 | + headers: anyNamed('headers'), |
| 169 | + )).thenAnswer((_) async => http.Response(weightUnitsResponse, 200)); |
| 170 | + |
| 171 | + // Load the entries |
| 172 | + final WorkoutPlansProvider provider = |
| 173 | + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); |
| 174 | + |
| 175 | + await provider.fetchAndSetUnits(); |
| 176 | + |
| 177 | + dynamic prefsJson = jsonDecode(prefs.getString('workoutUnits')!); |
| 178 | + |
| 179 | + expect(prefsJson["repetitionUnits"].length, 7); |
| 180 | + expect(prefsJson["weightUnit"].length, 6); |
| 181 | + expect(true, DateTime.parse(prefsJson["date"]).isBefore(DateTime.now())); |
| 182 | + expect(true, DateTime.parse(prefsJson["expiresIn"]).isAfter(DateTime.now())); |
| 183 | + }); |
| 184 | + }); |
| 185 | +} |
0 commit comments