|
| 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 { toFirehoseRecords, fromFirehose } from '../../../src/from/firehose'; |
| 10 | +import { firehoseTransform, firehoseDrop } from '../../../src/flavors/firehose'; |
| 11 | + |
| 12 | +describe('flavors/firehose.js', () => { |
| 13 | + beforeEach(() => { |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(sinon.restore); |
| 17 | + |
| 18 | + it('should execute', (done) => { |
| 19 | + const events = toFirehoseRecords([ |
| 20 | + { |
| 21 | + type: 't1', |
| 22 | + timestamp: 1734754684001, |
| 23 | + thing: { |
| 24 | + id: '1', |
| 25 | + name: 'Thing One', |
| 26 | + description: 'This is thing one', |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + type: 't2', |
| 31 | + timestamp: 1734754684001, |
| 32 | + thing: { |
| 33 | + id: '2', |
| 34 | + name: 'Thing Two', |
| 35 | + description: 'This is thing two', |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + type: 't3', // should be dropped |
| 40 | + timestamp: 1734754684001, |
| 41 | + }, |
| 42 | + ]); |
| 43 | + |
| 44 | + initialize({ |
| 45 | + ...initializeFrom(rules), |
| 46 | + drop: firehoseDrop(rules), |
| 47 | + }) |
| 48 | + .assemble(fromFirehose(events), false) |
| 49 | + .collect() |
| 50 | + // .tap((records) => console.log(JSON.stringify(records, null, 2))) |
| 51 | + .tap((records) => { |
| 52 | + expect(records.length).to.equal(3); |
| 53 | + expect(records[0].pipeline).to.equal('ft1'); |
| 54 | + expect(records[0].event.type).to.equal('t1'); |
| 55 | + expect(records[0].transformed).to.deep.equal({ |
| 56 | + ID: '1', |
| 57 | + NM: 'Thing One', |
| 58 | + DESC: 'This is thing one', |
| 59 | + }); |
| 60 | + expect(records[0].metadata).to.deep.equal({ |
| 61 | + partitionKeys: { |
| 62 | + table: 'T1', |
| 63 | + year: '2024', |
| 64 | + month: '12', |
| 65 | + day: '20', |
| 66 | + hour: '23', |
| 67 | + minute: '18', |
| 68 | + }, |
| 69 | + }); |
| 70 | + expect(records[0].data).to.equal('eyJJRCI6IjEiLCJOTSI6IlRoaW5nIE9uZSIsIkRFU0MiOiJUaGlzIGlzIHRoaW5nIG9uZSJ9'); |
| 71 | + expect(records[0].result).to.equal('Ok'); |
| 72 | + expect(records[1].result).to.equal('Ok'); |
| 73 | + expect(records[2].result).to.equal('Dropped'); |
| 74 | + }) |
| 75 | + .done(done); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +const transform = (uow) => ({ |
| 80 | + ID: uow.event.thing.id, |
| 81 | + NM: uow.event.thing.name, |
| 82 | + DESC: uow.event.thing.description, |
| 83 | +}); |
| 84 | + |
| 85 | +const rules = [ |
| 86 | + { |
| 87 | + id: 'ft1', |
| 88 | + flavor: firehoseTransform, |
| 89 | + eventType: 't1', |
| 90 | + tableName: 'T1', |
| 91 | + transform, |
| 92 | + }, |
| 93 | + { |
| 94 | + id: 'ft2', |
| 95 | + flavor: firehoseTransform, |
| 96 | + eventType: 't2', |
| 97 | + filters: [() => true], |
| 98 | + tableName: 'T2', |
| 99 | + transform, |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 'other1', |
| 103 | + eventType: 'x9', |
| 104 | + flavor: firehoseTransform, |
| 105 | + }, |
| 106 | +]; |
0 commit comments