Skip to content

Commit

Permalink
[Hari|Siddharth] Making connectionFactory, execute function in Databa…
Browse files Browse the repository at this point in the history
…se Model #42 Improving Controller Tests #19
  • Loading branch information
hogaur committed Jul 9, 2014
1 parent f2abf95 commit 82ba5f1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
28 changes: 27 additions & 1 deletion app/models/database.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,38 @@ function parseInfo (info) {
return info;
}

DatabaseSchema.methods.connectionFactory = function(callback){
var client = redis.createClient(this.port, this.host);
client.on('error', function(error){ callback(error) });
client.on('connect', function(){ callback(null, client); });
};

function commandAllowed(command) {
if(/^flush*/.test(command))
return new Error('You can not flush db!');
else if(/^eval*/.test(command))
return new Error('Eval is evil!');
return null;
}

DatabaseSchema.methods.execute = function(command, callback){
var error = commandAllowed(command)
if(error) return callback(error, null);
command = command.split(' ');
this.connectionFactory(function(error, client){
if(error) return callback(error);
client.send_command(command[0],command.splice(1),function(error, response){
callback(error, response);
});
client.quit();
});
}

DatabaseSchema.methods.fetchInfoFromClient = function(callback){
var _this = this;
var client = redis.createClient(_this.port, _this.host);
client.on('error', function(error){console.log(error); client.quit(); });
client.on('connect', function(){
console.log('yay');
client.info(function(){
var info = new Info({
database: _this,
Expand Down
17 changes: 7 additions & 10 deletions tests/controllers/database.server.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ describe('Database Controller Tests:', function() {
.send(database)
.expect(200)
.end(function(err,res){
Database.findOne({ '_id' : res.body['_id']}, function(err, db){
db.name.should.equal(database['name']);
done();
});
res.body['name'].should.equal(database['name']);
done();
});
});
});
Expand Down Expand Up @@ -159,7 +157,7 @@ describe('Database Controller Tests:', function() {
.set('cookie',cookie)
.expect(200)
.end(function(err, res) {
if(res.body!==null) JSON.stringify(res.body).should.containDeep('keys');
JSON.stringify(res.body).should.containDeep('keys');
done();
});
});
Expand Down Expand Up @@ -417,7 +415,7 @@ describe('Database Controller Tests:', function() {
.delete('/databases/' + database._id)
.set('cookie',cookie)
.end(function(err, res){
JSON.stringify(res.body._id).should.eql(databaseId);
JSON.stringify(res.body._id).should.equal(databaseId);
done();
});
});
Expand Down Expand Up @@ -461,7 +459,7 @@ describe('Database Controller Tests:', function() {
.end(function(err, res){
Info.find({database : database._id}).sort('-timestamp').limit(10)
.exec(function(error, data){
data.should.eql(res.body);
JSON.stringify(data).should.equal(JSON.stringify(res.body));
done();
});
});
Expand Down Expand Up @@ -558,8 +556,7 @@ describe('Database Controller Tests:', function() {
.send({searchKeyword:'foo',selectedCollection:'db0'})
.set('cookie',cookie)
.end(function(err, res){
res.body.result.should.eql(["foo234567890","foo","foo123435366","foo34534253425234567890"]);
done();
JSON.stringify(res.body).should.equal(JSON.stringify({result:["foo234567890","foo","foo123435366","foo34534253425234567890"]})); done();
});
});
});
Expand All @@ -573,7 +570,7 @@ describe('Database Controller Tests:', function() {
.send({searchKeyword:'d', selectedCollection:'All Collections'})
.set('cookie', cookie)
.end(function(err, res){
res.body.result.should.eql(['dsafad-db0', 'dsafadf-db0', 'dsaf2-db2','dasfs-db2']);
JSON.stringify(res.body).should.equal(JSON.stringify({result:['dsafad-db0', 'dsafadf-db0', 'dsaf2-db2','dasfs-db2']}));
done();
});
});
Expand Down
62 changes: 58 additions & 4 deletions tests/models/database.server.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('Database Model Unit Tests:', function() {
user.save(function() {
database = new Database({
name: 'Database Name',
host:'host',
port: 1000,
host:'localhost',
port: 6379,
user: user
});

Expand All @@ -59,9 +59,8 @@ describe('Database Model Unit Tests:', function() {
});
});

it('should be able to show an error when try to save without host', function(done) {
it('should be able to show an error when try to save without host', function(done) {
database.host = '';

return database.save(function(err) {
should.exist(err);
done();
Expand All @@ -78,6 +77,61 @@ describe('Database Model Unit Tests:', function() {
});
});

describe('Method execute', function(){
it('should return command result', function(done){
database.save();
database.execute('select 1', function(err, res){
res.should.equal('OK');
done();
});
});

it('should disable flush,eval commands', function(done){
database.save();
database.execute('flushdb',function(err, res){
should.exist(err);
err.message.should.equal('You can not flush db!');
});
database.execute('eval', function(err, res){
should.exist(err);
err.message.should.equal('Eval is evil!');
done();
});
});

it('should give error for invalid commands', function(done){
database.save();
database.execute('dadsf',function(err, res){
should.exist(err);
done();
});
});
});

describe('Method connectionFactory',function () {
it('should create client', function(done){
database.save();
database.connectionFactory(function(error, client){
should.exist(client);
done();
});
});

it('should give error when tries to connect to an unavailable redis', function(done){
var db = new Database({
name: 'New DB',
host: '1.1.1.1',
port: 1000,
user: user
});
db.save();
db.connectionFactory(function (error, client) {
should.exist(error);
done();
})
});

});
describe('#fetchInfoFromClient',function(){
it('should fetch info from redis and save as Info model', function(done){
var database1 = new Database({
Expand Down

0 comments on commit 82ba5f1

Please sign in to comment.