Skip to content

Commit 28f0996

Browse files
author
Peter Svetlichny
committed
refactor(create): return validated data on create
1 parent 6a3681c commit 28f0996

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ module.exports = class {
7676
}, [])
7777
const query = `INSERT INTO ${this.tableName} (${cols.join(',')}) VALUES (${vals.join(',')})`
7878
return this.query(query)
79+
.then(res => {
80+
if (res.rowCount) return data
81+
throw new Error('Unable to create record')
82+
})
7983
})
8084
}
8185

test/src/index.spec.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,37 @@ describe('postgres', () => {
2727
describe('createTable', () => {
2828
it('creates a new table based on object passed (if not exists)', () => {
2929
return inst.createTable(fixd.postgres.createTable)
30-
.then((result) => {
31-
expect(result.command).to.equal('CREATE')
30+
.then((res) => {
31+
expect(res.command).to.equal('CREATE')
3232
})
3333
})
3434
})
3535
describe('create', () => {
3636
it('creates a new record based on object passed', () => {
3737
return inst.create(fixd.postgres.testData)
38-
.then((result) => {
39-
expect(result.command).to.equal('INSERT')
40-
expect(result.rowCount).to.equal(1)
38+
.then((res) => {
39+
expect(res).to.containSubset(fixd.postgres.testData)
4140
})
4241
})
42+
it('rejects if record is not created', () => {
43+
sandbox.stub(inst, 'query').resolves({ rowCount: 0 })
44+
return expect(inst.create(fixd.postgres.testData))
45+
.to.be.rejectedWith('Unable to create record')
46+
})
4347
})
4448
describe('read', () => {
4549
it('reads all when no query specified', () => {
4650
return inst.read()
47-
.then((result) => {
48-
expect(result.length).to.equal(1)
49-
expect(result[0].email).to.equal(fixd.postgres.testData.email)
51+
.then((res) => {
52+
expect(res.length).to.equal(1)
53+
expect(res[0].email).to.equal(fixd.postgres.testData.email)
5054
})
5155
})
5256
it('reads specific records when query supplied', () => {
5357
return inst.read('fname=\'John\'', 1)
54-
.then((result) => {
55-
expect(result.length).to.equal(1)
56-
expect(result[0].email).to.equal(fixd.postgres.testData.email)
58+
.then((res) => {
59+
expect(res.length).to.equal(1)
60+
expect(res[0].email).to.equal(fixd.postgres.testData.email)
5761
})
5862
})
5963
it('fails when a bad query is provided', () => {

0 commit comments

Comments
 (0)