14
14
const should = require ( 'should' ) ;
15
15
const Redis = require ( 'ioredis' ) ;
16
16
17
+ // allow the default url to be overriden for local testing
18
+ const redisUrl = process . env . REDIS_URL ? process . env . REDIS_URL : 'redis://localhost:6379/' ;
19
+
17
20
function event ( object , name ) {
18
21
// Convert events to promises
19
22
return new Promise ( resolve => {
@@ -23,7 +26,7 @@ function event(object, name) {
23
26
24
27
describe ( 'test/koa-redis.test.js' , ( ) => {
25
28
it ( 'should connect and ready with external client and quit ok' , function * ( ) {
26
- const store = require ( '..' ) ( { client : new Redis ( ) } ) ;
29
+ const store = require ( '..' ) ( { client : new Redis ( redisUrl ) } ) ;
27
30
yield event ( store , 'connect' ) ;
28
31
store . connected . should . eql ( true ) ;
29
32
yield event ( store , 'ready' ) ;
@@ -34,7 +37,7 @@ describe('test/koa-redis.test.js', () => {
34
37
35
38
it ( 'should connect and ready with duplicated external client and disconnect ok' , function * ( ) {
36
39
const store = require ( '..' ) ( {
37
- client : new Redis ( ) ,
40
+ client : new Redis ( redisUrl ) ,
38
41
duplicate : true
39
42
} ) ;
40
43
yield event ( store , 'connect' ) ;
@@ -47,7 +50,7 @@ describe('test/koa-redis.test.js', () => {
47
50
48
51
it ( 'should connect and ready with url and quit ok' , function * ( ) {
49
52
const store = require ( '..' ) ( {
50
- url : 'redis://localhost:6379/'
53
+ url : redisUrl
51
54
} ) ;
52
55
yield event ( store , 'connect' ) ;
53
56
store . connected . should . eql ( true ) ;
@@ -58,8 +61,8 @@ describe('test/koa-redis.test.js', () => {
58
61
} ) ;
59
62
60
63
it ( 'should set and delete with db ok' , function * ( ) {
61
- const store = require ( '..' ) ( { db : 2 } ) ;
62
- const client = new Redis ( ) ;
64
+ const store = require ( '..' ) ( { db : 2 , url : redisUrl } ) ;
65
+ const client = new Redis ( redisUrl ) ;
63
66
client . select ( 2 ) ;
64
67
yield store . set ( 'key:db1' , { a : 2 } ) ;
65
68
( yield store . get ( 'key:db1' ) ) . should . eql ( { a : 2 } ) ;
@@ -71,15 +74,15 @@ describe('test/koa-redis.test.js', () => {
71
74
} ) ;
72
75
73
76
it ( 'should set with ttl ok' , function * ( ) {
74
- const store = require ( '..' ) ( ) ;
77
+ const store = require ( '..' ) ( { url : redisUrl } ) ;
75
78
yield store . set ( 'key:ttl' , { a : 1 } , 86400000 ) ;
76
79
( yield store . get ( 'key:ttl' ) ) . should . eql ( { a : 1 } ) ;
77
80
( yield store . client . ttl ( 'key:ttl' ) ) . should . equal ( 86400 ) ;
78
81
yield store . quit ( ) ;
79
82
} ) ;
80
83
81
84
it ( 'should not throw error with bad JSON' , function * ( ) {
82
- const store = require ( '..' ) ( ) ;
85
+ const store = require ( '..' ) ( { url : redisUrl } ) ;
83
86
yield store . client . set ( 'key:badKey' , '{I will cause an error!}' ) ;
84
87
should . not . exist ( yield store . get ( 'key:badKey' ) ) ;
85
88
yield store . quit ( ) ;
@@ -88,7 +91,8 @@ describe('test/koa-redis.test.js', () => {
88
91
it ( 'should use default JSON.parse/JSON.stringify without serialize/unserialize function' , function * ( ) {
89
92
const store = require ( '..' ) ( {
90
93
serialize : 'Not a function' ,
91
- unserialize : 'Not a function'
94
+ unserialize : 'Not a function' ,
95
+ url : redisUrl
92
96
} ) ;
93
97
yield store . set ( 'key:notserialized' , { a : 1 } ) ;
94
98
( yield store . get ( 'key:notserialized' ) ) . should . eql ( { a : 1 } ) ;
@@ -98,23 +102,24 @@ describe('test/koa-redis.test.js', () => {
98
102
it ( 'should parse bad JSON with custom unserialize function' , function * ( ) {
99
103
const store = require ( '..' ) ( {
100
104
serialize : value => 'JSON:' + JSON . stringify ( value ) ,
101
- unserialize : value => JSON . parse ( value . slice ( 5 ) )
105
+ unserialize : value => JSON . parse ( value . slice ( 5 ) ) ,
106
+ url : redisUrl
102
107
} ) ;
103
108
yield store . set ( 'key:notserialized' , { a : 1 } ) ;
104
109
( yield store . get ( 'key:notserialized' ) ) . should . eql ( { a : 1 } ) ;
105
110
yield store . quit ( ) ;
106
111
} ) ;
107
112
108
113
it ( 'should set without ttl ok' , function * ( ) {
109
- const store = require ( '..' ) ( ) ;
114
+ const store = require ( '..' ) ( { url : redisUrl } ) ;
110
115
yield store . set ( 'key:nottl' , { a : 1 } ) ;
111
116
( yield store . get ( 'key:nottl' ) ) . should . eql ( { a : 1 } ) ;
112
117
( yield store . client . ttl ( 'key:nottl' ) ) . should . equal ( - 1 ) ;
113
118
yield store . quit ( ) ;
114
119
} ) ;
115
120
116
121
it ( 'should destroy ok' , function * ( ) {
117
- const store = require ( '..' ) ( ) ;
122
+ const store = require ( '..' ) ( { url : redisUrl } ) ;
118
123
yield store . destroy ( 'key:nottl' ) ;
119
124
yield store . destroy ( 'key:ttl' ) ;
120
125
yield store . destroy ( 'key:badKey' ) ;
@@ -132,7 +137,7 @@ describe('test/koa-redis.test.js', () => {
132
137
} ) ;
133
138
}
134
139
135
- const store = require ( '..' ) ( ) ;
140
+ const store = require ( '..' ) ( { url : redisUrl } ) ;
136
141
yield store . set ( 'key:ttl2' , { a : 1 , b : 2 } , 1000 ) ;
137
142
yield sleep ( 1200 ) ; // Some odd delay introduced by co-mocha
138
143
should . not . exist ( yield store . get ( 'key:ttl2' ) ) ;
0 commit comments