Permalink
Browse files

added demo-test and fixed auto_increment bug

  • Loading branch information...
1 parent 28998f7 commit e59889acd737a68accb8e61c2bb5bff643e0ed7b @chrisdew committed Nov 30, 2012
Showing with 36 additions and 2 deletions.
  1. +12 −1 lib/table.js
  2. +2 −0 test/database-test.js
  3. +21 −0 test/demo-test.js
  4. +1 −1 test/table-test.js
View
13 lib/table.js
@@ -14,15 +14,21 @@ function hello() {
function Table(spec) {
+ console.log("Tabke::Table spec:", spec);
+
for (var p in spec) {
this[p] = spec[p];
}
+ this.ai = false;
this.rowsByPk = {};
this.pkName = null;
if (spec && spec.fields) {
for (var i in spec.fields) {
if (spec.fields[i].pk) {
this.pkName = spec.fields[i].field;
+ if (spec.fields[i].ai) {
+ this.ai = true;
+ }
}
}
}
@@ -32,6 +38,8 @@ function Table(spec) {
util.inherits(Table, events.EventEmitter);
Table.prototype.insert = function(spec, callback) {
+ console.log("Table::insert", spec);
+
// check for sanity
if (spec.values.length !== spec.fields.length) {
return ("There must be the smae number of values as keys.");
@@ -40,7 +48,9 @@ Table.prototype.insert = function(spec, callback) {
// find index of pk
var idxPk = null;
for (var i in spec.fields) {
- if (spec.fields[i] === this.pkName) { idxPk = i; break; }
+ if (spec.fields[i] === this.pkName) {
+ idxPk = i; break;
+ }
}
// get/generate pk
@@ -49,6 +59,7 @@ Table.prototype.insert = function(spec, callback) {
if (this.ai) {
pk = this.nextPk++;
} else {
+ console.log("Table::insert this.ai:", this.ai);
callback("No primary key found.");
}
} else {
View
2 test/database-test.js
@@ -22,6 +22,7 @@ describe('Database', function() {
, rowsByPk: {}
, pkName: 'id'
, nextPk: 1
+ , ai: true
} } }
, db.createTable(
{ name:"mytable"
@@ -38,6 +39,7 @@ describe('Database', function() {
, rowsByPk: {1: { id: 1, foo: 'hello', _version: 1 } }
, pkName: 'id'
, nextPk: 2
+ , ai: true
} } }
, db.insert( {"table":"mytable","fields":["id","foo"],"values":[1,"hello"]}, done));
});
View
21 test/demo-test.js
@@ -0,0 +1,21 @@
+var assert = require("assert")
+
+var session = require('../lib/session.js');
+var database = require('../lib/database.js');
+
+
+describe('Session', function() {
+ var db = new database.Database();
+ var sess = new session.Session(db);
+ it('should run a complex script', function(done) {
+ sess.exec('create table events (id integer primary key auto_increment, name varchar, start integer, duration integer)', function(err, res) {
+ assert.equal('Table created.', res);
+ sess.exec('insert into events (name, start, duration) values ("dentist", 1000, 100)', function(err, res) {
+ console.log("err", err);
+ assert.equal('1 row inserted.', res);
+ done();
+ });
+ });
+ });
+});
+
View
2 test/table-test.js
@@ -6,7 +6,7 @@ describe('Table', function() {
assert.equal("hello table", table.hello());
});
it('should create an instance"', function() {
- assert.deepEqual({"rowsByPk":{},"pkName":null,"nextPk":1}, new table.Table());
+ assert.deepEqual({"rowsByPk":{},"pkName":null,"nextPk":1,"ai":false}, new table.Table());
});
it('should be an instance of event emitter', function() {
assert.deepEqual("EventEmitter",

0 comments on commit e59889a

Please sign in to comment.