-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
With association autoFetch cache may return incomplete instance. #685
Open
stueynz
wants to merge
11
commits into
dresende:master
Choose a base branch
from
stueynz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5b368d8
Merge pull request #2 from stueynz/hasOne-with-zeroID
2e805b1
When doing autofetch on associations, cached instance may not have al…
stueynz d267788
Check cache returns correct objects, auto-fetch on & off
stueynz fc20525
Merge complete
stueynz c23f1c5
Ensure cache is still working as expected
stueynz 350b4a4
Using the 'autoFetch' option on get() and find() will override the 'a…
stueynz 02824e2
if get() or find() has locally set 'autoFetch' option, then don't use…
stueynz 51307fd
renamed checkAssociations() to hasAutoFetchAssociationsLoaded() as it…
stueynz e38be11
Whoops, we can't assume what the Id values will be when testing get()…
stueynz d753091
When hasMany relationship has extra data, then the UID used by Single…
stueynz 4fe75f8
Merge pull request #4 from stueynz/hasmany-extra-cache
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
var ORM = require('../../'); | ||
var helper = require('../support/spec_helper'); | ||
var should = require('should'); | ||
var async = require('async'); | ||
var _ = require('lodash'); | ||
|
||
describe("hasOne", function() { | ||
var db = null; | ||
var Animal = null; | ||
|
||
var setup = function (cache) { | ||
return function (done) { | ||
db.settings.set('instance.cache', cache); | ||
db.settings.set('instance.returnAllErrors', true); | ||
|
||
Animal = db.define('animal', { | ||
id : {type : "integer", key:true}, | ||
name : {type : "text", size:"255"}, | ||
damId : {type : "integer"}, | ||
sireId : {type : "integer"} | ||
}); | ||
|
||
Animal.hasOne('sire', Animal, {field: 'sireId', autoFetch:true}); | ||
Animal.hasOne('dam', Animal, {field: 'damId', autoFetch:false}); | ||
|
||
helper.dropSync([Animal], function(err) { | ||
if (err) return done(err); | ||
Animal.create([{ id:1, name: 'Bronson', sireId:10, damId:20}, | ||
|
||
{ id:10, name: 'McTavish', sireId:11, damId:12}, | ||
{ id:11, name:'Todd'}, | ||
{ id:12, name:'Jemima'}, | ||
|
||
{ id:20, name: 'Suzy', sireId:21, damId:22}, | ||
{ id:21, name:'Liam'}, | ||
{ id:22, name:'Glencora'} | ||
], done); | ||
}); | ||
}; | ||
}; | ||
|
||
before(function(done) { | ||
helper.connect(function (connection) { | ||
db = connection; | ||
done(); | ||
}); | ||
}); | ||
|
||
[false, 1].forEach(function(cache) { | ||
|
||
describe("recursive hasOne() with " + (cache ? "" : "out ") + "cache", function () { | ||
before(setup(cache)); | ||
|
||
it("should get Bronson's Sire but not Dam", function (done) { | ||
Animal.find({name: "Bronson"}, function(err, animals) { | ||
should.not.exist(err); | ||
|
||
animals[0].name.should.equal("Bronson"); | ||
|
||
// All of Bronson's Sire & some of his Dam stuff should be present | ||
animals[0].sireId.should.equal(10); | ||
animals[0].damId.should.equal(20); | ||
|
||
animals[0].should.have.property("sire"); | ||
animals[0].should.not.have.property("dam"); // no auto-fetch | ||
|
||
animals[0].sire.name.should.equal("McTavish"); | ||
|
||
// Bronson's paternal GrandSire & GrandDam shouldn't be present - autoFetchLimit | ||
animals[0].sire.should.not.have.property("sire"); | ||
animals[0].sire.should.not.have.property("dam"); | ||
|
||
// but Bronson's paternal GrandSire & GrandDam ID's should be known | ||
animals[0].sire.sireId.should.equal(11); | ||
animals[0].sire.damId.should.equal(12); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it("should get McTavish's Sire but not Dam", function (done) { | ||
Animal.find({name: "McTavish"}, function(err, animals) { | ||
should.not.exist(err); | ||
|
||
animals[0].name.should.equal("McTavish"); | ||
|
||
// All of McTavish's Sire & some of his Dam stuff should be present | ||
animals[0].sireId.should.equal(11); | ||
animals[0].damId.should.equal(12); | ||
|
||
animals[0].should.have.property("sire"); // now fixed by stueynz | ||
animals[0].should.not.have.property("dam"); // no auto-fetch | ||
|
||
animals[0].sire.name.should.equal("Todd"); // just to be sure | ||
|
||
// Let's make sure the cache is still working... | ||
Animal.get(animals[0].id, function(err, McTavish) { | ||
should.not.exist(err); | ||
|
||
if(cache) { | ||
McTavish.should.equal(animals[0]); | ||
} else { | ||
McTavish.should.not.equal(animals[0]); | ||
} | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should get Suzy's Sire but not Dam", function (done) { | ||
Animal.find({name: "Suzy"}, function(err, animals) { | ||
should.not.exist(err); | ||
|
||
animals[0].name.should.equal("Suzy"); | ||
|
||
// All of Suzy's Sire & Dam stuff should be present | ||
animals[0].sireId.should.equal(21); | ||
animals[0].damId.should.equal(22); | ||
|
||
animals[0].should.have.property("sire") // now fixed by stueynz | ||
animals[0].should.not.have.property("dam"); // no auto-fetch | ||
|
||
animals[0].sire.name.should.equal("Liam"); // just to be sure | ||
|
||
// Let's make sure the cache is still working... | ||
Animal.get(animals[0].id, function(err, Suzy) { | ||
should.not.exist(err); | ||
|
||
if(cache) { | ||
Suzy.should.equal(animals[0]); | ||
} else { | ||
Suzy.should.not.equal(animals[0]); | ||
} | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please rename this to
hasAutoFetchAssociationsLoaded
?It's a bit long, but more indicative of what it's actually doing.