Permalink
Browse files

Ensure we have a geo index

Ensure that we have a geo index on server start, which lets the
responses tests pass.
Make the server and test setup friendlier to multiple starts and stops.
  • Loading branch information...
prashtx committed Jul 27, 2012
1 parent 49f4d31 commit e22b33298da6d2e10626dddd23ec97ef061f0568
Showing with 44 additions and 15 deletions.
  1. +1 −1 Makefile
  2. +9 −4 test/test.responses.js
  3. +11 −4 test/test.surveys.js
  4. +23 −6 web.js
View
@@ -1,6 +1,6 @@
MOCHA = "./node_modules/.bin/mocha"
test:
- @$(MOCHA) --ui tdd
+ @$(MOCHA) --ui tdd --reporter spec
.PHONY: test
View
@@ -1,5 +1,5 @@
/*jslint node: true, indent: 2, white: true, vars: true */
-/*globals suite, test, setup, suiteSetup, done, teardown */
+/*globals suite, test, setup, suiteSetup, suiteTeardown, done, teardown */
'use strict';
var server = require('../web.js');
@@ -12,10 +12,15 @@ var settings = require('../settings-test.js');
var BASEURL = 'http://localhost:' + settings.port;
-var app;
-server.run(settings);
-
suite('Responses', function () {
+ suiteSetup(function (done) {
+ server.run(settings, done);
+ });
+
+ suiteTeardown(function () {
+ server.stop();
+ });
+
suite('POST', function () {
var data = {
"responses": [
View
@@ -1,5 +1,5 @@
/*jslint node: true, indent: 2, white: true, vars: true */
-/*globals suite, test, setup, done, teardown */
+/*globals suite, test, setup, suiteSetup, suiteTeardown, done, teardown */
'use strict';
var server = require('../web.js');
@@ -11,10 +11,17 @@ var settings = require('../settings-test.js');
var BASEURL = 'http://localhost:' + settings.port;
-var app;
-server.run(settings);
-
suite('Surveys', function () {
+ suiteSetup(function (done) {
+ server.run(settings, function () {
+ setTimeout(done, 1000);
+ });
+ });
+
+ suiteTeardown(function () {
+ server.stop();
+ });
+
suite('POST', function () {
var data = {
"surveys" : [ {
View
29 web.js
@@ -90,6 +90,16 @@ function setupRoutes(db, settings) {
scans.setup(app, db, idgen, SCANIMAGES, settings);
}
+// Ensure certain database structure.
+function ensureStructure(db, callback) {
+ db.collection(RESPONSES, function (error, collection) {
+ if (error) { throw error; }
+ collection.ensureIndex({'geo_info.centroid': '2d'}, function (error) {
+ callback(error);
+ });
+ });
+}
+
// Static files
// TODO: host these separately? Shift other routes to /api/ROUTES?
function sendFile(response, filename, type) {
@@ -157,22 +167,29 @@ function run(settings, cb) {
console.log('Mongo db: ' + settings.mongo_db);
console.log('Mongo user: ' + settings.mongo_user);
// Set up database
- db = new mongo.Db(settings.mongo_db, new mongo.Server(settings.mongo_host,
- settings.mongo_port,
- {}), {});
+ if (!db) {
+ db = new mongo.Db(settings.mongo_db, new mongo.Server(settings.mongo_host,
+ settings.mongo_port,
+ {}), {});
+ }
setupRoutes(db, settings);
-
db.open(function() {
if (settings.mongo_user !== undefined) {
db.authenticate(settings.mongo_user, settings.mongo_password, function(err, result) {
if (err) {
console.log(err.message);
return;
}
- startServer(settings.port, cb);
+ ensureStructure(db, function (error) {
+ if (error) { throw error; }
+ startServer(settings.port, cb);
+ });
});
} else {
- startServer(settings.port, cb);
+ ensureStructure(db, function (error) {
+ if (error) { throw error; }
+ startServer(settings.port, cb);
+ });
}
});
}

0 comments on commit e22b332

Please sign in to comment.