Skip to content

Commit

Permalink
tests: fix load data infile tests to match on-disk line endings
Browse files Browse the repository at this point in the history
fixes #1437
  • Loading branch information
dougwilson committed Jun 8, 2016
1 parent 36b1927 commit 65c4c0c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 8 additions & 0 deletions test/common.js
Expand Up @@ -76,6 +76,14 @@ common.createFakeServer = function(options) {
return new FakeServer(common.extend({}, options));
};

common.detectNewline = function detectNewline(path) {
var newlines = fs.readFileSync(path, 'utf8').match(/(?:\r?\n)/g) || [];
var crlf = newlines.filter(function (nl) { return nl === '\r\n'; }).length;
var lf = newlines.length - crlf;

return crlf > lf ? '\r\n' : '\n';
};

common.extend = function extend(dest, src) {
for (var key in src) {
dest[key] = src[key];
Expand Down
7 changes: 5 additions & 2 deletions test/integration/connection/test-load-data-infile.js
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs');
var badPath = common.fixtures + '/does_not_exist.csv';
var path = common.fixtures + '/data.csv';
var table = 'load_data_test';
var newline = common.detectNewline(path);

common.getTestConnection(function (err, connection) {
assert.ifError(err);
Expand All @@ -21,9 +22,11 @@ common.getTestConnection(function (err, connection) {

var sql =
'LOAD DATA LOCAL INFILE ? INTO TABLE ?? CHARACTER SET utf8 ' +
'FIELDS TERMINATED BY ? (id, title)';
'FIELDS TERMINATED BY ? ' +
'LINES TERMINATED BY ? ' +
'(id, title)';

connection.query(sql, [path, table, ','], function (err, result) {
connection.query(sql, [path, table, ',', newline], function (err, result) {
assert.ifError(err);
assert.equal(result.affectedRows, 5);
});
Expand Down
Expand Up @@ -2,8 +2,9 @@ var assert = require('assert');
var common = require('../../common');
var fs = require('fs');

var path = common.fixtures + '/data.csv';
var table = 'multi_load_data_test';
var path = common.fixtures + '/data.csv';
var table = 'multi_load_data_test';
var newline = common.detectNewline(path);

common.getTestConnection({multipleStatements: true}, function (err, connection) {
assert.ifError(err);
Expand All @@ -20,11 +21,13 @@ common.getTestConnection({multipleStatements: true}, function (err, connection)

var stmt =
'LOAD DATA LOCAL INFILE ? INTO TABLE ?? CHARACTER SET utf8 ' +
'FIELDS TERMINATED BY ? (id, title)';
'FIELDS TERMINATED BY ? ' +
'LINES TERMINATED BY ? ' +
'(id, title)';

var sql =
connection.format(stmt, [path, table, ',']) + ';' +
connection.format(stmt, [path, table, ',']) + ';';
connection.format(stmt, [path, table, ',', newline]) + ';' +
connection.format(stmt, [path, table, ',', newline]) + ';';

connection.query(sql, function (err, results) {
assert.ifError(err);
Expand Down

0 comments on commit 65c4c0c

Please sign in to comment.