From 8e9ebfb1a9a8863ed3c7fd564db25a7eb1b1d5e3 Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 3 Nov 2016 17:45:37 -0700 Subject: [PATCH] Adding find functions and tests to user and action models. --- models/action.js | 24 ++++++++++++++++-------- models/user.js | 23 ++++++++++++++++------- package.json | 3 ++- tests/models/action.js | 40 ++++++++++++++++++++++++++++++++++++++++ tests/models/comment.js | 4 ++-- tests/models/user.js | 40 ++++++++++++++++++++++++++++++++++++++++ tests/utils/mongoose.js | 3 --- 7 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 tests/models/action.js create mode 100644 tests/models/user.js diff --git a/models/action.js b/models/action.js index 581108a71..cb1d075b1 100644 --- a/models/action.js +++ b/models/action.js @@ -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; diff --git a/models/user.js b/models/user.js index d6b174075..6aba99f7b 100644 --- a/models/user.js +++ b/models/user.js @@ -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 diff --git a/package.json b/package.json index c25e54eb5..b490ca856 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/models/action.js b/tests/models/action.js new file mode 100644 index 000000000..8736edcab --- /dev/null +++ b/tests/models/action.js @@ -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); + }); + }); + }); +}); diff --git a/tests/models/comment.js b/tests/models/comment.js index 2eaa22a01..0f98db6ac 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -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; }); }); diff --git a/tests/models/user.js b/tests/models/user.js new file mode 100644 index 000000000..e1063e21c --- /dev/null +++ b/tests/models/user.js @@ -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); + }); + }); + }); + + // }); +}); diff --git a/tests/utils/mongoose.js b/tests/utils/mongoose.js index 002066001..7661eeb32 100644 --- a/tests/utils/mongoose.js +++ b/tests/utils/mongoose.js @@ -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();