Skip to content

Commit 6b31d51

Browse files
author
Peter Svetlichny
committed
test(index): add postgres start-up script and update tests
1 parent 03e36b4 commit 6b31d51

File tree

2 files changed

+102
-140
lines changed

2 files changed

+102
-140
lines changed

scripts/postgres_conn.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const net = require('net')
2+
const host = process.env.MODLI_POSTGRES_HOST
3+
const port = 5432
4+
const retries = 150
5+
let retryCount = 0
6+
7+
process.stdout.write(`Awaiting Connection to ${host}`)
8+
9+
const testConn = () => {
10+
const conn = net.createConnection(port, host)
11+
conn.on('connect', () => {
12+
process.stdout.write(`OK\n\n`)
13+
process.exit(0)
14+
})
15+
conn.on('error', () => {
16+
retryCount++
17+
if (retryCount >= retries) {
18+
process.stdout.write('FAILED\n\n')
19+
process.exit(1)
20+
}
21+
process.stdout.write('.')
22+
setTimeout(testConn, 150)
23+
})
24+
}
25+
26+
testConn()

test/src/index.spec.js

Lines changed: 76 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,119 @@
11
/* eslint no-unused-expressions: 0 */
22
/* global expect, request, describe, it, before, after */
3-
import '../setup';
4-
import postgres from '../../src/index';
3+
require('../setup')
4+
const PostgresAdapter = require('../../src/index')
55

66
const config = {
77
host: process.env.MODLI_POSTGRES_HOST,
8-
username: process.env.MODLI_POSTGRES_USERNAME,
9-
password: process.env.MODLI_POSTGRES_PASSWORD,
10-
database: process.env.MODLI_POSTGRES_DATABASE
11-
};
12-
13-
const testInstance = new postgres(config);
14-
testInstance.tableName = 'foo';
15-
16-
// Mock validation method, this is automatically done by the model
17-
testInstance.validate = (body) => {
18-
// Test validation failure by passing `failValidate: true`
19-
if (body.failValidate) {
20-
return { error: true };
21-
}
22-
// Mock passing validation, return null
23-
return null;
24-
};
25-
26-
// Mock sanitize method, this is automatically done by the model
27-
testInstance.sanitize = (body) => {
28-
return body;
29-
};
8+
user: process.env.POSTGRES_USER,
9+
password: process.env.POSTGRES_PASSWORD,
10+
database: process.env.POSTGRES_DB
11+
}
3012

3113
// Test record
3214
const testData = {
3315
fname: 'John',
3416
lname: 'Smith',
3517
email: 'jsmith@gmail.com'
36-
};
18+
}
3719

