|
1 | | -import { IPGQueryable } from '../lib/utils' |
2 | 1 | import sql = require('../pg') |
3 | 2 |
|
4 | 3 | const sampleBooks = ['book1', 'book2'] |
5 | | -const db: IPGQueryable = { |
| 4 | +const db = { |
6 | 5 | query: async ({ text, values }) => { |
7 | | - if (text === 'select * from books') { |
8 | | - return { rows: sampleBooks, rowCount: sampleBooks.length } |
| 6 | + if (text === 'select * from books where read = $1') { |
| 7 | + return { |
| 8 | + oid: 1, |
| 9 | + rowCount: sampleBooks.length, |
| 10 | + rows: sampleBooks |
| 11 | + } |
| 12 | + } |
| 13 | + return { |
| 14 | + oid: 0, |
| 15 | + rowCount: 0, |
| 16 | + rows: [] |
9 | 17 | } |
10 | | - return { rows: [], rowCount: 0 } |
11 | 18 | } |
12 | 19 | } |
13 | 20 |
|
14 | 21 | test('sql should return the query config', async () => { |
15 | | - const queryConfig = sql`select * from books` |
| 22 | + const queryConfig = sql`select * from books where read = ${false}` |
16 | 23 | expect(queryConfig._sql).toBeTruthy() |
17 | 24 | }) |
18 | 25 |
|
19 | 26 | test("sql.query should return pg's query result", async () => { |
20 | | - const { rows, rowCount } = await sql.query(db)`select * from books` |
| 27 | + const { rows, rowCount, oid } = await sql.query( |
| 28 | + db |
| 29 | + )`select * from books where read = ${false}` |
21 | 30 | expect(rows).toBe(sampleBooks) |
22 | 31 | expect(rowCount).toBe(sampleBooks.length) |
| 32 | + expect(oid).toBe(1) |
23 | 33 | }) |
24 | 34 |
|
25 | 35 | test('sql.one should return the first row', async () => { |
26 | | - const book = await sql.one(db)`select * from books` |
| 36 | + const book = await sql.one(db)`select * from books where read = ${false}` |
27 | 37 | expect(book).toBe(sampleBooks[0]) |
28 | 38 | }) |
29 | 39 |
|
30 | 40 | test('sql.many should return rows', async () => { |
31 | | - const books = await sql.many(db)`select * from books` |
| 41 | + const books = await sql.many(db)`select * from books where read = ${false}` |
32 | 42 | expect(books).toBe(sampleBooks) |
33 | 43 | }) |
34 | 44 |
|
35 | 45 | test('sql.count should return rowCount', async () => { |
36 | | - const nbBooks = await sql.count(db)`select * from books` |
| 46 | + const nbBooks = await sql.count(db)`select * from books where read = ${false}` |
37 | 47 | expect(nbBooks).toBe(sampleBooks.length) |
38 | 48 | }) |
0 commit comments