UNPKG

couchbase-index-manager

Version:
117 lines 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const configuration_1 = require("../configuration"); const definition_1 = require("../definition"); const create_index_mutation_1 = require("./create-index-mutation"); const plan_1 = require("./plan"); const MockManager = jest.fn(() => ({ buildDeferredIndexes: jest.fn().mockResolvedValue([]), waitForIndexBuild: jest.fn().mockResolvedValue(true), getIndexes: jest.fn().mockResolvedValue([]), })); const planOptions = { logger: { log: () => { }, error: () => { }, warn: () => { }, info: () => { }, debug: () => { } }, buildDelay: 0, // don't delay during unit tests }; describe('execute', function () { it('applies in order', async function () { const first = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'first', index_key: 'key', })); const firstStub = first.execute = jest.fn().mockImplementation(() => Promise.resolve()); const second = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'second', index_key: 'key', })); const secondStub = second.execute = jest.fn().mockImplementation(() => Promise.resolve()); const plan = new plan_1.Plan(new MockManager(), [first, second], planOptions); await plan.execute(); expect(firstStub) .toHaveBeenCalledBefore(secondStub); expect(secondStub) .toHaveBeenCalledTimes(1); }); it('groups phases', async function () { const first = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'first', index_key: 'key', })); const firstStub = first.execute = jest.fn().mockImplementation(() => Promise.resolve()); const second = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'second', index_key: 'key', })); second.phase = 2; const secondStub = second.execute = jest.fn().mockImplementation(() => Promise.resolve()); const third = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'third', index_key: 'key', })); const thirdStub = third.execute = jest.fn().mockImplementation(() => Promise.resolve()); const plan = new plan_1.Plan(new MockManager(), [first, second, third], planOptions); await plan.execute(); expect(firstStub) .toHaveBeenCalledBefore(thirdStub); expect(thirdStub) .toHaveBeenCalledBefore(secondStub); expect(secondStub).toHaveBeenCalledTimes(1); }); it('phase failure runs rest of phase', async function () { const first = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'first', index_key: 'key', })); first.execute = jest.fn().mockImplementation(() => { throw new Error('Error'); }); const second = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'second', index_key: 'key', })); const secondStub = second.execute = jest.fn().mockImplementation(() => Promise.resolve()); const plan = new plan_1.Plan(new MockManager(), [first, second], planOptions); try { await plan.execute(); } catch (_a) { // eat errors } expect(secondStub).toHaveBeenCalledTimes(1); }); it('phase failure skips subsequent phases', async function () { const first = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'first', index_key: 'key', })); first.execute = jest.fn().mockImplementation(() => { throw new Error('Error'); }); const second = new create_index_mutation_1.CreateIndexMutation(new definition_1.IndexDefinition({ type: configuration_1.ConfigurationType.Index, name: 'second', index_key: 'key', })); second.phase = 2; const secondStub = second.execute = jest.fn().mockImplementation(() => Promise.resolve()); const plan = new plan_1.Plan(new MockManager(), [first, second], planOptions); try { await plan.execute(); } catch (_a) { // eat errors } expect(secondStub).not.toHaveBeenCalled(); }); }); //# sourceMappingURL=plan.spec.js.map