Adding find functions and tests to user and action models.

This commit is contained in:
David Jay
2016-11-03 17:45:37 -07:00
parent 084ee98ce2
commit 8e9ebfb1a9
7 changed files with 116 additions and 21 deletions
+16 -8
View File
@@ -1,5 +1,3 @@
'use strict';
const mongoose = require('../mongoose');
const uuid = require('uuid');
const Schema = mongoose.Schema;
@@ -14,12 +12,12 @@ const ActionSchema = new Schema({
item_type: String,
item_id: String,
user_id: String
},{
_id: false,
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
// },{
// _id: false,
// timestamps: {
// createdAt: 'created_at',
// updatedAt: 'updated_at'
// }
});
/**
@@ -30,6 +28,16 @@ ActionSchema.statics.findById = function(id) {
return Action.findOne({id});
};
/**
* Finds users in an array of ids.
* @param {String} ids array of user identifiers (uuid)
*/
ActionSchema.statics.findByItemIdArray = function(item_ids) {
return Action.find({
'item_id': {$in: item_ids}
});
};
const Action = mongoose.model('Action', ActionSchema);
module.exports = Action;
+16 -7
View File
@@ -1,4 +1,3 @@
'use strict';
const mongoose = require('../mongoose');
const uuid = require('uuid');
@@ -12,12 +11,12 @@ const UserProfileSchema = new Schema({
},
display_name: String,
auth_user_id: String
},{
_id: false,
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
// },{
// _id: false,
// timestamps: {
// createdAt: 'created_at',
// updatedAt: 'updated_at'
// }
});
/**
@@ -28,6 +27,16 @@ UserProfileSchema.statics.findById = function(id) {
return UserProfile.findOne({id});
};
/**
* Finds users in an array of idd.
* @param {String} idd array of user identifiers (uuid)
*/
UserProfileSchema.statics.findByIdArray = function(ids) {
return UserProfile.find({
'id': {$in: ids}
});
};
// TO DO: methods
// modifications to user as statics
// find by auth user id
+2 -1
View File
@@ -8,7 +8,8 @@
"build": "webpack --config ./client/coral-embed-stream/webpack.config.js",
"lint": "eslint .",
"pretest": "npm install",
"test": "mocha tests",
"test": "mocha tests --recursive",
"test-watch": "mocha tests --recursive -w",
"embed-start": "node client/coral-embed-stream/dev-server.js"
},
"config": {
+40
View File
@@ -0,0 +1,40 @@
/* eslint-env node, mocha */
require('../utils/mongoose');
const Action = require('../../models/action');
const expect = require('chai').expect;
describe('Action: models', () => {
var mockActions;
beforeEach(() => {
return Action.create([{
action_type: 'flag',
item_id: '123'
},{
action_type: 'like',
item_id: '789'
},{
action_type: 'flag',
item_id: '456'
}]).then((actions) => {
mockActions = actions;
});
});
describe('#findById()', () => {
it('should find an action by id', () => {
return Action.findById(mockActions[0].id).then((result) => {
expect(result).to.have.property('action_type')
.and.to.equal('flag');
});
});
});
describe('#findByItemIdArray()', () => {
it('should find an array of actions from an array of item_ids', () => {
return Action.findByItemIdArray(['123','456']).then((result) => {
expect(result).to.have.length(2);
});
});
});
});
+2 -2
View File
@@ -5,7 +5,7 @@ const Comment = require('../../models/comment');
const expect = require('chai').expect;
describe('Comment: models', () => {
var mockComments
var mockComments;
beforeEach(() => {
return Comment.create([{
body: 'comment 1',
@@ -17,7 +17,7 @@ describe('Comment: models', () => {
body: 'comment 3',
asset_id: '456'
}]).then((comments) => {
mockComments = comments
mockComments = comments;
});
});
+40
View File
@@ -0,0 +1,40 @@
/* eslint-env node, mocha */
require('../utils/mongoose');
const User = require('../../models/user');
const expect = require('chai').expect;
describe('User: models', () => {
var mockUsers;
beforeEach(() => {
return User.create([{
display_name: 'Stampi',
},{
display_name: 'Sockmonster',
},{
display_name: 'Marvel',
}]).then((users) => {
mockUsers = users;
});
});
describe('#findById()', () => {
it('should find a user by id', () => {
return User.findById(mockUsers[0].id).then((result) => {
expect(result).to.have.property('display_name')
.and.to.equal('Stampi');
});
});
});
describe('#findByIdArray()', () => {
it('should find an array of users from an array of ids', () => {
const ids = mockUsers.map((user) => user.id)
return User.findByIdArray(ids).then((result) => {
expect(result).to.have.length(3);
});
});
});
// });
});
-3
View File
@@ -8,8 +8,6 @@ var mongoose = require('mongoose');
process.env.NODE_ENV = 'test';
beforeEach(function (done) {
function clearDB() {
for (var i in mongoose.connection.collections) {
mongoose.connection.collections[i].remove(function() {});
@@ -30,7 +28,6 @@ beforeEach(function (done) {
}
});
after(function (done) {
mongoose.disconnect();
return done();