diff --git a/AUTHORS.md b/AUTHORS.md index 577861e81..bc73c98be 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -15,6 +15,7 @@ - Ogundoyin Toluwani - - Nenza Nurfirmansyah - - Florian Schmitz - +- Adam Bujdoš - - Aman Negi - - Sandi Milohanic - diff --git a/lib/providers/gallery.dart b/lib/providers/gallery.dart index d15bf32dc..15dc4090f 100644 --- a/lib/providers/gallery.dart +++ b/lib/providers/gallery.dart @@ -102,7 +102,7 @@ class GalleryProvider extends WgerBaseProvider with ChangeNotifier { } Future deleteImage(gallery.Image image) async { - await deleteRequest(_galleryUrlPath, image.id!); + var response = await deleteRequest(_galleryUrlPath, image.id!); images.removeWhere((element) => element.id == image.id); notifyListeners(); diff --git a/lib/providers/workout_plans.dart b/lib/providers/workout_plans.dart index 95712cb8e..e27c3b6a1 100644 --- a/lib/providers/workout_plans.dart +++ b/lib/providers/workout_plans.dart @@ -18,6 +18,7 @@ import 'dart:convert'; import 'dart:developer' as dev; +import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; @@ -90,6 +91,10 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier { return _repetitionUnit.firstWhere((element) => element.id == DEFAULT_REPETITION_UNIT); } + List getPlans() { + return _workoutPlans; + } + WorkoutPlan findById(int id) { return _workoutPlans.firstWhere((workoutPlan) => workoutPlan.id == id); } @@ -360,6 +365,7 @@ class WorkoutPlansProvider extends WgerBaseProvider with ChangeNotifier { 'weightUnit': _weightUnits.map((e) => e.toJson()).toList(), }; prefs.setString('workoutUnits', json.encode(exerciseData)); + log(json.encode(exerciseData)); notifyListeners(); } diff --git a/test/gallery/gallery_provider_test.dart b/test/gallery/gallery_provider_test.dart new file mode 100644 index 000000000..c74b1b426 --- /dev/null +++ b/test/gallery/gallery_provider_test.dart @@ -0,0 +1,82 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020, 2021 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as http; +import 'package:mockito/mockito.dart'; +import 'package:wger/providers/gallery.dart'; +import '../other/base_provider_test.mocks.dart'; +import 'package:wger/models/gallery/image.dart' as gallery; +import '../utils.dart'; + +void main() { + group('test gallery provider', () { + test('Test that fetch and set gallery', () async { + final client = MockClient(); + + when(client.get( + Uri.https('localhost', 'api/v2/gallery/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response( + '{"count":1,"next":null,"previous":null,"results":[' + '{"id":58,' + '"date":"2022-01-09",' + '"image":"https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg",' + '"description":"eggsaddjujuit\'ddayhadIforcanview",' + '"height":1280,"width":960}]}', + 200)); + + GalleryProvider galleryProvider = GalleryProvider(testAuthProvider, [], client); + + await galleryProvider.fetchAndSetGallery(); + + // Check that everything is ok + expect(galleryProvider.images.length, 1); + }); + + test('Test that delete gallery photo', () async { + final client = MockClient(); + + when(client.delete( + Uri.https('localhost', 'api/v2/gallery/58/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response( + '{"id":58,' + '"date":"2022-01-09",' + '"image":"https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg",' + '"description":"eggsaddjujuit\'ddayhadIforcanview",' + '"height":1280,"width":960}', + 200)); + + GalleryProvider galleryProvider = GalleryProvider(testAuthProvider, [], client); + + gallery.Image image = gallery.Image( + id: 58, + date: DateTime(2022, 01, 09), + url: "https://wger.de/media/gallery/170335/d2b9c9e0-d541-41ae-8786-a2ab459e3538.jpg", + description: "eggsaddjujuit\'ddayhadIforcanview"); + + galleryProvider.images.add(image); + + await galleryProvider.deleteImage(image); + + // Check that everything is ok + expect(galleryProvider.images.length, 0); + }); + }); +} diff --git a/test/workout/workout_provider_test.dart b/test/workout/workout_provider_test.dart new file mode 100644 index 000000000..3483472cc --- /dev/null +++ b/test/workout/workout_provider_test.dart @@ -0,0 +1,185 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020, 2021 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'dart:convert'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as http; +import 'package:mockito/mockito.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:wger/models/workouts/repetition_unit.dart'; +import 'package:wger/models/workouts/weight_unit.dart'; +import 'package:wger/models/workouts/workout_plan.dart'; +import 'package:wger/providers/exercises.dart'; +import 'package:wger/providers/workout_plans.dart'; +import '../measurements/measurement_provider_test.mocks.dart'; +import '../other/base_provider_test.mocks.dart'; +import '../utils.dart'; + +void main() { + var repetitionUnitsResponse = '{"count":7,"next":null,"previous":null,' + '"results":[' + '{"id":6,"name":"Kilometers"},' + '{"id":7,"name":"MaxReps"},' + '{"id":5,"name":"Miles"},' + '{"id":4,"name":"Minutes"},' + '{"id":1,"name":"Repetitions"},' + '{"id":3,"name":"Seconds"},' + '{"id":2,"name":"UntilFailure"}]}'; + + var weightUnitsResponse = '{"count":6,"next":null,"previous":null,' + '"results":[' + '{"id":3,"name":"BodyWeight"},' + '{"id":1,"name":"kg"},' + '{"id":5,"name":"KilometersPerHour"},' + '{"id":2,"name":"lb"},' + '{"id":6,"name":"MilesPerHour"},' + '{"id":4,"name":"Plates"}]}'; + + group('test workout plans provider', () { + test('Test that fetch and set plans', () async { + final client = MockClient(); + + final mockBaseProvider = MockWgerBaseProvider(); + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); + + when(client.get( + Uri.https('localhost', 'api/v2/workout/325397/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response( + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', + 200)); + + // Load the entries + final WorkoutPlansProvider provider = + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); + + final plan = await provider.fetchAndSetPlanSparse(325397); + final plans = provider.getPlans(); + + // Check that everything is ok + expect(plan, isA()); + expect(plan.id, 325397); + expect(plans.length, 1); + }); + + test('Test that deletes workout plan', () async { + final client = MockClient(); + + final mockBaseProvider = MockWgerBaseProvider(); + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); + + when(client.get( + Uri.https('localhost', 'api/v2/workout/325397/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response( + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', + 200)); + + when(client.delete( + Uri.https('localhost', 'api/v2/workout/325397/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response( + '{"id": 325397,"name": "Test workout","creation_date": "2022-10-10","description": "It is test workout to fix a bug."}', + 200)); + + // Load the entries + final WorkoutPlansProvider provider = + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); + + await provider.fetchAndSetPlanSparse(325397); + await provider.deleteWorkout(325397); + final plans = provider.getPlans(); + expect(plans.length, 0); + }); + + test('Test that fetch and set repetition units for workout', () async { + final client = MockClient(); + + final mockBaseProvider = MockWgerBaseProvider(); + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); + + when(client.get( + Uri.https('localhost', 'api/v2/setting-repetitionunit/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response(repetitionUnitsResponse, 200)); + + // Load the entries + final WorkoutPlansProvider provider = + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); + + await provider.fetchAndSetRepetitionUnits(); + + List repetitionUnits = provider.repetitionUnits; + + expect(repetitionUnits, isA>()); + expect(repetitionUnits.length, 7); + }); + test('Test that fetch and set weight units for workout', () async { + final client = MockClient(); + + final mockBaseProvider = MockWgerBaseProvider(); + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); + + when(client.get( + Uri.https('localhost', 'api/v2/setting-weightunit/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response(weightUnitsResponse, 200)); + + // Load the entries + final WorkoutPlansProvider provider = + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); + + await provider.fetchAndSetWeightUnits(); + + List weightUnits = provider.weightUnits; + + expect(weightUnits, isA>()); + expect(weightUnits.length, 6); + }); + + test('Test that fetch and set both type of units', () async { + final client = MockClient(); + + final mockBaseProvider = MockWgerBaseProvider(); + final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider); + final prefs = await SharedPreferences.getInstance(); + + when(client.get( + Uri.https('localhost', 'api/v2/setting-repetitionunit/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response(repetitionUnitsResponse, 200)); + when(client.get( + Uri.https('localhost', 'api/v2/setting-weightunit/'), + headers: anyNamed('headers'), + )).thenAnswer((_) async => http.Response(weightUnitsResponse, 200)); + + // Load the entries + final WorkoutPlansProvider provider = + WorkoutPlansProvider(testAuthProvider, testExercisesProvider, [], client); + + await provider.fetchAndSetUnits(); + + dynamic prefsJson = jsonDecode(prefs.getString('workoutUnits')!); + + expect(prefsJson["repetitionUnits"].length, 7); + expect(prefsJson["weightUnit"].length, 6); + expect(true, DateTime.parse(prefsJson["date"]).isBefore(DateTime.now())); + expect(true, DateTime.parse(prefsJson["expiresIn"]).isAfter(DateTime.now())); + }); + }); +}