Skip to content

Commit

Permalink
fix: errors when DB response is null. fixes #6 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndianabasi authored Mar 3, 2022
1 parent 4f05e55 commit afd72b8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/Attachment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
111 changes: 111 additions & 0 deletions test/attachment-decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
29 changes: 17 additions & 12 deletions test/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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')
})
})

Expand Down

0 comments on commit afd72b8

Please sign in to comment.