Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generate code for entity relations #59

Merged
merged 17 commits into from
May 1, 2024
14 changes: 9 additions & 5 deletions _tests_/lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User extends Entity<User> {
required this.homeAddress,
});

// HasMany<Post> get posts => hasMany<Post>();
HasMany<User, Post> get posts => hasMany<Post>();
}

@Table('posts')
Expand Down Expand Up @@ -56,15 +56,19 @@ class Post extends Entity<Post> {
required this.updatedAt,
});

// HasMany<PostComment> get comments => hasMany<PostComment>();
HasMany<Post, PostComment> get comments => hasMany<PostComment>();

BelongsTo<Post, User> get owner => belongsTo<User>();
}

@Table('post_comments')
class PostComment extends Entity<PostComment> {
@primaryKey
final String id;
final int id;
final String comment;
final int? postId;

PostComment(this.id, this.comment, {this.postId});
@reference(Post, name: 'post_id', onDelete: ForeignKeyAction.cascade)
final int postId;

PostComment(this.id, this.comment, {required this.postId});
}
307 changes: 139 additions & 168 deletions _tests_/test/integration/e2e_relation.dart
Original file line number Diff line number Diff line change
@@ -1,168 +1,139 @@
// import 'package:test/test.dart';
// import 'package:yaroorm/yaroorm.dart';

// import 'fixtures/migrator.dart';
// import 'fixtures/test_data.dart';

// void runRelationsE2ETest(String connectionName) {
// final driver = DB.driver(connectionName);

// final tables = [User, Post, PostComment].map((e) => getEntityTableName(e));

// test('tables names',
// () => expect(tables, ['users', 'posts', 'post_comments']));

// return group('with ${driver.type.name} driver', () {
// User? testUser1, anotherUser;

// setUpAll(() async {
// await driver.connect();

// expect(driver.isOpen, isTrue);

// var hasTables = await Future.wait(tables.map(driver.hasTable));
// expect(hasTables.every((e) => e), isFalse);

// await runMigrator(connectionName, 'migrate');

// hasTables = await Future.wait(tables.map(driver.hasTable));
// expect(hasTables.every((e) => e), isTrue);

// testUser1 = await User(
// firstname: 'Baba',
// lastname: 'Tunde',
// age: 29,
// homeAddress: "Owerri, Nigeria")
// .withDriver(driver)
// .save();
// expect(
// testUser1, isA<User>().having((p0) => p0.id, 'has primary key', 1));
// });

// test('should add many posts for User', () async {
// final postsToAdd = <Post>[
// Post('Foo Bar 1', 'foo bar 4'),
// Post('Mee Moo 2', 'foo bar 5'),
// Post('Coo Kie 3', 'foo bar 6'),
// ];

// await testUser1!.posts.addAll(postsToAdd);

// final posts = await testUser1!.posts.orderByAsc('title').get();
// expect(posts, hasLength(3));
// expect(
// posts.every((e) =>
// e.createdAt != null &&
// e.updatedAt != null &&
// e.userId == testUser1!.id),
// isTrue);
// expect(
// posts.map((e) => {
// 'id': e.id,
// 'title': e.title,
// 'desc': e.description,
// 'userId': e.userId!
// }),
// [
// {'id': 3, 'title': 'Coo Kie 3', 'desc': 'foo bar 6', 'userId': 1},
// {'id': 1, 'title': 'Foo Bar 1', 'desc': 'foo bar 4', 'userId': 1},
// {'id': 2, 'title': 'Mee Moo 2', 'desc': 'foo bar 5', 'userId': 1},
// ]);
// });

// test('should add comments for post', () async {
// final post = await testUser1!.posts.first();
// expect(post, isA<Post>());

// var comments = await post!.comments.get();
// expect(comments, isEmpty);

// final c = PostComment('this post looks abit old')
// ..id = 'some_random_uuid_32893782738';
// await post.comments.add(c);

// comments = await post.comments.get();
// expect(
// comments.map(
// (e) => {'id': c.id, 'comment': c.comment, 'postId': e.postId}),
// [
// {
// 'id': 'some_random_uuid_32893782738',
// 'comment': 'this post looks abit old',
// 'postId': 1
// }
// ]);

