Skip to content

Commit

Permalink
Add specs for both ways of eager loading one_to_one associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Mar 18, 2010
1 parent dd17cb1 commit 94b63a9
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/model/eager_loading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ def fetch_rows(sql)
MODEL_DB.sqls.length.should == 2
end

it "should eagerly load a single one_to_one association" do
EagerAlbum.one_to_one :track, :class=>'EagerTrack', :key=>:album_id
a = EagerAlbum.eager(:track).all
a.should == [EagerAlbum.load(:id => 1, :band_id => 2)]
MODEL_DB.sqls.should == ['SELECT * FROM albums', 'SELECT * FROM tracks WHERE (tracks.album_id IN (1))']
a.first.track.should == EagerTrack.load(:id => 3, :album_id=>1)
MODEL_DB.sqls.length.should == 2
end

it "should eagerly load a single one_to_many association" do
a = EagerAlbum.eager(:tracks).all
a.should be_a_kind_of(Array)
Expand Down Expand Up @@ -718,6 +727,18 @@ def ds.fetch_rows(sql, &block)
a.band.should be_a_kind_of(GraphBand)
a.band.values.should == {:id => 2, :vocalist_id=>3}
end

it "should eagerly load a single one_to_one association" do
GraphAlbum.one_to_one :track, :class=>'GraphTrack', :key=>:album_id
ds = GraphAlbum.eager_graph(:track)
ds.sql.should == 'SELECT albums.id, albums.band_id, track.id AS track_id, track.album_id FROM albums LEFT OUTER JOIN tracks AS track ON (track.album_id = albums.id)'
def ds.fetch_rows(sql, &block)
yield({:id=>1, :band_id=>2, :track_id=>3, :album_id=>1})
end
a = ds.all
a.should == [GraphAlbum.load(:id => 1, :band_id => 2)]
a.first.track.should == GraphTrack.load(:id => 3, :album_id=>1)
end

it "should eagerly load a single one_to_many association" do
ds = GraphAlbum.eager_graph(:tracks)
Expand Down

0 comments on commit 94b63a9

Please sign in to comment.