platform/archived_packages/relations/test/has_many_test.dart

61 lines
1.7 KiB
Dart
Raw Normal View History

2017-01-30 02:39:11 +00:00
import 'package:angel_framework/angel_framework.dart';
2021-02-16 02:10:09 +00:00
//import 'package:angel_relations/angel_relations.dart' as relations;
2017-01-30 02:39:11 +00:00
import 'package:angel_seeder/angel_seeder.dart';
import 'package:test/test.dart';
import 'common.dart';
2021-06-20 12:37:20 +00:00
void main() {
late Angel app;
2017-01-30 02:39:11 +00:00
setUp(() async {
2021-02-21 02:47:23 +00:00
app = Angel()..use('/authors', MapService())..use('/books', MapService());
2017-01-30 02:39:11 +00:00
await app.configure(seed(
'authors',
2021-02-21 02:47:23 +00:00
SeederConfiguration<Map>(
2017-01-30 02:39:11 +00:00
count: 10,
template: {'name': (Faker faker) => faker.person.name()},
callback: (Map author, seed) {
return seed(
'books',
2021-02-21 02:47:23 +00:00
SeederConfiguration(delete: false, count: 10, template: {
2017-01-30 02:39:11 +00:00
'authorId': author['id'],
'title': (Faker faker) =>
'I love to eat ${faker.food.dish()}'
}));
})));
2021-02-16 02:10:09 +00:00
// TODO: Missing afterAll method
// app
// .findService('authors')
// .afterAll(relations.hasMany('books', foreignKey: 'authorId'));
2017-01-30 02:39:11 +00:00
});
test('index', () async {
2021-06-20 12:37:20 +00:00
var authors = await app.findService('authors')!.index();
2017-01-30 02:39:11 +00:00
print(authors);
expect(authors, allOf(isList, isNotEmpty));
2021-06-20 12:37:20 +00:00
for (var author in authors.whereType<Map>()) {
2017-01-30 02:39:11 +00:00
expect(author.keys, contains('books'));
2021-06-20 12:37:20 +00:00
var books = author['books'] as List<Map>;
2017-01-30 02:39:11 +00:00
for (var book in books) {
expect(book['authorId'], equals(author['id']));
}
}
});
test('create', () async {
var tolstoy = await app
2021-06-20 12:37:20 +00:00
.findService('authors')!
2021-02-21 02:47:23 +00:00
.create(Author(name: 'Leo Tolstoy').toJson());
2017-01-30 02:39:11 +00:00
print(tolstoy);
expect(tolstoy.keys, contains('books'));
expect(tolstoy['books'], allOf(isList, isEmpty));
});
}