Add paginated fetches

This allows the app to fetch all available items from an enpoint, without
having to hope that the ?limit=999 is enough

Closes #133
This commit is contained in:
Roland Geider
2023-02-06 20:33:29 +01:00
parent 17af1983c6
commit 4f4f6f553a
39 changed files with 561 additions and 266 deletions

View File

@@ -18,10 +18,14 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/providers/base_provider.dart';
import '../fixtures/fixture_reader.dart';
import '../utils.dart';
import 'base_provider_test.mocks.dart';
@GenerateMocks([http.Client])
void main() {
@@ -94,4 +98,50 @@ void main() {
);
});
});
group('Retrieving and fetching data', () {
test('Test loading paginated data', () async {
// Arrange
final mockHttpClient = MockClient();
final response1 = Response(fixture('pagination/pagination1.json'), 200);
final response2 = Response(fixture('pagination/pagination2.json'), 200);
final response3 = Response(fixture('pagination/pagination3.json'), 200);
final Uri paginationUri1 = Uri(
scheme: 'https',
host: 'localhost',
path: 'api/v2/itcrowd/',
);
final Uri paginationUri2 = Uri(
scheme: 'https',
host: 'localhost',
path: 'api/v2/itcrowd/',
query: 'limit=20&offset=20',
);
final Uri paginationUri3 = Uri(
scheme: 'https',
host: 'localhost',
path: 'api/v2/itcrowd/',
query: 'limit=20&offset=40',
);
when(mockHttpClient.get(paginationUri1, headers: anyNamed('headers')))
.thenAnswer((_) => Future.value(response1));
when(mockHttpClient.get(paginationUri2, headers: anyNamed('headers')))
.thenAnswer((_) => Future.value(response2));
when(mockHttpClient.get(paginationUri3, headers: anyNamed('headers')))
.thenAnswer((_) => Future.value(response3));
// Act
final WgerBaseProvider provider = WgerBaseProvider(testAuthProvider, mockHttpClient);
final data = await provider.fetchPaginated(paginationUri1);
// Assert
expect(data.length, 5);
expect(data[0], {'id': 1, 'value': "You wouldn't steal a handbag."});
expect(data[1], {'id': 2, 'value': "You wouldn't steal a car."});
expect(data[2], {'id': 3, 'value': "You wouldn't steal a baby."});
expect(data[3], {'id': 4, 'value': "You wouldn't shoot a policeman."});
expect(data[4], {'id': 5, 'value': 'And then steal his helmet.'});
});
});
}

View File

@@ -31,7 +31,8 @@ class _FakeResponse_0 extends _i1.SmartFake implements _i2.Response {
);
}
class _FakeStreamedResponse_1 extends _i1.SmartFake implements _i2.StreamedResponse {
class _FakeStreamedResponse_1 extends _i1.SmartFake
implements _i2.StreamedResponse {
_FakeStreamedResponse_1(
Object parent,
Invocation parentInvocation,
@@ -236,12 +237,14 @@ class MockClient extends _i1.Mock implements _i2.Client {
returnValue: _i3.Future<_i5.Uint8List>.value(_i5.Uint8List(0)),
) as _i3.Future<_i5.Uint8List>);
@override
_i3.Future<_i2.StreamedResponse> send(_i2.BaseRequest? request) => (super.noSuchMethod(
_i3.Future<_i2.StreamedResponse> send(_i2.BaseRequest? request) =>
(super.noSuchMethod(
Invocation.method(
#send,
[request],
),
returnValue: _i3.Future<_i2.StreamedResponse>.value(_FakeStreamedResponse_1(
returnValue:
_i3.Future<_i2.StreamedResponse>.value(_FakeStreamedResponse_1(
this,
Invocation.method(
#send,