From afd72b8f4f9b49e92a2938f02d6ba66f094daf83 Mon Sep 17 00:00:00 2001 From: Ndianabasi Udonkang Date: Thu, 3 Mar 2022 02:31:37 +0100 Subject: [PATCH] fix: errors when DB response is null. fixes #6 (#7) --- src/Attachment/index.ts | 4 ++ test/attachment-decorator.spec.ts | 111 ++++++++++++++++++++++++++++++ test/attachment.spec.ts | 29 ++++---- 3 files changed, 132 insertions(+), 12 deletions(-) diff --git a/src/Attachment/index.ts b/src/Attachment/index.ts index a2e09e4..3ac7adc 100644 --- a/src/Attachment/index.ts +++ b/src/Attachment/index.ts @@ -61,6 +61,10 @@ export class Attachment implements AttachmentContract { * Create attachment instance from the database response */ public static fromDbResponse(response: any) { + if (response === null) { + return null + } + const attributes = typeof response === 'string' ? JSON.parse(response) : response /** diff --git a/test/attachment-decorator.spec.ts b/test/attachment-decorator.spec.ts index 6f6317f..8e9b64f 100644 --- a/test/attachment-decorator.spec.ts +++ b/test/attachment-decorator.spec.ts @@ -1172,6 +1172,44 @@ test.group('@attachment | find', (group) => { assert.isTrue(await Drive.exists(body.avatar.name)) }) + test('Attachment response should be null when column value is null', async (assert) => { + const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') + const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext') + + class User extends BaseModel { + @column({ isPrimary: true }) + public id: string + + @column() + public username: string + + @attachment({ preComputeUrl: true }) + public avatar: AttachmentContract | null + } + + const server = createServer((req, res) => { + const ctx = HttpContext.create('/', {}, req, res) + + app.container.make(BodyParserMiddleware).handle(ctx, async () => { + const file = ctx.request.file('avatar')! + + let user = new User() + user.username = 'virk' + user.avatar = file ? Attachment.fromFile(file) : null + await user.save() + + user = await User.firstOrFail() + + ctx.response.send(user) + ctx.response.finish() + }) + }) + + const { body } = await supertest(server).post('/') + + assert.isNull(body.avatar) + }) + test('do not pre compute when preComputeUrl is not enabled', async (assert) => { const Drive = app.container.resolveBinding('Adonis/Core/Drive') const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') @@ -1278,6 +1316,43 @@ test.group('@attachment | fetch', (group) => { assert.isTrue(await Drive.exists(body.avatar.name)) }) + test('Attachment response should be null when column value is null', async (assert) => { + const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') + const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext') + + class User extends BaseModel { + @column({ isPrimary: true }) + public id: string + + @column() + public username: string + + @attachment({ preComputeUrl: true }) + public avatar: AttachmentContract | null + } + + const server = createServer((req, res) => { + const ctx = HttpContext.create('/', {}, req, res) + + app.container.make(BodyParserMiddleware).handle(ctx, async () => { + await Promise.all( + ['virk', 'ndianabasi'].map((username) => User.firstOrCreate({ username })) + ) + + const users = await User.all() + + ctx.response.send(users) + ctx.response.finish() + }) + }) + + await supertest(server).post('/') + const { body } = await supertest(server).post('/') + + assert.isNull(body[0].avatar) + assert.isNull(body[1].avatar) + }) + test('do not pre compute when preComputeUrl is not enabled', async (assert) => { const Drive = app.container.resolveBinding('Adonis/Core/Drive') const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') @@ -1384,6 +1459,42 @@ test.group('@attachment | paginate', (group) => { assert.isTrue(await Drive.exists(body.avatar.name)) }) + test('Attachment response should be null when column value is null', async (assert) => { + const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') + const HttpContext = app.container.resolveBinding('Adonis/Core/HttpContext') + + class User extends BaseModel { + @column({ isPrimary: true }) + public id: string + + @column() + public username: string + + @attachment({ preComputeUrl: true }) + public avatar: AttachmentContract | null + } + + const server = createServer((req, res) => { + const ctx = HttpContext.create('/', {}, req, res) + + app.container.make(BodyParserMiddleware).handle(ctx, async () => { + await Promise.all( + ['virk', 'ndianabasi'].map((username) => User.firstOrCreate({ username })) + ) + + const users = await User.query().paginate(1) + + ctx.response.send(users) + ctx.response.finish() + }) + }) + + const { body } = await supertest(server).post('/') + + assert.isNull(body.data[0].avatar) + assert.isNull(body.data[1].avatar) + }) + test('do not pre compute when preComputeUrl is not enabled', async (assert) => { const Drive = app.container.resolveBinding('Adonis/Core/Drive') const { column, BaseModel } = app.container.use('Adonis/Lucid/Orm') diff --git a/test/attachment.spec.ts b/test/attachment.spec.ts index 5066950..2fff12f 100644 --- a/test/attachment.spec.ts +++ b/test/attachment.spec.ts @@ -44,8 +44,8 @@ test.group('Attachment | fromDbResponse', (group) => { }) ) - assert.isTrue(attachment.isPersisted) - assert.isFalse(attachment.isLocal) + assert.isTrue(attachment?.isPersisted) + assert.isFalse(attachment?.isLocal) }) test('save method should result in noop when attachment is created from db response', async (assert) => { @@ -58,8 +58,13 @@ test.group('Attachment | fromDbResponse', (group) => { }) ) - await attachment.save() - assert.equal(attachment.name, 'foo.jpg') + await attachment?.save() + assert.equal(attachment?.name, 'foo.jpg') + }) + + test('Attachment should be null when db response is null', async (assert) => { + const attachment = Attachment.fromDbResponse(null) + assert.isNull(attachment) }) test('delete persisted file', async (assert) => { @@ -72,8 +77,8 @@ test.group('Attachment | fromDbResponse', (group) => { }) ) - await attachment.delete() - assert.isTrue(attachment.isDeleted) + await attachment?.delete() + assert.isTrue(attachment?.isDeleted) }) test('compute file url', async (assert) => { @@ -86,10 +91,10 @@ test.group('Attachment | fromDbResponse', (group) => { }) ) - attachment.setOptions({ preComputeUrl: true }) + attachment?.setOptions({ preComputeUrl: true }) - await attachment.computeUrl() - assert.match(attachment.url, /\/uploads\/foo\.jpg\?signature=/) + await attachment?.computeUrl() + assert.match(attachment?.url!, /\/uploads\/foo\.jpg\?signature=/) }) test('compute file url from a custom method', async (assert) => { @@ -102,14 +107,14 @@ test.group('Attachment | fromDbResponse', (group) => { }) ) - attachment.setOptions({ + attachment?.setOptions({ preComputeUrl: async (_, file) => { return `/${file.name}` }, }) - await attachment.computeUrl() - assert.equal(attachment.url, '/foo.jpg') + await attachment?.computeUrl() + assert.equal(attachment?.url, '/foo.jpg') }) })