Skip to content

Instantly share code, notes, and snippets.

@royw
Created September 25, 2009 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royw/193909 to your computer and use it in GitHub Desktop.
Save royw/193909 to your computer and use it in GitHub Desktop.
# demos unexpected behavior when using logical functions on find result sets
require 'mongomapper'
require 'spec'
require 'ruby-debug'
MongoMapper.connection = XGen::Mongo::Driver::Connection.new('localhost')
MongoMapper.database = 'features_test'
class Phone
include MongoMapper::EmbeddedDocument
key :number, String, :required => true
key :type, String, :default => 'unknown'
end
class Person
include MongoMapper::Document
key :name, String, :required => true, :index => true
many :phones
end
describe "feature testing" do
before :each do
Person.collection.clear
jane = Person.create :name => 'Jane'
jane.phones << Phone.new(:number => '214-555-1234', :type => 'home')
jane.phones << Phone.new(:number => '214-555-5678', :type => 'work')
jane.save
bob = Person.create :name => 'Bob'
bob.phones << Phone.new(:number => '214-555-1111', :type => 'home')
bob.phones << Phone.new(:number => '214-555-2222', :type => 'work')
bob.phones << Phone.new(:number => '214-555-3333', :type => 'mobile')
bob.save
sue = Person.create :name => 'Sue'
sue.phones << Phone.new(:number => '214-555-0000')
sue.save
@home_phones = Person.all(:conditions => {'phones.type' => 'home'})
@work_phones = Person.all(:conditions => {'phones.type' => 'work'})
@mobile_phones = Person.all(:conditions => {'phones.type' => 'mobile'})
@unknown_phones = Person.all(:conditions => {'phones.type' => 'unknown'})
end
it "should be correctly loaded" do
@home_phones.size.should == 2
@work_phones.size.should == 2
@mobile_phones.size.should == 1
@unknown_phones.size.should == 1
puts "\nhome_phones: " + @home_phones.inspect
puts "\nwork_phones: " + @work_phones.inspect
puts "\nmobile_phones: " + @mobile_phones.inspect
puts "\nunknown_phones: " + @unknown_phones.inspect
end
# #eql? fails while == works
it "should handle #eql? between search results" do
bob_home = @home_phones.find_all{|person| person.name == 'Bob'}
bob_mobile = @mobile_phones.find_all{|person| person.name == 'Bob'}
bob_home.eql?(bob_mobile).should be_true
end
it "should handle == between search results" do
bob_home = @home_phones.find_all{|person| person.name == 'Bob'}
bob_mobile = @mobile_phones.find_all{|person| person.name == 'Bob'}
(bob_home == bob_mobile).should be_true
end
# intersections, unions, diffs, and uniq fail on Person objects
# but work on their corresponding _id values
it "should handle intersections" do
intersection = (@home_phones & @mobile_phones)
puts "\nintersection: " + intersection.inspect
intersection.size.should == 1
end
it "should handle intersections on _id" do
intersection = (@home_phones.map(&:_id) & @mobile_phones.map(&:_id))
puts "\nintersection _id: " + intersection.inspect
intersection.size.should == 1
end
it "should handle unions" do
union = (@home_phones | @mobile_phones)
puts "\nunion: " + union.inspect
union.size.should == 2
end
it "should handle unions on _id" do
union = (@home_phones.map(&:_id) | @mobile_phones.map(&:_id))
puts "\nunion _id: " + union.inspect
union.size.should == 2
end
it "should handle diffs" do
diff = (@home_phones - @mobile_phones)
puts "\ndiff: " + diff.inspect
diff.size.should == 1
end
it "should handle diffs on _id" do
diff = (@home_phones.map(&:_id) - @mobile_phones.map(&:_id))
puts "\ndiff _id: " + diff.inspect
diff.size.should == 1
end
it "should handle combining" do
combine = (@home_phones + @mobile_phones)
puts "\ncombine: " + combine.inspect
combine.size.should == 3
end
it "should handle combining on _id" do
combine = (@home_phones.map(&:_id) + @mobile_phones.map(&:_id))
puts "\ncombine _id: " + combine.inspect
combine.size.should == 3
end
it "should handle uniq" do
combine = (@home_phones + @mobile_phones).uniq
puts "\nuniq: " + combine.inspect
combine.size.should == 2
end
it "should handle uniq on _id" do
combine = (@home_phones.map(&:_id) + @mobile_phones.map(&:_id)).uniq
puts "\nuniq _id: " + combine.inspect
combine.size.should == 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment