1 | var assert = require('assert');
|
2 | var ok = require('okay');
|
3 | var query = require('../');
|
4 | var pg = require('pg');
|
5 | pg.defaults.poolSize = 1;
|
6 |
|
7 | describe('query.first', function() {
|
8 |
|
9 | before(function(done) {
|
10 | query('CREATE TEMP TABLE something(name text, age int)', ok(done, function() {
|
11 | query("INSERT INTO something VALUES ('brian', 30)", function() {
|
12 | query("INSERT INTO something VALUES ('aaron', 28)", done)
|
13 | })
|
14 | }))
|
15 | })
|
16 |
|
17 | it('returns null with no result', function(done) {
|
18 | query.first('SELECT * FROM something WHERE name = \'robot\'', function(err, res) {
|
19 | assert.equal(arguments.length, 2)
|
20 | assert.ifError(err)
|
21 | assert(!res)
|
22 | done()
|
23 | })
|
24 | })
|
25 |
|
26 | it('returns first row with 1 result', function(done) {
|
27 | query.first('SELECT name FROM something WHERE age = 30', function(err, res) {
|
28 | assert.ifError(err)
|
29 | assert.equal(res.name, 'brian')
|
30 | done()
|
31 | })
|
32 | })
|
33 |
|
34 | it('returns first row if there are more than 1', function(done) {
|
35 | query.first('SELECT name FROM something ORDER BY age', ok(done, function(res) {
|
36 | assert.equal(res.name, 'aaron')
|
37 | done()
|
38 | }))
|
39 | })
|
40 |
|
41 | it('turns non-array values into array values for you', function(done) {
|
42 | query.first('SELECT name FROM something WHERE name = $1', 'brian', ok(done, function(res) {
|
43 | assert.equal(res.name, 'brian')
|
44 | done()
|
45 | }))
|
46 | })
|
47 |
|
48 |
|
49 |
|
50 | it('handles errors as you would expect', function(done) {
|
51 | query.first('SELELKJSDLKSDJF SD F', function(err, res) {
|
52 | assert(err)
|
53 | assert.equal(res, null)
|
54 | done()
|
55 | })
|
56 | })
|
57 |
|
58 | });
|