Last active
December 20, 2015 22:19
-
-
Save connors511/6204164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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