1 |
|
2 | 'use strict'
|
3 | const tymly = require('@wmfs/tymly')
|
4 | const path = require('path')
|
5 | const expect = require('chai').expect
|
6 | const glob = require('glob')
|
7 | const _ = require('lodash')
|
8 | const csv = require('csvtojson')
|
9 | const STATE_MACHINE_NAME = 'tymlyTest_people_1_0'
|
10 |
|
11 | describe('CSV and tymly test', function () {
|
12 | const fixture = path.resolve(__dirname, 'fixtures', 'people')
|
13 |
|
14 | this.timeout(process.env.TIMEOUT || 5000)
|
15 | let tymlyService
|
16 | let statebox
|
17 |
|
18 | it('start Tymly service', async () => {
|
19 | const tymlyServices = await tymly.boot(
|
20 | {
|
21 | pluginPaths: [
|
22 | path.resolve(__dirname, './../lib'),
|
23 | path.resolve(__dirname, '../node_modules/@wmfs/tymly-test-helpers/plugins/allow-everything-rbac-plugin')
|
24 | ],
|
25 | blueprintPaths: [
|
26 | path.resolve(__dirname, './fixtures/people/blueprints/people-blueprint')
|
27 | ]
|
28 | }
|
29 | )
|
30 |
|
31 | tymlyService = tymlyServices.tymly
|
32 | statebox = tymlyServices.statebox
|
33 | })
|
34 |
|
35 | it('process CSV file', async () => {
|
36 | const executionDescription = await statebox.startExecution(
|
37 | {
|
38 | sourceFilePaths: path.resolve(fixture, 'input', 'people.csv'),
|
39 | outputDirRootPath: path.resolve(fixture, 'output'),
|
40 | sourceDir: path.resolve(fixture, 'output')
|
41 | },
|
42 | STATE_MACHINE_NAME,
|
43 | {
|
44 | sendResponse: 'COMPLETE'
|
45 | }
|
46 | )
|
47 |
|
48 | expect(executionDescription.status).to.eql('SUCCEEDED')
|
49 | expect(executionDescription.currentStateName).to.eql('ProcessingCsvFiles')
|
50 | })
|
51 |
|
52 | it('create delete and upserts directories', () => {
|
53 | const files = glob.sync(path.resolve(fixture, 'output', '*'))
|
54 |
|
55 | expect(files).to.deep.equal([
|
56 | _.replace(path.resolve(fixture, 'output', 'delete'), /\\/g, '/'),
|
57 | _.replace(path.resolve(fixture, 'output', 'manifest.json'), /\\/g, '/'),
|
58 | _.replace(path.resolve(fixture, 'output', 'upserts'), /\\/g, '/')
|
59 | ])
|
60 | })
|
61 |
|
62 | it('check delete files have been split correctly', (done) => {
|
63 | const csvDeletesPath = path.resolve(fixture, 'output', 'delete', 'people.csv')
|
64 | csv()
|
65 | .fromFile(csvDeletesPath)
|
66 | .on('json', function (json) {
|
67 | expect(json.action).to.equal('d')
|
68 | })
|
69 | .on('done', function (err) {
|
70 | expect(err).to.eql(undefined)
|
71 | done()
|
72 | })
|
73 | })
|
74 |
|
75 | it('check upserts files have been split correctly', (done) => {
|
76 | const csvUpsertsPath = path.resolve(fixture, 'output', 'upserts', 'people.csv')
|
77 | csv()
|
78 | .fromFile(csvUpsertsPath)
|
79 | .on('json', function (json) {
|
80 | expect(json.action).to.satisfy(function (action) {
|
81 | if (action === 'x' || action === 'u' || action === 'i') {
|
82 | return true
|
83 | } else {
|
84 | return false
|
85 | }
|
86 | })
|
87 | })
|
88 | .on('done', function (err) {
|
89 | expect(err).to.eql(undefined)
|
90 | done()
|
91 | })
|
92 | })
|
93 |
|
94 | after('shutdown Tymly', async () => {
|
95 | await tymlyService.shutdown()
|
96 | })
|
97 | })
|