mirror of
https://github.com/wassname/talk.git
synced 2026-08-13 12:40:11 +08:00
Implement asset tests
This commit is contained in:
+40
-4
@@ -27,6 +27,7 @@ const AssetSchema = new Schema({
|
||||
publication_date: Date
|
||||
},{
|
||||
_id: false,
|
||||
versionKey: false,
|
||||
timestamps: {
|
||||
createdAt: 'created_at',
|
||||
updatedAt: 'updated_at'
|
||||
@@ -34,9 +35,18 @@ const AssetSchema = new Schema({
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Search for assets. Currently only returns all.
|
||||
*/
|
||||
AssetSchema.statics.search = function() {
|
||||
|
||||
return Asset.find({});
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds an asset by its id.
|
||||
* @param {String} id identifier of the asset (uuid)
|
||||
* @param {String} id identifier of the asset (uuid).
|
||||
*/
|
||||
AssetSchema.statics.findById = function(id) {
|
||||
|
||||
@@ -46,11 +56,11 @@ AssetSchema.statics.findById = function(id) {
|
||||
|
||||
/**
|
||||
* Finds a asset by its url.
|
||||
* @param {String} url identifier of the asset (uuid)
|
||||
* @param {String} url identifier of the asset (uuid).
|
||||
*/
|
||||
AssetSchema.statics.findByUrl = function(url) {
|
||||
|
||||
return Asset.findOne({url: url});
|
||||
return Asset.findOne({'url': url});
|
||||
|
||||
};
|
||||
|
||||
@@ -65,10 +75,36 @@ AssetSchema.statics.upsert = function(data) {
|
||||
data.id = uuid.v4();
|
||||
}
|
||||
|
||||
return Asset.update({id: data.id}, data, {upsert: true});
|
||||
// Perform the upsert against the id field.
|
||||
let updatePromise = Asset.update({id: data.id}, data, {upsert: true})
|
||||
.then((updateRes) => {
|
||||
|
||||
// Pull the freshly minted asset out and return.
|
||||
return Asset.findById(data.id);
|
||||
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
console.error('Error upserting asset.', err);
|
||||
//return new Promise(); // ??? what do we return on error?
|
||||
|
||||
});
|
||||
|
||||
return updatePromise;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove assets from the db.
|
||||
* @param {String} query bson query to identify assets to be removed.
|
||||
*/
|
||||
AssetSchema.statics.removeAll = function(query) {
|
||||
|
||||
return Asset.remove(query);
|
||||
|
||||
};
|
||||
|
||||
|
||||
const Asset = mongoose.model('Asset', AssetSchema);
|
||||
|
||||
module.exports = Asset;
|
||||
|
||||
+1
-1
@@ -7,7 +7,6 @@
|
||||
"start": "./bin/www",
|
||||
"build": "webpack --config ./client/coral-embed-stream/webpack.config.js",
|
||||
"lint": "eslint .",
|
||||
"pretest": "npm install",
|
||||
"test": "mocha tests",
|
||||
"embed-start": "node client/coral-embed-stream/dev-server.js"
|
||||
},
|
||||
@@ -42,6 +41,7 @@
|
||||
"homepage": "https://github.com/coralproject/talk#readme",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.15.2",
|
||||
"chai-http": "^3.0.0",
|
||||
"debug": "^2.2.0",
|
||||
"express": "^4.14.0",
|
||||
"mongoose": "^4.6.5"
|
||||
|
||||
@@ -2,6 +2,16 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const Asset = require('../../../models/asset');
|
||||
|
||||
// Get many assets
|
||||
router.get('/', (req, res) => {
|
||||
|
||||
Asset.search(req.params.id)
|
||||
.then((asset) => {
|
||||
res.json(asset);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// Get an asset by id
|
||||
router.get('/:id', (req, res) => {
|
||||
|
||||
@@ -22,12 +32,14 @@ router.get('/url/:url', (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
// Upsert an asset
|
||||
// Upsert an asset and return the affected document.
|
||||
router.put('/', (req, res) => {
|
||||
|
||||
Asset.upsert(req.body)
|
||||
.then((asset) => {
|
||||
|
||||
res.json(asset);
|
||||
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
/* eslint-env node, mocha */
|
||||
const Asset = require('../models/asset');
|
||||
|
||||
const expect = require('chai').expect;
|
||||
let chai = require('chai');
|
||||
let chaiHttp = require('chai-http');
|
||||
let server = require('../app');
|
||||
let should = chai.should();
|
||||
|
||||
chai.use(chaiHttp);
|
||||
|
||||
var fixture = {
|
||||
"url": "simple",
|
||||
"type": "article",
|
||||
"headline": "The Total Perspective Vortex",
|
||||
"summary": "You are an insignificant dot on an insignificant dot.",
|
||||
"section": "Everything",
|
||||
"authors": ["Ford Prefect"]
|
||||
};
|
||||
|
||||
|
||||
describe('Asset', () => {
|
||||
|
||||
beforeEach((done) => {
|
||||
|
||||
// TODO: implement asset remove
|
||||
Asset.removeAll({})
|
||||
.then((asset) => {
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('/GET Asset', () => {
|
||||
describe.only('#get', () => {
|
||||
it('It should get an empty array when there are no assets.', (done) => {
|
||||
|
||||
chai.request(server)
|
||||
.get('/api/v1/asset')
|
||||
.end((err, res) => {
|
||||
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
res.should.have.status(200);
|
||||
res.body.should.be.a('array');
|
||||
res.body.length.should.be.eql(0);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// This test checks PUT and read
|
||||
describe('/PUT Asset', () => {
|
||||
describe.only('#put', () => {
|
||||
it('It should save an asset and load it again.', (done) => {
|
||||
|
||||
chai.request(server)
|
||||
.put('/api/v1/asset')
|
||||
.send(fixture)
|
||||
.end((err, res) => {
|
||||
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
res.should.have.status(200);
|
||||
res.body.should.be.a('object');
|
||||
|
||||
// Id should be generated by the model if absent.
|
||||
res.body.should.have.property('id');
|
||||
|
||||
// Save the asset id to compare with GET result.
|
||||
let assetId = res.body.id;
|
||||
|
||||
// Load the asset to make sure it's really there.
|
||||
chai.request(server)
|
||||
.get('/api/v1/asset/url/' + fixture.url)
|
||||
.end((err, res) => {
|
||||
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
||||
res.should.have.status(200);
|
||||
res.body.should.be.a('object');
|
||||
res.body.should.have.property('id');
|
||||
|
||||
// ensure the asset has the same id as above
|
||||
expect(assetId).to.equal(res.body.id);
|
||||
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}); // End describe /PUT Asset
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user