Skip to content

Commit f1a04ef

Browse files
authored
test: Uses service factory instead of repo factory. (#8)
BREAKING CHANGE: Tests in `RepoFacade` implementations need to use a service factory.
1 parent a045e9e commit f1a04ef

File tree

9 files changed

+29
-48
lines changed

9 files changed

+29
-48
lines changed

src/Facade.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import MigrateSignature from './migrate/Signature';
22
import MigrateByKeySignature from './migrateByKey/Signature';
33
import RollbackSignature from './rollback/Signature';
44
import RollbackByKeySignature from './rollbackByKey/Signature';
5+
import Migration from './utils/types/Migration';
56

67
export default interface Facade {
8+
readonly clearMigrations: () => Promise<void>;
9+
readonly getMigrations: () => Promise<Migration[]>;
710
readonly migrate: MigrateSignature;
811
readonly migrateByKey: MigrateByKeySignature;
912
readonly rollback: RollbackSignature;

src/factory.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as sourceMapSupport from 'source-map-support';
22
sourceMapSupport.install();
33

4+
import factory from './factory';
45
import factoryTest from './factoryTest';
56
import testRepoFactory from './utils/tests/testRepoFactory';
67

7-
factoryTest(testRepoFactory);
8+
factoryTest((migrations) => {
9+
const log = () => null;
10+
return factory({ log, repo: testRepoFactory(migrations) });
11+
});

src/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import rollbackByKey from './rollbackByKey';
77

88
export default (config: FacadeConfig): Facade => {
99
return {
10+
clearMigrations: config.repo.clearMigrations,
11+
getMigrations: config.repo.getMigrations,
1012
migrate: migrate(config),
1113
migrateByKey: migrateByKey(config),
1214
rollback: rollback(config),

src/factoryTest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import rollbackTest from './rollback/test';
44
import rollbackByKeyTest from './rollbackByKey/test';
55
import TestFactory from './utils/tests/TestFactory';
66

7-
const testFactory: TestFactory = (repoFactory) => {
7+
const testFactory: TestFactory = (serviceFactory) => {
88
describe('factory', () => {
99
beforeEach(async () => {
10-
await repoFactory([]).clearMigrations();
10+
await serviceFactory([]).clearMigrations();
1111
});
1212

13-
migrateTest(repoFactory);
14-
migrateByKeyTest(repoFactory);
15-
rollbackTest(repoFactory);
16-
rollbackByKeyTest(repoFactory);
13+
migrateTest(serviceFactory);
14+
migrateByKeyTest(serviceFactory);
15+
rollbackTest(serviceFactory);
16+
rollbackByKeyTest(serviceFactory);
1717
});
1818
};
1919

src/migrate/test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import * as assert from 'assert';
22
import * as assertRejects from 'assert-rejects';
33
import 'mocha'; // tslint:disable-line:no-import-side-effect
4-
import factory from '../factory';
54
import DuplicateKeyError from '../utils/errors/DuplicateKeyError';
65
import FailingMigrationError from '../utils/errors/FailingMigrationError';
76
import assertLocked from '../utils/tests/assertLocked';
87
import createMigrationProcess from '../utils/tests/createMigrationProcess';
98
import createTestUpMigration from '../utils/tests/createTestUpMigration';
109
import TestFactory from '../utils/tests/TestFactory';
11-
import Migration from '../utils/types/Migration';
1210

13-
const testMigrate: TestFactory = (repoFactory) => {
11+
const testMigrate: TestFactory = (createService) => {
1412
const successfulMigration = createTestUpMigration(undefined, 'successfulMigration');
1513
const failingMigration = createTestUpMigration(() => { throw new Error(); }, 'failingMigration');
1614
const skippableKey = 'skippableMigration';
1715
const skippableMigration = createTestUpMigration(undefined, skippableKey);
1816

19-
const createService = (migrations: Migration[] = []) => {
20-
const log = () => null;
21-
return factory({ log, repo: repoFactory(migrations) });
22-
};
23-
2417
describe('migrate', () => {
2518
it('should not error when there are no migrations', async () => {
26-
const service = createService();
19+
const service = createService([]);
2720
await service.migrate();
2821
});
2922

@@ -82,7 +75,7 @@ const testMigrate: TestFactory = (repoFactory) => {
8275
});
8376

8477
it('should error when migrations are locked', async () => {
85-
const service = createService();
78+
const service = createService([]);
8679
await assertLocked([service.migrate(), service.migrate()]);
8780
});
8881
});

src/migrateByKey/test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as assert from 'assert';
22
import * as assertRejects from 'assert-rejects';
33
import 'mocha'; // tslint:disable-line:no-import-side-effect
4-
import factory from '../factory';
54
import DuplicateKeyError from '../utils/errors/DuplicateKeyError';
65
import FailingMigrationError from '../utils/errors/FailingMigrationError';
76
import MissingMigrationError from '../utils/errors/MissingMigrationError';
@@ -10,20 +9,14 @@ import assertLocked from '../utils/tests/assertLocked';
109
import createMigrationProcess from '../utils/tests/createMigrationProcess';
1110
import createTestUpMigration, { testMigrationKey } from '../utils/tests/createTestUpMigration';
1211
import TestFactory from '../utils/tests/TestFactory';
13-
import Migration from '../utils/types/Migration';
1412

15-
const testMigrateByKey: TestFactory = (repoFactory) => {
13+
const testMigrateByKey: TestFactory = (createService) => {
1614
const successfulMigration = createTestUpMigration();
1715
const failingMigration = createTestUpMigration(() => { throw new Error(); });
1816

19-
const createService = (migrations: Migration[] = []) => {
20-
const log = () => null;
21-
return factory({ log, repo: repoFactory(migrations) });
22-
};
23-
2417
describe('migrateByKey', () => {
2518
it('should error when the migration is missing', async () => {
26-
const service = createService();
19+
const service = createService([]);
2720
const promise = service.migrateByKey({ key: 'missingKey' });
2821
await assertRejects(promise, MissingMigrationError);
2922
});

src/rollback/test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,25 @@
22
import * as assert from 'assert';
33
import * as assertRejects from 'assert-rejects';
44
import 'mocha'; // tslint:disable-line:no-import-side-effect
5-
import factory from '../factory';
65
import DuplicateKeyError from '../utils/errors/DuplicateKeyError';
76
import FailingMigrationError from '../utils/errors/FailingMigrationError';
87
import MissingMigrationError from '../utils/errors/MissingMigrationError';
98
import assertLocked from '../utils/tests/assertLocked';
109
import createMigrationProcess from '../utils/tests/createMigrationProcess';
1110
import createTestDownMigration from '../utils/tests/createTestDownMigration';
1211
import TestFactory from '../utils/tests/TestFactory';
13-
import Migration from '../utils/types/Migration';
1412

15-
const testRollback: TestFactory = (repoFactory) => {
13+
const testRollback: TestFactory = (createService) => {
1614
const successfulMigration = createTestDownMigration(undefined, 'successfulMigration');
1715
const failingMigration = createTestDownMigration(() => {
1816
throw new Error();
1917
}, 'failingMigration');
2018
const unskippableKey = 'unskippableMigration';
2119
const unskippableMigration = createTestDownMigration(undefined, unskippableKey);
2220

23-
const createService = (migrations: Migration[] = []) => {
24-
const log = () => null;
25-
return factory({ log, repo: repoFactory(migrations) });
26-
};
27-
2821
describe('rollback', () => {
2922
it('should not error when there are no migrations', async () => {
30-
const service = createService();
23+
const service = createService([]);
3124
await service.rollback();
3225
});
3326

@@ -40,7 +33,7 @@ const testRollback: TestFactory = (repoFactory) => {
4033

4134
it('should error when a processed migration is missing', async () => {
4235
await createService([successfulMigration]).migrate();
43-
const promise = createService().rollback();
36+
const promise = createService([]).rollback();
4437
await assertRejects(promise, MissingMigrationError);
4538
});
4639

@@ -98,7 +91,7 @@ const testRollback: TestFactory = (repoFactory) => {
9891
});
9992

10093
it('should error when migrations are locked', async () => {
101-
const service = createService();
94+
const service = createService([]);
10295
await assertLocked([service.rollback(), service.rollback()]);
10396
});
10497
});

src/rollbackByKey/test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as assert from 'assert';
22
import * as assertRejects from 'assert-rejects';
33
import 'mocha'; // tslint:disable-line:no-import-side-effect
4-
import factory from '../factory';
54
import DuplicateKeyError from '../utils/errors/DuplicateKeyError';
65
import FailingMigrationError from '../utils/errors/FailingMigrationError';
76
import UnprocessedMigrationError from '../utils/errors/UnprocessedMigrationError';
@@ -10,20 +9,14 @@ import createMigrationProcess from '../utils/tests/createMigrationProcess';
109
import createTestDownMigration from '../utils/tests/createTestDownMigration';
1110
import { testMigrationKey } from '../utils/tests/createTestUpMigration';
1211
import TestFactory from '../utils/tests/TestFactory';
13-
import Migration from '../utils/types/Migration';
1412

15-
const testRollbackByKey: TestFactory = (repoFactory) => {
13+
const testRollbackByKey: TestFactory = (createService) => {
1614
const successfulMigration = createTestDownMigration();
1715
const failingMigration = createTestDownMigration(() => { throw new Error(); });
1816

19-
const createService = (migrations: Migration[] = []) => {
20-
const log = () => null;
21-
return factory({ log, repo: repoFactory(migrations) });
22-
};
23-
2417
describe('rollbackByKey', () => {
2518
it('should error when the migration is missing', async () => {
26-
const service = createService();
19+
const service = createService([]);
2720
const promise = service.rollbackByKey({ key: 'missingKey' });
2821
await assertRejects(promise, UnprocessedMigrationError);
2922
});

src/utils/tests/TestFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import RepoFacade from '../../RepoFacade';
1+
import Facade from '../../Facade';
22
import Migration from '../types/Migration';
33

4-
type TestFactory = (repoFactory: (migrations: Migration[]) => RepoFacade) => void;
4+
type TestFactory = (serviceFactory: (migrations: Migration[]) => Facade) => void;
55

66
export default TestFactory;

0 commit comments

Comments
 (0)