platform/packages/framework/test/service_map_test.dart

81 lines
1.8 KiB
Dart
Raw Normal View History

2021-05-14 10:34:09 +00:00
import 'package:angel3_framework/angel3_framework.dart';
2018-10-22 16:29:09 +00:00
import 'package:test/test.dart';
void main() {
MapService inner;
2021-03-20 08:11:18 +00:00
late Service<String?, Todo> mapped;
2018-10-22 16:29:09 +00:00
setUp(() {
2019-05-02 22:48:31 +00:00
inner = MapService();
2018-10-22 16:29:09 +00:00
mapped = inner.map<Todo>(Todo.fromMap, Todo.toMap);
});
test('create', () async {
var result = await mapped.create(
2019-05-02 22:48:31 +00:00
Todo(text: 'hello', complete: false),
2018-10-22 16:29:09 +00:00
);
print(result);
expect(
result,
2019-05-02 22:48:31 +00:00
Todo(text: 'hello', complete: false),
2018-10-22 16:29:09 +00:00
);
});
group('after create', () {
2021-03-20 08:11:18 +00:00
late Todo result;
String? id;
2018-10-22 16:29:09 +00:00
setUp(() async {
2019-05-02 22:48:31 +00:00
result = await mapped.create(Todo(text: 'hello', complete: false));
2018-10-22 16:29:09 +00:00
id = result.id;
});
test('index', () async {
expect(await mapped.index(), [result]);
});
test('modify', () async {
2019-05-02 22:48:31 +00:00
var newTodo = Todo(text: 'yes', complete: true);
2018-10-22 16:29:09 +00:00
expect(await mapped.update(id, newTodo), newTodo);
});
test('update', () async {
2019-05-02 22:48:31 +00:00
var newTodo = Todo(id: 'hmmm', text: 'yes', complete: true);
2018-10-22 16:29:09 +00:00
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 {
2021-03-20 08:11:18 +00:00
final String? id, text;
final bool? complete;
2018-10-22 16:29:09 +00:00
Todo({this.id, this.text, this.complete});
static Todo fromMap(Map<String, dynamic> json) {
2019-05-02 22:48:31 +00:00
return Todo(
2021-03-20 08:11:18 +00:00
id: json['id'] as String?,
text: json['text'] as String?,
complete: json['complete'] as bool?);
2018-10-22 16:29:09 +00:00
}
static Map<String, dynamic> 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)';
}