Move some stuff to utils.dart

This commit is contained in:
Roland Geider
2020-12-01 22:12:53 +01:00
parent c9b6e16067
commit a69f8045ed
3 changed files with 19 additions and 15 deletions

View File

@@ -16,7 +16,7 @@ class WeightEntry {
final DateTime date;
WeightEntry({
@required this.id,
this.id,
@required this.weight,
@required this.date,
});

11
test/utils.dart Normal file
View File

@@ -0,0 +1,11 @@
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import 'package:wger/providers/auth.dart';
// Test Auth provider
final Auth testAuth = Auth()
..token = 'FooBar'
..serverUrl = 'https://localhost';
// Mocked HTTP client
class MockClient extends Mock implements http.Client {}

View File

@@ -2,16 +2,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import 'package:wger/models/body_weight/weight_entry.dart';
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/body_weight.dart';
// Create a MockClient
class MockClient extends Mock implements http.Client {}
// Test Auth provider
final Auth auth = Auth()
..token = 'FooBar'
..serverUrl = 'https://localhost';
import 'utils.dart';
void main() {
group('fetchPost', () {
@@ -21,7 +14,7 @@ void main() {
// Mock the server response
when(client.get(
'https://localhost/api/v2/weightentry/',
headers: <String, String>{'Authorization': 'Token ${auth.token}'},
headers: <String, String>{'Authorization': 'Token ${testAuth.token}'},
)).thenAnswer((_) async => http.Response(
'{"results": [{"id": 1, "date": "2021-01-01", "weight": "80.00"}, '
'{"id": 2, "date": "2021-01-10", "weight": "99"},'
@@ -29,7 +22,7 @@ void main() {
200));
// Load the entries
BodyWeight provider = BodyWeight(auth, []);
BodyWeight provider = BodyWeight(testAuth, []);
await provider.fetchAndSetEntries(client: client);
// Check that everything is ok
@@ -43,7 +36,7 @@ void main() {
// Mock the server response
when(client.post('https://localhost/api/v2/weightentry/',
headers: {
'Authorization': 'Token ${auth.token}',
'Authorization': 'Token ${testAuth.token}',
'Content-Type': 'application/json; charset=UTF-8',
},
body: '{"id":null,"weight":"80","date":"2021-01-01T00:00:00.000"}'))
@@ -52,7 +45,7 @@ void main() {
// POST the data to the server
final WeightEntry weightEntry = WeightEntry(date: DateTime(2021, 1, 1), weight: 80);
final BodyWeight provider = BodyWeight(auth, []);
final BodyWeight provider = BodyWeight(testAuth, []);
final WeightEntry weightEntryNew = await provider.addEntry(weightEntry, client: client);
// Check that the server response is what we expect
@@ -67,11 +60,11 @@ void main() {
// Mock the server response
when(client.delete(
'https://localhost/api/v2/weightentry/4/',
headers: {'Authorization': 'Token ${auth.token}'},
headers: {'Authorization': 'Token ${testAuth.token}'},
)).thenAnswer((_) async => http.Response('', 200));
// DELETE the data from the server
final BodyWeight provider = BodyWeight(auth, [
final BodyWeight provider = BodyWeight(testAuth, [
WeightEntry(id: 4, weight: 80, date: DateTime(2021, 1, 1)),
WeightEntry(id: 2, weight: 100, date: DateTime(2021, 2, 2)),
WeightEntry(id: 5, weight: 60, date: DateTime(2021, 2, 2))