Permalink
Browse files

Pull out insertMany into a common test lib

  • Loading branch information...
1 parent 8710ae2 commit 7f34eb06b96940e4f5b728d297079812f58cf78a @orlandov committed Sep 9, 2010
Showing with 179 additions and 9 deletions.
  1. +40 −0 tests/lib/common.js
  2. +13 −1 tests/test-affected-rows.js
  3. +6 −8 tests/test-last-inserted-id.js
  4. +120 −0 tests/test-statement-step.js
View
@@ -0,0 +1,40 @@
+exports.insertMany = function (db, table, fields, rows, callback) {
+ var columns_fragment = fields.join(",");
+ var placeholders_fragment = [];
+ var i = fields.length;
+
+ while (i--) {
+ placeholders_fragment.push('?');
+ }
+ placeholders_fragment = placeholders_fragment.join(", ");
+
+ var sql = 'INSERT INTO ' + table
+ + ' (' + columns_fragment + ')'
+ + ' VALUES (' + placeholders_fragment + ')';
+
+ var i = rows.length;
+
+ var statement;
+
+ function doStep(i) {
+ statement.bindArray(rows[i], function () {
+ statement.step(function (error, row) {
+ if (error) throw error;
+ statement.reset();
+ if (i) {
+ doStep(--i);
+ }
+ else {
+ statement.finalize(function () {
+ callback();
+ });
+ }
+ });
+ });
+ }
+
+ db.prepare(sql, function (error, stmt) {
+ statement = stmt;
+ doStep(--i);
+ });
+}
@@ -59,7 +59,19 @@ var tests = [
var updateSQL
= 'UPDATE table1 SET name="o hai"';
- insertRows(self.db, 10, function () {
+ common.insertMany(self.db, 'table1',
+ ['name'],
+ [['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai'],
+ ['o hai']],
+ function () {
self.db.prepare(updateSQL
, { affectedRows: true }
, updateStatementCreated);
@@ -4,6 +4,7 @@ path = require('path');
TestSuite = require('async-testing/async_testing').TestSuite;
sqlite = require('sqlite3_bindings');
+common = require('./lib/common');
puts = sys.puts;
inspect = sys.inspect;
@@ -46,17 +47,14 @@ var tests = [
createTestTable(self.db,
function () {
- var insertSQL
- = 'INSERT INTO table1 (id, name) VALUES (100, "first post!")';
-
- self.db.prepareAndStep(insertSQL, function (error, row) {
- if (error) throw error;
-
- assert.ok(!row, "Row was trueish, but should be falseish");
-
+ common.insertMany(self.db , 'table1'
+ , ['id', 'name']
+ , [ [100, "first post!"] ]
+ , function () {
self.db.prepare('INSERT INTO table1 (name) VALUES ("orlando")'
, { affectedRows: true, lastInsertRowID: true }
, createStatement);
+
});
});
});
@@ -0,0 +1,120 @@
+sys = require('sys');
+fs = require('fs');
+path = require('path');
+common = require('./lib/common');
+
+TestSuite = require('async-testing/async_testing').TestSuite;
+sqlite = require('sqlite3_bindings');
+
+puts = sys.puts;
+inspect = sys.inspect;
+
+var name = "Fetching all results";
+var suite = exports[name] = new TestSuite(name);
+
+function createTestTable(db, callback) {
+ db.prepare('CREATE TABLE table1 (id INTEGER, name TEXT, age FLOAT)',
+ function (error, createStatement) {
+ if (error) throw error;
+ createStatement.step(function (error, row) {
+ if (error) throw error;
+ callback();
+ });
+ });
+}
+
+var testRows = [ [ 1, "foo", 9 ]
+ , [ 2, "bar", 8 ]
+ , [ 3, "baz", 7 ]
+ , [ 4, "quux", 6 ]
+ , [ 5, "juju", 5 ]
+ ];
+
+var testRowsExpected = [ { id: 5, name: 'juju', age: 5 }
+ , { id: 4, name: 'quux', age: 6 }
+ , { id: 3, name: 'baz', age: 7 }
+ , { id: 2, name: 'bar', age: 8 }
+ , { id: 1, name: 'foo', age: 9 }
+ ];
+
+var tests = [
+ { 'insert a row with lastinsertedid':
+ function (assert, finished) {
+ var self = this;
+
+ self.db.open(':memory:', function (error) {
+ function selectStatementPrepared(error, statement) {
+ if (error) throw error;
+ statement.fetchAll(function (error, rows) {
+ if (error) throw error;
+
+ assert.deepEqual(testRowsExpected, rows);
+
+ self.db.close(function () {
+ finished();
+ });
+ });
+ }
+
+ createTestTable(self.db,
+ function () {
+ function insertRows(db, rows, callback) {
+ var i = rows.length;
+ db.prepare('INSERT INTO table1 (id, name, age) VALUES (?, ?, ?)',
+ function (error, statement) {
+ function doStep(i) {
+ statement.bindArray(rows[i], function () {
+ statement.step(function (error, row) {
+ if (error) throw error;
+ assert.ok(!row, "Row should be unset");
+ statement.reset();
+ if (i) {
+ doStep(--i);
+ }
+ else {
+ statement.finalize(function () {
+ callback();
+ });
+ }
+ });
+ });
+ }
+
+ doStep(--i);
+ });
+ }
+
+ var selectSQL
+ = 'SELECT * from table1';
+
+ common.insertMany(self.db, 'table1', ['id', 'name', 'age'], testRows, function () {
+ self.db.prepare(selectSQL, selectStatementPrepared);
+ });
+ });
+ });
+ }
+ }
+];
+
+// order matters in our tests
+for (var i=0,il=tests.length; i < il; i++) {
+ suite.addTests(tests[i]);
+}
+
+var currentTest = 0;
+var testCount = tests.length;
+
+suite.setup(function(finished, test) {
+ this.db = new sqlite.Database();
+ finished();
+});
+suite.teardown(function(finished) {
+ if (this.db) this.db.close(function (error) {
+ finished();
+ });
+ ++currentTest == testCount;
+});
+
+if (module == require.main) {
+ suite.runTests();
+}

0 comments on commit 7f34eb0

Please sign in to comment.