3820
describe('postgres', () => {
39-
after((done) => {
40-
testInstance.query(`DROP TABLE ${testInstance.tableName};`)
41-
.then(() => {
42-
done();
43-
})
44-
.catch(done);
45-
});
46-
21+
let inst
22+
beforeEach(() => {
23+
inst = new PostgresAdapter(config)
24+
inst.tableName = 'foo'
25+
// Mock validation method, this is automatically done by the model
26+
inst.validate = (body) => Promise.resolve(body)
27+
// Mock sanitize method, this is automatically done by the model
28+
inst.sanitize = (body) => body
29+
})
30+
afterEach(() => {
31+
inst.query(`DROP TABLE ${inst.tableName}`)
32+
})
4733
describe('query', () => {
48-
it('fails when a bad connection config is passed', (done) => {
49-
const testPostgres = new postgres({});
50-
testPostgres.query('')
51-
.catch((err) => {
52-
expect(err).to.be.an.object;
53-
done();
54-
});
55-
});
56-
it('fails when an invalid query is run', (done) => {
57-
const testPostgres = new postgres(config);
58-
testPostgres.query('`')
34+
it('fails when invalid query is run', () => {
35+
return inst.query('`')
5936
.catch((err) => {
60-
expect(err).to.be.an.object;
61-
expect(err.name).to.equal('error');
62-
done();
63-
});
64-
});
65-
it('runs a query against the database when connection is good', (done) => {
66-
const testPostgres = new postgres(config);
67-
testPostgres.query('SELECT 1 + 1 AS number')
37+
expect(err).to.be.an.instanceof(Error)
38+
})
39+
})
40+
it('runs a query against the database when connection is good', () => {
41+
return inst.query('SELECT 1 + 1 AS number')
6842
.then((result) => {
69-
expect(result.rows[0].number).to.equal(2);
70-
done();
43+
expect(result.rows[0].number).to.equal(2)
7144
})
72-
.catch((err) => done(err));
73-
});
74-
});
75-
45+
})
46+
})
7647
describe('createTable', () => {
77-
it('creates a new table based on object passed (if not exists)', (done) => {
78-
testInstance.createTable({
48+
it('creates a new table based on object passed (if not exists)', () => {
49+
return inst.createTable({
7950
'id': [ 'serial', 'NOT NULL', 'PRIMARY KEY'],
8051
'fname': [ 'varchar(255)' ],
8152
'lname': [ 'varchar(255)' ],
8253
'email': [ 'varchar(255)' ]
8354
})
8455
.then((result) => {
85-
expect(result).to.be.an.object;
86-
done();
56+
expect(result.command).to.equal('CREATE')
8757
})
88-
.catch((err) => done(err));
89-
});
90-
});
91-
58+
})
59+
})
9260
describe('create', () => {
93-
it('fails when validation does not pass', (done) => {
94-
testInstance.create({
95-
failValidate: true
96-
})
97-
.catch((err) => {
98-
expect(err).to.have.property('error');
99-
done();
100-
});
101-
});
102-
it('creates a new record based on object passed', (done) => {
103-
testInstance.create(testData)
104-
.then((result) => {
105-
expect(result.rowCount).to.be.a.number;
106-
done();
107-
})
108-
.catch((err) => done(err));
109-
});
110-
});
111-
61+
it('creates a new record based on object passed', () => {
62+
return inst.create(testData)
63+
.then((result) => {
64+
expect(result.command).to.equal('INSERT')
65+
expect(result.rowCount).to.equal(1)
66+
})
67+
})
68+
})
11269
describe('read', () => {
113-
it('reads all when no query specified', (done) => {
114-
testInstance.read()
70+
it('reads all when no query specified', () => {
71+
return inst.read()
11572
.then((result) => {
116-
expect(result).to.be.an.array;
117-
expect(result[0].email).to.equal(testData.email);
118-
done();
73+
expect(result.length).to.equal(1)
74+
expect(result[0].email).to.equal(testData.email)
11975
})
120-
.catch((err) => done(err));
121-
});
122-
it('reads specific records when query supplied', (done) => {
123-
testInstance.read('fname=\'John\'', 1)
76+
})
77+
it('reads specific records when query supplied', () => {
78+
return inst.read('fname=\'John\'', 1)
12479
.then((result) => {
125-
expect(result).to.be.an.array;
126-
done();
80+
expect(result.length).to.equal(1)
81+
expect(result[0].email).to.equal(testData.email)
12782
})
128-
.catch((err) => done(err));
129-
});
130-
it('fails when a bad query is provided', (done) => {
131-
testInstance.read('`fart=`knocker')
83+
})
84+
it('fails when a bad query is provided', () => {
85+
return inst.read('`fart=`knocker')
13286
.catch((err) => {
133-
expect(err).to.be.an.instanceof(Error);
134-
done();
135-
});
136-
});
137-
});
138-
87+
expect(err).to.be.an.instanceof(Error)
88+
})
89+
})
90+
})
13991
describe('update', () => {
140-
it('fails when validation does not pass', (done) => {
141-
testInstance.update({}, {
142-
failValidate: true
143-
})
144-
.catch((err) => {
145-
expect(err).to.have.property('error');
146-
done();
147-
});
148-
});
149-
it('updates record(s) based on query and body', (done) => {
150-
testInstance.update('fname=\'John\'', {
92+
it('updates record(s) based on query and body', () => {
93+
return inst.update('fname=\'John\'', {
15194
fname: 'Bob',
15295
email: 'bsmith@gmail.com'
15396
}, 1)
154-
.then((result) => {
155-
expect(result.rowCount).to.equal(1);
156-
done();
157-
})
158-
.catch((err) => done(err));
159-
});
160-
});
161-
97+
.then((result) => {
98+
expect(result.command).to.equal('UPDATE')
99+
expect(result.rowCount).to.equal(1)
100+
})
101+
})
102+
})
162103
describe('delete', () => {
163-
it('deletes record(s) based on query', (done) => {
164-
testInstance.delete('fname=\'Bob\'')
104+
it('deletes record(s) based on query', () => {
105+
return inst.delete('fname=\'Bob\'')
165106
.then((result) => {
166-
expect(result.rowCount).to.equal(1);
167-
done();
107+
expect(result.rowCount).to.equal(1)
168108
})
169-
.catch((err) => done(err));
170-
});
171-
});
172-
109+
})
110+
})
173111
describe('extend', () => {
174112
it('extends the adapter with a custom method', () => {
175113
// Extend
176-
testInstance.extend('sayFoo', () => {
177-
return 'foo';
178-
});
114+
inst.extend('sayFoo', () => 'foo')
179115
// Execute
180-
expect(testInstance.sayFoo()).to.equal('foo');
181-
});
182-
});
183-
});
116+
expect(inst.sayFoo()).to.equal('foo')
117+
})
118+
})
119+
})

0 commit comments

Comments
 (0)