// await post.comments.add(PostComment('oh, another comment')
// ..id = 'jagaban_299488474773_uuid_3i848');
// comments = await post.comments.orderByDesc('comment').get();
// expect(comments, hasLength(2));
// expect(
// comments.map(
// (e) => {'id': e.id, 'comment': e.comment, 'postId': e.postId}),
// [
// {
// 'id': 'some_random_uuid_32893782738',
// 'comment': 'this post looks abit old',
// 'postId': 1
// },
// {
// 'id': 'jagaban_299488474773_uuid_3i848',
// 'comment': 'oh, another comment',
// 'postId': 1
// }
// ]);
// });

// test('should add post for another user', () async {
// anotherUser = await usersList.last.withDriver(driver).save();
// final user = anotherUser as User;

// expect(user.id, isNotNull);
// expect(user.id != testUser1!.id, isTrue);

// var anotherUserPosts = await user.posts.get();
// expect(anotherUserPosts, isEmpty);

// await user.posts.add(Post('Another Post', 'wham bamn'));
// anotherUserPosts = await user.posts.get();
// expect(anotherUserPosts, hasLength(1));

// final anotherUserPost = anotherUserPosts.first;
// expect(anotherUserPost.userId!, anotherUser!.id!);

// await anotherUserPost.comments.addAll([
// PostComment('ah ah')..id = '_id_394',
// PostComment('oh oh')..id = '_id_394885',
// ]);

// expect(await anotherUserPost.comments.get(), hasLength(2));
// });

// test('should delete comments for post', () async {
// expect(testUser1, isNotNull);
// final posts = await testUser1!.posts.get();
// expect(posts, hasLength(3));

// // ignore: curly_braces_in_flow_control_structures
// for (final post in posts) await post.comments.delete();

// expect(
// await Future.wait(posts.map((e) => e.comments.get())), [[], [], []]);

// await testUser1!.posts.delete();

// expect(await testUser1!.posts.get(), isEmpty);
// });

// tearDownAll(() async {
// await runMigrator(connectionName, 'migrate:reset');

// final hasTables = await Future.wait(tables.map(driver.hasTable));
// expect(hasTables.every((e) => e), isFalse);

// await driver.disconnect();
// expect(driver.isOpen, isFalse);
// });
// });
// }
import 'package:test/test.dart';
import 'package:yaroorm/yaroorm.dart';
import 'package:yaroorm_tests/src/models.dart';
import 'package:yaroorm_tests/test_data.dart';

import '../util.dart';

