Skip to content

Instantly share code, notes, and snippets.

@rjlee
Created July 9, 2009 19:39
Show Gist options
  • Save rjlee/143944 to your computer and use it in GitHub Desktop.
Save rjlee/143944 to your computer and use it in GitHub Desktop.
test/test_associations.rb
require 'test_helper'
module Thing
class Address
include MongoMapper::EmbeddedDocument
key :address, String
key :city, String
key :state, String
key :zip, Integer
end
end
module Thing
class Project
include MongoMapper::Document
key :name, String
many :statuses, :class_name => 'Thing::Status'
many :addresses, :class_name => 'Thing::Address'
end
end
module Thing
class Status
include MongoMapper::Document
belongs_to :project, :class_name => 'Thing::Project'
belongs_to :target, :polymorphic => true, :class_name => 'Thing::Target'
key :name, String
end
end
module Thing
class Person
include MongoMapper::EmbeddedDocument
key :name, String
key :child, Thing::Person
end
end
class AssociationsTest < Test::Unit::TestCase
def setup
Thing::Project.collection.clear
Thing::Status.collection.clear
end
context "Polymorphic Belongs To" do
should "default to nil" do
status = Thing::Status.new
status.target.should be_nil
end
should "store the association" do
status = Thing::Status.new
project = Thing::Project.new(:name => "mongomapper")
status.target = project
status.save.should be_true
from_db = Thing::Status.find(status.id)
from_db.target.should_not be_nil
from_db.target_id.should == project.id
from_db.target_type.should == "Thing::Project"
from_db.target.name.should == "mongomapper"
end
should "unset the association" do
status = Thing::Status.new
project = Thing::Project.new(:name => "mongomapper")
status.target = project
status.save.should be_true
from_db = Thing::Status.find(status.id)
from_db.target = nil
from_db.target_type.should be_nil
from_db.target_id.should be_nil
from_db.target.should be_nil
end
end
context "Belongs To" do
should "default to nil" do
status = Thing::Status.new
status.project.should be_nil
end
should "store the association" do
status = Thing::Status.new
project = Thing::Project.new(:name => "mongomapper")
status.project = project
status.save.should be_true
from_db = Thing::Status.find(status.id)
from_db.project.should_not be_nil
from_db.project.name.should == "mongomapper"
end
should "unset the association" do
status = Thing::Status.new
project = Thing::Project.new(:name => "mongomapper")
status.project = project
status.save.should be_true
from_db = Thing::Status.find(status.id)
from_db.project = nil
from_db.project.should be_nil
end
end
context "Many documents" do
should "default reader to empty array" do
project = Thing::Project.new
project.statuses.should == []
end
should "allow adding to association like it was an array" do
project = Thing::Project.new
project.statuses << Thing::Status.new
project.statuses.push Thing::Status.new
project.statuses.size.should == 2
end
should "store the association" do
project = Thing::Project.new
project.statuses = [ Thing::Status.new("name" => "ready") ]
project.save.should be_true
from_db = Thing::Project.find(project.id)
from_db.statuses.size.should == 1
from_db.statuses[0].name.should == "ready"
end
end
context "Many embedded documents" do
should "default reader to empty array" do
project = Thing::Project.new
project.addresses.should == []
end
should "allow adding to association like it was an array" do
project = Thing::Project.new
project.addresses << Thing::Address.new
project.addresses.push Thing::Address.new
project.addresses.size.should == 2
end
should "be embedded in document on save" do
sb = Thing::Address.new(:city => 'South Bend', :state => 'IN')
chi = Thing::Address.new(:city => 'Chicago', :state => 'IL')
project = Thing::Project.new
project.addresses << sb
project.addresses << chi
project.save
from_db = Thing::Project.find(project.id)
from_db.addresses.size.should == 2
from_db.addresses[0].should == sb
from_db.addresses[1].should == chi
end
should "allow embedding arbitrarily deep" do
@document = Class.new do
include MongoMapper::Document
key :person, Thing::Person
end
meg = Thing::Person.new(:name => "Meg")
meg.child = Thing::Person.new(:name => "Steve")
meg.child.child = Thing::Person.new(:name => "Linda")
doc = @document.new(:person => meg)
doc.save
from_db = @document.find(doc.id)
from_db.person.name.should == 'Meg'
from_db.person.child.name.should == 'Steve'
from_db.person.child.child.name.should == 'Linda'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment