Skip to content

Instantly share code, notes, and snippets.

@s-leroux
Last active April 2, 2016 21:25
Show Gist options
  • Save s-leroux/4aff39ebd1814d78ede2 to your computer and use it in GitHub Desktop.
Save s-leroux/4aff39ebd1814d78ede2 to your computer and use it in GitHub Desktop.
"Introduction to Server-side Development" course by Jogesh Muppala on Coursera -- test files for week 3
/* test/fixtures/fixtures_dishes.js */
module.exports = [
{
"_id" : "000000000000000000001100",
"updatedAt" : "2016-03-21T23:14:53.548Z",
"createdAt" : "2016-03-21T23:14:53.548Z",
"name" : "Uthapizza",
"image" : "images/pizza.png",
"category" : "main",
"price" : 1050,
"description" : "Red and Hot",
"comments" : [ ],
"label" : "",
"__v" : 0
},
{
"_id" : "000000000000000000001200",
"updatedAt" : "2016-03-21T23:15:58.548Z",
"createdAt" : "2016-03-21T23:15:58.548Z",
"name" : "Fish and Chips",
"image" : "images/fish.png",
"category" : "main",
"price" : 1799,
"description" : "Silver fish with golden chips",
"comments" : [ ],
"label" : "",
"__v" : 0,
"comments" : [
{
"updatedAt" : "2016-03-22T16:20:57.564Z",
"createdAt" : "2016-03-22T14:45:13.111Z",
"author" : "Sonia",
"comment" : "Makes you LOVE fish",
"rating" : 5,
"_id" : "000000000000000000001201"
},
]
},
{
"_id" : "000000000000000000001300",
"updatedAt" : "2016-03-22T16:20:57.567Z",
"createdAt" : "2016-03-22T14:03:37.678Z",
"name" : "French fries",
"description" : "Delicious potatoes",
"category" : "main",
"price" : 1999,
"image" : "images/ffreis.png",
"comments" : [
{
"updatedAt" : "2016-03-22T16:20:57.562Z",
"createdAt" : "2016-03-22T14:44:21.955Z",
"author" : "Sylvain",
"comment" : "Just like at home !",
"rating" : 5,
"_id" : "000000000000000000001301"
},
{
"updatedAt" : "2016-03-22T16:20:57.564Z",
"createdAt" : "2016-03-22T14:45:13.111Z",
"author" : "Sonia",
"comment" : "Good but fat",
"rating" : 4,
"_id" : "000000000000000000001302"
},
{
"updatedAt" : "2016-03-22T16:20:57.564Z",
"createdAt" : "2016-03-22T14:50:34.037Z",
"author" : "Jogesh",
"comment" : "Yummy",
"rating" : 5,
"_id" : "000000000000000000001303"
},
],
"label" : "",
"__v" : 12
}
];
/* test/fixtures/fixtures_leadership.js */
module.exports = [
];
/* test/fixtures/fixtures_promotions.js */
module.exports = [
];
/* test/test_dish.js*/
var request = require('supertest');
var assert = require('assert');
var mongoose = require('mongoose');
var app = require('../app');
var Dishes = require('../models/dishes');
var STRICT_REST = true; // change that to false depending on https://www.coursera.org/learn/server-side-development/lecture/bKtMl/exercise-video-rest-api-with-express-mongodb-and-mongoose/discussions/x1AZIu9SEeWB0QpuSDkq-Q
var HTTP_OK = 200;
var HTTP_CREATED = (STRICT_REST) ? 201 : HTTP_OK;
var HTTP_NOT_FOUND = 404;
/*
* Data
*/
var dishes_fixture = require('./fixtures/fixtures_dishes');
var new_dish = {
name: "Apple Pie",
description: "America's favorite",
category: "main",
price: "$8.55",
image:"images/pie.png",
};
var new_comment = {
author: "Bob",
rating: 3,
comment: "Greeeaaaaat dish",
};
/*
* Tests
*/
describe('Dishes', function(){
beforeEach(function(done){
Dishes.remove({}, function(err, res) { // don't use drop() as this will occasionnnaly raise a background operation error
Dishes.insertMany(dishes_fixture, done);
});
});
describe('GET /dish', function(){
it('respond with code 404 (wrong spelling)', function(done){
request(app)
.get('/dish')
.expect(HTTP_NOT_FOUND, done);
});
});
describe('GET /dishes', function(){
it('respond with code HTTP_OK + list of dishes', function(done){
request(app)
.get('/dishes')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, dishes_fixture);
})
.end(done);
});
});
describe('DELETE /dishes', function(){
it('responds with code HTTP_OK', function(done){
request(app)
.delete('/dishes')
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, { ok: 1, n: 3 });
})
.end(done);
});
});
describe('POST /dishes', function(){
it('HTTP_CREATED + data content', function(done){
request(app)
.post('/dishes')
.set('Accept', 'application/json')
.send(new_dish)
.expect(HTTP_CREATED)
.expect(function(res) {
assert.ok(res.body._id);
assert.equal(res.body.name, new_dish.name);
assert.deepEqual(res.body.comments, []);
})
.end(done);
});
});
describe('GET /dishes/000000000000000000001200', function(){
it('respond with code HTTP_OK + data content', function(done){
request(app)
.get('/dishes/000000000000000000001200')
.set('Accept', 'application/json')
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, dishes_fixture[1]);
})
.end(done);
});
});
describe('PUT /dishes/000000000000000000001200', function(){
it('respond with code HTTP_OK + data content', function(done){
request(app)
.put('/dishes/000000000000000000001200')
.set('Accept', 'application/json')
.send(new_dish)
.expect(HTTP_OK)
.expect(function(res) {
assert.equal(res.body._id, "000000000000000000001200");
assert.equal(res.body.name, new_dish.name);
assert.deepEqual(res.body.comments, dishes_fixture[1].comments); // Keep old comments : this is *not* a REPLACE operation !
})
.end(done);
});
});
describe('DELETE /dishes/000000000000000000001200', function(){
it('respond with code HTTP_OK + data content', function(done){
request(app)
.delete('/dishes/000000000000000000001200')
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, dishes_fixture[1]);
})
.end(done);
});
});
describe('GET /dishes/000000000000000000001300/comments', function(){
it('respond with code HTTP_OK + data content', function(done){
request(app)
.get('/dishes/000000000000000000001300/comments')
.set('Accept', 'application/json')
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, dishes_fixture[2].comments);
})
.end(done);
});
});
describe('POST /dishes/000000000000000000001300/comments', function(){
it('HTTP_CREATED + data content', function(done){
request(app)
.post('/dishes/000000000000000000001300/comments')
.set('Accept', 'application/json')
.send(new_comment)
.expect(HTTP_CREATED)
.expect(function(res) {
assert.ok(res.body.comments[3]._id);
assert.equal(res.body.comments[3].author, new_comment.author);
assert.equal(res.body.comments[3].rating, new_comment.rating);
assert.equal(res.body.comments[3].comment, new_comment.comment);
})
.end(done);
});
});
describe('DELETE /dishes/000000000000000000001300/comments', function(){
it('result code HTTP_OK + all comment cleared', function(done){
request(app)
.delete('/dishes/000000000000000000001300/comments')
.set('Accept', 'application/json')
.send(new_comment)
.expect(HTTP_OK)
.end(function() {
request(app)
.get('/dishes/000000000000000000001300/comments')
.set('Accept', 'application/json')
.expect(HTTP_OK)
.expect(function(res) {
assert.deepEqual(res.body, []);
})
.end(done);
});
});
});
});
/* test/test_leader.js*/
var request = require('supertest');
var assert = require('assert');
var mongoose = require('mongoose');
var app = require('../app');
var Leaders = require('../models/leadership');
var STRICT_REST = true; // change that to false depending on https://www.coursera.org/learn/server-side-development/lecture/bKtMl/exercise-video-rest-api-with-express-mongodb-and-mongoose/discussions/x1AZIu9SEeWB0QpuSDkq-Q
var HTTP_OK = 200;
var HTTP_CREATED = (STRICT_REST) ? 201 : HTTP_OK;
var HTTP_NOT_FOUND = 404;
/*
* Data
*/
var leaders_fixture = require('./fixtures/fixtures_leadership');
var new_leader = {
};
/*
* Tests
*/
/* test/test_promo.js*/
var request = require('supertest');
var assert = require('assert');
var mongoose = require('mongoose');
var app = require('../app');
var Promotions = require('../models/promotions');
var STRICT_REST = true; // change that to false depending on https://www.coursera.org/learn/server-side-development/lecture/bKtMl/exercise-video-rest-api-with-express-mongodb-and-mongoose/discussions/x1AZIu9SEeWB0QpuSDkq-Q
var HTTP_OK = 200;
var HTTP_CREATED = (STRICT_REST) ? 201 : HTTP_OK;
var HTTP_NOT_FOUND = 404;
/*
* Data
*/
var promotions_fixture = require('./fixtures/fixtures_promotions');
var new_promo = {
};
/*
* Tests
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment