|
| 1 | +var ORM = require('../../'); |
| 2 | +var helper = require('../support/spec_helper'); |
| 3 | +var should = require('should'); |
| 4 | +var async = require('async'); |
| 5 | +var _ = require('lodash'); |
| 6 | + |
| 7 | +describe("hasOne", function() { |
| 8 | + var db = null; |
| 9 | + var Person = null; |
| 10 | + var Pet = null; |
| 11 | + |
| 12 | + var setup = function (autoFetch) { |
| 13 | + return function (done) { |
| 14 | + db.settings.set('instance.cache', false); |
| 15 | + db.settings.set('instance.returnAllErrors', true); |
| 16 | + |
| 17 | + Person = db.define('person', { |
| 18 | + id : {type : "integer", key:true}, |
| 19 | + firstName : {type : "text", size:"255"}, |
| 20 | + lastName : {type : "text", size:"255"} |
| 21 | + }); |
| 22 | + |
| 23 | + Pet = db.define('pet', { |
| 24 | + id : {type : "integer", key:true}, |
| 25 | + petName : {type : "text", size:"255"}, |
| 26 | + ownerID : {type : "integer", size:"4"} |
| 27 | + }); |
| 28 | + |
| 29 | + Pet.hasOne('owner', Person, {field: 'ownerID', autoFetch:autoFetch}); |
| 30 | + |
| 31 | + helper.dropSync([Person, Pet], function(err) { |
| 32 | + if (err) return done(err); |
| 33 | + Pet.create([{ |
| 34 | + id: 10, |
| 35 | + petName: 'Muttley', |
| 36 | + owner: { |
| 37 | + id: 12, |
| 38 | + firstName: 'Stuey', |
| 39 | + lastName: 'McG' |
| 40 | + } |
| 41 | + }, |
| 42 | + { |
| 43 | + id: 11, |
| 44 | + petName: 'Snagglepuss', |
| 45 | + owner: { |
| 46 | + id: 0, |
| 47 | + firstName: 'John', |
| 48 | + lastName: 'Doe' |
| 49 | + } |
| 50 | + }], done); |
| 51 | + }); |
| 52 | + }; |
| 53 | + }; |
| 54 | + |
| 55 | + before(function(done) { |
| 56 | + helper.connect(function (connection) { |
| 57 | + db = connection; |
| 58 | + done(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("auto fetch", function () { |
| 63 | + before(setup(true)); |
| 64 | + |
| 65 | + it("should work for non-zero ownerID ", function (done) { |
| 66 | + Pet.find({petName: "Muttley"}, function(err, pets) { |
| 67 | + should.not.exist(err); |
| 68 | + |
| 69 | + pets[0].petName.should.equal("Muttley"); |
| 70 | + pets[0].should.have.property("id"); |
| 71 | + pets[0].id.should.equal(10); |
| 72 | + pets[0].ownerID.should.equal(12); |
| 73 | + |
| 74 | + pets[0].should.have.property("owner"); |
| 75 | + pets[0].owner.firstName.should.equal("Stuey"); |
| 76 | + |
| 77 | + return done(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should work for zero ownerID ", function (done) { |
| 82 | + Pet.find({petName: "Snagglepuss"}, function(err, pets) { |
| 83 | + should.not.exist(err); |
| 84 | + |
| 85 | + pets[0].petName.should.equal("Snagglepuss"); |
| 86 | + pets[0].should.have.property("id"); |
| 87 | + pets[0].id.should.equal(11); |
| 88 | + pets[0].ownerID.should.equal(0); |
| 89 | + |
| 90 | + pets[0].should.have.property("owner"); |
| 91 | + pets[0].owner.firstName.should.equal("John"); |
| 92 | + |
| 93 | + return done(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("no auto fetch", function () { |
| 99 | + before(setup(false)); |
| 100 | + |
| 101 | + it("should work for non-zero ownerID ", function (done) { |
| 102 | + Pet.find({petName: "Muttley"}, function(err, pets) { |
| 103 | + should.not.exist(err); |
| 104 | + |
| 105 | + pets[0].petName.should.equal("Muttley"); |
| 106 | + pets[0].should.have.property("id"); |
| 107 | + pets[0].id.should.equal(10); |
| 108 | + pets[0].ownerID.should.equal(12); |
| 109 | + |
| 110 | + pets[0].should.not.have.property("owner"); |
| 111 | + |
| 112 | + // But we should be able to see if its there |
| 113 | + pets[0].hasOwner(function(err, result) { |
| 114 | + should.equal(result, true); |
| 115 | + }); |
| 116 | + |
| 117 | + // ...and then get it |
| 118 | + pets[0].getOwner(function(err, result) { |
| 119 | + result.firstName.should.equal("Stuey"); |
| 120 | + }); |
| 121 | + |
| 122 | + return done(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should work for zero ownerID ", function (done) { |
| 127 | + Pet.find({petName: "Snagglepuss"}, function(err, pets) { |
| 128 | + should.not.exist(err); |
| 129 | + |
| 130 | + pets[0].petName.should.equal("Snagglepuss"); |
| 131 | + pets[0].should.have.property("id"); |
| 132 | + pets[0].id.should.equal(11); |
| 133 | + pets[0].ownerID.should.equal(0); |
| 134 | + |
| 135 | + pets[0].should.not.have.property("owner"); |
| 136 | + |
| 137 | + // But we should be able to see if its there |
| 138 | + pets[0].hasOwner(function(err, result) { |
| 139 | + should.equal(result, true); |
| 140 | + }); |
| 141 | + |
| 142 | + // ...and then get it |
| 143 | + pets[0].getOwner(function(err, result) { |
| 144 | + result.firstName.should.equal("John"); |
| 145 | + }); |
| 146 | + |
| 147 | + return done(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments