Skip to content

Commit

Permalink
Fix reverse has one association findBy*.
Browse files Browse the repository at this point in the history
Closes #450
  • Loading branch information
dxg committed Mar 13, 2014
1 parent 465f205 commit ae7a562
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Associations/One.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ exports.prepare = function (Model, associations, association_properties, model_f
}

options.__merge = {
from : { table: association.model.table, field: association.reversed ? Object.keys(association.field) : association.model.id },
to : { table: Model.table, field: Object.keys(association.field) },
from : { table: association.model.table, field: (association.reversed ? Object.keys(association.field) : association.model.id) },
to : { table: Model.table, field: (association.reversed ? association.model.id : Object.keys(association.field) ) },
where : [ association.model.table, conditions ],
table : Model.table
};
Expand Down
49 changes: 48 additions & 1 deletion test/integration/association-hasone-reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,55 @@ describe("hasOne", function () {
});
});
});
});

// broken in mongo
if (common.protocol() != "mongodb") {
describe("findBy()", function () {
before(setup());

before(function (done) {
Person.one({ name: "Jane Doe" }, function (err, jane) {
Pet.one({ name: "Deco" }, function (err, deco) {
deco.setOwner(jane, function (err) {
should.not.exist(err);
done();
});
});
});
});

it("should throw if no conditions passed", function (done) {
(function () {
Pet.findByOwner(function () {});
}).should.throw();

return done();
});

it("should lookup reverse Model based on associated model properties", function (done) {
Pet.findByOwner({
name: "Jane Doe"
}, function (err, pets) {
should.not.exist(err);
should.equal(Array.isArray(pets), true);
should.equal(pets.length, 1);
should.equal(pets[0].name, 'Deco');

return done();
});
});

it("should return a ChainFind if no callback passed", function (done) {
var ChainFind = Pet.findByOwner({
name: "John Doe"
});
ChainFind.run.should.be.a("function");

return done();
});
});
}
});

describe("reverse find", function () {
it("should be able to find given an association id", function (done) {
Expand Down

0 comments on commit ae7a562

Please sign in to comment.