From 60cb727ca82d20a98a9ddd6281946bda9ad5f0e7 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 12 Jul 2018 11:19:25 -0400 Subject: [PATCH] Added `Service.findOne` tests --- .idea/libraries/Dart_Packages.xml | 20 +- .../tests_in_find_one_test_dart.xml | 7 + .idea/workspace.xml | 208 +++++++++--------- CHANGELOG.md | 4 + lib/src/core/anonymous_service.dart | 41 ++-- lib/src/core/request_context.dart | 4 +- lib/src/http/angel_http.dart | 4 +- performance/hello/main.dart | 4 +- test/all.dart | 2 + test/find_one_test.dart | 23 ++ 10 files changed, 177 insertions(+), 140 deletions(-) create mode 100644 .idea/runConfigurations/tests_in_find_one_test_dart.xml create mode 100644 test/find_one_test.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index d09f170a..3cbc4924 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -68,7 +68,7 @@ - @@ -159,7 +159,7 @@ - @@ -180,7 +180,7 @@ - @@ -222,7 +222,7 @@ - @@ -355,7 +355,7 @@ - @@ -504,7 +504,7 @@ - + @@ -517,16 +517,16 @@ - + - + - + @@ -545,7 +545,7 @@ - + diff --git a/.idea/runConfigurations/tests_in_find_one_test_dart.xml b/.idea/runConfigurations/tests_in_find_one_test_dart.xml new file mode 100644 index 00000000..ad42b586 --- /dev/null +++ b/.idea/runConfigurations/tests_in_find_one_test_dart.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7c5c1122..5485fef2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,12 @@ + + - + + + @@ -26,12 +30,12 @@ - + - - + + @@ -50,7 +54,7 @@ - + @@ -74,11 +78,14 @@ - - + + - - + + + + + @@ -101,27 +108,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -134,7 +120,6 @@ - HttpStatus.FORBIDDEN HttpStatus. JSON ContentType.JSON.toString() @@ -164,6 +149,7 @@ redirectTo finalizers.t `Iterable` + instance logger?.warning @@ -219,7 +205,6 @@ @@ -353,6 +339,7 @@ + @@ -393,7 +380,7 @@ - + @@ -682,14 +670,7 @@ - - - - 1517975664302 - 1517976649593 @@ -1027,19 +1008,17 @@ - - - - - - - - - - @@ -1061,9 +1040,18 @@ + + + + + + + + + - @@ -1098,9 +1086,10 @@ - + - + + @@ -1111,7 +1100,6 @@ - @@ -1126,7 +1114,6 @@ @@ -1159,13 +1147,6 @@ - - - - - - - @@ -1237,13 +1218,6 @@ - - - - - - - @@ -1335,17 +1309,13 @@ - - - - - - - + + + @@ -1360,6 +1330,9 @@ + + + @@ -1398,13 +1371,6 @@ - - - - - - - @@ -1461,27 +1427,27 @@ - + - - + + + + + - + - - + + - - - - - - - + + + + @@ -1492,10 +1458,44 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index b5b580ca..396cd60c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.1.5 +* `AnonymousService` methods can now return `FutureOr`. +* Added `findOne` to `Service`. + # 1.1.4+11 * Close the response, etc. *after* finalizers are done. diff --git a/lib/src/core/anonymous_service.dart b/lib/src/core/anonymous_service.dart index 0faf3b12..4878b54c 100644 --- a/lib/src/core/anonymous_service.dart +++ b/lib/src/core/anonymous_service.dart @@ -5,17 +5,17 @@ import 'service.dart'; /// /// Well-suited for testing. class AnonymousService extends Service { - Future Function([Map]) _index; - Future Function(Object, [Map]) _read, _create, _remove; - Future Function(Object, Object, [Map]) _modify, _update; + FutureOr Function([Map]) _index; + FutureOr Function(Object, [Map]) _read, _create, _remove; + FutureOr Function(Object, Object, [Map]) _modify, _update; AnonymousService( - {Future index([Map params]), - Future read(id, [Map params]), - Future create(data, [Map params]), - Future modify(id, data, [Map params]), - Future update(id, data, [Map params]), - Future remove(id, [Map params])}) + {FutureOr index([Map params]), + FutureOr read(id, [Map params]), + FutureOr create(data, [Map params]), + FutureOr modify(id, data, [Map params]), + FutureOr update(id, data, [Map params]), + FutureOr remove(id, [Map params])}) : super() { _index = index; _read = read; @@ -26,27 +26,28 @@ class AnonymousService extends Service { } @override - index([Map params]) => _index != null ? _index(params) : super.index(params); + index([Map params]) => new Future.sync( + () => _index != null ? _index(params) : super.index(params)); @override - read(id, [Map params]) => - _read != null ? _read(id, params) : super.read(id, params); + read(id, [Map params]) => new Future.sync( + () => _read != null ? _read(id, params) : super.read(id, params)); @override - create(data, [Map params]) => - _create != null ? _create(data, params) : super.create(data, params); + create(data, [Map params]) => new Future.sync(() => + _create != null ? _create(data, params) : super.create(data, params)); @override - modify(id, data, [Map params]) => _modify != null + modify(id, data, [Map params]) => new Future.sync(() => _modify != null ? _modify(id, data, params) - : super.modify(id, data, params); + : super.modify(id, data, params)); @override - update(id, data, [Map params]) => _update != null + update(id, data, [Map params]) => new Future.sync(() => _update != null ? _update(id, data, params) - : super.update(id, data, params); + : super.update(id, data, params)); @override - remove(id, [Map params]) => - _remove != null ? _remove(id, params) : super.remove(id, params); + remove(id, [Map params]) => new Future.sync( + () => _remove != null ? _remove(id, params) : super.remove(id, params)); } diff --git a/lib/src/core/request_context.dart b/lib/src/core/request_context.dart index cc4edf54..daee6ab1 100644 --- a/lib/src/core/request_context.dart +++ b/lib/src/core/request_context.dart @@ -167,8 +167,8 @@ abstract class RequestContext { void inject(type, value) { if (!app.isProduction && type is Type) { if (!reflect(value).type.isAssignableTo(reflectType(type))) - throw new ArgumentError('Cannot inject $value (${value - .runtimeType}) as an instance of $type.'); + throw new ArgumentError( + 'Cannot inject $value (${value.runtimeType}) as an instance of $type.'); } _injections[type] = value; diff --git a/lib/src/http/angel_http.dart b/lib/src/http/angel_http.dart index be85a714..d23cce48 100644 --- a/lib/src/http/angel_http.dart +++ b/lib/src/http/angel_http.dart @@ -334,8 +334,8 @@ class AngelHttp { if (sw.isRunning) { sw?.stop(); - app.logger.info("${res.statusCode} ${req.method} ${req.uri} (${sw - ?.elapsedMilliseconds ?? 'unknown'} ms)"); + app.logger.info( + "${res.statusCode} ${req.method} ${req.uri} (${sw?.elapsedMilliseconds ?? 'unknown'} ms)"); } } }); diff --git a/performance/hello/main.dart b/performance/hello/main.dart index 6127e30c..73b69b35 100644 --- a/performance/hello/main.dart +++ b/performance/hello/main.dart @@ -35,7 +35,7 @@ void start(int id) { }; http.startServer('127.0.0.1', 3000).then((server) { - print('Instance #$id listening at http://${server.address.address}:${server - .port}'); + print( + 'Instance #$id listening at http://${server.address.address}:${server.port}'); }); } diff --git a/test/all.dart b/test/all.dart index 8d69d1c1..9473c038 100644 --- a/test/all.dart +++ b/test/all.dart @@ -9,6 +9,7 @@ import 'di_test.dart' as di; import 'encoders_buffer_test.dart' as encoders_buffer; import 'exception_test.dart' as exception; import 'extension_test.dart' as extension; +import 'find_one_test.dart' as find_one; import 'general_test.dart' as general; import 'hooked_test.dart' as hooked; import 'parameter_meta_test.dart' as parameter_meta; @@ -34,6 +35,7 @@ main() { group('encoders_buffer', encoders_buffer.main); group('exception', exception.main); group('extension', extension.main); + group('find_one', find_one.main); group('general', general.main); group('hooked', hooked.main); group('parameter_meta', parameter_meta.main); diff --git a/test/find_one_test.dart b/test/find_one_test.dart new file mode 100644 index 00000000..530047ff --- /dev/null +++ b/test/find_one_test.dart @@ -0,0 +1,23 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:test/test.dart'; +import 'common.dart'; + +void main() { + var throwsAnAngelHttpException = + throwsA(const IsInstanceOf()); + + test('throw 404 on null', () { + var service = new AnonymousService(index: ([p]) => null); + expect(() => service.findOne(), throwsAnAngelHttpException); + }); + + test('throw 404 on empty iterable', () { + var service = new AnonymousService(index: ([p]) => []); + expect(() => service.findOne(), throwsAnAngelHttpException); + }); + + test('return first element of iterable', () async { + var service = new AnonymousService(index: ([p]) => [2]); + expect(await service.findOne(), 2); + }); +}