Skip to content

Commit

Permalink
Add more tests for eager_load on has_one relations
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurnn committed Oct 17, 2013
1 parent b35ab2d commit 05d5b29
Showing 1 changed file with 96 additions and 17 deletions.
113 changes: 96 additions & 17 deletions spec/mongoid/relations/eager/has_one_spec.rb
Expand Up @@ -2,29 +2,29 @@

describe Mongoid::Relations::Eager::HasOne do

let(:person) do
Person.create!
end
describe ".grouped_doc" do

let!(:cat) do
Cat.create!(person: person)
end
let(:person) do
Person.create!
end

let(:docs) do
Person.all.to_a
end
let(:docs) do
Person.all.to_a
end

let(:cat_metadata) do
Person.reflect_on_association(:cat)
end
let(:metadata) do
Person.reflect_on_association(:cat)
end

let(:eager) do
described_class.new(Person, [cat_metadata], docs).tap do |b|
b.shift_relation
let(:eager) do
described_class.new(Person, [metadata], docs).tap do |b|
b.shift_relation
end
end
end

describe ".grouped_doc" do
before do
Cat.create!(person: person)
end

it "aggregates by the relation primary key" do
expect(eager.grouped_docs.keys).to eq([person.username])
Expand All @@ -33,6 +33,28 @@

describe ".set_on_parent" do

let(:person) do
Person.create!
end

let(:docs) do
Person.all.to_a
end

let(:metadata) do
Person.reflect_on_association(:cat)
end

let(:eager) do
described_class.new(Person, [metadata], docs).tap do |b|
b.shift_relation
end
end

before do
Cat.create!(person: person)
end

it "sets the relation into the parent" do
docs.each do |doc|
expect(doc).to receive(:set_relation).with(:cat, :foo)
Expand All @@ -43,6 +65,10 @@

describe ".includes" do

let(:person) do
Person.create!
end

before do
3.times { Cat.create!(person: person) }
Cat.create!(person: Person.create!)
Expand Down Expand Up @@ -73,5 +99,58 @@
end
end
end

context "when the relation is not polymorphic" do

let!(:game) do
person.create_game(name: "Tron")
end

let!(:eager) do
Person.all.includes(:game).first
end

it "puts the documents in the parent document" do
expect(eager.ivar(:game)).to eq(game)
end

it "does not query when touching the association" do
expect_query(0) do
expect(eager.game).to eq(game)
end
end

it "does not query when updating the association" do
expect_query(0) do
eager.game.name = "Revenge of Racing of Magic"
expect(eager.game.name).to eq("Revenge of Racing of Magic")
end
end
end

context "when the relation is polymorphic" do

let!(:book) do
Book.create(name: "Game of Thrones")
end

let!(:rating) do
book.create_rating(value: 10)
end

let!(:eager) do
Book.all.includes(:rating).first
end

it "puts the found documents in the parent document" do
expect(eager.ivar(:rating)).to eq(rating)
end

it "does not query when touching the association" do
expect_query(0) do
expect(eager.rating).to eq(rating)
end
end
end
end
end

0 comments on commit 05d5b29

Please sign in to comment.