Skip to content

Commit

Permalink
Add integration serialize() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmerlier committed Jun 1, 2016
1 parent 155480a commit aeb0a5e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lib/integrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,26 @@ function integration(communeFilePath, codesPostauxFilePath, serialiseDestination
let count = 0;

return new Promise((resolve, reject) => {
streamify(Array.from(communes.values()))
.on('error', reject)
.pipe(t((commune, enc, cb) => {
commune.codesPostaux = Array.from(commune.codesPostaux);
count++;
cb(null, commune);
}))
.on('error', reject)
.pipe(JSONStream.stringify())
.on('error', reject)
.pipe(fs.createWriteStream(serialiseDestination))
.on('error', reject)
.on('finish', () => {
debug('Nombre de communes écrites : %d', count);
resolve(count);
});
if (communes.size < 1) {
reject('No communes');
} else {
streamify(Array.from(communes.values()))
.on('error', reject)
.pipe(t((commune, enc, cb) => {
commune.codesPostaux = Array.from(commune.codesPostaux);
count++;
cb(null, commune);
}))
.on('error', reject)
.pipe(JSONStream.stringify())
.on('error', reject)
.pipe(fs.createWriteStream(serialiseDestination))
.on('error', reject)
.on('finish', () => {
debug('Nombre de communes écrites : %d', count);
resolve(count);
});
}
});
}

Expand Down
50 changes: 50 additions & 0 deletions test/integrate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env mocha */
const integrate = require('../lib/integrate').integration;
const fs = require('fs');
const expect = require('expect.js');


Expand Down Expand Up @@ -115,4 +116,53 @@ describe('integration', function() {
});
});
});

describe('serialize()', function() {
describe('no communes', function() {
const path = __dirname + '/integration-data/serialize-test.json';
const integration = integrate(null, null, path);
it('should return an error', function(done) {
integration.serialize()
.then(function(data) {
done(data);
}).catch(function(error) {
expect(error).to.equal('No communes');
done();
});
});
it('should not create file', function(done) {
fs.stat(path, function(err) {
expect(err.code).to.equal('ENOENT');
done();
});
});
});

describe('normal way', function() {
const communesPath = __dirname + '/integration-data/communes.json';
const destinationPath = __dirname + '/integration-data/serialize-test.json';
const integration = integrate(communesPath, null, destinationPath);
it('should return 1 and create file', function(done) {
integration.loadCommunes()
.then(integration.serialize)
.then(function(data) {
expect(data).to.equal(1);
fs.stat(destinationPath, function(err) {
expect(err).to.be.undefined;
});
const result = require(destinationPath);
expect(result[0].codeInsee).to.equal('66213');
expect(result[0].codesPostaux).to.empty;
expect(result[0].nom).to.equal('Toulouges');
expect(result[0].contour).to.exist;
expect(result[0].centre).to.exist;
expect(result[0].surface).to.equal(801);
fs.unlink(destinationPath);
done();
}).catch(function(error) {
done(error);
});
});
});
});
});

0 comments on commit aeb0a5e

Please sign in to comment.