|
| 1 | +import 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | + |
| 5 | +import { |
| 6 | + initialize, initializeFrom, |
| 7 | +} from '../../../src'; |
| 8 | + |
| 9 | +import { toDynamodbRecords, fromDynamodb } from '../../../src/from/dynamodb'; |
| 10 | + |
| 11 | +import Connector from '../../../src/connectors/scheduler'; |
| 12 | +import { scheduler } from '../../../src/flavors/scheduler'; |
| 13 | + |
| 14 | +describe('flavors/scheduler.js', () => { |
| 15 | + beforeEach(() => { |
| 16 | + sinon.stub(Connector.prototype, 'schedule').resolves({}); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + sinon.restore(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should execute', (done) => { |
| 24 | + const events = toDynamodbRecords([ |
| 25 | + { |
| 26 | + timestamp: 1572832690, |
| 27 | + keys: { |
| 28 | + pk: '1', |
| 29 | + sk: 'thing', |
| 30 | + }, |
| 31 | + newImage: { |
| 32 | + pk: '1', |
| 33 | + sk: 'thing', |
| 34 | + discriminator: 'thing', |
| 35 | + name: 'Thing One', |
| 36 | + description: 'This is thing one', |
| 37 | + otherThing: 'thing|2', |
| 38 | + ttl: 1549053422, |
| 39 | + timestamp: 1548967022000, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + timestamp: 1572832690, |
| 44 | + keys: { |
| 45 | + pk: '1', |
| 46 | + sk: 'other', |
| 47 | + }, |
| 48 | + newImage: { |
| 49 | + pk: '1', |
| 50 | + sk: 'other', |
| 51 | + discriminator: 'other', |
| 52 | + name: 'Other One', |
| 53 | + description: 'This is other one', |
| 54 | + ttl: 1549053422, |
| 55 | + timestamp: 1548967022000, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ]); |
| 59 | + |
| 60 | + initialize({ |
| 61 | + ...initializeFrom(rules), |
| 62 | + }) |
| 63 | + .assemble(fromDynamodb(events), false) |
| 64 | + .collect() |
| 65 | + // .tap((collected) => console.log(JSON.stringify(collected, null, 2))) |
| 66 | + .tap((collected) => { |
| 67 | + expect(collected.length).to.equal(1); |
| 68 | + expect(collected[0].pipeline).to.equal('schedule1'); |
| 69 | + expect(collected[0].event.type).to.equal('thing-created'); |
| 70 | + expect(collected[0].scheduleRequest).to.deep.equal({ |
| 71 | + Name: 'test schedule', |
| 72 | + ActionAfterCompletion: 'DELETE', |
| 73 | + FlexibleTimeWindow: { |
| 74 | + Mode: 'OFF', |
| 75 | + }, |
| 76 | + }); |
| 77 | + expect(collected[0].scheduleResponse).to.deep.equal({}); |
| 78 | + }) |
| 79 | + .done(done); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +export const toScheduleRequest = (uow) => ({ |
| 84 | + Name: 'test schedule', |
| 85 | + ActionAfterCompletion: 'DELETE', |
| 86 | + FlexibleTimeWindow: { |
| 87 | + Mode: 'OFF', |
| 88 | + }, |
| 89 | +}); |
| 90 | + |
| 91 | +const rules = [ |
| 92 | + { |
| 93 | + id: 'schedule1', |
| 94 | + flavor: scheduler, |
| 95 | + eventType: /thing-*/, |
| 96 | + filters: [() => true], |
| 97 | + toScheduleRequest, |
| 98 | + }, |
| 99 | + { |
| 100 | + id: 'update-other1', |
| 101 | + flavor: scheduler, |
| 102 | + eventType: 'x9', |
| 103 | + }, |
| 104 | +]; |
0 commit comments