Skip to content

Commit a98a15e

Browse files
authored
Merge pull request #289 from bujdy/tests
Gallery and workout plans tests
2 parents 2f42e7b + cd13788 commit a98a15e

File tree

5 files changed

+275
-1
lines changed

5 files changed

+275
-1
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Ogundoyin Toluwani - <https://github.com/Tolu007>
1616
- Nenza Nurfirmansyah - <https://github.com/nenzan>
1717
- Florian Schmitz - <https://github.com/floodoo>
18+
- Adam Bujdoš - <https://github.com/bujdy>
1819
- Aman Negi - <https://github.com/AmanNegi>
1920
- Sandi Milohanic - <https://github.com/sandimilohanic>
2021

lib/providers/gallery.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class GalleryProvider extends WgerBaseProvider with ChangeNotifier {
102102
}
103103

104104
Future<void> deleteImage(gallery.Image image) async {
105-
await deleteRequest(_galleryUrlPath, image.id!);
105+
var response = await deleteRequest(_galleryUrlPath, image.id!);
106106
images.removeWhere((element) => element.id == image.id);
107107

108108
notifyListeners();

lib/providers/workout_plans.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import 'dart:convert';
2020
import 'dart:developer' as dev;
21+
import 'dart:developer';
2122

2223
import 'package:flutter/material.dart';
2324
import 'package:http/http.dart' as http;
@@ -90,6 +91,10 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier {
9091
return _repetitionUnit.firstWhere((element) => element.id == DEFAULT_REPETITION_UNIT);
9192
}
9293

94+
List<WorkoutPlan> getPlans() {
95+
return _workoutPlans;
96+
}
97+
9398
WorkoutPlan findById(int id) {
9499
return _workoutPlans.firstWhere((workoutPlan) => workoutPlan.id == id);
95100
}
@@ -360,6 +365,7 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier {
360365
'weightUnit': _weightUnits.map((e) => e.toJson()).toList(),
361366
};
362367
prefs.setString('workoutUnits', json.encode(exerciseData));
368+
log(json.encode(exerciseData));
363369
notifyListeners();
364370
}
365371

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 'package:flutter_test/flutter_test.dart';
20+
import 'package:http/http.dart' as http;
21+
import 'package:mockito/mockito.dart';
22+
import 'package:wger/providers/gallery.dart';
23+
import '../other/base_provider_test.mocks.dart';
24+
import 'package:wger/models/gallery/image.dart' as gallery;
25+
import '../utils.dart';
26+
27+
void main() {
28+
group('test gallery provider', () {
29+
test('Test that fetch and set gallery', () async {
30+
final client = MockClient();
31+
32+
when(client.get(
33+
Uri.https('localhost', 'api/v2/gallery/'),
34+
headers: anyNamed('headers'),
35+
)).thenAnswer((_) async => http.Response(
36+
'{"count":1,"next":null,"previous":null,"results":['
37+
'{"id":58,'
38+
'"date":"2022-01-09",'
39+
'"image":"https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg",'
40+
'"description":"eggsaddjujuit\'ddayhadIforcanview",'
41+
'"height":1280,"width":960}]}',
42+
200));
43+
44+
GalleryProvider galleryProvider = GalleryProvider(testAuthProvider, [], client);
45+
46+
await galleryProvider.fetchAndSetGallery();
47+
48+
// Check that everything is ok
49+
expect(galleryProvider.images.length, 1);
50+
});
51+
52+
test('Test that delete gallery photo', () async {
53+
final client = MockClient();
54+
55+
when(client.delete(
56+
Uri.https('localhost', 'api/v2/gallery/58/'),
57+
headers: anyNamed('headers'),
58+
)).thenAnswer((_) async => http.Response(
59+
'{"id":58,'
60+
'"date":"2022-01-09",'
61+
'"image":"https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg",'
62+
'"description":"eggsaddjujuit\'ddayhadIforcanview",'
63+
'"height":1280,"width":960}',
64+
200));
65+
66+
GalleryProvider galleryProvider = GalleryProvider(testAuthProvider, [], client);
67+
68+
gallery.Image image = gallery.Image(
69+
id: 58,
70+
date: DateTime(2022, 01, 09),
71+
url: "https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg",
72+
description: "eggsaddjujuit\'ddayhadIforcanview");
73+
74+
galleryProvider.images.add(image);
75+
76+
await galleryProvider.deleteImage(image);
77+
78+
// Check that everything is ok
79+
expect(galleryProvider.images.length, 0);
80+
});
81+
});
82+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)