1 |
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const process = require('process')
|
6 | const expect = require('chai').expect
|
7 | const HlPgClient = require('@wmfs/hl-pg-client')
|
8 | const supercopy = require('./../lib')
|
9 | const path = require('path')
|
10 | const fs = require('fs')
|
11 | const rimraf = require('rimraf')
|
12 |
|
13 | describe('Run some basic tests', function () {
|
14 | this.timeout(process.env.TIMEOUT || 5000)
|
15 |
|
16 | const connectionString = process.env.PG_CONNECTION_STRING
|
17 | let client
|
18 |
|
19 | before(function () {
|
20 | if (process.env.PG_CONNECTION_STRING && !/^postgres:\/\/[^:]+:[^@]+@(?:localhost|127\.0\.0\.1).*$/.test(process.env.PG_CONNECTION_STRING)) {
|
21 | console.log(`Skipping tests due to unsafe PG_CONNECTION_STRING value (${process.env.PG_CONNECTION_STRING})`)
|
22 | this.skip()
|
23 | }
|
24 | })
|
25 |
|
26 | it('Should create a new pg client', function () {
|
27 | client = new HlPgClient(connectionString)
|
28 | })
|
29 |
|
30 | it('Should remove output directory ahead of csv tests, if it exists already', function (done) {
|
31 | const outputPath = path.resolve(__dirname, './output')
|
32 | if (fs.existsSync(outputPath)) {
|
33 | rimraf(outputPath, {}, done)
|
34 | } else {
|
35 | done()
|
36 | }
|
37 | })
|
38 |
|
39 | it('Should load some test data', async () => {
|
40 | for (const filename of ['uninstall.sql', 'install.sql']) { await client.runFile(path.resolve(__dirname, path.join('fixtures', 'scripts', filename))) }
|
41 | })
|
42 |
|
43 | it('Should promise to supercopy some people', function () {
|
44 | return supercopy(
|
45 | {
|
46 | sourceDir: path.resolve(__dirname, './fixtures/input-data/people'),
|
47 | topDownTableOrder: ['adults', 'children'],
|
48 | headerColumnNamePkPrefix: '.',
|
49 | client: client,
|
50 | schemaName: 'supercopy_test',
|
51 | debug: true
|
52 | }
|
53 | )
|
54 | })
|
55 |
|
56 | it('Should return correctly modified adult rows', function (done) {
|
57 | client.query(
|
58 | 'select adult_no,first_name,last_name from supercopy_test.adults order by adult_no',
|
59 | function (err, result) {
|
60 | if (err) {
|
61 | return done(err)
|
62 | }
|
63 | expect(result.rows).to.eql(
|
64 | [
|
65 | { adult_no: 10, first_name: 'Homer', last_name: 'Simpson' },
|
66 | { adult_no: 20, first_name: 'Marge', last_name: 'Simpson' },
|
67 | { adult_no: 30, first_name: 'Maud', last_name: 'Flanders' },
|
68 | { adult_no: 40, first_name: 'Ned', last_name: 'Flanders' },
|
69 | { adult_no: 50, first_name: 'Seymour', last_name: 'Skinner' },
|
70 | { adult_no: 60, first_name: 'Charles', last_name: 'Burns' },
|
71 | { adult_no: 80, first_name: 'Clancy', last_name: 'Wiggum' },
|
72 | { adult_no: 90, first_name: 'Abraham', last_name: 'Simpson' },
|
73 | { adult_no: 100, first_name: 'Mona', last_name: 'Simpson' }
|
74 | ]
|
75 | )
|
76 | done()
|
77 | }
|
78 | )
|
79 | })
|
80 |
|
81 | it('Should return correctly modified children rows', function (done) {
|
82 | client.query(
|
83 | 'select child_no,first_name,last_name from supercopy_test.children order by child_no',
|
84 | function (err, result) {
|
85 | if (err) {
|
86 | return done(err)
|
87 | }
|
88 | expect(result.rows).to.eql(
|
89 | [
|
90 | { child_no: 10, first_name: 'Lisa', last_name: 'Simpson' },
|
91 | { child_no: 20, first_name: 'Bart', last_name: 'Simpson' },
|
92 | { child_no: 30, first_name: 'Maggie', last_name: 'Simpson' },
|
93 | { child_no: 40, first_name: 'Rod', last_name: 'Flanders' },
|
94 | { child_no: 50, first_name: 'Todd', last_name: 'Flanders' },
|
95 | { child_no: 60, first_name: 'Nelson', last_name: 'Muntz' },
|
96 | { child_no: 70, first_name: 'Milhouse', last_name: 'Van Houten' }
|
97 | ]
|
98 | )
|
99 | done()
|
100 | }
|
101 | )
|
102 | })
|
103 |
|
104 | it('Should fail supercopy-if some bad-people files', function (done) {
|
105 | supercopy(
|
106 | {
|
107 | sourceDir: path.resolve(__dirname, './fixtures/input-data/people-with-an-error'),
|
108 | topDownTableOrder: ['adults', 'children'],
|
109 | headerColumnNamePkPrefix: '.',
|
110 | client: client,
|
111 | schemaName: 'supercopy_test',
|
112 | debug: true
|
113 | },
|
114 | function (err) {
|
115 | expect(err).to.not.equal(null)
|
116 | done()
|
117 | }
|
118 | )
|
119 | })
|
120 |
|
121 | it('Should error on mis-shapen data', function (done) {
|
122 | supercopy(
|
123 | {
|
124 | sourceDir: path.resolve(__dirname, './fixtures/input-data/bad-people'),
|
125 | topDownTableOrder: ['adults'],
|
126 | headerColumnNamePkPrefix: '.',
|
127 | client: client,
|
128 | schemaName: 'supercopy_test',
|
129 | truncateTables: true,
|
130 | debug: true
|
131 | },
|
132 | function (err) {
|
133 | expect(err).to.not.equal(null)
|
134 | done()
|
135 | }
|
136 | )
|
137 | })
|
138 |
|
139 | it('Should supercopy some people, truncating the tables first', function (done) {
|
140 | supercopy(
|
141 | {
|
142 | sourceDir: path.resolve(__dirname, './fixtures/input-data/people'),
|
143 | topDownTableOrder: ['adults', 'children'],
|
144 | headerColumnNamePkPrefix: '.',
|
145 | client: client,
|
146 | schemaName: 'supercopy_test',
|
147 | truncateTables: true,
|
148 | debug: true
|
149 | },
|
150 | function (err) {
|
151 | done(err)
|
152 | }
|
153 | )
|
154 | })
|
155 |
|
156 | it('Should return correctly modified adult rows (truncated)', function (done) {
|
157 | client.query(
|
158 | 'select adult_no,first_name,last_name from supercopy_test.adults order by adult_no',
|
159 | function (err, result) {
|
160 | if (err) {
|
161 | return done(err)
|
162 | }
|
163 | expect(result.rows).to.eql(
|
164 | [
|
165 | { adult_no: 30, first_name: 'Maud', last_name: 'Flanders' },
|
166 | { adult_no: 40, first_name: 'Ned', last_name: 'Flanders' },
|
167 | { adult_no: 80, first_name: 'Clancy', last_name: 'Wiggum' },
|
168 | { adult_no: 90, first_name: 'Abraham', last_name: 'Simpson' },
|
169 | { adult_no: 100, first_name: 'Mona', last_name: 'Simpson' }
|
170 | ]
|
171 | )
|
172 | done()
|
173 | }
|
174 | )
|
175 | })
|
176 |
|
177 | it('Should return correctly modified children rows (truncated)', function (done) {
|
178 | client.query(
|
179 | 'select child_no,first_name,last_name from supercopy_test.children order by child_no',
|
180 | function (err, result) {
|
181 | if (err) {
|
182 | return done(err)
|
183 | }
|
184 | expect(result.rows).to.eql(
|
185 | [
|
186 | { child_no: 50, first_name: 'Todd', last_name: 'Flanders' },
|
187 | { child_no: 70, first_name: 'Milhouse', last_name: 'Van Houten' }
|
188 | ]
|
189 | )
|
190 | done()
|
191 | }
|
192 | )
|
193 | })
|
194 |
|
195 | it('Should cleanup the test data', async () => {
|
196 | await client.runFile(path.resolve(__dirname, path.join('fixtures', 'scripts', 'uninstall.sql')))
|
197 | })
|
198 |
|
199 | it('Should close database connections', function (done) {
|
200 | client.end()
|
201 | done()
|
202 | })
|
203 | })
|