forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix find_or_(create|initialize)_by on scoped relations.
[ fix #2707 ]
- Loading branch information
Showing
6 changed files
with
289 additions
and
239 deletions.
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
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,191 @@ | ||
require "spec_helper" | ||
|
||
describe Mongoid::Criterion::Modifiable do | ||
|
||
describe ".find_or_create_by" do | ||
|
||
context "when the document is found" do | ||
|
||
context "when providing an attribute" do | ||
|
||
let!(:person) do | ||
Person.create(title: "Senior") | ||
end | ||
|
||
it "returns the document" do | ||
Person.find_or_create_by(title: "Senior").should eq(person) | ||
end | ||
end | ||
|
||
context "when providing a document" do | ||
|
||
context "with an owner with a BSON identity type" do | ||
|
||
let!(:person) do | ||
Person.create | ||
end | ||
|
||
let!(:game) do | ||
Game.create(person: person) | ||
end | ||
|
||
context "when providing the object directly" do | ||
|
||
let(:from_db) do | ||
Game.find_or_create_by(person: person) | ||
end | ||
|
||
it "returns the document" do | ||
from_db.should eq(game) | ||
end | ||
end | ||
|
||
context "when providing the proxy relation" do | ||
|
||
let(:from_db) do | ||
Game.find_or_create_by(person: game.person) | ||
end | ||
|
||
it "returns the document" do | ||
from_db.should eq(game) | ||
end | ||
end | ||
end | ||
|
||
context "with an owner with an Integer identity type" do | ||
|
||
let!(:jar) do | ||
Jar.create | ||
end | ||
|
||
let!(:cookie) do | ||
Cookie.create(jar: jar) | ||
end | ||
|
||
let(:from_db) do | ||
Cookie.find_or_create_by(jar: jar) | ||
end | ||
|
||
it "returns the document" do | ||
from_db.should eq(cookie) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when the document is not found" do | ||
|
||
context "when providing a document" do | ||
|
||
let!(:person) do | ||
Person.create | ||
end | ||
|
||
let!(:game) do | ||
Game.create | ||
end | ||
|
||
let(:from_db) do | ||
Game.find_or_create_by(person: person) | ||
end | ||
|
||
it "returns the new document" do | ||
from_db.person.should eq(person) | ||
end | ||
|
||
it "does not return an existing false document" do | ||
from_db.should_not eq(game) | ||
end | ||
end | ||
|
||
context "when not providing a block" do | ||
|
||
let!(:person) do | ||
Person.find_or_create_by(title: "Senorita") | ||
end | ||
|
||
it "creates a persisted document" do | ||
person.should be_persisted | ||
end | ||
|
||
it "sets the attributes" do | ||
person.title.should eq("Senorita") | ||
end | ||
end | ||
|
||
context "when providing a block" do | ||
|
||
let!(:person) do | ||
Person.find_or_create_by(title: "Senorita") do |person| | ||
person.pets = true | ||
end | ||
end | ||
|
||
it "creates a persisted document" do | ||
person.should be_persisted | ||
end | ||
|
||
it "sets the attributes" do | ||
person.title.should eq("Senorita") | ||
end | ||
|
||
it "calls the block" do | ||
person.pets.should be_true | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe ".find_or_initialize_by" do | ||
|
||
context "when the document is found" do | ||
|
||
let!(:person) do | ||
Person.create(title: "Senior") | ||
end | ||
|
||
it "returns the document" do | ||
Person.find_or_initialize_by(title: "Senior").should eq(person) | ||
end | ||
end | ||
|
||
context "when the document is not found" do | ||
|
||
context "when not providing a block" do | ||
|
||
let!(:person) do | ||
Person.find_or_initialize_by(title: "Senorita") | ||
end | ||
|
||
it "creates a new document" do | ||
person.should be_new_record | ||
end | ||
|
||
it "sets the attributes" do | ||
person.title.should eq("Senorita") | ||
end | ||
end | ||
|
||
context "when providing a block" do | ||
|
||
let!(:person) do | ||
Person.find_or_initialize_by(title: "Senorita") do |person| | ||
person.pets = true | ||
end | ||
end | ||
|
||
it "creates a new document" do | ||
person.should be_new_record | ||
end | ||
|
||
it "sets the attributes" do | ||
person.title.should eq("Senorita") | ||
end | ||
|
||
it "calls the block" do | ||
person.pets.should be_true | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.