platform/archived_packages/seeder/test/all_test.dart

70 lines
1.8 KiB
Dart
Raw Normal View History

2021-02-21 02:47:23 +00:00
import 'dart:async';
import 'package:angel_framework/angel_framework.dart';
2021-06-20 12:37:20 +00:00
import 'package:angel_container/mirrors.dart';
2021-02-21 02:47:23 +00:00
import 'package:angel_seeder/angel_seeder.dart';
import 'package:test/test.dart';
2021-06-20 12:37:20 +00:00
void main() {
2021-02-21 02:47:23 +00:00
test('create one', () async {
2021-06-20 12:37:20 +00:00
var app = Angel(reflector: MirrorsReflector())
..use('/todos', TodoService());
2021-02-21 02:47:23 +00:00
await app.configure(seed(
'todos',
2021-06-20 12:37:20 +00:00
SeederConfiguration<Todo>(delete: false, count: 10, template: {
2021-02-21 02:47:23 +00:00
'text': (Faker faker) => 'Clean your room, ${faker.person.name()}!',
'completed': false
})));
2021-06-20 12:37:20 +00:00
var todos = await app.findService('todos')!.index();
2021-02-21 02:47:23 +00:00
print('Todos: \n${todos.map((todo) => " - $todo").join("\n")}');
expect(todos, isList);
expect(todos, hasLength(10));
});
}
class TodoService extends Service {
final List<Todo> todos = [];
@override
2021-06-20 12:37:20 +00:00
Future<List<Todo>> index([params]) => myData();
2021-02-21 02:47:23 +00:00
Future<List<Todo>> myData() {
var completer = Completer<List<Todo>>();
completer.complete(todos);
return completer.future;
}
@override
2021-06-20 12:37:20 +00:00
Future<Object> create(data, [params]) async {
2021-02-21 02:47:23 +00:00
if (data is Todo) {
todos.add(data..id = todos.length.toString());
return data;
} else if (data is Map) {
2021-06-20 12:37:20 +00:00
todos.add(Todo.fromJson(data)..id = todos.length.toString());
2021-02-21 02:47:23 +00:00
return data;
2021-06-20 12:37:20 +00:00
} else {
throw AngelHttpException.badRequest();
}
2021-02-21 02:47:23 +00:00
}
}
class Todo extends Model {
2021-06-20 12:37:20 +00:00
final String? text;
final bool? completed;
2021-02-21 02:47:23 +00:00
2021-06-26 13:13:43 +00:00
Todo({String? id, this.text, this.completed = false}) {
2021-02-21 02:47:23 +00:00
this.id = id;
}
2021-06-20 12:37:20 +00:00
factory Todo.fromJson(Map data) => Todo(
id: data['id'] as String?,
text: data['text'] as String?,
completed: data['completed'] as bool?);
2021-02-21 02:47:23 +00:00
@override
2021-06-20 12:37:20 +00:00
String toString() => '${completed! ? "Complete" : "Incomplete"}: $text';
2021-02-21 02:47:23 +00:00
}