Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for arrays of embedded documents
- Loading branch information
Karl Seguin
committed
Jul 31, 2011
1 parent
15ec243
commit dde4976
Showing
9 changed files
with
138 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'spec_helper' | ||
|
||
describe 'embedded array behavior' do | ||
it "saves a single embedded document" do | ||
child = Child.new({:name => 'child', :power => 88}) | ||
parent = BusyParent.new({:name => 'my name', :children => [child]}) | ||
parent.save | ||
found = BusyParent.find_one({:name => 'my name'}) | ||
found.children.length.should == 1 | ||
found.children[0].name.should == 'child' | ||
found.children[0].power.should == 88 | ||
end | ||
it "saves a muliple embedded document" do | ||
child1 = Child.new({:name => 'child1', :power => 88}) | ||
child2 = Child.new({:name => 'child2', :power => 89}) | ||
parent = BusyParent.new({:name => 'my name', :children => [child1, child2]}) | ||
parent.save | ||
found = BusyParent.find_one({:name => 'my name'}) | ||
found.children.length.should == 2 | ||
found.children[0].name.should == 'child1' | ||
found.children[0].power.should == 88 | ||
found.children[1].name.should == 'child2' | ||
found.children[1].power.should == 89 | ||
end | ||
end |
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,25 @@ | ||
require 'spec_helper' | ||
|
||
describe 'embedded behavior' do | ||
it "saves the embedded document" do | ||
child = Child.new({:name => 'child', :power => 88}) | ||
parent = Parent.new({:name => 'my name', :child => child}) | ||
parent.save | ||
found = Parent.find_one({:name => 'my name'}) | ||
found.child.name.should == 'child' | ||
found.child.power.should == 88 | ||
end | ||
it "returns the raw document with embedded objects" do | ||
child = Child.new({:name => 'child', :power => 88}) | ||
parent = Parent.new({:name => 'my name', :child => child}) | ||
parent.save | ||
found = Parent.find_one({:name => 'my name'}, {:raw => true}) | ||
found.should == {:_id => parent.id, :name => 'my name', :child => {:name => 'child', :power => 88}} | ||
end | ||
it "finds a document by it's child" do | ||
child = Child.new({:name => 'child', :power => 88}) | ||
parent = Parent.new({:name => 'my name', :child => child}) | ||
parent.save | ||
Parent.count({'c.p' => 88}).should == 1 | ||
end | ||
end |
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,25 @@ | ||
require 'spec_helper' | ||
|
||
describe 'remove behavior' do | ||
it "removes the matching documents" do | ||
Factory.create(:simple, {:name => 'paul', :power => 49}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 123}) | ||
Simple.remove({:power => 49}) | ||
Simple.count.should == 1 | ||
Simple.count({:name => 'jessica'}).should == 1 | ||
end | ||
|
||
it "removes all documents" do | ||
Factory.create(:simple, {:name => 'paul', :power => 49}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 123}) | ||
Simple.remove | ||
Simple.count.should == 0 | ||
end | ||
|
||
it "removes no document" do | ||
Factory.create(:simple, {:name => 'paul', :power => 49}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 123}) | ||
Simple.remove({:over9000 => true}) | ||
Simple.count.should == 2 | ||
end | ||
end |
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,7 @@ | ||
require 'child' | ||
class BusyParent | ||
include MongoLight::Document | ||
mongo_accessor({ | ||
:name => :name, | ||
:children => {:field => :c, :class => Child, :array => true}}) | ||
end |
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,6 @@ | ||
class Child | ||
include MongoLight::EmbeddedDocument | ||
mongo_accessor({:name => :n, :power => :p}) | ||
end | ||
|
||
|
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,7 @@ | ||
require 'child' | ||
class Parent | ||
include MongoLight::Document | ||
mongo_accessor({ | ||
:name => :name, | ||
:child => {:field => :c, :class => Child}}) | ||
end |
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,27 @@ | ||
require 'spec_helper' | ||
|
||
describe 'update behavior' do | ||
it "updates the matching documents" do | ||
Factory.create(:simple, {:name => 'paul', :power => 2}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 2}) | ||
Simple.update({:power => 2}, {'$set' => {:power => 4}}, {:multi => true}) | ||
Simple.count.should == 2 | ||
Simple.count({:power => 4}).should == 2 | ||
end | ||
|
||
it "updates a single document" do | ||
Factory.create(:simple, {:name => 'paul', :power => 2}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 2}) | ||
Simple.update({:power => 2}, {:power => 4}) | ||
Simple.count.should == 2 | ||
Simple.count({:power => 4}).should == 1 | ||
end | ||
|
||
it "updates no document" do | ||
Factory.create(:simple, {:name => 'paul', :power => 49}) | ||
Factory.create(:simple, {:name => 'jessica', :power => 123}) | ||
Simple.remove({:over9000 => true, :power => 442}) | ||
Simple.count.should == 2 | ||
Simple.count({:power => 442}).should == 0 | ||
end | ||
end |