Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Changed tests to accommodate pouch views
Browse files Browse the repository at this point in the history
Make sure views are up to date before testing.
Build indexes on all views before testing.
  • Loading branch information
jkleinsc committed Oct 4, 2016
1 parent ceafc99 commit e17395a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
50 changes: 37 additions & 13 deletions app/utils/pouch-views.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import Ember from 'ember';
/* global req */
/* global compareStrings */
/* global getCompareDate */

function buildIndex(indexName, db) {
return db.query(indexName, {
limit: 0
}).catch(function(err) {
console.log('index error:' + JSON.stringify(err, null, 2));
});
}

function createDesignDoc(item, rev) {
var ddoc = {
_id: '_design/' + item.name,
Expand All @@ -21,6 +30,23 @@ function createDesignDoc(item, rev) {
return ddoc;
}

function checkForUpdate(view, db, runningTest, testDumpFile) {
return db.get('_design/' + view.name).then(function(doc) {
if (doc.version !== view.version) {
return updateDesignDoc(view, db, doc._rev, runningTest, testDumpFile);
} else {
if (runningTest) {
// Indexes need to be built when running tests
return buildIndex(view.name, db);
} else {
return Ember.RSVP.resolve();
}
}
}, function() {
return updateDesignDoc(view, db, null, runningTest, testDumpFile);
});
}

function generateSortFunction(sortFunction, includeCompareDate, filterFunction) {
var generatedFunction = 'function(head, req) {' +
'function keysEqual(keyA, keyB) {' +
Expand Down Expand Up @@ -99,14 +125,16 @@ function generateView(viewDocType, viewBody) {
'}';
}

function updateDesignDoc(item, db, rev) {
function updateDesignDoc(item, db, rev, runningTest, testDumpFile) {
var designDoc = createDesignDoc(item, rev);
db.put(designDoc).then(function() {
// design doc created!
if (runningTest) {
console.log(`WARNING: The view ${item.name} is out of date. Please update the pouch dump ${testDumpFile} to the latest version of ${item.name}`);
}
return db.put(designDoc).then(function() {
// Update index
db.query(item.name, { stale: 'update_after' });
return buildIndex(item.name, db);
}, function(err) {
console.log('ERR updateDesignDoc:', err);
console.log('ERR updating design doc:', JSON.stringify(err, null, 2));
// ignored, design doc already exists
});
}
Expand Down Expand Up @@ -391,14 +419,10 @@ var designDocs = [{
version: 4
}];

export default function(db) {
export default function(db, runningTest, testDumpFile) {
var viewUpdates = [];
designDocs.forEach(function(item) {
db.get('_design/' + item.name).then(function(doc) {
if (doc.version !== item.version) {
updateDesignDoc(item, db, doc._rev);
}
}, function() {
updateDesignDoc(item, db);
});
viewUpdates.push(checkForUpdate(item, db, runningTest, testDumpFile));
});
return Ember.RSVP.all(viewUpdates);
}
23 changes: 14 additions & 9 deletions tests/helpers/run-with-pouch-dump.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* jshint ignore:start */
import createPouchViews from 'hospitalrun/utils/pouch-views';
import Ember from 'ember';
import PouchDB from 'pouchdb';
import DatabaseService from 'hospitalrun/services/database';
Expand Down Expand Up @@ -39,9 +40,7 @@ function runWithPouchDumpAsyncHelper(app, dumpName, functionToRun) {
adapter: 'memory'
});
const dump = require(`hospitalrun/tests/fixtures/${dumpName}`).default;
const promise = db.load(dump, {
proxy: 'main'
});
const promise = db.load(dump);

const InMemoryDatabaseService = DatabaseService.extend({
createDB() {
Expand All @@ -68,13 +67,19 @@ function runWithPouchDumpAsyncHelper(app, dumpName, functionToRun) {

return new Ember.RSVP.Promise(function(resolve) {
promise.then(function() {
functionToRun();
andThen(function() {
cleanupDatabases({
config: configDB,
main: db
}).then(resolve);
createPouchViews(db, true, dumpName).then(function() {
functionToRun();
andThen(function() {
cleanupDatabases({
config: configDB,
main: db
}).then(resolve, function(err) {
console.log('error cleaning up dbs:', JSON.stringify(err, null, 2));
});
});
});
}, function(err) {
console.log('error loading db', JSON.stringify(err, null, 2));
});
});
}
Expand Down

0 comments on commit e17395a

Please sign in to comment.