leo-connector-common 5.0.0 → 5.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/base.js +45 -45
- package/checksum/index.js +793 -793
- package/checksum/lib/basicConnector.js +330 -330
- package/checksum/lib/checksumNibbler.js +401 -401
- package/checksum/lib/handler.js +75 -75
- package/checksum/lib/nibbler.js +168 -168
- package/checksum/lib/test.js +205 -205
- package/checksum/tests/body-too-large.utest.js +296 -296
- package/datawarehouse/combine.js +149 -149
- package/datawarehouse/load.js +366 -366
- package/datawarehouse/transform.js +64 -64
- package/dol.js +733 -733
- package/package.json +38 -38
- package/sql/nibbler.js +164 -164
- package/sql/snapshotter.js +287 -287
- package/sql/test/test.js +97 -97
- package/test/checksum/index.test.js +10 -10
- package/utils/validation.js +127 -127
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
process.env.NODE_ENV = "dev";
|
|
2
|
-
//process.env.LEO_LOGGER = "/.*/tide";
|
|
3
|
-
let config = require("leo-config").bootstrap({
|
|
4
|
-
dev: {
|
|
5
|
-
leosdk: {
|
|
6
|
-
LeoCron: "LeoCronTable",
|
|
7
|
-
LeoSettings: "LeoSettingsTable"
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
require("chai").should();
|
|
12
|
-
const assert = require("chai").assert;
|
|
13
|
-
const moment = require("moment");
|
|
14
|
-
let leoaws = mock(require("leo-aws"), {
|
|
15
|
-
dynamodb: {
|
|
16
|
-
LeoCronTable: {},
|
|
17
|
-
LeoSettingsTable: {}
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const checksum = require("..");
|
|
22
|
-
|
|
23
|
-
describe('checksum', function() {
|
|
24
|
-
it('body-too-large error', async function() {
|
|
25
|
-
this.timeout(1000 * 10);
|
|
26
|
-
let options = {
|
|
27
|
-
shouldDelete: true,
|
|
28
|
-
sample: false,
|
|
29
|
-
reverse: true,
|
|
30
|
-
limit: 2,
|
|
31
|
-
maxLimit: 2,
|
|
32
|
-
stop_at: moment().add({
|
|
33
|
-
seconds: 30
|
|
34
|
-
})
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
let master = checksum.basicConnector("Master DB", {
|
|
39
|
-
id_column: "id"
|
|
40
|
-
}, ObjectConnectorMock([{
|
|
41
|
-
id: 1,
|
|
42
|
-
data: "hello"
|
|
43
|
-
}, {
|
|
44
|
-
id: 2,
|
|
45
|
-
data: "hello"
|
|
46
|
-
}, {
|
|
47
|
-
id: 10000000
|
|
48
|
-
}]));
|
|
49
|
-
|
|
50
|
-
let slave = checksum.basicConnector("Slave DB", {
|
|
51
|
-
id_column: "id"
|
|
52
|
-
}, ObjectConnectorMock([{
|
|
53
|
-
id: 1,
|
|
54
|
-
data: "hello"
|
|
55
|
-
}, ...((cnt) => {
|
|
56
|
-
var a = [];
|
|
57
|
-
for (var i = 2; i < cnt + 2; i++) {
|
|
58
|
-
a.push({
|
|
59
|
-
id: i
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
return a;
|
|
63
|
-
})(100), {
|
|
64
|
-
id: 1000000,
|
|
65
|
-
data: "hello2"
|
|
66
|
-
}, {
|
|
67
|
-
id: 10000000
|
|
68
|
-
}], {
|
|
69
|
-
individual: [() => {
|
|
70
|
-
return Promise.reject('{ "errorMessage" : "body size is too long"}');
|
|
71
|
-
}]
|
|
72
|
-
}));
|
|
73
|
-
|
|
74
|
-
let results = await checksum.checksum("test", "test-bot-id", master, slave, options);
|
|
75
|
-
//console.log(JSON.stringify(await leoaws.dynamodb.get(config.leosdk.LeoCron, "test-bot-id"), null, 2));
|
|
76
|
-
assert.equal(results.totals.correct, 2, "Correct count doesn't match");
|
|
77
|
-
assert.equal(results.totals.incorrect, 1, "Incorrect count doesn't match");
|
|
78
|
-
assert.equal(results.totals.missing, 0, "Missing count doesn't match");
|
|
79
|
-
assert.equal(results.totals.extra, 100, "Extra count doesn't match");
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
function ObjectConnectorMock(database, config) {
|
|
87
|
-
config = {
|
|
88
|
-
id: "id",
|
|
89
|
-
batch: [],
|
|
90
|
-
individual: [],
|
|
91
|
-
calls: {},
|
|
92
|
-
...config
|
|
93
|
-
};
|
|
94
|
-
let db = database;
|
|
95
|
-
if (Array.isArray(db)) {
|
|
96
|
-
let tmp = {};
|
|
97
|
-
db.map(r => {
|
|
98
|
-
tmp[r[config.id]] = r;
|
|
99
|
-
});
|
|
100
|
-
db = tmp;
|
|
101
|
-
}
|
|
102
|
-
let obj = {
|
|
103
|
-
getConfig: function() {
|
|
104
|
-
return config;
|
|
105
|
-
},
|
|
106
|
-
// Respond to start and end
|
|
107
|
-
// Return Stream, Array, or a Hash
|
|
108
|
-
batch: function(start, end) {
|
|
109
|
-
//console.log(`batch: ${start}, ${end}`);
|
|
110
|
-
let data = [];
|
|
111
|
-
for (let i = start; i <= end; i++) {
|
|
112
|
-
let v = db[i];
|
|
113
|
-
if (v !== undefined) {
|
|
114
|
-
data.push(v);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return Promise.resolve(data);
|
|
118
|
-
},
|
|
119
|
-
|
|
120
|
-
// Respond to start and end
|
|
121
|
-
// Return Stream, Array, or a Hash
|
|
122
|
-
individual: function(start, end) {
|
|
123
|
-
//console.log(`individual: ${start}, ${end}`);
|
|
124
|
-
let data = [];
|
|
125
|
-
for (let i = start; i <= end; i++) {
|
|
126
|
-
let v = db[i];
|
|
127
|
-
if (v !== undefined) {
|
|
128
|
-
data.push(v);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return Promise.resolve(data);
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
// Respond to ids
|
|
135
|
-
// Return Stream, Array
|
|
136
|
-
sample: function(ids) {
|
|
137
|
-
//console.log(`sample: ${ids}`);
|
|
138
|
-
let data = [];
|
|
139
|
-
ids.map(id => {
|
|
140
|
-
let v = db[id];
|
|
141
|
-
if (v !== undefined) {
|
|
142
|
-
data.push(v);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
return Promise.resolve(data);
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
// Respond to start and end -- options
|
|
150
|
-
// Return object with min, max, total
|
|
151
|
-
range: function(start, end) {
|
|
152
|
-
|
|
153
|
-
//console.log(`range: ${start}, ${end}`);
|
|
154
|
-
let min = null;
|
|
155
|
-
let max = null;
|
|
156
|
-
let total = 0;
|
|
157
|
-
Object.keys(db).map(id => {
|
|
158
|
-
id = db[id][this.settings.id_column];
|
|
159
|
-
if ((start == undefined || id >= start) && (end == undefined || id <= end)) {
|
|
160
|
-
total++;
|
|
161
|
-
if (min == null || id < min) {
|
|
162
|
-
min = id;
|
|
163
|
-
}
|
|
164
|
-
if (max == null || id > max) {
|
|
165
|
-
max = id;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
return Promise.resolve({
|
|
170
|
-
min,
|
|
171
|
-
max,
|
|
172
|
-
total
|
|
173
|
-
});
|
|
174
|
-
},
|
|
175
|
-
|
|
176
|
-
// Responds to start, end, limit, reverse
|
|
177
|
-
// Returns object with next, current
|
|
178
|
-
nibble: function(start, end, limit, reverse) {
|
|
179
|
-
|
|
180
|
-
//console.log(`nibble: ${start}, ${end}, ${limit}, ${reverse}`);
|
|
181
|
-
let current = null;
|
|
182
|
-
let next = null;
|
|
183
|
-
let dir = 1;
|
|
184
|
-
let ostart = start;
|
|
185
|
-
let oend = end;
|
|
186
|
-
if (reverse) {
|
|
187
|
-
start = end;
|
|
188
|
-
end = ostart;
|
|
189
|
-
dir = -1;
|
|
190
|
-
}
|
|
191
|
-
let cnt = 0;
|
|
192
|
-
for (let i = start; i >= ostart && i <= oend; i += dir) {
|
|
193
|
-
let v = db[i];
|
|
194
|
-
if (v !== undefined) {
|
|
195
|
-
cnt++;
|
|
196
|
-
if (cnt >= limit) {
|
|
197
|
-
if (!current) {
|
|
198
|
-
current = v[this.settings.id_column];
|
|
199
|
-
} else {
|
|
200
|
-
next = v[this.settings.id_column];
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return Promise.resolve({
|
|
208
|
-
current,
|
|
209
|
-
next
|
|
210
|
-
});
|
|
211
|
-
},
|
|
212
|
-
|
|
213
|
-
// Called With data
|
|
214
|
-
// Returns a session
|
|
215
|
-
initialize: function(data) {
|
|
216
|
-
//console.log(`initialize: ${JSON.stringify(data,null,2)}`);
|
|
217
|
-
return Promise.resolve({});
|
|
218
|
-
},
|
|
219
|
-
|
|
220
|
-
// Called With data
|
|
221
|
-
destroy: function(data) {
|
|
222
|
-
//console.log(`destroy: ${JSON.stringify(data,null,2)}`);
|
|
223
|
-
return Promise.resolve();
|
|
224
|
-
},
|
|
225
|
-
|
|
226
|
-
// Respond to ids
|
|
227
|
-
// No return
|
|
228
|
-
delete: function(ids) {
|
|
229
|
-
//console.log(`delete: ${ids}, not: ${this.not_ids} start: ${this.start} end:${this.end}`);
|
|
230
|
-
|
|
231
|
-
(ids || []).map(id => {
|
|
232
|
-
if (id in db) {
|
|
233
|
-
delete db[id];
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
if (this.not_ids && this.start && this.end) {
|
|
238
|
-
let not = {};
|
|
239
|
-
(this.not_ids || []).map(id => not[id] = true);
|
|
240
|
-
for (let i = this.start; i <= this.end; i++) {
|
|
241
|
-
if (!(i in not)) {
|
|
242
|
-
delete db[i];
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return Promise.resolve();
|
|
248
|
-
},
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
Object.keys(obj).map(key => {
|
|
252
|
-
let fn = obj[key];
|
|
253
|
-
obj[key] = function(...args) {
|
|
254
|
-
let index = config.calls[key] || 0;
|
|
255
|
-
config.calls[key] = index + 1;
|
|
256
|
-
if (config[key] && config[key][index]) {
|
|
257
|
-
return config[key][index].apply(this, args);
|
|
258
|
-
} else {
|
|
259
|
-
return fn.apply(this, args);
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
return obj;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
let merge = require("lodash.merge");
|
|
269
|
-
|
|
270
|
-
function mock(leoaws, options = {}) {
|
|
271
|
-
let db = Object.assign({}, options.dynamodb);
|
|
272
|
-
leoaws.dynamodb = {
|
|
273
|
-
get: function(table, id, opts = {}) {
|
|
274
|
-
if (!(table in db)) {
|
|
275
|
-
return Promise.reject(new Error("Table does not exist:" + table))
|
|
276
|
-
}
|
|
277
|
-
if (id in db[table]) {
|
|
278
|
-
return Promise.resolve(db[table][id]);
|
|
279
|
-
}
|
|
280
|
-
return Promise.resolve();
|
|
281
|
-
},
|
|
282
|
-
merge: function(table, id, data) {
|
|
283
|
-
if (!(table in db)) {
|
|
284
|
-
db[table] = {}
|
|
285
|
-
}
|
|
286
|
-
merge(db[table], {
|
|
287
|
-
[id]: data
|
|
288
|
-
});
|
|
289
|
-
return Promise.resolve({})
|
|
290
|
-
},
|
|
291
|
-
getDB: function() {
|
|
292
|
-
return db;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
return leoaws;
|
|
296
|
-
}
|
|
1
|
+
process.env.NODE_ENV = "dev";
|
|
2
|
+
//process.env.LEO_LOGGER = "/.*/tide";
|
|
3
|
+
let config = require("leo-config").bootstrap({
|
|
4
|
+
dev: {
|
|
5
|
+
leosdk: {
|
|
6
|
+
LeoCron: "LeoCronTable",
|
|
7
|
+
LeoSettings: "LeoSettingsTable"
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
require("chai").should();
|
|
12
|
+
const assert = require("chai").assert;
|
|
13
|
+
const moment = require("moment");
|
|
14
|
+
let leoaws = mock(require("leo-aws"), {
|
|
15
|
+
dynamodb: {
|
|
16
|
+
LeoCronTable: {},
|
|
17
|
+
LeoSettingsTable: {}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const checksum = require("..");
|
|
22
|
+
|
|
23
|
+
describe('checksum', function() {
|
|
24
|
+
it('body-too-large error', async function() {
|
|
25
|
+
this.timeout(1000 * 10);
|
|
26
|
+
let options = {
|
|
27
|
+
shouldDelete: true,
|
|
28
|
+
sample: false,
|
|
29
|
+
reverse: true,
|
|
30
|
+
limit: 2,
|
|
31
|
+
maxLimit: 2,
|
|
32
|
+
stop_at: moment().add({
|
|
33
|
+
seconds: 30
|
|
34
|
+
})
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
let master = checksum.basicConnector("Master DB", {
|
|
39
|
+
id_column: "id"
|
|
40
|
+
}, ObjectConnectorMock([{
|
|
41
|
+
id: 1,
|
|
42
|
+
data: "hello"
|
|
43
|
+
}, {
|
|
44
|
+
id: 2,
|
|
45
|
+
data: "hello"
|
|
46
|
+
}, {
|
|
47
|
+
id: 10000000
|
|
48
|
+
}]));
|
|
49
|
+
|
|
50
|
+
let slave = checksum.basicConnector("Slave DB", {
|
|
51
|
+
id_column: "id"
|
|
52
|
+
}, ObjectConnectorMock([{
|
|
53
|
+
id: 1,
|
|
54
|
+
data: "hello"
|
|
55
|
+
}, ...((cnt) => {
|
|
56
|
+
var a = [];
|
|
57
|
+
for (var i = 2; i < cnt + 2; i++) {
|
|
58
|
+
a.push({
|
|
59
|
+
id: i
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return a;
|
|
63
|
+
})(100), {
|
|
64
|
+
id: 1000000,
|
|
65
|
+
data: "hello2"
|
|
66
|
+
}, {
|
|
67
|
+
id: 10000000
|
|
68
|
+
}], {
|
|
69
|
+
individual: [() => {
|
|
70
|
+
return Promise.reject('{ "errorMessage" : "body size is too long"}');
|
|
71
|
+
}]
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
let results = await checksum.checksum("test", "test-bot-id", master, slave, options);
|
|
75
|
+
//console.log(JSON.stringify(await leoaws.dynamodb.get(config.leosdk.LeoCron, "test-bot-id"), null, 2));
|
|
76
|
+
assert.equal(results.totals.correct, 2, "Correct count doesn't match");
|
|
77
|
+
assert.equal(results.totals.incorrect, 1, "Incorrect count doesn't match");
|
|
78
|
+
assert.equal(results.totals.missing, 0, "Missing count doesn't match");
|
|
79
|
+
assert.equal(results.totals.extra, 100, "Extra count doesn't match");
|
|
80
|
+
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
function ObjectConnectorMock(database, config) {
|
|
87
|
+
config = {
|
|
88
|
+
id: "id",
|
|
89
|
+
batch: [],
|
|
90
|
+
individual: [],
|
|
91
|
+
calls: {},
|
|
92
|
+
...config
|
|
93
|
+
};
|
|
94
|
+
let db = database;
|
|
95
|
+
if (Array.isArray(db)) {
|
|
96
|
+
let tmp = {};
|
|
97
|
+
db.map(r => {
|
|
98
|
+
tmp[r[config.id]] = r;
|
|
99
|
+
});
|
|
100
|
+
db = tmp;
|
|
101
|
+
}
|
|
102
|
+
let obj = {
|
|
103
|
+
getConfig: function() {
|
|
104
|
+
return config;
|
|
105
|
+
},
|
|
106
|
+
// Respond to start and end
|
|
107
|
+
// Return Stream, Array, or a Hash
|
|
108
|
+
batch: function(start, end) {
|
|
109
|
+
//console.log(`batch: ${start}, ${end}`);
|
|
110
|
+
let data = [];
|
|
111
|
+
for (let i = start; i <= end; i++) {
|
|
112
|
+
let v = db[i];
|
|
113
|
+
if (v !== undefined) {
|
|
114
|
+
data.push(v);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return Promise.resolve(data);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
// Respond to start and end
|
|
121
|
+
// Return Stream, Array, or a Hash
|
|
122
|
+
individual: function(start, end) {
|
|
123
|
+
//console.log(`individual: ${start}, ${end}`);
|
|
124
|
+
let data = [];
|
|
125
|
+
for (let i = start; i <= end; i++) {
|
|
126
|
+
let v = db[i];
|
|
127
|
+
if (v !== undefined) {
|
|
128
|
+
data.push(v);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return Promise.resolve(data);
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
// Respond to ids
|
|
135
|
+
// Return Stream, Array
|
|
136
|
+
sample: function(ids) {
|
|
137
|
+
//console.log(`sample: ${ids}`);
|
|
138
|
+
let data = [];
|
|
139
|
+
ids.map(id => {
|
|
140
|
+
let v = db[id];
|
|
141
|
+
if (v !== undefined) {
|
|
142
|
+
data.push(v);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
return Promise.resolve(data);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
// Respond to start and end -- options
|
|
150
|
+
// Return object with min, max, total
|
|
151
|
+
range: function(start, end) {
|
|
152
|
+
|
|
153
|
+
//console.log(`range: ${start}, ${end}`);
|
|
154
|
+
let min = null;
|
|
155
|
+
let max = null;
|
|
156
|
+
let total = 0;
|
|
157
|
+
Object.keys(db).map(id => {
|
|
158
|
+
id = db[id][this.settings.id_column];
|
|
159
|
+
if ((start == undefined || id >= start) && (end == undefined || id <= end)) {
|
|
160
|
+
total++;
|
|
161
|
+
if (min == null || id < min) {
|
|
162
|
+
min = id;
|
|
163
|
+
}
|
|
164
|
+
if (max == null || id > max) {
|
|
165
|
+
max = id;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return Promise.resolve({
|
|
170
|
+
min,
|
|
171
|
+
max,
|
|
172
|
+
total
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
// Responds to start, end, limit, reverse
|
|
177
|
+
// Returns object with next, current
|
|
178
|
+
nibble: function(start, end, limit, reverse) {
|
|
179
|
+
|
|
180
|
+
//console.log(`nibble: ${start}, ${end}, ${limit}, ${reverse}`);
|
|
181
|
+
let current = null;
|
|
182
|
+
let next = null;
|
|
183
|
+
let dir = 1;
|
|
184
|
+
let ostart = start;
|
|
185
|
+
let oend = end;
|
|
186
|
+
if (reverse) {
|
|
187
|
+
start = end;
|
|
188
|
+
end = ostart;
|
|
189
|
+
dir = -1;
|
|
190
|
+
}
|
|
191
|
+
let cnt = 0;
|
|
192
|
+
for (let i = start; i >= ostart && i <= oend; i += dir) {
|
|
193
|
+
let v = db[i];
|
|
194
|
+
if (v !== undefined) {
|
|
195
|
+
cnt++;
|
|
196
|
+
if (cnt >= limit) {
|
|
197
|
+
if (!current) {
|
|
198
|
+
current = v[this.settings.id_column];
|
|
199
|
+
} else {
|
|
200
|
+
next = v[this.settings.id_column];
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return Promise.resolve({
|
|
208
|
+
current,
|
|
209
|
+
next
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
// Called With data
|
|
214
|
+
// Returns a session
|
|
215
|
+
initialize: function(data) {
|
|
216
|
+
//console.log(`initialize: ${JSON.stringify(data,null,2)}`);
|
|
217
|
+
return Promise.resolve({});
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
// Called With data
|
|
221
|
+
destroy: function(data) {
|
|
222
|
+
//console.log(`destroy: ${JSON.stringify(data,null,2)}`);
|
|
223
|
+
return Promise.resolve();
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
// Respond to ids
|
|
227
|
+
// No return
|
|
228
|
+
delete: function(ids) {
|
|
229
|
+
//console.log(`delete: ${ids}, not: ${this.not_ids} start: ${this.start} end:${this.end}`);
|
|
230
|
+
|
|
231
|
+
(ids || []).map(id => {
|
|
232
|
+
if (id in db) {
|
|
233
|
+
delete db[id];
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
if (this.not_ids && this.start && this.end) {
|
|
238
|
+
let not = {};
|
|
239
|
+
(this.not_ids || []).map(id => not[id] = true);
|
|
240
|
+
for (let i = this.start; i <= this.end; i++) {
|
|
241
|
+
if (!(i in not)) {
|
|
242
|
+
delete db[i];
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return Promise.resolve();
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
Object.keys(obj).map(key => {
|
|
252
|
+
let fn = obj[key];
|
|
253
|
+
obj[key] = function(...args) {
|
|
254
|
+
let index = config.calls[key] || 0;
|
|
255
|
+
config.calls[key] = index + 1;
|
|
256
|
+
if (config[key] && config[key][index]) {
|
|
257
|
+
return config[key][index].apply(this, args);
|
|
258
|
+
} else {
|
|
259
|
+
return fn.apply(this, args);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
return obj;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
let merge = require("lodash.merge");
|
|
269
|
+
|
|
270
|
+
function mock(leoaws, options = {}) {
|
|
271
|
+
let db = Object.assign({}, options.dynamodb);
|
|
272
|
+
leoaws.dynamodb = {
|
|
273
|
+
get: function(table, id, opts = {}) {
|
|
274
|
+
if (!(table in db)) {
|
|
275
|
+
return Promise.reject(new Error("Table does not exist:" + table))
|
|
276
|
+
}
|
|
277
|
+
if (id in db[table]) {
|
|
278
|
+
return Promise.resolve(db[table][id]);
|
|
279
|
+
}
|
|
280
|
+
return Promise.resolve();
|
|
281
|
+
},
|
|
282
|
+
merge: function(table, id, data) {
|
|
283
|
+
if (!(table in db)) {
|
|
284
|
+
db[table] = {}
|
|
285
|
+
}
|
|
286
|
+
merge(db[table], {
|
|
287
|
+
[id]: data
|
|
288
|
+
});
|
|
289
|
+
return Promise.resolve({})
|
|
290
|
+
},
|
|
291
|
+
getDB: function() {
|
|
292
|
+
return db;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return leoaws;
|
|
296
|
+
}
|