-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
156 lines (128 loc) · 4.78 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const tap = require('tap');
const cfServices = require('./');
const postgres1 = {
label: 'postgres',
plan: 'small',
name: 'postgres1',
tags: ['postgresql', 'sql'],
credentials: {
user: 'postgres1-user',
password: 'postgres1-pass'
}
};
const postgres2 = {
label: 'postgres',
plan: 'large',
name: 'postgres2',
tags: ['postgresql', 'sql', 'db'],
credentials: {
user: 'postgres2-user',
password: 'postgres2-pass'
}
};
const redis1 = {
label: 'redis',
plan: 'small',
name: 'redis1',
credentials: {
user: 'redis1-user',
password: 'redis1-pass'
}
};
const redis2 = {
label: 'redis',
plan: 'large',
name: 'redis2',
credentials: {
user: 'redis2-user',
password: 'redis2-pass'
}
};
const VCAP_SERVICES = JSON.stringify({
postgres: [
postgres1,
postgres2
],
redis: [
redis1,
redis2
]
});
tap.test('cfServices', t => {
delete process.env.VCAP_SERVICES;
t.throws(() => cfServices(), /VCAP_SERVICES/,
'Throws if VCAP_SERVICES is not defined');
process.env.VCAP_SERVICES = 'ala-bala';
t.throws(() => cfServices(), /VCAP_SERVICES/,
'Throws if VCAP_SERVICES is not a valid JSON string');
process.env.VCAP_SERVICES = '5';
t.strictSame(cfServices(), {},
'Returns an empty object if VCAP_SERVICES is not an object');
process.env.VCAP_SERVICES = '{ "a":"x", "b":"y" }';
t.strictSame(cfServices(), {},
'Returns an empty object if VCAP_SERVICES does not contain the right structure');
process.env.VCAP_SERVICES = VCAP_SERVICES;
t.strictSame(cfServices(), {
postgres1,
postgres2,
redis1,
redis2
}, 'Returns an object with all service bindings using instance names as keys');
t.strictSame(cfServices('redis1'), redis1,
'Returns the binding for the service instance with given name');
t.throws(() => cfServices('nosuch'),
/No service instance with name nosuch/,
'Throws, if there is no service instance with given name');
t.throws(() => cfServices({ tags: ['sql'] }),
/Multiple.*sql.*postgres1.*postgres2/,
'Throws, if multiple service bindings match the given query');
t.throws(() => cfServices({ tags: ['sql'] }, 'SQL db'),
/Multiple.*SQL db.*postgres1.*postgres2/,
'Includes the query description the error message');
t.strictSame(cfServices({ tags: ['db', 'sql'] }), postgres2,
'Returns the single service binding matching the query');
t.strictSame(cfServices({ label: 'redis', plan: 'large' }), redis2,
'Returns the service binding with matching label and plan');
t.strictSame(cfServices(binding => /res2/.test(binding.name)), postgres2,
'Returns the single service binding matching the filter function');
t.throws(() => cfServices(binding => /s3/.test(binding.label), 'S3'),
/No.*S3/,
'Throws, if no service binding matches the given filter function');
t.throws(() => cfServices(binding => /res/.test(binding.label)),
/Multiple.*filter.*postgres1.*postgres2/,
'Throws, if multiple service bindings match the given filter function');
t.end();
});
tap.test('cfServices.filter', t => {
delete process.env.VCAP_SERVICES;
t.throws(() => cfServices.filter(), /VCAP_SERVICES/,
'Throws if VCAP_SERVICES is not defined');
process.env.VCAP_SERVICES = 'ala-bala';
t.throws(() => cfServices.filter(), /VCAP_SERVICES/,
'Throws if VCAP_SERVICES is not a valid JSON string');
process.env.VCAP_SERVICES = '5';
t.strictSame(cfServices.filter(), [],
'Returns an empty array if VCAP_SERVICES is not an object');
process.env.VCAP_SERVICES = '{ "a":"x", "b":"y" }';
t.strictSame(cfServices.filter(), [],
'Returns an empty array if VCAP_SERVICES does not contain the right structure');
process.env.VCAP_SERVICES = VCAP_SERVICES;
t.strictSame(cfServices.filter(), [postgres1, postgres2, redis1, redis2],
'Returns all service bindings when called without arguments');
t.strictSame(cfServices.filter('redis1'), [],
'Returns an empty array when called with a string');
t.strictSame(cfServices.filter({ name: 'redis1' }), [redis1],
'Returns all servce bindings with matching instance name');
t.strictSame(cfServices.filter({ name: 'nosuch' }), [],
'Returns an empty array, if there are no matching servce bindings');
t.strictSame(cfServices.filter({ tags: ['sql'] }), [postgres1, postgres2],
'Returns all servce bindings with matching tag');
t.strictSame(cfServices.filter({ tags: ['db', 'sql'] }), [postgres2],
'Returns all servce bindings with matching tags');
t.strictSame(cfServices.filter({ label: 'redis', plan: 'large' }), [redis2],
'Returns all servce bindings with matching label and plan');
t.strictSame(cfServices.filter(binding => /postgre/.test(binding.label)), [postgres1, postgres2],
'Returns all servce bindings matching a custom filter function');
t.end();
});