|
1 | 1 | /* eslint no-unused-expressions: 0 */
|
2 | 2 | /* 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') |
5 | 5 |
|
6 | 6 | const config = {
|
7 | 7 | 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 | +} |
30 | 12 |
|
31 | 13 | // Test record
|
32 | 14 | const testData = {
|
33 | 15 | fname: 'John',
|
34 | 16 | lname: 'Smith',
|
35 | 17 | email: 'jsmith@gmail.com'
|
36 |
| -}; |
| 18 | +} |
37 | 19 |
|
38 | 20 | 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 | + }) |
47 | 33 | 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('`') |
59 | 36 | .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') |
68 | 42 | .then((result) => {
|
69 |
| - expect(result.rows[0].number).to.equal(2); |
70 |
| - done(); |
| 43 | + expect(result.rows[0].number).to.equal(2) |
71 | 44 | })
|
72 |
| - .catch((err) => done(err)); |
73 |
| - }); |
74 |
| - }); |
75 |
| - |
| 45 | + }) |
| 46 | + }) |
76 | 47 | 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({ |
79 | 50 | 'id': [ 'serial', 'NOT NULL', 'PRIMARY KEY'],
|
80 | 51 | 'fname': [ 'varchar(255)' ],
|
81 | 52 | 'lname': [ 'varchar(255)' ],
|
82 | 53 | 'email': [ 'varchar(255)' ]
|
83 | 54 | })
|
84 | 55 | .then((result) => {
|
85 |
| - expect(result).to.be.an.object; |
86 |
| - done(); |
| 56 | + expect(result.command).to.equal('CREATE') |
87 | 57 | })
|
88 |
| - .catch((err) => done(err)); |
89 |
| - }); |
90 |
| - }); |
91 |
| - |
| 58 | + }) |
| 59 | + }) |
92 | 60 | 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 | + }) |
112 | 69 | 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() |
115 | 72 | .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) |
119 | 75 | })
|
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) |
124 | 79 | .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) |
127 | 82 | })
|
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') |
132 | 86 | .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 | + }) |
139 | 91 | 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\'', { |
151 | 94 | fname: 'Bob',
|
152 | 95 | email: 'bsmith@gmail.com'
|
153 | 96 | }, 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 | + }) |
162 | 103 | 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\'') |
165 | 106 | .then((result) => {
|
166 |
| - expect(result.rowCount).to.equal(1); |
167 |
| - done(); |
| 107 | + expect(result.rowCount).to.equal(1) |
168 | 108 | })
|
169 |
| - .catch((err) => done(err)); |
170 |
| - }); |
171 |
| - }); |
172 |
| - |
| 109 | + }) |
| 110 | + }) |
173 | 111 | describe('extend', () => {
|
174 | 112 | it('extends the adapter with a custom method', () => {
|
175 | 113 | // Extend
|
176 |
| - testInstance.extend('sayFoo', () => { |
177 |
| - return 'foo'; |
178 |
| - }); |
| 114 | + inst.extend('sayFoo', () => 'foo') |
179 | 115 | // Execute
|
180 |
| - expect(testInstance.sayFoo()).to.equal('foo'); |
181 |
| - }); |
182 |
| - }); |
183 |
| -}); |
| 116 | + expect(inst.sayFoo()).to.equal('foo') |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
0 commit comments