|
6 | 6 | // This test written in mocha+should.js |
7 | 7 | 'use strict'; |
8 | 8 | var should = require('./init.js'); |
| 9 | +var assert = require('assert'); |
9 | 10 | var jdb = require('../'); |
10 | 11 | var DataSource = jdb.DataSource; |
11 | 12 | var createPromiseCallback = require('../lib/utils.js').createPromiseCallback; |
@@ -562,7 +563,7 @@ describe('relations', function() { |
562 | 563 | before(function(done) { |
563 | 564 | // db = getSchema(); |
564 | 565 | Physician = db.define('Physician', {name: String}); |
565 | | - Patient = db.define('Patient', {name: String}); |
| 566 | + Patient = db.define('Patient', {name: String, age: Number}); |
566 | 567 | Appointment = db.define('Appointment', {date: {type: Date, |
567 | 568 | default: function() { |
568 | 569 | return new Date(); |
@@ -714,40 +715,142 @@ describe('relations', function() { |
714 | 715 | } |
715 | 716 | }); |
716 | 717 |
|
717 | | - it('should fetch scoped instances with paging filters', function(done) { |
718 | | - Physician.create(function(err, physician) { |
719 | | - physician.patients.create({name: 'a'}, function() { |
720 | | - physician.patients.create({name: 'z'}, function() { |
721 | | - physician.patients.create({name: 'c'}, function() { |
722 | | - verify(physician); |
| 718 | + describe('fetch scoped instances with paging filters', function() { |
| 719 | + var samplePatientId; |
| 720 | + var physician; |
| 721 | + |
| 722 | + beforeEach(createSampleData); |
| 723 | + |
| 724 | + context('with filter skip', function() { |
| 725 | + it('skips the first patient', function(done) { |
| 726 | + physician.patients({skip: 1}, function(err, ch) { |
| 727 | + should.not.exist(err); |
| 728 | + should.exist(ch); |
| 729 | + ch.should.have.lengthOf(2); |
| 730 | + ch[0].name.should.eql('z'); |
| 731 | + ch[1].name.should.eql('c'); |
| 732 | + done(); |
| 733 | + }); |
| 734 | + }); |
| 735 | + }); |
| 736 | + context('with filter order', function() { |
| 737 | + it('orders the result by patient name', function(done) { |
| 738 | + physician.patients({order: 'name DESC'}, function(err, ch) { |
| 739 | + should.not.exist(err); |
| 740 | + should.exist(ch); |
| 741 | + ch.should.have.lengthOf(3); |
| 742 | + ch[0].name.should.eql('z'); |
| 743 | + ch[2].name.should.eql('a'); |
| 744 | + done(); |
| 745 | + }); |
| 746 | + }); |
| 747 | + }); |
| 748 | + context('with filter limit', function() { |
| 749 | + it('limits to 1 result', function(done) { |
| 750 | + physician.patients({limit: 1}, function(err, ch) { |
| 751 | + should.not.exist(err); |
| 752 | + should.exist(ch); |
| 753 | + ch.should.have.lengthOf(1); |
| 754 | + ch[0].name.should.eql('a'); |
| 755 | + done(); |
| 756 | + }); |
| 757 | + }); |
| 758 | + }); |
| 759 | + context('with filter fields', function() { |
| 760 | + it('includes field \'name\' but not \'age\'', function(done) { |
| 761 | + var fieldsFilter = {fields: {name: true, age: false}}; |
| 762 | + physician.patients(fieldsFilter, function(err, ch) { |
| 763 | + should.not.exist(err); |
| 764 | + should.exist(ch); |
| 765 | + should.exist(ch[0].name); |
| 766 | + ch[0].name.should.eql('a'); |
| 767 | + should.not.exist(ch[0].age); |
| 768 | + done(); |
| 769 | + }); |
| 770 | + }); |
| 771 | + }); |
| 772 | + context('with filter include', function() { |
| 773 | + it('returns physicians inluced in patient', function(done) { |
| 774 | + var includeFilter = {include: 'physicians'}; |
| 775 | + physician.patients(includeFilter, function(err, ch) { |
| 776 | + should.not.exist(err); |
| 777 | + ch.should.have.lengthOf(3); |
| 778 | + should.exist(ch[0].physicians); |
| 779 | + done(); |
| 780 | + }); |
| 781 | + }); |
| 782 | + }); |
| 783 | + context('with filter where', function() { |
| 784 | + it('returns patient where id equal to samplePatientId', function(done) { |
| 785 | + var whereFilter = {where: {id: samplePatientId}}; |
| 786 | + physician.patients(whereFilter, function(err, ch) { |
| 787 | + should.not.exist(err); |
| 788 | + should.exist(ch); |
| 789 | + ch.should.have.lengthOf(1); |
| 790 | + ch[0].id.should.eql(samplePatientId); |
| 791 | + done(); |
| 792 | + }); |
| 793 | + }); |
| 794 | + it('returns patients where id in an array', function(done) { |
| 795 | + var idArr = []; |
| 796 | + var whereFilter; |
| 797 | + physician.patients.create({name: 'b'}, function(err, p) { |
| 798 | + idArr.push(samplePatientId, p.id); |
| 799 | + whereFilter = {where: {id: {inq: idArr}}}; |
| 800 | + physician.patients(whereFilter, function(err, ch) { |
| 801 | + should.not.exist(err); |
| 802 | + should.exist(ch); |
| 803 | + ch.should.have.lengthOf(2); |
| 804 | + var resultIdArr = [ch[0].id, ch[1].id]; |
| 805 | + assert.deepEqual(resultIdArr, idArr); |
| 806 | + done(); |
723 | 807 | }); |
724 | 808 | }); |
725 | 809 | }); |
726 | 810 | }); |
727 | | - function verify(physician) { |
728 | | - //limit plus skip |
729 | | - physician.patients({limit: 1, skip: 1}, function(err, ch) { |
730 | | - should.not.exist(err); |
731 | | - should.exist(ch); |
732 | | - ch.should.have.lengthOf(1); |
733 | | - ch[0].name.should.eql('z'); |
734 | | - //offset plus skip |
735 | | - physician.patients({limit: 1, offset: 1}, function(err1, ch1) { |
736 | | - should.not.exist(err1); |
737 | | - should.exist(ch1); |
738 | | - ch1.should.have.lengthOf(1); |
739 | | - ch1[0].name.should.eql('z'); |
740 | | - //order |
741 | | - physician.patients({order: 'patientId DESC'}, function(err2, ch2) { |
742 | | - should.not.exist(err2); |
743 | | - should.exist(ch2); |
744 | | - ch2.should.have.lengthOf(3); |
745 | | - ch2[0].name.should.eql('c'); |
| 811 | + context('findById with filter include', function() { |
| 812 | + it('returns patient where id equal to \'samplePatientId\'' + |
| 813 | + 'with included physicians', function(done) { |
| 814 | + var includeFilter = {include: 'physicians'}; |
| 815 | + physician.patients.findById(samplePatientId, |
| 816 | + includeFilter, function(err, ch) { |
| 817 | + should.not.exist(err); |
| 818 | + should.exist(ch); |
| 819 | + ch.id.should.eql(samplePatientId); |
| 820 | + should.exist(ch.physicians); |
| 821 | + done(); |
| 822 | + }); |
| 823 | + }); |
| 824 | + }); |
| 825 | + context('findById with filter fields', function() { |
| 826 | + it('returns patient where id equal to \'samplePatientId\'' + |
| 827 | + 'with field \'name\' but not \'age\'', function(done) { |
| 828 | + var fieldsFilter = {fields: {name: true, age: false}}; |
| 829 | + physician.patients.findById(samplePatientId, |
| 830 | + fieldsFilter, function(err, ch) { |
| 831 | + should.not.exist(err); |
| 832 | + should.exist(ch); |
| 833 | + should.exist(ch.name); |
| 834 | + ch.name.should.eql('a'); |
| 835 | + should.not.exist(ch.age); |
746 | 836 | done(); |
747 | 837 | }); |
| 838 | + }); |
| 839 | + }); |
| 840 | + |
| 841 | + function createSampleData(done) { |
| 842 | + Physician.create(function(err, result) { |
| 843 | + result.patients.create({name: 'a', age: '10'}, function(err, p) { |
| 844 | + samplePatientId = p.id; |
| 845 | + result.patients.create({name: 'z', age: '20'}, function() { |
| 846 | + result.patients.create({name: 'c'}, function() { |
| 847 | + physician = result; |
| 848 | + done(); |
| 849 | + }); |
| 850 | + }); |
748 | 851 | }); |
749 | 852 | }); |
750 | | - } |
| 853 | + }; |
751 | 854 | }); |
752 | 855 |
|
753 | 856 | it('should find scoped record', function(done) { |
|
0 commit comments