forked from mongomapper/mongomapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for many polymorphic documents. Separated out association…
… tests to make them easier to work with.
- Loading branch information
1 parent
dcead51
commit fc96045
Showing
11 changed files
with
717 additions
and
561 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module MongoMapper | ||
module Associations | ||
class ManyArrayProxy < Proxy | ||
delegate :klass, :to => :@association | ||
|
||
def find(*args) | ||
options = args.extract_options! | ||
klass.find(*args << scoped_options(options)) | ||
end | ||
|
||
def paginate(options) | ||
klass.paginate(scoped_options(options)) | ||
end | ||
|
||
def all(options={}) | ||
find(:all, scoped_options(options)) | ||
end | ||
|
||
def first(options={}) | ||
find(:first, scoped_options(options)) | ||
end | ||
|
||
def last(options={}) | ||
find(:last, scoped_options(options)) | ||
end | ||
|
||
protected | ||
def scoped_options(options) | ||
options.dup.deep_merge({:conditions => {self.foreign_key => @owner.id}}) | ||
end | ||
|
||
def find_target | ||
find(:all) | ||
end | ||
|
||
def foreign_key | ||
@association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id" | ||
end | ||
end | ||
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
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
39 changes: 39 additions & 0 deletions
39
test/functional/associations/test_belongs_to_polymorphic_proxy.rb
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,39 @@ | ||
require 'test_helper' | ||
require 'models' | ||
|
||
class BelongsToPolymorphicProxyTest < Test::Unit::TestCase | ||
def setup | ||
clear_all_collections | ||
end | ||
|
||
should "default to nil" do | ||
status = Status.new | ||
status.target.should be_nil | ||
end | ||
|
||
should "store the association" do | ||
status = Status.new | ||
project = Project.new(:name => "mongomapper") | ||
status.target = project | ||
status.save.should be_true | ||
|
||
from_db = Status.find(status.id) | ||
from_db.target.should_not be_nil | ||
from_db.target_id.should == project.id | ||
from_db.target_type.should == "Project" | ||
from_db.target.name.should == "mongomapper" | ||
end | ||
|
||
should "unset the association" do | ||
status = Status.new | ||
project = Project.new(:name => "mongomapper") | ||
status.target = project | ||
status.save.should be_true | ||
|
||
from_db = 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 |
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,35 @@ | ||
require 'test_helper' | ||
require 'models' | ||
|
||
class BelongsToProxyTest < Test::Unit::TestCase | ||
def setup | ||
clear_all_collections | ||
end | ||
|
||
should "default to nil" do | ||
status = Status.new | ||
status.project.should be_nil | ||
end | ||
|
||
should "store the association" do | ||
status = Status.new | ||
project = Project.new(:name => "mongomapper") | ||
status.project = project | ||
status.save.should be_true | ||
|
||
from_db = Status.find(status.id) | ||
from_db.project.should_not be_nil | ||
from_db.project.name.should == "mongomapper" | ||
end | ||
|
||
should "unset the association" do | ||
status = Status.new | ||
project = Project.new(:name => "mongomapper") | ||
status.project = project | ||
status.save.should be_true | ||
|
||
from_db = Status.find(status.id) | ||
from_db.project = nil | ||
from_db.project.should be_nil | ||
end | ||
end |
131 changes: 131 additions & 0 deletions
131
test/functional/associations/test_many_embedded_polymorphic_proxy.rb
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,131 @@ | ||
require 'test_helper' | ||
require 'models' | ||
|
||
class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase | ||
def setup | ||
clear_all_collections | ||
end | ||
|
||
should "default reader to empty array" do | ||
catalog = Catalog.new | ||
catalog.medias.should == [] | ||
end | ||
|
||
should "allow adding to association like it was an array" do | ||
catalog = Catalog.new | ||
catalog.medias << Video.new | ||
catalog.medias.push Video.new | ||
catalog.medias.size.should == 2 | ||
end | ||
|
||
should "store the association" do | ||
catalog = Catalog.new | ||
catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)] | ||
catalog.save.should be_true | ||
|
||
from_db = Catalog.find(catalog.id) | ||
from_db.medias.size.should == 1 | ||
from_db.medias[0].file.should == "video.mpg" | ||
end | ||
|
||
should "store different associations" do | ||
catalog = Catalog.new | ||
catalog.medias = [ | ||
Video.new("file" => "video.mpg", "length" => 3600), | ||
Music.new("file" => "music.mp3", "bitrate" => "128kbps"), | ||
Image.new("file" => "image.png", "width" => 800, "height" => 600) | ||
] | ||
catalog.save.should be_true | ||
|
||
from_db = Catalog.find(catalog.id) | ||
from_db.medias.size.should == 3 | ||
from_db.medias[0].file.should == "video.mpg" | ||
from_db.medias[0].length.should == 3600 | ||
from_db.medias[1].file.should == "music.mp3" | ||
from_db.medias[1].bitrate.should == "128kbps" | ||
from_db.medias[2].file.should == "image.png" | ||
from_db.medias[2].width.should == 800 | ||
from_db.medias[2].height.should == 600 | ||
end | ||
|
||
context "With modularized models" do | ||
should "set associations correctly" do | ||
fleet_attributes = { | ||
"name" => "My Fleet", | ||
"transports" => [ | ||
{"_type" => "TrModels::Ambulance", "license_plate" => "GGG123", "icu" => true}, | ||
{"_type" => "TrModels::Car", "license_plate" => "ABC123", "model" => "VW Golf", "year" => 2001}, | ||
{"_type" => "TrModels::Car", "license_plate" => "DEF123", "model" => "Honda Accord", "year" => 2008}, | ||
] | ||
} | ||
|
||
fleet = TrModels::Fleet.new(fleet_attributes) | ||
fleet.transports.size.should == 3 | ||
fleet.transports[0].class.should == TrModels::Ambulance | ||
fleet.transports[0].license_plate.should == "GGG123" | ||
fleet.transports[0].icu.should be_true | ||
fleet.transports[1].class.should == TrModels::Car | ||
fleet.transports[1].license_plate.should == "ABC123" | ||
fleet.transports[1].model.should == "VW Golf" | ||
fleet.transports[1].year.should == 2001 | ||
fleet.transports[2].class.should == TrModels::Car | ||
fleet.transports[2].license_plate.should == "DEF123" | ||
fleet.transports[2].model.should == "Honda Accord" | ||
fleet.transports[2].year.should == 2008 | ||
fleet.save.should be_true | ||
|
||
from_db = TrModels::Fleet.find(fleet.id) | ||
from_db.transports.size.should == 3 | ||
from_db.transports[0].license_plate.should == "GGG123" | ||
from_db.transports[0].icu.should be_true | ||
from_db.transports[1].license_plate.should == "ABC123" | ||
from_db.transports[1].model.should == "VW Golf" | ||
from_db.transports[1].year.should == 2001 | ||
from_db.transports[2].license_plate.should == "DEF123" | ||
from_db.transports[2].model.should == "Honda Accord" | ||
from_db.transports[2].year.should == 2008 | ||
end | ||
|
||
should "default reader to empty array" do | ||
fleet = TrModels::Fleet.new | ||
fleet.transports.should == [] | ||
end | ||
|
||
should "allow adding to association like it was an array" do | ||
fleet = TrModels::Fleet.new | ||
fleet.transports << TrModels::Car.new | ||
fleet.transports.push TrModels::Bus.new | ||
fleet.transports.size.should == 2 | ||
end | ||
|
||
should "store the association" do | ||
fleet = TrModels::Fleet.new | ||
fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")] | ||
fleet.save.should be_true | ||
|
||
from_db = TrModels::Fleet.find(fleet.id) | ||
from_db.transports.size.should == 1 | ||
from_db.transports[0].license_plate.should == "DCU2013" | ||
end | ||
|
||
should "store different associations" do | ||
fleet = TrModels::Fleet.new | ||
fleet.transports = [ | ||
TrModels::Car.new("license_plate" => "ABC1223", "model" => "Honda Civic", "year" => 2003), | ||
TrModels::Bus.new("license_plate" => "XYZ9090", "max_passengers" => 51), | ||
TrModels::Ambulance.new("license_plate" => "HDD3030", "icu" => true) | ||
] | ||
fleet.save.should be_true | ||
|
||
from_db = TrModels::Fleet.find(fleet.id) | ||
from_db.transports.size.should == 3 | ||
from_db.transports[0].license_plate.should == "ABC1223" | ||
from_db.transports[0].model.should == "Honda Civic" | ||
from_db.transports[0].year.should == 2003 | ||
from_db.transports[1].license_plate.should == "XYZ9090" | ||
from_db.transports[1].max_passengers.should == 51 | ||
from_db.transports[2].license_plate.should == "HDD3030" | ||
from_db.transports[2].icu.should == true | ||
end | ||
end | ||
end |
Oops, something went wrong.