Commit 82c2a674 by Orlando Vazquez

Clean up tests

parent 2e80b78b
path = require('path');
require.paths.unshift(path.join(__dirname, 'lib'));
sys = require('sys'); sys = require('sys');
fs = require('fs'); fs = require('fs');
path = require('path');
TestSuite = require('async-testing/async_testing').TestSuite; TestSuite = require('async-testing/async_testing').TestSuite;
sqlite = require('sqlite3_bindings'); sqlite = require('sqlite');
common = require('common');
puts = sys.puts; puts = sys.puts;
inspect = sys.inspect; inspect = sys.inspect;
var name = "Caching of affectedRows"; var name = "Caching of affectedRows";
var suite = exports[name] = new TestSuite(name); var suite = exports[name] = new TestSuite(name);
function createTestTable(db, callback) {
db.prepare('CREATE TABLE table1 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)',
function (error, createStatement) {
if (error) throw error;
createStatement.step(function (error, row) {
if (error) throw error;
callback();
});
});
}
var tests = [ var tests = [
{ 'insert a row with lastinsertedid': { 'insert a row with lastinsertedid':
function (assert, finished) { function (assert, finished) {
var self = this; var self = this;
self.db.open(':memory:', function (error) { var data = [ ['foo 0']
function updateStatementCreated(error, statement) { , ['foo 1']
if (error) throw error; , ['foo 2']
statement.step(function (error, row) { , ['foo 3']
if (error) throw error; , ['foo 4']
assert.equal(this.affectedRows, 10, "Last inserted id should be 10"); , ['foo 5']
, ['foo 6']
, ['foo 7']
, ['foo 8']
, ['foo 9']
];
finished(); common.createTable
}); ( self.db
} , 'table1'
, [ { name: 'id', type: 'INT' }
, { name: 'name', type: 'TEXT' }
]
, function (error) {
if (error) throw error;
var updateSQL
= 'UPDATE table1 SET name="o hai"';
createTestTable(self.db, common.insertMany
function () { ( self.db
function insertRows(db, count, callback) { , 'table1'
var i = count; , ['name']
db.prepare('INSERT INTO table1 (name) VALUES ("oh boy")', , data
function (error, statement) { , function () {
statement.step(function (error, row) { self.db.prepare
if (error) throw error; ( updateSQL
assert.ok(!row, "Row should be unset"); , { affectedRows: true }
statement.reset(); , onUpdateStatementCreated
if (--i) );
statement.step(arguments.callee);
else
callback();
});
}); });
} }
);
var updateSQL function onUpdateStatementCreated(error, statement) {
= 'UPDATE table1 SET name="o hai"'; if (error) throw error;
statement.step(function (error, row) {
if (error) throw error;
assert.equal
( this.affectedRows
, data.length
, "Last inserted id should be 10"
);
common.insertMany(self.db, 'table1', finished();
['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);
});
});
});
} }
} }
]; ];
...@@ -92,7 +83,9 @@ var testCount = tests.length; ...@@ -92,7 +83,9 @@ var testCount = tests.length;
suite.setup(function(finished, test) { suite.setup(function(finished, test) {
this.db = new sqlite.Database(); this.db = new sqlite.Database();
finished(); this.db.open(':memory:', function (error) {
finished();
});
}); });
suite.teardown(function(finished) { suite.teardown(function(finished) {
if (this.db) this.db.close(function (error) { if (this.db) this.db.close(function (error) {
......
...@@ -52,7 +52,6 @@ var tests = [ ...@@ -52,7 +52,6 @@ var tests = [
, testRows , testRows
, function () { , function () {
common.getResultsStep(self.db, function (rows) { common.getResultsStep(self.db, function (rows) {
console.log(sys.inspect(arguments));
assert.deepEqual(rows, testRowsExpected); assert.deepEqual(rows, testRowsExpected);
finished(); finished();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment