platform/packages/paginate/test/bounds_test.dart

58 lines
1.6 KiB
Dart
Raw Normal View History

2019-02-01 14:39:10 +00:00
import 'dart:convert';
2021-06-26 11:55:17 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_paginate/angel3_paginate.dart';
import 'package:angel3_test/angel3_test.dart';
2019-02-01 14:39:10 +00:00
import 'package:logging/logging.dart';
2017-05-28 18:22:57 +00:00
import 'package:test/test.dart';
2019-02-01 14:39:10 +00:00
final List<Map<String, String>> mjAlbums = [
2017-05-28 18:22:57 +00:00
{'billie': 'jean'},
{'off': 'the_wall'},
{'michael': 'jackson'}
];
2021-06-20 12:37:20 +00:00
void main() {
late TestClient client;
2017-05-28 18:22:57 +00:00
setUp(() async {
2021-02-21 02:47:23 +00:00
var app = Angel();
2019-02-01 14:39:10 +00:00
app.get('/api/songs', (req, res) {
var p = Paginator(mjAlbums, itemsPerPage: mjAlbums.length);
2021-06-20 12:37:20 +00:00
p.goToPage(int.parse(req.queryParameters['page'] as String? ?? '1'));
2019-02-01 14:39:10 +00:00
return p.current;
});
2017-05-28 18:22:57 +00:00
client = await connectTo(app);
2019-02-01 14:39:10 +00:00
app.logger = Logger('angel_paginate')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2017-05-28 18:22:57 +00:00
});
tearDown(() => client.close());
test('limit exceeds size of collection', () async {
2019-02-01 14:39:10 +00:00
var response = await client.get(Uri(
path: '/api/songs',
queryParameters: {r'$limit': (mjAlbums.length + 1).toString()}));
2021-02-21 02:47:23 +00:00
var page = PaginationResult<Map<String, dynamic>>.fromMap(
2021-06-20 12:37:20 +00:00
json.decode(response.body) as Map<String, dynamic>);
2017-05-28 18:22:57 +00:00
print('page: ${page.toJson()}');
2019-02-01 14:39:10 +00:00
expect(page.total, mjAlbums.length);
expect(page.itemsPerPage, mjAlbums.length);
2017-05-28 18:22:57 +00:00
expect(page.previousPage, -1);
expect(page.currentPage, 1);
expect(page.nextPage, -1);
expect(page.startIndex, 0);
2019-02-01 14:39:10 +00:00
expect(page.endIndex, mjAlbums.length - 1);
expect(page.data, mjAlbums);
2017-05-28 18:22:57 +00:00
});
}