Skip to content

Instantly share code, notes, and snippets.

@okitan
Created May 16, 2011 12:51
Show Gist options
  • Save okitan/974391 to your computer and use it in GitHub Desktop.
Save okitan/974391 to your computer and use it in GitHub Desktop.
embedded_object_id=false bugs
FFF
Failures:
1) embedde many object making two grandsons failed childs is one
Failure/Error: Parent.where(:foo => 'foo').first.sons.should have(1).items
expected 1 items, got 2
# ./embedded_object_id_spec.rb:55:in `block (3 levels) in <top (required)>'
2) embedde many object making two grandsons failed grandsons is two
Failure/Error: Parent.where(:foo => 'foo').first.sons.first.grandsons.should have(2).items
expected 2 items, got 1
# ./embedded_object_id_spec.rb:59:in `block (3 levels) in <top (required)>'
3) embedde many object making two grandsons failed to_json has grandsons
Failure/Error: Parent.where(:foo => 'foo').first.to_json.should =~ /grandson/
expected: /grandson/
got: "{\"_id\":\"4dd11c0573c0b6be98000001\",\"foo\":\"foo\",\"sons\":[{\"bar\":\"bar\"},{\"bar\":\"bar\"}]}" (using =~)
Diff:
@@ -1,2 +1,2 @@
-/grandson/
+{"_id":"4dd11c0573c0b6be98000001","foo":"foo","sons":[{"bar":"bar"},{"bar":"bar"}]}
# ./embedded_object_id_spec.rb:63:in `block (3 levels) in <top (required)>'
Finished in 0.0237 seconds
3 examples, 3 failures
MONGODB test['parents'].insert([{"foo"=>"foo", "_id"=>BSON::ObjectId('4dd11c0573c0b6be98000001'), "sons"=>[{"bar"=>"bar", "grandsons"=>[{"buzz"=>1}]}]}])
MONGODB test['parents'].find({:foo=>"foo"})
MONGODB test['parents'].update({"_id"=>BSON::ObjectId('4dd11c0573c0b6be98000001')}, {"$pushAll"=>{"sons"=>[{"bar"=>"bar", "grandsons"=>[{"buzz"=>1}, {"buzz"=>2}]}]}})
MONGODB test['parents'].find({:foo=>"foo"})
MONGODB test['parents'].find({:foo=>"foo"})
MONGODB test['parents'].find({:foo=>"foo"})
MONGODB test['$cmd'].find({"count"=>"parents", "query"=>{}, "fields"=>nil})
MONGODB test['parents'].remove({})
require './model'
Parent.logger = Logger.new('./actual_query')
describe 'embedded many object' do
context 'making two grandsons failed' do
before(:all) do
parent = Parent.new(:foo => 'foo')
son = parent.sons.build(:bar => 'bar')
son.grandsons.build(:buzz => 1)
parent.save
end
# and another grandson
before(:all) do
parent = Parent.where(:foo => 'foo').first
son = parent.sons.where(:bar => 'bar').first
son.grandsons.build(:buzz => 2)
parent.save
end
it 'sons are one' do
Parent.where(:foo => 'foo').first.sons.should have(1).items
end
it 'grandsons are two' do
Parent.where(:foo => 'foo').first.sons.first.grandsons.should have(2).items
end
it 'to_json has grandsons' do
Parent.where(:foo => 'foo').first.to_json.should =~ /grandson/
end
after(:all) { Parent.delete_all }
end
end
require 'mongoid'
require 'logger'
Mongoid.configure do |c|
c.master = Mongo::Connection.new.db('test')
c.embedded_object_id = false
end
class Parent
include Mongoid::Document
embeds_many :sons
field :foo
end
class Son
include Mongoid::Document
embedded_in :parent
embeds_many :grandsons
field :bar
end
class Grandson
include Mongoid::Document
embedded_in :son
field :buzz
end
> db.parents.find()
{ "_id" : ObjectId("4dd11db673c0b64c6e000001"), "foo" : "foo", "sons" : [
{
"bar" : "bar",
"grandsons" : [
{
"buzz" : 1
}
]
},
{
"bar" : "bar",
"grandsons" : [
{
"buzz" : 1
},
{
"buzz" : 2
}
]
}
] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment