Skip to content

Commit b3659ff

Browse files
author
Peter Svetlichny
committed
test(postgres): add fixd, update tests
1 parent 849d844 commit b3659ff

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

test/fixtures/postgres.fixture.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fixd.postgres = {
2+
config: {
3+
host: process.env.MODLI_POSTGRES_HOST,
4+
user: process.env.POSTGRES_USER,
5+
password: process.env.POSTGRES_PASSWORD,
6+
database: process.env.POSTGRES_DB
7+
},
8+
testData: {
9+
fname: 'John',
10+
lname: 'Smith',
11+
email: 'jsmith@gmail.com'
12+
},
13+
createTable: {
14+
id: [ 'serial', 'NOT NULL', 'PRIMARY KEY' ],
15+
fname: [ 'varchar(255)' ],
16+
lname: [ 'varchar(255)' ],
17+
email: [ 'varchar(255)' ]
18+
}
19+
}

test/src/index.spec.js

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,40 @@
11
/* eslint no-unused-expressions: 0 */
2-
/* global expect, describe, it, beforeEach, afterEach */
32
require('../setup')
4-
const PostgresAdapter = require('../../src/index')
5-
6-
const config = {
7-
host: process.env.MODLI_POSTGRES_HOST,
8-
user: process.env.POSTGRES_USER,
9-
password: process.env.POSTGRES_PASSWORD,
10-
database: process.env.POSTGRES_DB
11-
}
12-
13-
// Test record
14-
const testData = {
15-
fname: 'John',
16-
lname: 'Smith',
17-
email: 'jsmith@gmail.com'
18-
}
3+
const PostgresAdapter = require('src')
194

205
describe('postgres', () => {
216
let inst
22-
beforeEach(() => {
23-
inst = new PostgresAdapter(config)
7+
before(() => {
8+
inst = new PostgresAdapter(fixd.postgres.config)
249
inst.tableName = 'foo'
2510
// Mock validation method, this is automatically done by the model
2611
inst.validate = (body) => Promise.resolve(body)
2712
// Mock sanitize method, this is automatically done by the model
2813
inst.sanitize = (body) => body
2914
})
30-
afterEach(() => {
31-
inst.query(`DROP TABLE ${inst.tableName}`)
32-
})
15+
after(() => inst.pg.end())
3316
describe('query', () => {
34-
it('fails when invalid query is run', () => {
35-
return inst.query('`')
36-
.catch((err) => {
37-
expect(err).to.be.an.instanceof(Error)
38-
})
39-
})
4017
it('runs a query against the database when connection is good', () => {
4118
return inst.query('SELECT 1 + 1 AS number')
4219
.then((result) => {
4320
expect(result.rows[0].number).to.equal(2)
4421
})
4522
})
23+
it('fails when invalid query is run', () => {
24+
return expect(inst.query('`')).to.be.rejectedWith(Error)
25+
})
4626
})
4727
describe('createTable', () => {
4828
it('creates a new table based on object passed (if not exists)', () => {
49-
return inst.createTable({
50-
'id': [ 'serial', 'NOT NULL', 'PRIMARY KEY' ],
51-
'fname': [ 'varchar(255)' ],
52-
'lname': [ 'varchar(255)' ],
53-
'email': [ 'varchar(255)' ]
54-
})
55-
.then((result) => {
56-
expect(result.command).to.equal('CREATE')
57-
})
29+
return inst.createTable(fixd.postgres.createTable)
30+
.then((result) => {
31+
expect(result.command).to.equal('CREATE')
32+
})
5833
})
5934
})
6035
describe('create', () => {
6136
it('creates a new record based on object passed', () => {
62-
return inst.create(testData)
37+
return inst.create(fixd.postgres.testData)
6338
.then((result) => {
6439
expect(result.command).to.equal('INSERT')
6540
expect(result.rowCount).to.equal(1)
@@ -71,21 +46,18 @@ describe('postgres', () => {
7146
return inst.read()
7247
.then((result) => {
7348
expect(result.length).to.equal(1)
74-
expect(result[0].email).to.equal(testData.email)
49+
expect(result[0].email).to.equal(fixd.postgres.testData.email)
7550
})
7651
})
7752
it('reads specific records when query supplied', () => {
7853
return inst.read('fname=\'John\'', 1)
7954
.then((result) => {
8055
expect(result.length).to.equal(1)
81-
expect(result[0].email).to.equal(testData.email)
56+
expect(result[0].email).to.equal(fixd.postgres.testData.email)
8257
})
8358
})
8459
it('fails when a bad query is provided', () => {
85-
return inst.read('`fart=`knocker')
86-
.catch((err) => {
87-
expect(err).to.be.an.instanceof(Error)
88-
})
60+
return expect(inst.read('`')).to.be.rejectedWith(Error)
8961
})
9062
})
9163
describe('update', () => {

0 commit comments

Comments
 (0)