Permalink
Browse files

remove database automatic creation & fix tests

To run tests with authentication you need to create a test/credentials.js file
to store specifics credentials for your database.
Example of file :
    module.exports = {"username": "admin",
                      "password": "password"}
In tools directory you will now find every files you need to create and setup
database. Database will be created if not exists, design document will replace
any existing one with the same name and _revs_limit option will be set.
  • Loading branch information...
1 parent d7f133c commit 1677e4bf53799a58731954338ec2dff1db7d1ec5 @tdebarochez committed Sep 11, 2011
Showing with 274 additions and 99 deletions.
  1. +3 −1 .gitignore
  2. +12 −4 README.md
  3. +36 −23 lib/connect-couchdb.js
  4. +1 −1 package.json
  5. +0 −61 test/store.js
  6. +29 −9 test/{couch.js → test.couch.js}
  7. +86 −0 test/test.store.js
  8. +26 −0 tools/put_database.js
  9. +26 −0 tools/put_design_docs.js
  10. +27 −0 tools/put_options.js
  11. +28 −0 tools/setup.js
View
@@ -1,2 +1,4 @@
.idea/*
-*.iml
+*.iml
+test/credentials.js
+
View
@@ -34,10 +34,18 @@ Via npm:
var server = connect.createServer();
server.use(connect.session({secret: 'YourSecretKey', store: store });
-If the database specified doesn't already exist `connect-couch` will create it
-and setup its primary design document. It is highly recommended that you use
-a separate database for your sessions for performance of both the session
-views and any other document views you may have.
+If the database specified doesn't already exist you have to create it with
+`tools/` files. Run following command to create database, populate with the
+design document and setup the CouchDB database specific option `_revs_limit` :
+
+ $ node tools/setup.js <database_name> <revs_limit> [username] [password]
+
+For more informations about the `_revs_limit` option, read
+[this](http://wiki.apache.org/couchdb/HTTP_database_API#Accessing_Database-specific_options).
+
+It is highly recommended that you use a separate database for your
+sessions for performance of both the session views and any other document
+views you may have.
See `example.js` file for an example connect server using `connect-couch`.
View
@@ -5,8 +5,12 @@ var env = process.env.NODE_ENV;
module.exports = function(connect) {
var Store = connect.session.Store;
- function ConnectCouchDB(opts, fn) {
+ function ConnectCouchDB(opts, fn) {
opts = opts || {};
+ if (fn) {
+ console.error('DEPRECATED usage of callback function in ConnectCouchDB\'s constructor');
+ }
+
Store.call(this, opts);
this.db = null;
@@ -19,7 +23,8 @@ module.exports = function(connect) {
setInterval(this.reap.bind(this), this.reapInterval);
setInterval(this.compact.bind(this), this.compactInterval);
}
- this.setup(opts, fn);
+ if (!opts.name) throw "You must define a database";
+ this.db = new Couch(opts);
};
function _check_error(err) {
@@ -36,40 +41,48 @@ module.exports = function(connect) {
}
ConnectCouchDB.prototype.__proto__ = Store.prototype;
-
- function _put_design_docs(self, revs_limit, cb) {
- self.db.putDesignDocs([__dirname + '/connect-session.json'], function(err) {
+
+ ConnectCouchDB.prototype.setupOptions = function (opts, fn) {
+ this.db.putOpt('_revs_limit', opts.revs_limit, function(err) {
_check_error(err);
- self.db.putOpt('_revs_limit', revs_limit, function(err) {
- _check_error(err);
- cb && cb();
- }.bind(self));
- }.bind(self));
+ fn && fn(err);
+ });
}
- ConnectCouchDB.prototype.setup = function(opts, fn) {
- opts = opts || {};
- opts.revs_limit = opts.revs_limit || '1';
- if (!opts.name) throw "You must define a database";
+ ConnectCouchDB.prototype.setupDesignDocs = function (fn) {
+ this.db.putDesignDocs([__dirname + '/connect-session.json'], function(err) {
+ _check_error(err);
+ fn && fn(err);
+ });
+ }
- this.db = new Couch(opts);
+ ConnectCouchDB.prototype.setupDatabase = function (fn) {
this.db.get('', function(err, doc) {
- // Database already exists.
- if (!err) {
- _put_design_docs(this, opts.revs_limit, fn);
- // Database does not exist.
- } else if (err.statusCode === 404) {
+ if (err && err.statusCode === 404) {
this.db.dbPut(function(err) {
_check_error(err);
- _put_design_docs(this, opts.revs_limit, fn);
+ fn && fn(err);
}.bind(this));
- // Non-404 error occurred.
- } else {
+ }
+ else {
_check_error(err);
+ fn && fn(err);
}
}.bind(this));
};
+ ConnectCouchDB.prototype.setup = function (opts, fn) {
+ this.setupDatabase(function (err) {
+ if (err) return fn && fn(err);
+ this.setupDesignDocs(function (err) {
+ if (err) return fn && fn(err);
+ this.setupOptions(opts, function (err) {
+ fn && fn(err);
+ });
+ }.bind(this));
+ }.bind(this));
+ };
+
ConnectCouchDB.prototype.get = function (sid, fn) {
sid = _uri_encode(sid);
var now = +new Date;
View
@@ -18,7 +18,7 @@
"expresso": "0.8.0"
},
"scripts": {
- "test": "NODE_ENV=test expresso"
+ "test": "NODE_ENV=test expresso test/test.*.js"
},
"engines": { "node": ">= 0.3.0" },
"repository" : {"type" : "git",
View
@@ -1,61 +0,0 @@
-var assert = require('assert'),
- connect = require('connect'),
- ConnectCouchDB = require('../')(connect);
-
-module.exports = {
- // Test basic set/get/clear/length functionality.
- 'basic': function () {
- var store = new ConnectCouchDB({ name: 'connect-couch-test' }, function() {
- // #set()
- store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(err, ok){
- assert.ok(!err, '#set() got an error');
- // #get()
- store.get('123', function(err, data){
- assert.ok(!err, '#get() got an error');
- assert.deepEqual({ cookie: { maxAge: 2000 }, name: 'tj' }, data);
- // #length()
- store.length(function(err, len){
- assert.ok(!err, '#length() got an error');
- assert.equal(1, len, '#length() with keys');
- // #clear()
- store.clear(function(err, ok){
- assert.ok(!err, '#clear()');
- // #length()
- store.length(function(err, len){
- assert.ok(!err, '#length()');
- assert.equal(0, len, '#length() without keys');
- // #set null
- store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(){
- store.destroy('123', function(){
- store.length(function(err, len){
- assert.equal(0, len, '#set() null');
- store.db.dbDel();
- });
- });
- });
- });
- });
- });
- });
- });
- });
- },
- // Test expired session reaping.
- 'reap': function () {
- var store = new ConnectCouchDB({
- name: 'connect-couch-reap',
- reapInterval: 500
- }, function() {
- store.set('1', { cookie: { maxAge: 250 } });
- store.set('2', { cookie: { maxAge: 250 } });
- store.set('3', { cookie: { maxAge: 5000 } });
- store.set('4', { cookie: { maxAge: 5000 } });
- setTimeout(function() {
- store.length(function(err, len) {
- assert.equal(2, len, '#length() after reap');
- store.db.dbDel();
- });
- }, 1000);
- });
- }
-};
@@ -1,11 +1,19 @@
var couch = require('../lib/couch')
- , assert = require('assert');
+ , assert = require('assert')
+ , path = require('path')
+ , global_opts = {"name": 'connect-couchdb-' + +new Date};
-var db_name = 'connect-couchdb-' + +new Date;
+if (path.existsSync('./test/credentials.js')) {
+ var credentials = require('./credentials.js');
+ global_opts.username = credentials.username;
+ global_opts.password = credentials.password;
+}
module.exports = {
'create & delete database': function () {
- var db = new couch({name: db_name + '1'});
+ var opts = global_opts;
+ opts.name = opts.name + '1';
+ var db = new couch(opts);
db.dbPut(function (err, res) {
assert.strictEqual(err, null);
db.dbDel(function (err, res) {
@@ -18,17 +26,25 @@ module.exports = {
new couch();
});
},
- 'database _rev_limit option': function () {
- var db = new couch({name: db_name + '2'});
+ 'database _revs_limit option': function () {
+ var opts = global_opts;
+ opts.name = opts.name + '2';
+ var db = new couch(opts);
db.dbPut(function (err, res) {
db.putOpt('_revs_limit', '1', function(err, res) {
assert.strictEqual(err, null);
- db.dbDel();
+ db.getOpt('_revs_limit', function (err, res) {
+ assert.strictEqual(err, null);
+ assert.strictEqual(res, 1);
+ db.dbDel();
+ });
});
});
},
'create & remove document': function () {
- var db = new couch({name: db_name + '3'});
+ var opts = global_opts;
+ opts.name = opts.name + '3';
+ var db = new couch(opts);
db.dbPut(function (err, res) {
db.put({_id: "qsd", aze: 3}, function (err, res) {
assert.strictEqual(err, null);
@@ -43,7 +59,9 @@ module.exports = {
});
},
'get & update document': function () {
- var db = new couch({name: db_name + '4'});
+ var opts = global_opts;
+ opts.name = opts.name + '4';
+ var db = new couch(opts);
db.dbPut(function (err, res) {
db.put({_id: "wxc", aze: 4}, function (err, res) {
db.get("wxc", function (err, res) {
@@ -74,7 +92,9 @@ module.exports = {
});
},
'create document with post & views': function () {
- var db = new couch({name: db_name + '5'});
+ var opts = global_opts;
+ opts.name = opts.name + '5';
+ var db = new couch(opts);
db.dbPut(function (err, res) {
db.putDesignDocs([__dirname + '/../lib/connect-session.json'], function (err) {
assert.strictEqual(err, null);
View
@@ -0,0 +1,86 @@
+var assert = require('assert')
+ , connect = require('connect')
+ , path = require('path')
+ , ConnectCouchDB = require('../')(connect)
+ , global_opts = {"name": 'connect-couchdb-' + +new Date};
+
+if (path.existsSync('./test/credentials.js')) {
+ var credentials = require('./credentials.js');
+ global_opts.username = credentials.username;
+ global_opts.password = credentials.password;
+}
+
+module.exports = {
+ // Test basic set/get/clear/length functionality.
+ 'basic': function () {
+ var opts = global_opts;
+ opts.name = 'connect-couch-test';
+ opts.revs_limit = '2';
+ var store = new ConnectCouchDB(opts);
+ store.setup(opts, function (err, res) {
+ assert.ok(!err);
+ store.db.getOpt('_revs_limit', function (err, res) {
+ assert.ok(!err);
+ assert.equal(res, opts.revs_limit);
+ // #set()
+ store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(err, ok){
+ assert.ok(!err, '#set() got an error');
+ // #get()
+ store.get('123', function(err, data){
+ assert.ok(!err, '#get() got an error');
+ assert.deepEqual({ cookie: { maxAge: 2000 }, name: 'tj' }, data);
+ // #length()
+ store.length(function(err, len){
+ assert.ok(!err, '#length() got an error');
+ assert.equal(1, len, '#length() with keys');
+ // #clear()
+ store.clear(function(err, ok){
+ assert.ok(!err, '#clear()');
+ // #length()
+ store.length(function(err, len){
+ assert.ok(!err, '#length()');
+ assert.equal(0, len, '#length() without keys');
+ // #set null
+ store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(){
+ store.destroy('123', function(){
+ store.length(function(err, len){
+ assert.equal(0, len, '#set() null');
+ store.db.dbDel();
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ },
+ // Test expired session reaping.
+ 'reap': function () {
+ var opts = global_opts;
+ opts.name = 'connect-couch-reap';
+ opts.reapInterval = 500;
+ var store = new ConnectCouchDB(opts);
+ store.setupDatabase(function (err, res) {
+ assert.ok(!err);
+ store.setupDesignDocs(function (err, res) {
+ assert.ok(!err);
+ store.setupOptions(opts, function(err, res) {
+ assert.ok(!err);
+ store.set('1', { cookie: { maxAge: 250 } });
+ store.set('2', { cookie: { maxAge: 250 } });
+ store.set('3', { cookie: { maxAge: 5000 } });
+ store.set('4', { cookie: { maxAge: 5000 } });
+ setTimeout(function() {
+ store.length(function(err, len) {
+ assert.equal(2, len, '#length() after reap');
+ store.db.dbDel();
+ });
+ }, 1000);
+ });
+ });
+ });
+ }
+};
View
@@ -0,0 +1,26 @@
+var connect = require('connect');
+
+if (process.argv.length < 3) {
+ console.error('Usage : $ node put_database.js <database_name> [username] [password]');
+ process.exit(1);
+}
+
+var opts = {"name": process.argv[2]};
+if (process.argv.length >= 4) {
+ opts.username = process.argv[3];
+}
+if (process.argv.length >= 5) {
+ opts.password = process.argv[4];
+}
+
+var connect_couchdb = new (require(__dirname + '/../lib/connect-couchdb.js')(connect))(opts);
+
+connect_couchdb.setupDatabase(function (err) {
+ if (err) {
+ return;
+ process.exit(1);
+ }
+ console.log('ok !');
+ process.exit(0);
+});
+
Oops, something went wrong.

0 comments on commit 1677e4b

Please sign in to comment.