Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added some session tests

  • Loading branch information...
commit f26cd0fd4e8d26afc92e140ad63d6250c9c7011b 1 parent 5716c5a
@chrisdew authored
View
12 lib/database.js
@@ -18,13 +18,19 @@ function Database() {
util.inherits(Database, events.EventEmitter);
Database.prototype.createTable = function(tableSpec) {
+ console.log(tableSpec);
+ if (this.tablesByName === tableSpec.name) return "Error: table alreay exists.";
+
this.tablesByName[tableSpec.name] = new table.Table(tableSpec);
//console.log(this);
this.emit('createTable', tableSpec);
return this;
}
-
-Database.prototype.executeJson = function(json) {
-
+// command is a Javascript object, the result of parsing a line
+Database.prototype.exec = function(command) {
+ //console.log(command);
+ for (var p in command) { // there will only be one
+ if (p === 'createTable') this.createTable(command[p]);
+ }
}
View
4 lib/grammar.pegjs
@@ -4,8 +4,8 @@ start
{ return {select:{exprs:el,from:fc}}; }
/ "subscribe" el:ExprList fc:FromClause?
{ return {subscribe:{exprs:el,from:fc}}; }
- / "create" _ "table" _ TableName _? "(" _? lines:CreateLines _? ")"
- { return {createTable:{fields:lines}}; }
+ / "create" _ "table" _ tn:TableName _? "(" _? lines:CreateLines _? ")"
+ { return {createTable:{name:tn, fields:lines}}; }
/ "use" _ DatabaseName
/ "create" _ DatabaseName
/ "insert into" _ tn:TableName _? "(" _? fields:FieldList _? ")" _? "values" _? "(" _? values:ValueList ")"
View
21 lib/session.js
@@ -31,18 +31,27 @@ function spaces(num) {
return ret.join('');
}
-// This method takes a line, executes it and retunrs the error or the result.
-Session.prototype.exec = function(line) {
+// This method takes a line and executes it. Results and/or errors are returned
+// as events
+Session.prototype.exec = function(line, callback) {
if (line === '') return '';
try {
var command = parser.parse(line);
} catch (e) {
//console.log(e);
- return [ spaces(e.offset + (this.promptSize ? this.promptSize : 0)) + "^"
- , 'Syntax error: ' + e.message
- ].join('\n');
+ return callback([ spaces(e.offset + (this.promptSize ? this.promptSize : 0)) + "^"
+ , 'Syntax error: ' + e.message
+ ].join('\n'));
+ }
+ try {
+ this.db.once('createTable', function() {
+ console.log('emitting result Table created.');
+ callback(null, 'Table created.');
+ });
+ this.db.exec(command);
+ } catch (e) {
+ return callback(e.message);
}
- return 'OK';
}
// the unregisters all the resources this session is using
View
6 telnet.js
@@ -53,9 +53,11 @@ var server = telnet.createServer(function (client) {
rl.setPrompt('SubSQL> ');
rl.prompt();
+
+ session.on('error', function(message) { client.write(message + '\n'); rl.prompt(); });
+ session.on('result', function(message) { client.write(message + '\n'); rl.prompt(); });
rl.on('line', function(line) {
- client.write(session.exec(line) + '\n');
- rl.prompt();
+ return session.exec(line);
}).on('close', function() {
session.close();
});
View
2  test/parser-test.js
@@ -8,7 +8,7 @@ describe('grammar', function() {
assert.deepEqual({"select":{"exprs":["id","foo"],"from":["bar"]}}, parser.parse('select id, foo from bar'));
});
it('create table bar (id integer primary key auto_increment, foo varchar)', function() {
- assert.deepEqual({"createTable":{fields:[{"field":"id","type":"integer","pk":true,"ai":true},{"field":"foo","type":"varchar"}]}},
+ assert.deepEqual({"createTable":{name:'bar',fields:[{"field":"id","type":"integer","pk":true,"ai":true},{"field":"foo","type":"varchar"}]}},
parser.parse('create table bar (id integer primary key auto_increment, foo varchar)'));
});
it('insert into bar (id, foo) values (1, "hello")', function() {
View
20 test/session-test.js
@@ -19,9 +19,25 @@ describe('Session', function() {
it('should return an empty string when executing no input', function() {
assert.deepEqual('', new session.Session(db).exec(''));
});
+
var sess = new session.Session(db);
- it('should return an error when executing nonsense', function() {
- assert.deepEqual(' ^\nSyntax error: Expected ",", "=" or "from" but "s" found.', sess.exec('select three sheep from a field'));
+ it('should event an error when executing nonsense', function(done) {
+ sess.exec('select three sheep from a field', function(err, res) {
+ assert.equal(' ^\nSyntax error: Expected ",", "=" or "from" but "s" found.',
+ err);
+ done();
+ });
});
+
+ var sess2 = new session.Session(db);
+ it('should create a table', function(done) {
+ sess2.exec('create table bar (id integer primary key, foo varchar)', function(err, res) {
+ //console.log('on res', res);
+ assert.equal('Table created.',
+ res);
+ done();
+ });
+ });
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.