diff --git a/test/fixtures/simple-integration-app/models.json b/test/fixtures/simple-integration-app/models.json index 494367d7..41acde0c 100644 --- a/test/fixtures/simple-integration-app/models.json +++ b/test/fixtures/simple-integration-app/models.json @@ -52,5 +52,60 @@ } } } + }, + "physician": { + "dataSource": "db", + "public": true, + "properties": { + "name": "string" + }, + "options": { + "relations": { + "patients": { + "model": "patient", + "type": "hasMany", + "through": "appointment", + "foreignKey": "patientId" + } + } + } + }, + "patient": { + "dataSource": "db", + "public": true, + "properties": { + "name": "string" + }, + "options": { + "relations": { + "physicians": { + "model": "physician", + "type": "hasMany", + "through": "appointment", + "foreignKey": "physicianId" + } + } + } + }, + "appointment": { + "dataSource": "db", + "public": true, + "properties": { + "date": "date" + }, + "options": { + "relations": { + "physician": { + "model": "physician", + "type": "belongsTo", + "foreignKey": "physicianId" + }, + "patient": { + "model": "patient", + "type": "belongsTo", + "foreignKey": "patientId" + } + } + } } } diff --git a/test/relations.integration.js b/test/relations.integration.js index f4f667c1..e8df6826 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -122,6 +122,147 @@ describe('relations - integration', function () { }); }); + describe('hasMany through', function() { + + describe('/physicians/:id/patients/add', function () { + + before(function (done) { + app.models.physician.destroyAll(function (err) { + app.models.patient.destroyAll(function (err) { + app.models.appointment.destroyAll(function (err) { + done(); + }); + }); + }); + }); + + before(function (done) { + var self = this; + app.models.physician.create({ + name: 'ph1' + }, function (err, physician) { + self.physician = physician; + done(); + }); + }); + + before(function (done) { + var self = this; + app.models.patient.create({ + name: 'pa1' + }, function (err, patient) { + self.patient = patient; + self.url = '/api/physicians/' + self.physician.id + + '/patients/add?foreignKey=' + self.patient.id; + done(); + }); + }); + + lt.describe.whenCalledRemotely('POST', '/api/physicians/:id/patients/add', function () { + it('should succeed with statusCode 200', function () { + assert.equal(this.res.statusCode, 200); + assert.equal(this.res.body.patientId, this.patient.id); + assert.equal(this.res.body.physicianId, this.physician.id); + }); + + 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(); + }); + }); + }); + + describe('/physicians/:id/patients/remove', function () { + + before(function (done) { + app.models.physician.destroyAll(function (err) { + app.models.patient.destroyAll(function (err) { + app.models.appointment.destroyAll(function (err) { + done(); + }); + }); + }); + }); + + before(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/remove?foreignKey=' + self.patient.id; + 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('POST', '/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('hasAndBelongsToMany', function() { beforeEach(function defineProductAndCategoryModels() { var product = app.model(