diff --git a/test/relations.test.js b/test/relations.test.js index 289a80b1d..635eb4eac 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -605,845 +605,862 @@ describe('relations', function() { db.automigrate(['Physician', 'Patient', 'Appointment', 'Address'], done); }); - it('should allow to use fields on related model', function(done) { - Physician.create(function(err, physician) { - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - Address.create({name: 'z'}, function(err, address) { - if (err) return done(err); - patient.address(address); - patient.save(function() { - verify(physician, address.id); + describe('scoped queries', function() { + describe('find', function() { + it('should allow to use fields on related model', function(done) { + Physician.create(function(err, physician) { + physician.patients.create({name: 'a', age: 5}, function(err, patient) { + Address.create({name: 'z'}, function(err, address) { + if (err) return done(err); + patient.address(address); + patient.save(function() { + verify(physician, address.id); + }); + }); }); }); + function verify(physician, addressId) { + physician.patients({fields: 'age'}, function(err, ch) { + if (err) return done(err); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); + should.not.exist(ch[0].name); + done(); + }); + } }); - }); - function verify(physician, addressId) { - physician.patients({fields: 'age'}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - should.not.exist(ch[0].name); - done(); - }); - } - }); - it('should find related model using model id', function(done) { - Physician.create(function(err, physician) { - if (err) return done(err); - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - if (err) return done(err); - physician.patients.create({name: 'b', age: 5}, function(err) { + it('should find related model using model id', function(done) { + Physician.create(function(err, physician) { if (err) return done(err); - physician.patients.create({name: 'c', age: 5}, function(err) { + physician.patients.create({name: 'a', age: 5}, function(err, patient) { if (err) return done(err); - verify(physician, patient.id); + physician.patients.create({name: 'b', age: 5}, function(err) { + if (err) return done(err); + physician.patients.create({name: 'c', age: 5}, function(err) { + if (err) return done(err); + verify(physician, patient.id); + }); + }); }); }); + function verify(physician, patientId) { + physician.patients({where: {id: patientId}}, function(err, ch) { + if (err) return done(err); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); + done(); + }); + } }); - }); - function verify(physician, patientId) { - physician.patients({where: {id: patientId}}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - done(); - }); - } - }); - it('should find related model using model id list', function(done) { - Physician.create(function(err, physician) { - if (err) return done(err); - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - if (err) return done(err); - physician.patients.create({name: 'b', age: 5}, function(err) { + it('should find related model using model id list', function(done) { + Physician.create(function(err, physician) { if (err) return done(err); - physician.patients.create({name: 'c', age: 5}, function(err) { + physician.patients.create({name: 'a', age: 5}, function(err, patient) { if (err) return done(err); - verify(physician, patient.id); + physician.patients.create({name: 'b', age: 5}, function(err) { + if (err) return done(err); + physician.patients.create({name: 'c', age: 5}, function(err) { + if (err) return done(err); + verify(physician, patient.id); + }); + }); }); }); + function verify(physician, patientId) { + physician.patients({where: {id: {inq: [patientId]}}}, function(err, ch) { + if (err) return done(err); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); + done(); + }); + } }); - }); - function verify(physician, patientId) { - physician.patients({where: {id: {inq: [patientId]}}}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - done(); - }); - } - }); - it('should count scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should find scoped record with promises based on related model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.count({ - name: 'a', - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); + function verify(physician) { + return physician.patients({ + where: { + name: 'a', + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); + }); + } }); - } - }); - it('should count scoped record with promises based on related model ' + - 'properties with no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should find scoped record with promises based on related model' + + ' properties with empty results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.count({ - id: 'bar', - }, function(err, count) { - if (err) return done(err); - count.should.equal(0); - done(); + function verify(physician) { + return physician.patients({ + where: { + id: 'bar', + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(0); + done(); + }); + } }); - } - }); - it('should count scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should find scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.count({ - id: id, - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); + function verify(physician) { + return physician.patients({ + where: { + id, + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); + }); + } }); - } - }); - it('should count scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should find scoped record with promises based on related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.count({ - id: { - inq: [id], - }, - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); + function verify(physician) { + return physician.patients({ + where: { + id: { + inq: [id], + }, + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); + }); + } }); - } - }); + }); - it('should find one scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + describe('find explicit', function() { + it('should find (explicit) scoped record with promises based on related' + + ' model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); + function verify(physician) { + return physician.patients.find({ + where: { + name: 'a', + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); + }); + } }); - } - }); - it('should find one scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should find (explicit) scoped record with promises based on related model' + + ' properties with empty results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.findOne({ - where: { - id: 'bar', - }, - }, function(err, patient) { - if (err) return done(err); - should.not.exist(patient); - done(); + function verify(physician) { + return physician.patients.find({ + where: { + id: 'bar', + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(0); + done(); + }); + } }); - } - }); - it('should find one scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - id, - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); - }); - } - }); + it('should find (explicit) scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should find one scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.find({ + where: { + id, + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - id: { - inq: [id], - }, - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); + } }); - } - }); - - it('should find scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - function verify(physician) { - return physician.patients({ - where: { - name: 'a', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } - }); + it('should find (explicit) scoped record with promises based on ' + + 'related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should find scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.find({ + where: { + id: { + inq: [id], + }, + }, + }, function(err, patients) { + if (err) return done(err); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); + done(); }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - id: 'bar', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(0); - done(); + } }); - } - }); - - it('should find scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + }); - function verify(physician) { - return physician.patients({ - where: { - id, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } - }); + describe('count', function() { + it('should count scoped record with promises based on related model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should find scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.count({ + name: 'a', + }, function(err, count) { + if (err) return done(err); + count.should.equal(1); + done(); }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - id: { - inq: [id], - }, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); + } }); - } - }); - - it('should find (explicit) scoped record with promises based on related' + - ' model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - function verify(physician) { - return physician.patients.find({ - where: { - name: 'a', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } - }); + it('should count scoped record with promises based on related model ' + + 'properties with no results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should find (explicit) scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.count({ + id: 'bar', + }, function(err, count) { + if (err) return done(err); + count.should.equal(0); + done(); }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - id: 'bar', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(0); - done(); + } }); - } - }); - - it('should find (explicit) scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - function verify(physician) { - return physician.patients.find({ - where: { - id, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } - }); + it('should count scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should find (explicit) scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.count({ + id: id, + }, function(err, count) { + if (err) return done(err); + count.should.equal(1); + done(); }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - id: { - inq: [id], - }, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); + } }); - } - }); - it('should update all scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.updateAll({ - name: 'a', - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); - }); - } - }); + it('should count scoped record with promises based on related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - it('should update all scoped record with promises based on related ' + - 'model properties no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + function verify(physician) { + return physician.patients.count({ + id: { + inq: [id], + }, + }, function(err, count) { + if (err) return done(err); + count.should.equal(1); + done(); }); - }).catch(done); + } + }); + }); - function verify(physician) { - return physician.patients.updateAll({ - id: 'bar', - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(0); - done(); + describe('find one', function() { + it('should find one scoped record with promises based on related model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.findOne({ + where: { + name: 'a', + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.name.should.equal('a'); + done(); + }); + } }); - } - }); - it('should update all scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + it('should find one scoped record with promises based on related model' + + ' properties with empty results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.findOne({ + where: { + id: 'bar', + }, + }, function(err, patient) { + if (err) return done(err); + should.not.exist(patient); + done(); }); - }).catch(done); + } + }); - function verify(physician) { - return physician.patients.updateAll({ - id, - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); + it('should find one scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.findOne({ + where: { + id, + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.name.should.equal('a'); + done(); + }); + } }); - } - }); - it('should update all scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + it('should find one scoped record with promises based on related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.findOne({ + where: { + id: { + inq: [id], + }, + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.name.should.equal('a'); + done(); }); - }).catch(done); + } + }); + }); - function verify(physician) { - return physician.patients.updateAll({ - id: { - inq: [id], - }, - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { + describe('update all', function() { + it('should update all scoped record with promises based on related model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.updateAll({ name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); + }, {age: 5}, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + physician.patients.findOne({ + where: { + name: 'a', + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.age.should.equal(5); + done(); + }); + }); + } }); - } - }); - it('should destroyAll all scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + it('should update all scoped record with promises based on related ' + + 'model properties no results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); - function verify(physician) { - return physician.patients.destroyAll({ - name: 'a', - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); + function verify(physician) { + return physician.patients.updateAll({ + id: 'bar', + }, {age: 5}, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(0); + done(); + }); + } }); - } - }); - it('should destroyAll all scoped record with promises based on related ' + - 'model properties no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + it('should update all scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.updateAll({ + id, + }, {age: 5}, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + physician.patients.findOne({ + where: { + name: 'a', + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.age.should.equal(5); + done(); + }); }); - }).catch(done); + } + }); - function verify(physician) { - return physician.patients.destroyAll({ - id: 'foo', - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(0); - done(); + it('should update all scoped record with promises based on related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.updateAll({ + id: { + inq: [id], + }, + }, {age: 5}, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + physician.patients.findOne({ + where: { + name: 'a', + }, + }, function(err, patient) { + if (err) return done(err); + should.exist(patient); + patient.age.should.equal(5); + done(); + }); + }); + } }); - } - }); + }); - it('should destroyAll all scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + describe('destroy all', function() { + it('should destroyAll all scoped record with promises based on ' + + 'related model properties', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.destroyAll({ + name: 'a', + }, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + done(); }); - }).catch(done); + } + }); - function verify(physician) { - return physician.patients.destroyAll({ - id, - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); + it('should destroyAll all scoped record with promises based on related ' + + 'model properties no results', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.destroyAll({ + id: 'foo', + }, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(0); + done(); + }); + } }); - } - }); - it('should destroyAll all scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); + it('should destroyAll all scoped record with promises based on related model id', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.destroyAll({ + id, + }, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + done(); }); - }).catch(done); + } + }); - function verify(physician) { - return physician.patients.destroyAll({ - id: { - inq: [id], - }, - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); + it('should destroyAll all scoped record with promises based on ' + + 'related model id list', function(done) { + let id; + Physician.create() + .then(function(physician) { + return physician.patients.create({name: 'a'}) + .then(function(ch) { + id = ch.id; + return physician.patients.create({name: 'z'}); + }) + .then(function() { + return physician.patients.create({name: 'c'}); + }) + .then(function() { + return verify(physician); + }); + }).catch(done); + + function verify(physician) { + return physician.patients.destroyAll({ + id: { + inq: [id], + }, + }, function(err, result) { + if (err) return done(err); + should.exist(result); + result.count.should.equal(1); + done(); + }); + } }); - } + }); }); it('should build record on scope', function(done) {