Added basic profile model

This commit is contained in:
Chris Blanchard 2015-08-27 19:42:45 +01:00
parent 79a5997055
commit 1d40317281
4 changed files with 80 additions and 0 deletions

View file

@ -9,5 +9,6 @@ mongoose.connect(config.mongo.uri);
// Load models
require(path.join(__dirname, "/models/message"));
require(path.join(__dirname, "/models/session"));
require(path.join(__dirname, "/models/profile"));
module.exports = mongoose;

19
db/models/profile.js Normal file
View file

@ -0,0 +1,19 @@
"use strict";
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var profileSchema = new Schema({
userId: { type: Number, required: true },
skills: {
lerk: { type: Boolean, default: false },
fade: { type: Boolean, default: false },
gorge: { type: Boolean, default: false },
onos: { type: Boolean, default: false },
commander: { type: Boolean, default: false }
}
});
profileSchema.path('userId').index({ unique: true });
module.exports = mongoose.model("Profile", profileSchema);

View file

@ -23,12 +23,19 @@ var db = require(path.join(__dirname, "../../db/index"));
var mongoose = require("mongoose");
var Message = helpers.Message = mongoose.model("Message");
var Session = helpers.Session = mongoose.model("Session");
var Profile = helpers.Profile = mongoose.model("Profile");
var async = require("async");
helpers.clearDb = function (callback) {
async.series([
function (cb) {
Message.remove({}, cb)
},
function (cb) {
Session.remove({}, cb)
},
function (cb) {
Profile.remove({}, cb)
}
], callback);
}

53
spec/profile.js Normal file
View file

@ -0,0 +1,53 @@
"use strict";
var helper = require("./helpers/index.js");
var Profile = helper.Profile;
var assert = require("chai").assert;
var async = require("async");
var userCount = 0;
describe("Profile model", () => {
describe(".create", () => {
var profile;
beforeEach(() => {
profile = {
userId: ++userCount + Math.floor(Math.random() * 10000)
};
});
it ("creates a new profile", done => {
Profile.create(profile, (error, result) => {
if (error) return done(error);
assert.equal(result.userId, profile.userId);
assert.isFalse(result.skills.lerk);
assert.isFalse(result.skills.fade);
assert.isFalse(result.skills.onos);
assert.isFalse(result.skills.commander);
done();
});
});
it ("requires userId", done => {
Profile.create({}, (error, result) => {
assert.isNotNull(error);
done();
});
});
it ("requires unique userId", done => {
let userId = profile.userId;
Profile.create({
userId: userId
}, (error, result) => {
if (error) return done(error);
Profile.create({
userId: userId
}, (error, result) => {
assert.match(error.message, /E11000/);
done();
});
});
});
});
});