Refine the test cases for relation REST APIs

This commit is contained in:
Raymond Feng 2014-07-03 11:18:34 -07:00
parent 0525f2c896
commit f649c92bcf
1 changed files with 158 additions and 86 deletions

View File

@ -6,6 +6,7 @@ var app = require(path.join(SIMPLE_APP, 'app.js'));
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('chai').expect;
var debug = require('debug')('loopback:test:relations.integration'); var debug = require('debug')('loopback:test:relations.integration');
var async = require('async');
describe('relations - integration', function () { describe('relations - integration', function () {
@ -124,41 +125,68 @@ describe('relations - integration', function () {
describe('hasMany through', function() { describe('hasMany through', function() {
describe('PUT /physicians/:id/patients/:fk', function () { function setup(connecting, cb) {
var root = {};
before(function (done) { async.series([
app.models.physician.destroyAll(function (err) { // Clean up models
app.models.patient.destroyAll(function (err) { function (done) {
app.models.appointment.destroyAll(function (err) { app.models.physician.destroyAll(function (err) {
done(); app.models.patient.destroyAll(function (err) {
app.models.appointment.destroyAll(function (err) {
done();
});
}); });
}); });
}); },
// Create a physician
function (done) {
app.models.physician.create({
name: 'ph1'
}, function (err, physician) {
root.physician = physician;
done();
});
},
// Create a patient
connecting ? function (done) {
root.physician.patients.create({
name: 'pa1'
}, function (err, patient) {
root.patient = patient;
root.relUrl = '/api/physicians/' + root.physician.id
+ '/patients/rel/' + root.patient.id;
done();
});
} : function (done) {
app.models.patient.create({
name: 'pa1'
}, function (err, patient) {
root.patient = patient;
root.relUrl = '/api/physicians/' + root.physician.id
+ '/patients/rel/' + root.patient.id;
done();
});
}], function (err, done) {
cb(err, root);
}); });
}
describe('PUT /physicians/:id/patients/rel/:fk', function () {
before(function (done) { before(function (done) {
var self = this; var self = this;
app.models.physician.create({ setup(false, function (err, root) {
name: 'ph1' self.url = root.relUrl;
}, function (err, physician) { self.patient = root.patient;
self.physician = physician; self.physician = root.physician;
done(); done();
}); });
}); });
before(function (done) { lt.describe.whenCalledRemotely('PUT', '/api/physicians/:id/patients/rel/:fk', function () {
var self = this;
app.models.patient.create({
name: 'pa1'
}, function (err, patient) {
self.patient = patient;
self.url = '/api/physicians/' + self.physician.id
+ '/patients/' + self.patient.id;
done();
});
});
lt.describe.whenCalledRemotely('PUT', '/api/physicians/:id/patients/:fk', function () {
it('should succeed with statusCode 200', function () { it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200); assert.equal(this.res.statusCode, 200);
assert.equal(this.res.body.patientId, this.patient.id); assert.equal(this.res.body.patientId, this.patient.id);
@ -183,85 +211,129 @@ describe('relations - integration', function () {
}); });
}); });
}); });
});
describe('DELETE /physicians/:id/patients/:fk', function () { describe('DELETE /physicians/:id/patients/rel/:fk', function () {
before(function (done) { before(function (done) {
app.models.physician.destroyAll(function (err) { var self = this;
app.models.patient.destroyAll(function (err) { setup(true, function (err, root) {
app.models.appointment.destroyAll(function (err) { self.url = root.relUrl;
done(); self.patient = root.patient;
}); self.physician = root.physician;
}); done();
}); });
});
it('should create a record in appointment', function (done) {
var self = this;
app.models.appointment.find(function (err, apps) {
assert.equal(apps.length, 1);
assert.equal(apps[0].patientId, self.patient.id);
done();
});
});
it('should connect physician to patient', function (done) {
var self = this;
self.physician.patients(function (err, patients) {
assert.equal(patients.length, 1);
assert.equal(patients[0].id, self.patient.id);
done();
});
});
lt.describe.whenCalledRemotely('DELETE', '/api/physicians/:id/patients/rel/:fk', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
}); });
before(function (done) { it('should remove the record in appointment', function (done) {
var self = this;
app.models.physician.create({
name: 'ph1'
}, function (err, physician) {
self.physician = physician;
done();
});
});
before(function (done) {
var self = this;
self.physician.patients.create({
name: 'pa1'
}, function (err, patient) {
self.patient = patient;
self.url = '/api/physicians/' + self.physician.id
+ '/patients/' + self.patient.id;
done();
});
});
it('should create a record in appointment', function (done) {
var self = this; var self = this;
app.models.appointment.find(function (err, apps) { app.models.appointment.find(function (err, apps) {
assert.equal(apps.length, 1); assert.equal(apps.length, 0);
assert.equal(apps[0].patientId, self.patient.id);
done(); done();
}); });
}); });
it('should connect physician to patient', function (done) { it('should remove the connection between physician and patient', function (done) {
var self = this; var self = this;
self.physician.patients(function (err, patients) { // Need to refresh the cache
assert.equal(patients.length, 1); self.physician.patients(true, function (err, patients) {
assert.equal(patients[0].id, self.patient.id); assert.equal(patients.length, 0);
done(); done();
}); });
}); });
lt.describe.whenCalledRemotely('DELETE', '/api/physicians/:id/patients/remove', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
});
it('should remove the record in appointment', function (done) {
var self = this;
app.models.appointment.find(function (err, apps) {
assert.equal(apps.length, 0);
done();
});
});
it('should remove the connection between physician and patient', function (done) {
var self = this;
// Need to refresh the cache
self.physician.patients(true, function (err, patients) {
assert.equal(patients.length, 0);
done();
});
});
});
}); });
}); });
});
describe('GET /physicians/:id/patients/:fk', function () {
before(function (done) {
var self = this;
setup(true, function (err, root) {
self.url = '/api/physicians/' + root.physician.id
+ '/patients/' + root.patient.id;
self.patient = root.patient;
self.physician = root.physician;
done();
});
});
lt.describe.whenCalledRemotely('GET', '/api/physicians/:id/patients/:fk', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
assert.equal(this.res.body.id, this.physician.id);
});
});
});
describe('DELETE /physicians/:id/patients/:fk', function () {
before(function (done) {
var self = this;
setup(true, function (err, root) {
self.url = '/api/physicians/' + root.physician.id
+ '/patients/' + root.patient.id;
self.patient = root.patient;
self.physician = root.physician;
done();
});
});
lt.describe.whenCalledRemotely('DELETE', '/api/physicians/:id/patients/:fk', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
});
it('should remove the record in appointment', function (done) {
var self = this;
app.models.appointment.find(function (err, apps) {
assert.equal(apps.length, 0);
done();
});
});
it('should remove the connection between physician and patient', function (done) {
var self = this;
// Need to refresh the cache
self.physician.patients(true, function (err, patients) {
assert.equal(patients.length, 0);
done();
});
});
it('should remove the record in patient', function (done) {
var self = this;
app.models.patient.find(function (err, patients) {
assert.equal(patients.length, 0);
done();
});
});
});
});
});
describe('hasAndBelongsToMany', function() { describe('hasAndBelongsToMany', function() {
beforeEach(function defineProductAndCategoryModels() { beforeEach(function defineProductAndCategoryModels() {