Skip to content

Commit

Permalink
Merge d55951b into 7cf3c91
Browse files Browse the repository at this point in the history
  • Loading branch information
who8mycakes committed Mar 13, 2017
2 parents 7cf3c91 + d55951b commit 91faeb7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
27 changes: 16 additions & 11 deletions lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ module.exports = Csv;
function Csv(filepath) {
this.filepath = filepath;
this.basename = path.basename(filepath, path.extname(filepath));
this.datasource = new mapnik.Datasource({
type: 'csv',
file: filepath,
filesize_max: 10,
layer: this.basename
});
try {
this.datasource = new mapnik.Datasource({
type: 'csv',
file: filepath,
filesize_max: 10,
layer: this.basename
});

this.extent = this.datasource.extent();
this.center = [
0.5 * (this.extent[0] + this.extent[2]),
0.5 * (this.extent[1] + this.extent[3])
];
this.extent = this.datasource.extent();
this.center = [
0.5 * (this.extent[0] + this.extent[2]),
0.5 * (this.extent[1] + this.extent[3])
];
} catch (err) {
if (err.message.indexOf('could not detect column(s)') > -1) throw invalid('Invalid CSV. Cannot detect column(s) with the name(s) of wkt, geojson, x/y, or latitude/longitude.')
throw invalid(err);
}
}

Csv.validFileType = ['csv'];
Expand Down
3 changes: 1 addition & 2 deletions lib/invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ var util = require('util');

module.exports = function invalid(message) {
if (typeof message === 'string') {
message = util.format.apply(this, arguments);
message = (util.format.apply(this, arguments)).slice(0, 255);
}

var err = message instanceof Error ? message : new Error(message);
err.code = 'EINVALID';

return err;
};
25 changes: 23 additions & 2 deletions test/csv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,31 @@ test('[CSV] constructor: invalid file', function(assert) {
assert.end();
});

test('[CSV] constructor: invalid file - empty string header', function(assert) {
var fixture = path.join(__dirname, 'fixtures', 'invalid-emptystring-header.csv');

assert.throws(function() {
new Csv(fixture);
}, 'throws an error');

assert.end();
});

test('[CSV] constructor: invalid file - incorrect header names', function(assert) {
var fixture = path.join(__dirname, 'fixtures', 'invalid-header-names.csv');

assert.throws(function() {
new Csv(fixture);
}, 'throws an error');

assert.end();
});

test('[CSV] constructor: valid file', function(assert) {
var csv;
var fixture = path.join(__dirname, 'fixtures', 'valid-nypd.csv');
var csv = new Csv(fixture);
assert.doesNotThrow(function() {
csv = new Csv(valid);
new Csv(fixture);
}, 'no error');

assert.equal(csv.detailsName, 'json', 'exposes details name');
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/invalid-emptystring-header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"",Precinct,Phone,Address,City,geo_longitude,geo_latitude,geo_accuracy,,,,
1,5th Precinct Fraternal Order of Police,(212) 555-0711,19 Street,"New York, NY",-70.0,40.0,house,,,,
2,9th Precinct Fraternal Order of Police,(212) 555-7811,130 Avenue z,"New York, NY",-73.0,41.0,house,,,,
3,7th Precinct Fraternal Order of Police,(212) 555-0711,19 Sometime Street,"New York, NY",-70.0,40.0,house,,,,
4,11th Precinct Fraternal Order of Police,(212) 555-7811,130 Avenue z,"New York, NY",-73.0,41.0,house,,,,
5,15th Precinct Fraternal Order of Police,(212) 555-0711,19 Sometime Street,"New York, NY",-70.0,40.0,house,,,,
6,19th Precinct Fraternal Order of Police,(212) 555-7811,130 Avenue z,"New York, NY",-73.0,41.0,house,,,,
7,25th Precinct Fraternal Order of Police,(212) 555-0711,19 Sometime Street,"New York, NY",-70.0,40.0,house,,,,
8,29th Precinct Fraternal Order of Police,(212) 555-7811,130 Avenue z,"New York, NY",-73.0,41.0,house,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,
5 changes: 5 additions & 0 deletions test/fixtures/invalid-header-names.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
index,first name,last name,allergies,lat/lon
1,john ,doe,bees,42.686744/-73.822852
2,jane,doe,wheat,42.718588/-73.755328
3,jim,doe,none,42.814403/-73.930967
4,james,doe,pollen,42.664351/-73.786562
3 changes: 3 additions & 0 deletions test/fixtures/valid-nypd.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Precinct,Phone,Address,City,geo_longitude,geo_latitude,geo_accuracy
5th Precinct,(212) 334-0711,19 Elizabeth Street,"New York, NY",-70.0,40.0,house
9th Precinct,(212) 477-7811,130 Avenue C,"New York, NY",-73.0,41.0,house

0 comments on commit 91faeb7

Please sign in to comment.