Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add tests for load data infile feature

  • Loading branch information...
commit 947ab0c0ab2671f7fa6607ea27c1c50bde10ab51 1 parent 8e416d6
@anton-kotenko anton-kotenko authored
View
5 Makefile
@@ -31,7 +31,8 @@ clean-all: clean
test: npm-install
./node_modules/.bin/nodeunit --reporter=minimal tests/low-level-sync tests/low-level-async \
- tests/high-level tests/complex tests/issues
+ tests/high-level tests/complex tests/issues \
+ tests/load-data-infile
test-slow: npm-install
./node_modules/.bin/nodeunit --reporter=minimal tests/slow
@@ -39,7 +40,7 @@ test-slow: npm-install
test-all: npm-install
./node_modules/.bin/nodeunit --reporter=minimal tests/low-level-sync tests/low-level-async \
tests/high-level tests/complex tests/issues \
- tests/slow
+ tests/slow tests/load-data-infile
test-profile: npm-install
rm -f v8.log
View
149 tests/load-data-infile/complex-load-infile.js
@@ -0,0 +1,149 @@
+var cfg = require('../config.js');
+var fs = require('fs');
+var createLoadInfileCapableConnection = function () {
+ var conn = new cfg.mysql_libmysqlclient.bindings.MysqlConnection();
+ conn.initSync();
+ conn.setOptionSync(cfg.mysql_libmysqlclient.MYSQL_OPT_LOCAL_INFILE);
+ conn.realConnectSync(cfg.host, cfg.user, cfg.password, cfg.database);
+ if (conn.connectErrno) {
+ console.log("Failed to connect as " + cfg.user + "@" + cfg.host + ": " + conn.connectError);
+ }
+ return conn;
+};
+var prepareData = function () {
+ var i,
+ data = [];
+ for (i = 0; i < 1000; i++) {
+ data.push(i + ',zzzz' + i);
+ }
+ return data.join('\n');
+};
+module.exports = {
+ setUp: function (callback) {
+ this.conn = createLoadInfileCapableConnection();
+ this.conn.querySync("DROP TABLE IF EXISTS " + cfg.test_table);
+ this.conn.querySync("CREATE TABLE " + cfg.test_table + " (field1 int, field2 text)");
+ this.exampleData = new Buffer(prepareData());
+ this.exampleQuery = 'LOAD DATA LOCAL INFILE \'/nothing\' INTO TABLE ' + cfg.test_table +
+ " FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
+ callback();
+ },
+ tearDown: function (callback) {
+ if (this.conn && this.conn.connectedSync()) {
+ this.conn.closeSync();
+ }
+ callback();
+ },
+ syncCloseSync: function (test) {
+ test.expect(2);
+ test.ok(this.conn.querySync(this.exampleQuery, this.exampleData), "load infile syncronous");
+ this.conn.closeSync();
+ test.throws(
+ this.conn.query.bind(
+ this.conn,
+ this.exampleQuery,
+ this.exampleData,
+ function (result) {
+ test.ok(false, "there has to be exception if calling query after close");
+ test.done();
+ }.bind(this)
+ ),
+ "there has to be exception in query after closeSync"
+ );
+ test.done();
+ },
+ asyncCloseAsync: function (test) {
+ test.expect(2);
+ try {
+ this.conn.query(
+ this.exampleQuery,
+ this.exampleData,
+ function (error) {
+ test.ok(error === null, "asyncronous load data infile breaks");
+ this.conn.closeSync();
+ test.throws(
+ this.conn.query.bind(
+ this.conn,
+ this.exampleQuery,
+ this.exampleData,
+ function (error) {
+ test.ok(false, "there has to be exception in query after closeSync");
+ test.done();
+ }
+ )
+ );
+ test.done();
+ }.bind(this)
+ );
+ } catch (e) {
+ test.ok(false, "asyncronous query LOAD DATA INFILE from Buffer breaks");
+ test.done();
+ }
+ },
+ syncAsyncSync: function (test) {
+ test.expect(4);
+ test.ok(
+ this.conn.querySync(
+ this.exampleQuery,
+ this.exampleData
+ ),
+ 'synchronous load data infile query'
+ );
+ try {
+ this.conn.query(
+ this.exampleQuery,
+ this.exampleData,
+ function (error) {
+ var result;
+ test.ok(error === null, "Asynchronous load data infile after syncronous");
+ result = this.conn.querySync("select count(*) as count from " + cfg.test_table);
+ test.ok(result, "syncronous query after sync + async");
+ result = result && result.fetchAllSync();
+ result = result && result[0] && result[0].count;
+ test.ok(result > 0, "Load infile fails, table is empty");
+ test.done();
+ }.bind(this)
+ );
+ } catch (e) {
+ test.ok(false, "Asynchronous load data infile after syncronous");
+ test.done();
+ }
+ },
+ asyncSyncAsync: function (test) {
+ test.expect(4);
+ try {
+ this.conn.query(
+ this.exampleQuery,
+ this.exampleData,
+ function (error) {
+ test.ok(error === null, "Asynchronous load data infile fails");
+ test.ok(
+ this.conn.querySync(
+ this.exampleQuery,
+ this.exampleData
+ ),
+ "synchronous data load fails"
+ );
+ try {
+ this.conn.query(
+ "SELECT count(*) as count from " + cfg.test_table,
+ function (error, result) {
+ test.ok(error === null, "Asyncronous query after synchronous fails");
+ result = result && result.fetchAllSync();
+ result = result && result[0] && result[0].count;
+ test.ok(result > 0, "Load data infile asynchounous and syncronous fails");
+ test.done();
+ }.bind(this)
+ );
+ } catch (e) {
+ test.ok(false, "Asyncronous query after synchronous fails");
+ test.done();
+ }
+ }.bind(this)
+ );
+ } catch (e) {
+ test.ok(false, "Asynchronous load data infile after syncronous");
+ test.done();
+ }
+ }
+};
View
152 tests/load-data-infile/simle-load-infile.js
@@ -0,0 +1,152 @@
+var cfg = require('../config.js');
+var fs = require('fs');
+var createLoadInfileCapableConnection = function (test) {
+ var conn = new cfg.mysql_libmysqlclient.bindings.MysqlConnection();
+ conn.initSync();
+ conn.setOptionSync(cfg.mysql_libmysqlclient.MYSQL_OPT_LOCAL_INFILE);
+ conn.realConnectSync(cfg.host, cfg.user, cfg.password, cfg.database);
+ if (conn.connectErrno) {
+ console.log("Failed to connect as " + cfg.user + "@" + cfg.host + ": " + conn.connectError);
+ }
+ return conn;
+};
+var formatData = function (data, fieldTerminator, rowTerminator) {
+ return data.map(function (row) {
+ return row.join(fieldTerminator);
+ }).join(rowTerminator);
+};
+var prepareData = function () {
+ var i,
+ data = [];
+ for (i = 0; i < 1000; i++) {
+ data.push([i, 'zzzz' + i]);
+ }
+ return data;
+};
+var prepareDataFile = function (filePath, data, fieldTerminator, rowTerminator) {
+ try {
+ if (fs.statSync(filePath)) {
+ fs.unlinkSync(filePath);
+ }
+ } catch (e) {}
+ fs.writeFileSync(filePath, formatData(data, fieldTerminator, rowTerminator));
+};
+var testQueryResult = function (test, conn) {
+ var result = conn.querySync("select sum(field1) as sum, count(*) as count from " +
+ cfg.test_table);
+ result = result && result.fetchAllSync();
+ result = result && result[0];
+ test.ok(
+ result &&
+ Number(result.sum) === 1000 * 999 / 2 && //arithmetic progression
+ Number(result.count) === 1000,
+ "load infile"
+ );
+};
+var createQuery = function (fileName, fieldTerminator, rowTerminator) {
+ return 'LOAD DATA LOCAL INFILE \'' + fileName + '\' INTO TABLE ' + cfg.test_table +
+ " FIELDS TERMINATED BY '" + fieldTerminator + "'" +
+ " LINES TERMINATED BY '" + rowTerminator + "'";
+};
+var setUp = function (callback) {
+ this.conn = createLoadInfileCapableConnection();
+ this.conn.querySync("DROP TABLE IF EXISTS " + cfg.test_table);
+ this.conn.querySync("CREATE TABLE " + cfg.test_table + " (field1 int, field2 text)");
+ this.fileName = this.fileName || 'nothing';
+ this.exampleData = prepareData();
+ callback();
+};
+var tearDown = function (callback) {
+ if (this.conn && this.conn.connectedSync()) {
+ this.conn.closeSync();
+ }
+ callback();
+};
+
+var syncTest = function (fieldTerminator, rowTerminator, bufferMode, test) {
+ test.expect(2);
+ if (!bufferMode) {
+ prepareDataFile(this.fileName, this.exampleData, fieldTerminator, rowTerminator);
+ } else {
+ this.exampleData = new Buffer(formatData(this.exampleData, fieldTerminator, rowTerminator));
+ }
+ test.ok(this.conn.querySync(
+ createQuery(this.fileName, fieldTerminator, rowTerminator),
+ bufferMode ? this.exampleData : undefined
+ ));
+ testQueryResult(test, this.conn);
+ test.done();
+};
+var asyncTest = function (fieldTerminator, rowTerminator, bufferMode, test) {
+ test.expect(2);
+ var queryCallback =
+ function (error) {
+ test.ok(error === null, "Asynchronous load data infile fails");
+ testQueryResult(test, this.conn);
+ test.done();
+ }.bind(this),
+ callArgs = [createQuery(this.fileName, fieldTerminator, rowTerminator)];
+ if (!bufferMode) {
+ prepareDataFile(this.fileName, this.exampleData, fieldTerminator, rowTerminator);
+ callArgs.push(queryCallback);
+ } else {
+ this.exampleData = new Buffer(formatData(this.exampleData, fieldTerminator, rowTerminator));
+ callArgs.push(this.exampleData, queryCallback);
+ }
+ try {
+ this.conn.query.apply(this.conn, callArgs);
+ } catch (e) {
+ test.ok(false, "Asynchronous load data infile fails");
+ test.done();
+ }
+};
+module.exports.checkServerOptions = function (test) {
+ test.expect(1);
+ var conn = createLoadInfileCapableConnection(),
+ result = conn.querySync("SHOW VARIABLES LIKE 'local_infile'");
+ result = result && result.fetchAllSync();
+ result = result && result[0] && result[0].Value;
+ test.ok(
+ result && result.toLowerCase() === 'on',
+ "LOAD INFILE OPERATION REQUIRES local_infile options set on the server\n" +
+ "execute 'SET GLOBAL local_infile=ON' in mysql server console"
+ );
+ test.done();
+};
+module.exports.realFileGroup = {
+ setUp: function (callback) {
+ this.fileName = '/tmp/load-infile-test.csv';
+ setUp.call(this, callback);
+ },
+ tearDown: tearDown,
+ syncCSV: function (test) {
+ syncTest.call(this, ',', '\n', false, test);
+ },
+ syncTSV: function (test) {
+ syncTest.call(this, '\t', '\n', false, test);
+ },
+ asyncCSV: function (test) {
+ asyncTest.call(this, ',', '\n', false, test);
+ },
+ asyncTSV: function (test) {
+ asyncTest.call(this, '\t', '\n', false, test);
+ }
+};
+module.exports.bufferGroup = {
+ setUp: function (callback) {
+ setUp.call(this, callback);
+ },
+ tearDown: tearDown,
+ syncCSV: function (test) {
+ syncTest.call(this, ',', '\n', true, test);
+ },
+ syncTSV: function (test) {
+ syncTest.call(this, '\t', '\n', true, test);
+ },
+ asyncCSV: function (test) {
+ asyncTest.call(this, ',', '\n', true, test);
+ },
+ asyncTSV: function (test) {
+ asyncTest.call(this, '\t', '\n', true, test);
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.