|
| 1 | +var should = require('should'); |
| 2 | +var helper = require('../support/spec_helper'); |
| 3 | +var ORM = require('../../'); |
| 4 | + |
| 5 | +describe("hasMany extra properties", function() { |
| 6 | + var db = null; |
| 7 | + var Person = null; |
| 8 | + var Pet = null; |
| 9 | + |
| 10 | + var setup = function (opts) { |
| 11 | + opts = opts || {}; |
| 12 | + return function (done) { |
| 13 | + db.settings.set('instance.cache', false); |
| 14 | + |
| 15 | + Person = db.define('person', { |
| 16 | + name : String, |
| 17 | + }, opts); |
| 18 | + Pet = db.define('pet', { |
| 19 | + name : String |
| 20 | + }); |
| 21 | + Person.hasMany('pets', Pet, { |
| 22 | + since : Date |
| 23 | + }); |
| 24 | + |
| 25 | + return helper.dropSync([ Person, Pet ], done); |
| 26 | + }; |
| 27 | + }; |
| 28 | + |
| 29 | + before(function(done) { |
| 30 | + helper.connect(function (connection) { |
| 31 | + db = connection; |
| 32 | + done(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("if passed to addAccessor", function () { |
| 37 | + before(setup()); |
| 38 | + |
| 39 | + it("should be added to association", function (done) { |
| 40 | + Person.create([{ |
| 41 | + name : "John" |
| 42 | + }], function (err, people) { |
| 43 | + Pet.create([{ |
| 44 | + name : "Deco" |
| 45 | + }, { |
| 46 | + name : "Mutt" |
| 47 | + }], function (err, pets) { |
| 48 | + people[0].addPets(pets, { since : new Date() }, function (err) { |
| 49 | + should.equal(err, null); |
| 50 | + |
| 51 | + Person.find({ name: "John" }, { autoFetch : true }).first(function (err, John) { |
| 52 | + should.equal(err, null); |
| 53 | + |
| 54 | + John.should.have.property("pets"); |
| 55 | + should(Array.isArray(pets)); |
| 56 | + |
| 57 | + John.pets.length.should.equal(2); |
| 58 | + |
| 59 | + John.pets[0].should.have.property("id"); |
| 60 | + John.pets[0].should.have.property("name"); |
| 61 | + John.pets[0].should.have.property("extra"); |
| 62 | + John.pets[0].extra.should.be.a("object"); |
| 63 | + John.pets[0].extra.should.have.property("since"); |
| 64 | + should(John.pets[0].extra.since instanceof Date); |
| 65 | + |
| 66 | + return done(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments