Skip to content

Instantly share code, notes, and snippets.

@connors511
Last active December 20, 2015 22:19
Show Gist options
  • Save connors511/6204164 to your computer and use it in GitHub Desktop.
Save connors511/6204164 to your computer and use it in GitHub Desktop.
module.exports = function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var _schema = new Schema({
name: { type: String, trim: true, required: true },
kitchen: { type: Schema.ObjectId, ref: 'Kitchen' },
users: [{
user: { type: Schema.ObjectId, ref: 'User' },
permissions: {
admin: { type: Boolean, default: false }
}
}
]
});
var _model = mongoose.model('ClubTest', _schema);
_model.singular = 'clubtest';
var _create = function(club) {
return new _model(club);
};
return {
schema: _schema,
model: _model,
create: _create
}
}();
module.exports = function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var _schema = new Schema({
name: { type : String, default : '', required: true, trim : true, index: {unique: true, dropDups: true} },
users: [{
user: { type : Schema.ObjectId, ref : 'User' },
permissions: {
admin: { type: Boolean, default: false }
}
}]
});
var _model = mongoose.model('KitchenTest', _schema);
_model.singular = 'kitchentest';
var _create = function(kitchen) {
return new _model(kitchen);
};
return {
schema: _schema,
model: _model,
create: _create
}
}();
'use strict';
var expect = require('chai').expect;
var assert = require('assert');
var mongoose = require('mongoose');
mongoose.connection.on('error', function (err) {
console.log('mongoose err: ', err);
});
var User, Kitchen, Transaction, Club, _user, _kitchen, _club;
User = require('./user');
Kitchen = require('./kitchen');
Club = require('./club');
Transaction = require('./transaction');
describe('Transaction test', function () {
before(function (done) {
mongoose.models = {};
mongoose.modelSchemas = {};
mongoose.connect('mongodb://localhost/kp_unittest');
_user = User.create({
email: '[email protected]',
password: 'test1',
name: 'Test'
});
_kitchen = Kitchen.create({
name: 'Test'
});
_user.save(function(){
_kitchen.users.push({
user: _user,
permissions: { admin: true }
});
_kitchen.save(done);
});
});
describe('ListForUserClub', function() {
beforeEach(function(done) {
var club = Club.create({
name: 'test'
});
club.users.push({
user: _user,
permissions: {
admin: true
}
});
club.kitchen = _kitchen;
club.save(function() {
_club = club;
var transaction1 = Transaction.create({
kitchen: _kitchen,
where: {
club: _club
},
origin: _user._id,
total: 100,
type: 'bank.deposit'
});
var transaction2 = Transaction.create({
kitchen: _kitchen,
where: {
club: _club
},
origin: _user._id,
total: 200,
type: 'cash.deposit'
});
transaction1.save(function() {
transaction2.save(function(d) {
done();
});
});
});
});
it.only('should list all transactions for a user', function (done) {
// This fails with uncaught MissingSchema
Transaction.listForUserClub(_user._id, _club._id, function (err, list) {
expect(list).to.have.length(2);
});
});
});
afterEach(function(done) {
Transaction.model.remove({}, done);
});
after(function(done){
User.model.remove({}, function() {
Kitchen.model.remove({}, function() {
mongoose.disconnect(done);
});
});
});
});
module.exports = function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var _schema = new Schema({
total: { type: Number, required: true },
type: { type: String, required: true },
where: {
kitchen: { type: Schema.ObjectId, ref: 'Kitchen' },
club: { type: Schema.ObjectId, ref: 'Club' }
},
origin: { type: Schema.ObjectId, ref: 'User', required: true },
kitchen: { type: Schema.ObjectId, ref: 'Kitchen', required: true }
});
var _model = mongoose.model('TransactionTest', _schema);
_model.singular = 'transactiontest';
var _create = function(purchase) {
return new _model(purchase);
};
var _listForUserClub = function(id, clubId, cb) {
_model.find({ 'where.club': clubId })
.populate('where.kitchen')
.populate('where.club')
.populate('origin')
.populate('kitchen')
.exec(cb)
};
return {
schema: _schema,
model: _model,
// Methods
create: _create,
listForUserClub: _listForUserClub
}
}();
module.exports = function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var _schema = new Schema({
name: { type: String, default: '' },
password: { type: String, default: '' },
email: { type: String, default: '' },
})
var _model = mongoose.model('UserTest', _schema);
_model.singular = 'usertest';
var _create = function(user) {
return new _model(user);
};
return {
schema: _schema,
model: _model,
create: _create
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment