Skip to content

Commit

Permalink
Add transaction rollback, debugOnly, null query.one tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Azzolini committed May 10, 2018
1 parent 980b5bf commit 867fff8
Showing 1 changed file with 72 additions and 45 deletions.
117 changes: 72 additions & 45 deletions src/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,96 @@ const { createPool, getPool } = require('./pool.js')
const { setupTestDb, teardownTestDb } = require('../test/helper.js')

describe('query', () => {
describe('runner', () => {
beforeAll(async () => {
const db = await setupTestDb()
createPool('default', {
user: process.env.PGUSER,
host: process.env.PGHOST,
password: process.env.PGPASSWORD,
database: db,
})
beforeAll(async () => {
const db = await setupTestDb()
createPool('default', {
user: process.env.PGUSER,
host: process.env.PGHOST,
password: process.env.PGPASSWORD,
database: db,
})
})

afterAll(async () => {
await getPool('default').end()
await teardownTestDb()
})
afterAll(async () => {
await getPool('default').end()
await teardownTestDb()
})

test('runs a query', async () => {
const res = await query(sql`SELECT * FROM one`)
expect(res).toEqual([{ id: 'a', name: 'aaa' }, { id: 'b', name: 'bbb' }])
})
test('runs a query', async () => {
const res = await query(sql`SELECT * FROM one`)
expect(res).toEqual([{ id: 'a', name: 'aaa' }, { id: 'b', name: 'bbb' }])
})

test('runs a one query', async () => {
const res = await query.one(sql`SELECT name FROM one WHERE id = ${'a'}`)
expect(res).toEqual({ name: 'aaa' })
})
test('does not run a debugOnly query', async () => {
await query(sql`INSERT INTO one VALUES ('z', 'zzz')`, { debugOnly: true })
expect(await query(sql`SELECT * FROM one`)).toHaveLength(2)
})

test('runs a one query', async () => {
const res = await query.one(sql`SELECT name FROM one WHERE id = ${'a'}`)
expect(res).toEqual({ name: 'aaa' })
})

test('returns null for a one query that resturns zero rows', async () => {
const res = await query.one(sql`SELECT name FROM one WHERE id = ${'q'}`)
expect(res).toBe(null)
})

test('throws an error if a one query returns more than one result', async () => {
const resPromise = query.one(sql`SELECT * FROM one WHERE id IN (${'a'}, ${'b'})`)
await expect(resPromise).rejects.toBeDefined()
test('throws an error if a one query returns more than one result', async () => {
const resPromise = query.one(sql`SELECT * FROM one WHERE id IN (${'a'}, ${'b'})`)
await expect(resPromise).rejects.toBeDefined()
})

test('maps rows with a provided function', async () => {
const res = await query(sql`SELECT * FROM one`, {
rowMapper: row => row.name,
})
expect(res).toEqual(['aaa', 'bbb'])
})

test('maps rows with a provided function', async () => {
const res = await query(sql`SELECT * FROM one`, {
rowMapper: row => row.name,
})
expect(res).toEqual(['aaa', 'bbb'])
test('maps rows with a provided function for one queries', async () => {
const res = await query.one(sql`SELECT * FROM one WHERE id = ${'a'}`, {
rowMapper: row => row.name,
})
expect(res).toEqual('aaa')
})

test('maps rows with a provided function for one queries', async () => {
const res = await query.one(sql`SELECT * FROM one WHERE id = ${'a'}`, {
rowMapper: row => row.name,
})
expect(res).toEqual('aaa')
test('runs a transaction query', async () => {
expect.assertions(4)

const res = await query.transaction(async tquery => {
await tquery(sql`INSERT INTO one VALUES ('c', 'ccc')`)

// If a different query uses a different client, they shouldn't see this new row
expect(await query(sql`SELECT * FROM one`)).toHaveLength(2)

// This client (tquery) should see the row though
expect(await tquery(sql`SELECT * FROM one`)).toHaveLength(3)

return 'ok'
})

test('runs a transaction query', async () => {
expect.assertions(4)
expect(res).toBe('ok')
expect(await query(sql`SELECT * FROM one`)).toHaveLength(3)
})

const res = await query.transaction(async tquery => {
await tquery(sql`INSERT INTO one VALUES ('c', 'ccc')`)
test('transactions roll back in case of errors', async () => {
expect.assertions(2)

// If a different query uses a different client, they shouldn't see this new row
expect(await query(sql`SELECT * FROM one`)).toHaveLength(2)
try {
await query.transaction(async tquery => {
await tquery(sql`INSERT INTO one VALUES ('d', 'ddd')`)

// This client (tquery) should see the row though
expect(await tquery(sql`SELECT * FROM one`)).toHaveLength(3)
expect(await tquery(sql`SELECT * FROM one`)).toHaveLength(4)

return 'ok'
throw Error('oh no')
})
} catch (e) {
// Do nothing
}

expect(res).toBe('ok')
expect(await query(sql`SELECT * FROM one`)).toHaveLength(3)
})
expect(await query(sql`SELECT * FROM one`)).toHaveLength(3)
})
})

0 comments on commit 867fff8

Please sign in to comment.