From 5107f76e79b10c65670ca2c41e378564bb330962 Mon Sep 17 00:00:00 2001 From: Isaac Maier Date: Thu, 29 Feb 2024 10:38:31 -0500 Subject: [PATCH 1/4] First pass at nested belongsTo unit test --- test/orm/base-model.spec.ts | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 4355d0234..93511c98e 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -6083,6 +6083,94 @@ test.group('Base Model | toObject', (group) => { $extras: {}, }) }) + + // TODO: I'm not really checking the author, so can probably just remove... + test('add preloaded belongsTo relationship to toObject result', async ({ assert }) => { + class Category extends BaseModel { + @column() + public name: string + + @column() + public parentId: number + + @belongsTo(() => Category) + public parent: BelongsTo + } + + class Post extends BaseModel { + @column() + public title: string + + @column() + public userId: number + + @belongsTo(() => User) + public author: BelongsTo + + @hasOne(() => Category) + public category: HasOne + } + + class User extends BaseModel { + @column({ isPrimary: true }) + public id: number + + @column() + public username: string + + @hasMany(() => Post) + public posts: HasMany + } + + const user = new User() + user.username = 'virk' + + const category = new Category() + category.name = 'Tutorials' + + const subCategory = new Category() + subCategory.name = 'Lucid' + + const post = new Post() + post.title = 'Adonis 101' + + // user.$setRelated('posts', [post]) + post.$setRelated('author', user) + subCategory.$setRelated('parent', category) + post.$setRelated('category', subCategory) + + assert.deepEqual(subCategory.toObject(), { + name: 'Lucid', + parent: { + name: 'Tutorials', + $extras: {}, + }, + $extras: {}, + }) + + assert.deepEqual(category.toObject(), { + name: 'Tutorials', + parent: null, + $extras: {}, + }) + + // assert.deepEqual(post.toObject(), { + // title: 'Adonis 101', + // author: { + // username: 'virk', + // $extras: {}, + // }, + // category: { + // name: 'Lucid', + // parent: { + // name: 'Tutorials', + // $extras: {}, + // }, + // $extras: {}, + // }, + // $extras: {}, + // }) + }).pin() }) test.group('Base model | inheritance', (group) => { From 6f6ec74911f8d123f166d4e04b9ccc38b1a6de51 Mon Sep 17 00:00:00 2001 From: Isaac Maier Date: Thu, 29 Feb 2024 16:38:37 -0500 Subject: [PATCH 2/4] Remove extraneous user model, make parent ID nullable --- test/orm/base-model.spec.ts | 51 ++++++++++--------------------------- 1 file changed, 14 insertions(+), 37 deletions(-) diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 93511c98e..5f53e1d02 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -6087,11 +6087,14 @@ test.group('Base Model | toObject', (group) => { // TODO: I'm not really checking the author, so can probably just remove... test('add preloaded belongsTo relationship to toObject result', async ({ assert }) => { class Category extends BaseModel { + @column({ isPrimary: true }) + public id: number + @column() public name: string @column() - public parentId: number + public parentId: number | null @belongsTo(() => Category) public parent: BelongsTo @@ -6104,45 +6107,34 @@ test.group('Base Model | toObject', (group) => { @column() public userId: number - @belongsTo(() => User) - public author: BelongsTo - @hasOne(() => Category) public category: HasOne } - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasMany(() => Post) - public posts: HasMany - } - - const user = new User() - user.username = 'virk' - const category = new Category() category.name = 'Tutorials' + category.id = 1 + category.parentId = null const subCategory = new Category() subCategory.name = 'Lucid' + subCategory.id = 2 + category.parentId = category.id const post = new Post() post.title = 'Adonis 101' - // user.$setRelated('posts', [post]) - post.$setRelated('author', user) subCategory.$setRelated('parent', category) post.$setRelated('category', subCategory) assert.deepEqual(subCategory.toObject(), { name: 'Lucid', + id: 2, + parentId: 1, parent: { name: 'Tutorials', + id: 1, + parentId: null, $extras: {}, }, $extras: {}, @@ -6150,26 +6142,11 @@ test.group('Base Model | toObject', (group) => { assert.deepEqual(category.toObject(), { name: 'Tutorials', + id: 1, + parentId: null, parent: null, $extras: {}, }) - - // assert.deepEqual(post.toObject(), { - // title: 'Adonis 101', - // author: { - // username: 'virk', - // $extras: {}, - // }, - // category: { - // name: 'Lucid', - // parent: { - // name: 'Tutorials', - // $extras: {}, - // }, - // $extras: {}, - // }, - // $extras: {}, - // }) }).pin() }) From ac500dde70d8658fd6fe1456b74ffbb78f33ad61 Mon Sep 17 00:00:00 2001 From: Isaac Maier Date: Thu, 29 Feb 2024 16:40:09 -0500 Subject: [PATCH 3/4] Missed a field --- test/orm/base-model.spec.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 5f53e1d02..7f3bdd5bc 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -6084,7 +6084,6 @@ test.group('Base Model | toObject', (group) => { }) }) - // TODO: I'm not really checking the author, so can probably just remove... test('add preloaded belongsTo relationship to toObject result', async ({ assert }) => { class Category extends BaseModel { @column({ isPrimary: true }) @@ -6104,9 +6103,6 @@ test.group('Base Model | toObject', (group) => { @column() public title: string - @column() - public userId: number - @hasOne(() => Category) public category: HasOne } From 5ac71d7bd4ac0247ce1758e2eacb564331328bf2 Mon Sep 17 00:00:00 2001 From: Isaac Maier Date: Sat, 23 Mar 2024 14:45:33 -0400 Subject: [PATCH 4/4] Don't pin the new test --- test/orm/base-model.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/orm/base-model.spec.ts b/test/orm/base-model.spec.ts index 7f3bdd5bc..090c6cdfb 100644 --- a/test/orm/base-model.spec.ts +++ b/test/orm/base-model.spec.ts @@ -6143,7 +6143,7 @@ test.group('Base Model | toObject', (group) => { parent: null, $extras: {}, }) - }).pin() + }) }) test.group('Base model | inheritance', (group) => {