-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.ts
156 lines (127 loc) 路 3.82 KB
/
index.ts
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
import { cliSeeder } from '../../src/index';
import { MongoClient } from 'mongodb';
import * as tsNode from 'ts-node';
console.error = jest.fn();
console.log = jest.fn();
describe('CLI', () => {
const previousArgv = process.argv;
beforeEach(() => {
jest.resetModules();
process.argv = { ...previousArgv };
});
afterEach(() => {
process.argv = previousArgv;
});
it('should import data', async () => {
const dbConnectionUri = process.env.DB_URI
? process.env.DB_URI
: 'mongodb://127.0.0.1:27017/clidb';
const databaseName = process.env.DB_NAME ? process.env.DB_NAME : 'clidb';
const exit = jest.spyOn(process, 'exit').mockImplementation();
process.argv = [
'',
'',
'--replace-id',
'--drop-collections',
'./test/integration/_importdata',
'--db-uri',
dbConnectionUri,
];
await cliSeeder.run();
expect(console.error).not.toBeCalled();
expect(exit).toBeCalledWith(0);
const client = await MongoClient.connect(dbConnectionUri, {
ignoreUndefined: true,
useNewUrlParser: true,
});
const db = client.db(databaseName);
const testCases = [
{
collectionName: 'cliobjects',
expectedLength: 3,
},
{
collectionName: 'cliarrays',
expectedLength: 6,
},
];
for (const testCase of testCases) {
const collection = await db
.collection(testCase.collectionName)
.find()
.toArray();
expect(collection).toHaveLength(testCase.expectedLength);
expect(collection).toContainEqual(
expect.objectContaining({
_id: 'onetest',
number: 1,
name: 'one',
}),
);
expect(collection).toContainEqual(
expect.objectContaining({
number: 2,
name: 'two',
}),
);
expect(collection).toContainEqual({
_id: 'threetest',
number: 3,
name: 'three',
});
}
await client.close(true);
});
it('should show help', async () => {
const exit = jest.spyOn(process, 'exit').mockImplementation();
process.argv = ['', '', '--help'];
await cliSeeder.run();
expect(console.log).toBeCalledWith(
expect.stringContaining('Mongo Seeding CLI'),
);
expect(exit).toBeCalledWith(0);
});
it('should exit without error when no data to import', async () => {
const exit = jest.spyOn(process, 'exit').mockImplementation();
process.argv = ['', '', './no-path'];
await cliSeeder.run();
expect(exit).toBeCalledWith(0);
});
it('should allow transpile only mode for TS files', async () => {
const registerTsNode = jest.spyOn(tsNode, 'register');
jest.spyOn(process, 'exit').mockImplementation();
process.argv = ['', '', '--transpile-only', './no-path'];
await cliSeeder.run();
expect(registerTsNode).toBeCalledWith({
transpileOnly: true,
});
});
it('should exit with error on incorrect values', async () => {
const exit = jest.spyOn(process, 'exit').mockImplementation();
const testCases = [
{
argv: ['', '', '--reconnect-timeout', '-5'],
},
{
argv: ['', '', '--db-port', '-5'],
},
];
for (const testCase of testCases) {
process.argv = testCase.argv;
await cliSeeder.run();
expect(console.error).toBeCalledWith(
expect.stringContaining('InvalidParameterError'),
);
expect(exit).toBeCalledWith(0);
}
});
it('should exit with error on command line arguments error', async () => {
const exit = jest.spyOn(process, 'exit').mockImplementation();
process.argv = ['', '', '--what-is-this-parameter', 'dunno'];
await cliSeeder.run();
expect(console.error).toBeCalledWith(
expect.stringContaining('InvalidParameterError'),
);
expect(exit).toBeCalledWith(0);
});
});