void runRelationsE2ETest(String connectionName) {
final driver = DB.driver(connectionName);

return group('with ${driver.type.name} driver', () {
late User testUser1, anotherUser;

final tableNames = [
getEntityTableName<User>(),
getEntityTableName<Post>(),
getEntityTableName<PostComment>(),
];

setUpAll(() async {
await driver.connect();

expect(driver.isOpen, isTrue);

var hasTables = await Future.wait(tableNames.map(driver.hasTable));
expect(hasTables.every((e) => e), isFalse);

await runMigrator(connectionName, 'migrate');

hasTables = await Future.wait(tableNames.map(driver.hasTable));
expect(hasTables.every((e) => e), isTrue);

testUser1 = await UserQuery.driver(driver).create(
firstname: 'Baba',
lastname: 'Tunde',
age: 29,
homeAddress: 'Owerri, Nigeria',
);
});

test('should add many posts for User', () async {
await testUser1.posts.add(title: 'Aoo bar 1', description: 'foo bar 4');
await testUser1.posts.add(title: 'Bee Moo 2', description: 'foo bar 5');
await testUser1.posts.add(title: 'Coo Kie 3', description: 'foo bar 6');

final posts = await testUser1.posts.get(
orderBy: [OrderPostBy.title(OrderDirection.desc)],
);
expect(posts, hasLength(3));
expect(posts.map((e) => {'id': e.id, 'title': e.title, 'desc': e.description, 'userId': e.userId}), [
{'id': 3, 'title': 'Coo Kie 3', 'desc': 'foo bar 6', 'userId': 1},
{'id': 2, 'title': 'Bee Moo 2', 'desc': 'foo bar 5', 'userId': 1},
{'id': 1, 'title': 'Aoo bar 1', 'desc': 'foo bar 4', 'userId': 1}
]);
});

test('should fetch posts with owners', () async {
final posts = await PostQuery.driver(driver).withRelations((post) => [post.owner]).findMany();
final post = posts.first;

final owner = await post.owner;
final result = await owner.get();
expect(owner.isUsingEntityCache, isTrue);
expect(result, isA<User>());
});

test('should add comments for post', () async {
final post = await testUser1.posts.first!;
expect(post, isA<Post>());

var comments = await post!.comments.get();
expect(comments, isEmpty);

await post.comments.add(comment: 'This post looks abit old');

comments = await post.comments.get();
expect(comments.map((c) => {'id': c.id, 'comment': c.comment, 'postId': c.postId}), [
{'id': 1, 'comment': 'This post looks abit old', 'postId': post.id}
]);

await post.comments.add(comment: 'oh, another comment');
});

test('should add post for another user', () async {
final testuser = usersList.last;
anotherUser = await UserQuery.driver(driver).create(
firstname: testuser.firstname,
lastname: testuser.lastname,
age: testuser.age,
homeAddress: testuser.homeAddress,
);

expect(anotherUser.id, isNotNull);
expect(anotherUser.id != testUser1.id, isTrue);

var anotherUserPosts = await anotherUser.posts.get();
expect(anotherUserPosts, isEmpty);

await anotherUser.posts.add(title: 'Another Post', description: 'wham bamn');
anotherUserPosts = await anotherUser.posts.get();
expect(anotherUserPosts, hasLength(1));

final anotherUserPost = anotherUserPosts.first;
expect(anotherUserPost.userId, anotherUser.id);

await anotherUserPost.comments.add(comment: 'ah ah');
await anotherUserPost.comments.add(comment: 'oh oh');

expect(await anotherUserPost.comments.get(), hasLength(2));
});

test('should delete comments for post', () async {
expect(testUser1, isNotNull);
final posts = await testUser1.posts.get();
expect(posts, hasLength(3));

// ignore: curly_braces_in_flow_control_structures
for (final post in posts) await post.comments.delete();

for (final post in posts) {
expect(await post.comments.get(), []);
}

await testUser1.posts.delete();

expect(await testUser1.posts.get(), isEmpty);
});

tearDownAll(() async {
await runMigrator(connectionName, 'migrate:reset');

final hasTables = await Future.wait(tableNames.map(driver.hasTable));
expect(hasTables.every((e) => e), isFalse);

await driver.disconnect();
expect(driver.isOpen, isFalse);
});
});
}
7 changes: 4 additions & 3 deletions _tests_/test/integration/mariadb.e2e.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:test/test.dart';

import '../../database/database.dart';
import '../../database/database.dart' as db;
import 'e2e_basic.dart';
import 'e2e_relation.dart';

void main() async {
initializeORM();
db.initializeORM();

group('MariaDB', () {
group('Basic E2E Test', () => runBasicE2ETest('bar_mariadb'));

// group('Relation E2E Test', () => runRelationsE2ETest('bar_mariadb'));
group('Relation E2E Test', () => runRelationsE2ETest('bar_mariadb'));
});
}
7 changes: 4 additions & 3 deletions _tests_/test/integration/mysql.e2e.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:test/test.dart';

import '../../database/database.dart';
import '../../database/database.dart' as db;
import 'e2e_basic.dart';
import 'e2e_relation.dart';

void main() async {
initializeORM();
db.initializeORM();

group('MySQL', () {
group('Basic E2E Test', () => runBasicE2ETest('moo_mysql'));

// group('Relation E2E Test', () => runRelationsE2ETest('moo_mysql'));
group('Relation E2E Test', () => runRelationsE2ETest('moo_mysql'));
});
}
7 changes: 4 additions & 3 deletions _tests_/test/integration/pgsql.e2e.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:test/test.dart';
import '../../database/database.dart';
import '../../database/database.dart' as db;
import 'e2e_basic.dart';
import 'e2e_relation.dart';

void main() async {
initializeORM();
db.initializeORM();

group('Postgres', () {
group('Basic E2E Test', () => runBasicE2ETest('foo_pgsql'));

// group('Relation E2E Test', () => runRelationsE2ETest('foo_pgsql'));
group('Relation E2E Test', () => runRelationsE2ETest('foo_pgsql'));
});
}
Loading
Loading