/
issue-4415.ts
108 lines (96 loc) · 4.32 KB
/
issue-4415.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
import sinon from "sinon";
import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../../src";
import { setupTestingConnections, createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Username } from "./entity/Username";
import { CommandUtils } from "../../../src/commands/CommandUtils";
import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerateCommand";
import { Post } from "./entity/Post";
import { resultsTemplates } from "./results-templates";
describe("github issues > #4415 allow beautify generated migrations", () => {
let connectionOptions: ConnectionOptions[];
let createFileStub: sinon.SinonStub;
let getConnectionOptionsStub: sinon.SinonStub;
let migrationGenerateCommand: MigrationGenerateCommand;
let connectionOptionsReader: ConnectionOptionsReader;
let baseConnectionOptions: ConnectionOptions;
const enabledDrivers = [
"postgres",
"mssql",
"mysql",
"mariadb",
"sqlite",
"better-sqlite3",
"oracle",
"cockroachdb"
] as DatabaseType[];
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory`
const testHandlerArgs = (options: Record<string, any>) => ({
"$0": "test",
"_": ["test"],
"name": "test-migration",
"dir": "test-directory",
...options
});
before(async () => {
// clean out db from any prior tests in case previous state impacts the generated migrations
const connections = await createTestingConnections({
entities: [],
enabledDrivers
});
await reloadTestingDatabases(connections);
await closeTestingConnections(connections);
connectionOptions = await setupTestingConnections({
entities: [Username, Post],
enabledDrivers
});
connectionOptionsReader = new ConnectionOptionsReader();
migrationGenerateCommand = new MigrationGenerateCommand();
createFileStub = sinon.stub(CommandUtils, "createFile");
});
after(() => createFileStub.restore());
it("writes regular migration file when no option is passed", async () => {
for (const connectionOption of connectionOptions) {
createFileStub.resetHistory();
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string);
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({
...baseConnectionOptions,
entities: [Username, Post]
});
await migrationGenerateCommand.handler(testHandlerArgs({
"connection": connectionOption.name
}));
// compare against control test strings in results-templates.ts
for (const control of resultsTemplates[connectionOption.type as string].control) {
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.ts/),
sinon.match(control)
);
}
getConnectionOptionsStub.restore();
}
});
it("writes pretty printed file when pretty option is passed", async () => {
for (const connectionOption of connectionOptions) {
createFileStub.resetHistory();
baseConnectionOptions = await connectionOptionsReader.get(connectionOption.name as string);
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({
...baseConnectionOptions,
entities: [Username, Post]
});
await migrationGenerateCommand.handler(testHandlerArgs({
"connection": connectionOption.name,
"pretty": true
}));
// compare against "pretty" test strings in results-templates.ts
for (const pretty of resultsTemplates[connectionOption.type as string].pretty) {
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.ts/),
sinon.match(pretty)
);
}
getConnectionOptionsStub.restore();
}
});
});