diff --git a/models/comment.js b/models/comment.js index 107708a3b..a80e0dae7 100644 --- a/models/comment.js +++ b/models/comment.js @@ -1,5 +1,3 @@ -'use strict'; - const mongoose = require('../mongoose'); const uuid = require('uuid'); const Schema = mongoose.Schema; @@ -13,7 +11,7 @@ const CommentSchema = new Schema({ body: { type: String, required: [true, 'The body is required.'], - minlength: 50 + minlength: 1 }, asset_id: String, author_id: String, @@ -23,22 +21,31 @@ const CommentSchema = new Schema({ default: '' }, parent_id: String -},{ - _id: false, - timestamps: { - createdAt: 'created_at', - updatedAt: 'updated_at' - } +// },{ +// _id: false, +// timestamps: { +// createdAt: 'created_at', +// updatedAt: 'updated_at' +// } }); /** * Finds a comment by the id. - * @param {String} id identifier of the comment (uuid) + * @param {String} asset_id identifier of comment (uuid) */ CommentSchema.statics.findById = function(id) { return Comment.findOne({id}); }; +/** + * Finds a comment by the asset_id. + * @param {String} asset_id identifier of the asset which owns this comment (uuid) +*/ +CommentSchema.statics.findByAssetId = function(asset_id) { + return Comment.find({asset_id}); +}; + + const Comment = mongoose.model('Comment', CommentSchema); module.exports = Comment; diff --git a/package.json b/package.json index 096863202..c25e54eb5 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "dependencies": { "debug": "^2.2.0", "express": "^4.14.0", - "mongoose": "^4.6.5" + "mongoose": "^4.6.5", + "uuid": "^2.0.3" }, "devDependencies": { "babel-core": "6.14.0", diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 155450d42..b8a4c76ba 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -1,8 +1,9 @@ const express = require('express'); - +const Setting = require('../../../models/comment'); const router = express.Router(); router.get('/', (req, res, next) => { + console.log('Stream endpoint has been hit with asset_id ', req.query.asset_id); res.json([ { diff --git a/tests/index.js b/tests/index.js index 0a212b7a4..51ccc54ee 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,10 +1,12 @@ /* eslint-env node, mocha */ 'use strict'; +// require('./utils/mongoose') const expect = require('chai').expect; + describe('Comment', () => { - describe.only('#add', () => { + describe('#add', () => { it('should add a comment', () => { expect(0).to.be.equal(0); }); diff --git a/tests/models/comment.js b/tests/models/comment.js new file mode 100644 index 000000000..2eaa22a01 --- /dev/null +++ b/tests/models/comment.js @@ -0,0 +1,46 @@ +/* eslint-env node, mocha */ + +require('../utils/mongoose'); +const Comment = require('../../models/comment'); +const expect = require('chai').expect; + +describe('Comment: models', () => { + var mockComments + beforeEach(() => { + return Comment.create([{ + body: 'comment 1', + asset_id: '123' + },{ + body: 'comment 2', + asset_id: '123' + },{ + body: 'comment 3', + asset_id: '456' + }]).then((comments) => { + mockComments = comments + }); + }); + + describe('#findById()', () => { + it('should find a comment by id', () => { + return Comment.findById(mockComments[0].id).then((result) => { + expect(result).to.have.property('body') + .and.to.equal('comment 1'); + }); + }); + }); + + describe('#findByAssetId()', () => { + it('should find an array of comments by asset id', () => { + return Comment.findByAssetId('123').then((result) => { + expect(result).to.have.length(2); + expect(result[0]).to.have.property('body') + .and.to.equal('comment 1'); + expect(result[1]).to.have.property('body') + .and.to.equal('comment 2'); + }); + }); + }); + + // }); +}); diff --git a/tests/utils/mongoose.js b/tests/utils/mongoose.js new file mode 100644 index 000000000..002066001 --- /dev/null +++ b/tests/utils/mongoose.js @@ -0,0 +1,37 @@ +// Modified from https://github.com/elliotf/mocha-mongoose + +var mongoose = require('mongoose'); + + +// ensure the NODE_ENV is set to 'test' +// this is helpful when you would like to change behavior when testing +process.env.NODE_ENV = 'test'; + +beforeEach(function (done) { + + + function clearDB() { + for (var i in mongoose.connection.collections) { + mongoose.connection.collections[i].remove(function() {}); + } + return done(); + } + + + if (mongoose.connection.readyState === 0) { + mongoose.connect('coral-talk-test', function (err) { + if (err) { + throw err; + } + return clearDB(); + }); + } else { + return clearDB(); + } +}); + + +after(function (done) { + mongoose.disconnect(); + return done(); +});