1
1
/* eslint no-unused-expressions: 0 */
2
- /* global expect, describe, it, beforeEach, afterEach */
3
2
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' )
19
4
20
5
describe ( 'postgres' , ( ) => {
21
6
let inst
22
- beforeEach ( ( ) => {
23
- inst = new PostgresAdapter ( config )
7
+ before ( ( ) => {
8
+ inst = new PostgresAdapter ( fixd . postgres . config )
24
9
inst . tableName = 'foo'
25
10
// Mock validation method, this is automatically done by the model
26
11
inst . validate = ( body ) => Promise . resolve ( body )
27
12
// Mock sanitize method, this is automatically done by the model
28
13
inst . sanitize = ( body ) => body
29
14
} )
30
- afterEach ( ( ) => {
31
- inst . query ( `DROP TABLE ${ inst . tableName } ` )
32
- } )
15
+ after ( ( ) => inst . pg . end ( ) )
33
16
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
- } )
40
17
it ( 'runs a query against the database when connection is good' , ( ) => {
41
18
return inst . query ( 'SELECT 1 + 1 AS number' )
42
19
. then ( ( result ) => {
43
20
expect ( result . rows [ 0 ] . number ) . to . equal ( 2 )
44
21
} )
45
22
} )
23
+ it ( 'fails when invalid query is run' , ( ) => {
24
+ return expect ( inst . query ( '`' ) ) . to . be . rejectedWith ( Error )
25
+ } )
46
26
} )
47
27
describe ( 'createTable' , ( ) => {
48
28
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
+ } )
58
33
} )
59
34
} )
60
35
describe ( 'create' , ( ) => {
61
36
it ( 'creates a new record based on object passed' , ( ) => {
62
- return inst . create ( testData )
37
+ return inst . create ( fixd . postgres . testData )
63
38
. then ( ( result ) => {
64
39
expect ( result . command ) . to . equal ( 'INSERT' )
65
40
expect ( result . rowCount ) . to . equal ( 1 )
@@ -71,21 +46,18 @@ describe('postgres', () => {
71
46
return inst . read ( )
72
47
. then ( ( result ) => {
73
48
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 )
75
50
} )
76
51
} )
77
52
it ( 'reads specific records when query supplied' , ( ) => {
78
53
return inst . read ( 'fname=\'John\'' , 1 )
79
54
. then ( ( result ) => {
80
55
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 )
82
57
} )
83
58
} )
84
59
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 )
89
61
} )
90
62
} )
91
63
describe ( 'update' , ( ) => {
0 commit comments