|
1 | 1 | // This test written in mocha+should.js
|
2 | 2 | var should = require('./init.js');
|
3 | 3 | var async = require('async');
|
| 4 | +var assert = require('assert'); |
| 5 | + |
| 6 | +var DataSource = require('../').DataSource; |
4 | 7 |
|
5 | 8 | var db, User, Profile, AccessToken, Post, Passport, City, Street, Building, Assembly, Part;
|
6 | 9 |
|
@@ -690,3 +693,95 @@ function clearAndCreate(model, data, callback) {
|
690 | 693 | itemIndex++;
|
691 | 694 | }
|
692 | 695 | }
|
| 696 | + |
| 697 | +describe('Model instance with included relation .toJSON()', function() { |
| 698 | + var db, ChallengerModel, GameParticipationModel, ResultModel; |
| 699 | + |
| 700 | + before(function(done) { |
| 701 | + db = new DataSource({connector: 'memory'}); |
| 702 | + ChallengerModel = db.createModel('Challenger', |
| 703 | + { |
| 704 | + name: String |
| 705 | + }, |
| 706 | + { |
| 707 | + relations: { |
| 708 | + gameParticipations: { |
| 709 | + type: 'hasMany', |
| 710 | + model: 'GameParticipation', |
| 711 | + foreignKey: '' |
| 712 | + } |
| 713 | + } |
| 714 | + } |
| 715 | + ); |
| 716 | + GameParticipationModel = db.createModel('GameParticipation', |
| 717 | + { |
| 718 | + date: Date |
| 719 | + }, |
| 720 | + { |
| 721 | + relations: { |
| 722 | + challenger: { |
| 723 | + type: 'belongsTo', |
| 724 | + model: 'Challenger', |
| 725 | + foreignKey: '' |
| 726 | + }, |
| 727 | + results: { |
| 728 | + type: 'hasMany', |
| 729 | + model: 'Result', |
| 730 | + foreignKey: '' |
| 731 | + } |
| 732 | + } |
| 733 | + } |
| 734 | + ); |
| 735 | + ResultModel = db.createModel('Result', { |
| 736 | + points: Number, |
| 737 | + }, { |
| 738 | + relations: { |
| 739 | + gameParticipation: { |
| 740 | + type: 'belongsTo', |
| 741 | + model: 'GameParticipation', |
| 742 | + foreignKey: '' |
| 743 | + } |
| 744 | + } |
| 745 | + }); |
| 746 | + |
| 747 | + async.waterfall([ |
| 748 | + createChallengers, |
| 749 | + createGameParticipations, |
| 750 | + createResults], |
| 751 | + function(err) { |
| 752 | + done(err); |
| 753 | + }); |
| 754 | + |
| 755 | + }); |
| 756 | + |
| 757 | + function createChallengers(callback) { |
| 758 | + ChallengerModel.create([{name: 'challenger1'}, {name: 'challenger2'}], callback); |
| 759 | + } |
| 760 | + |
| 761 | + function createGameParticipations(challengers, callback) { |
| 762 | + GameParticipationModel.create([ |
| 763 | + {challengerId: challengers[0].id, date: Date.now()}, |
| 764 | + {challengerId: challengers[0].id, date: Date.now()} |
| 765 | + ], callback); |
| 766 | + } |
| 767 | + |
| 768 | + function createResults(gameParticipations, callback) { |
| 769 | + ResultModel.create([ |
| 770 | + {gameParticipationId: gameParticipations[0].id, points: 10}, |
| 771 | + {gameParticipationId: gameParticipations[0].id, points: 20} |
| 772 | + ], callback); |
| 773 | + } |
| 774 | + |
| 775 | + it('should recursively serialize objects', function(done) { |
| 776 | + var filter = {include: {gameParticipations: 'results'}}; |
| 777 | + ChallengerModel.find(filter, function(err, challengers) { |
| 778 | + |
| 779 | + var levelOneInclusion = challengers[0].toJSON().gameParticipations[0]; |
| 780 | + assert(levelOneInclusion.__data === undefined, '.__data of a level 1 inclusion is undefined.'); |
| 781 | + |
| 782 | + var levelTwoInclusion = challengers[0].toJSON().gameParticipations[0].results[0]; |
| 783 | + assert(levelTwoInclusion.__data === undefined, '__data of a level 2 inclusion is undefined.'); |
| 784 | + done(); |
| 785 | + }); |
| 786 | + }); |
| 787 | +}); |
0 commit comments