Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify cleanAllDb in tests #412

Merged
merged 1 commit into from
Jun 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions test/activities.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ describe('activities.routes.test.js', () => {
var user2;
var group;


beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create users.
beforeEach(() => models.User.create(utils.data('user1')).tap(u => user = u));
Expand Down
7 changes: 1 addition & 6 deletions test/auth.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ describe('auth.routes.test.js', () => {

var application;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

beforeEach(() => models.User.create(userData));

Expand Down
4 changes: 1 addition & 3 deletions test/authorization.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ describe('authorization.middleware.test.js', () => {

var user, req;

beforeEach(done => {
utils.cleanAllDb(done);
});
beforeEach(() => utils.cleanAllDb());

beforeEach((done) => {
models.User.create(userData)
Expand Down
8 changes: 2 additions & 6 deletions test/connectedAccounts.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ const clientId = config.github.clientID;
describe('connectedAccounts.routes.test.js: GIVEN an application and group', () => {

var application, req;
beforeEach(done => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});

beforeEach(() => utils.cleanAllDb().tap(a => application = a));

describe('WHEN calling /connected-accounts/github', () => {

Expand Down
10 changes: 2 additions & 8 deletions test/donations.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,8 @@ describe('donations.routes.test.js', () => {
var stub = sinon.stub(app.stripe.accounts, 'create');
stub.yields(null, mock);
};


beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});

beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create a stub for clearbit
beforeEach((done) => {
Expand Down
7 changes: 1 addition & 6 deletions test/expenses.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ const PaymentMethod = models.PaymentMethod;
describe('expenses.routes.test.js: GIVEN an application, group, and host user', () => {
var application, user, group;

beforeEach(done => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

beforeEach(() => Promise.props({
user: models.User.create(utils.data('user1')),
Expand Down
7 changes: 1 addition & 6 deletions test/groups.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ describe('groups.routes.test.js', () => {
var application;
var user;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

beforeEach(() => models.User.create(userData).tap(u => user = u));

Expand Down
7 changes: 1 addition & 6 deletions test/images.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ describe('images.routes.test.js', () => {
var application;
var user;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

/**
* Create user
Expand Down
7 changes: 1 addition & 6 deletions test/notification.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ describe("notification.model.test.js", () => {
var group;
var group2;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

beforeEach(() => {
var promises = [User.create(userData), User.create(user2Data), Group.create(groupData), Group.create(group2Data)];
Expand Down
8 changes: 1 addition & 7 deletions test/paymentmethods.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ describe('paymentMethods.routes.test.js', () => {
var user2;
var paymentMethod1;


beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create users.
beforeEach(() => models.User.create(utils.data('user1')).tap(u => user = u));
Expand Down
2 changes: 1 addition & 1 deletion test/paypal.preapproval.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('paypal.preapproval.routes.test.js', () => {
beforeEach((done) => {
async.auto({
cleanAndCreateApplication: (cb) => {
utils.cleanAllDb(cb);
utils.cleanAllDb().asCallback(cb);
},
createUserA: ['cleanAndCreateApplication', (cb) => {
models.User.create(utils.data('user1'))
Expand Down
2 changes: 1 addition & 1 deletion test/populate_recurring_paypal_transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('scripts/populate_recurring_paypal_transactions', () => {
var subscription;
var runScript;

beforeEach((done) => utils.cleanAllDb(done));
beforeEach(() => utils.cleanAllDb());

beforeEach(() => models.User.create(data('user1')).tap(u => user = u));

Expand Down
7 changes: 1 addition & 6 deletions test/stripe.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ describe('stripe.routes.test.js', () => {
var group;
var application;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create a user.
beforeEach(() => models.User.create(utils.data('user1')).tap(u => user = u));
Expand Down
11 changes: 1 addition & 10 deletions test/subscriptions.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@ describe('subscriptions.routes.test.js', () => {
var application;
var paymentMethod;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
expect(e).to.not.exist;
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create user.
beforeEach(() => models.User.create(utils.data('user1')).tap((u => user = u)));

// Create the group.
beforeEach(() => models.Group.create(utils.data('group1')).tap((g => group = g)));

// Add user to the group.
beforeEach(() => group.addUserWithRole(user, roles.HOST));

// create stripe account
Expand Down
4 changes: 1 addition & 3 deletions test/transaction.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ const Transaction = models.Transaction;

describe('transaction model', () => {

beforeEach(done => {
utils.cleanAllDb(done);
});
beforeEach(() => utils.cleanAllDb());

it('isExpense is true if the amount is negative', done => {
Transaction.create({
Expand Down
6 changes: 2 additions & 4 deletions test/transactions.paypal.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ describe('transactions.paypal.routes.test.js', () => {
});
});

after(() => {
app.paypalAdaptive.pay.restore();
});
after(() => app.paypalAdaptive.pay.restore());

beforeEach((done) => {
async.auto({

cleanAndCreateApplication: (cb) => {
utils.cleanAllDb(cb);
utils.cleanAllDb().asCallback(cb);
},
createUserB: ['cleanAndCreateApplication', (cb) => {
models.User.create(utils.data('user2'))
Expand Down
14 changes: 3 additions & 11 deletions test/transactions.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,10 @@ describe('transactions.routes.test.js', () => {
var application3;
var sandbox = sinon.sandbox.create();

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create a stub for clearbit
beforeEach((done) => {
utils.clearbitStubBeforeEach(sandbox);
done();
});
beforeEach(() => utils.clearbitStubBeforeEach(sandbox));

// Create user.
beforeEach(() => models.User.create(utils.data('user1')).tap(u => user = u));
Expand Down Expand Up @@ -863,7 +855,7 @@ describe('transactions.routes.test.js', () => {
});
});

beforeEach(() =>
beforeEach(() =>
models.PaymentMethod.create({
service: 'paypal',
UserId: user.id,
Expand Down
13 changes: 3 additions & 10 deletions test/user.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ describe('user.models.test.js', () => {
var application;
var User;

beforeEach(() => {
User = app.get('models').User;
})

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => User = app.get('models').User);

beforeEach(() => utils.cleanAllDb().tap(a => application = a));

/**
* Create a user.
Expand Down
7 changes: 1 addition & 6 deletions test/usergroup.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ const createDonations = (cb) => {
describe('usergroup.routes.test.js', () => {
var application;

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create users.
beforeEach(() =>
Expand Down
9 changes: 2 additions & 7 deletions test/users.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ describe('users.routes.test.js', () => {
});
});

afterEach(() => sandbox.restore() );
afterEach(() => sandbox.restore());

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create a normal application.
beforeEach(() => models.Application.create(utils.data('application2')).tap(a => application2 = a));
Expand Down
18 changes: 7 additions & 11 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Dependencies.
*/
const _ = require('lodash');
const Promise = require('bluebird');
const app = require('../index');
const models = app.set('models');
Expand All @@ -12,7 +11,7 @@ const userlib = require('../server/lib/userlib');
/**
* Private methods.
*/
const getData = (item) => _.extend({}, data[item]); // to avoid changing these data
const getData = (item) => Object.assign({}, data[item]); // to avoid changing these data

const clearbitStubBeforeEach = sandbox => {
sandbox.stub(userlib.clearbit.Enrichment, 'find', () => {
Expand All @@ -29,15 +28,12 @@ module.exports = () => {

return {

cleanAllDb: (callback) => {
app.get('models').sequelize.sync({force: true})
.then(() => models.Application.create(getData('applicationSuper')))
.tap(a => callback(null, a))
.catch(e => {
console.error("test/utils.js> Sequelize Error: Couldn't recreate the schema", e);
process.exit(1);
});
},
cleanAllDb: () => app.get('models').sequelize.sync({force: true})
.then(() => models.Application.create(getData('applicationSuper')))
.catch(e => {
console.error("test/utils.js> Sequelize Error: Couldn't recreate the schema", e);
process.exit(1);
}),

/**
* Test data.
Expand Down
22 changes: 4 additions & 18 deletions test/webhooks.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,10 @@ describe('webhooks.routes.test.js', () => {
var donation;
var sandbox = sinon.sandbox.create();

beforeEach((done) => {
utils.cleanAllDb((e, app) => {
application = app;
done();
});
});
beforeEach(() => utils.cleanAllDb().tap(a => application = a));

// Create a stub for clearbit
beforeEach((done) => {
utils.clearbitStubBeforeEach(sandbox);
done();
});
beforeEach(() => utils.clearbitStubBeforeEach(sandbox));

// Create a user.
beforeEach(() => models.User.create(userData).tap(u => user = u));

// Create a group.
Expand Down Expand Up @@ -99,13 +89,9 @@ describe('webhooks.routes.test.js', () => {
})
.then((account) => user.setStripeAccount(account)));

afterEach(() => {
nock.cleanAll();
});
afterEach(() => nock.cleanAll());

afterEach(() => {
utils.clearbitStubAfterEach(sandbox);
});
afterEach(() => utils.clearbitStubAfterEach(sandbox));

describe('success', () => {
var planId = generatePlanId({
Expand Down