From 86152f7a04efdffeab3a87b6c4f6513840b9af14 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 22 Oct 2018 12:29:09 -0400 Subject: [PATCH] service map tests --- CHANGELOG.md | 3 ++ lib/src/core/service.dart | 2 +- test/service_map_test.dart | 80 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/service_map_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 414c0706..3bbbac2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-alpha.9 +* Add `Service.map`. + # 2.0.0-alpha.8 * No longer export HTTP-specific code from `angel_framework.dart`. An import of `import 'package:angel_framework/http.dart';` will be necessary in most cases now. diff --git a/lib/src/core/service.dart b/lib/src/core/service.dart index b6fc9e29..16a40fad 100644 --- a/lib/src/core/service.dart +++ b/lib/src/core/service.dart @@ -127,7 +127,7 @@ class Service extends Routable { /// Creates an [AnonymousService] that wraps over this one, and maps input and output /// using two converter functions. - /// + /// /// Handy utility for handling data in a type-safe manner. Service map(U Function(Data) encoder, Data Function(U) decoder) { return new AnonymousService( diff --git a/test/service_map_test.dart b/test/service_map_test.dart new file mode 100644 index 00000000..0694f128 --- /dev/null +++ b/test/service_map_test.dart @@ -0,0 +1,80 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:test/test.dart'; + +void main() { + MapService inner; + Service mapped; + + setUp(() { + inner = new MapService(); + mapped = inner.map(Todo.fromMap, Todo.toMap); + }); + + test('create', () async { + var result = await mapped.create( + new Todo(text: 'hello', complete: false), + ); + print(result); + expect( + result, + new Todo(text: 'hello', complete: false), + ); + }); + + group('after create', () { + Todo result; + String id; + + setUp(() async { + result = await mapped.create(new Todo(text: 'hello', complete: false)); + id = result.id; + }); + + test('index', () async { + expect(await mapped.index(), [result]); + }); + + test('modify', () async { + var newTodo = new Todo(text: 'yes', complete: true); + expect(await mapped.update(id, newTodo), newTodo); + }); + + test('update', () async { + var newTodo = new Todo(id: 'hmmm', text: 'yes', complete: true); + expect(await mapped.update(id, newTodo), newTodo); + }); + + test('read', () async { + expect(await mapped.read(id), result); + }); + + test('remove', () async { + expect(await mapped.remove(id), result); + }); + }); +} + +class Todo { + final String id, text; + final bool complete; + + Todo({this.id, this.text, this.complete}); + + static Todo fromMap(Map json) { + return new Todo( + id: json['id'] as String, + text: json['text'] as String, + complete: json['complete'] as bool); + } + + static Map toMap(Todo model) { + return {'id': model.id, 'text': model.text, 'complete': model.complete}; + } + + @override + bool operator ==(other) => + other is Todo && other.text == text && other.complete == complete; + + @override + String toString() => '$id:$text($complete)'; +}