hashrocket-mongomapper 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/History +70 -0
- data/LICENSE +20 -0
- data/README.rdoc +39 -0
- data/Rakefile +73 -0
- data/VERSION +1 -0
- data/bin/mmconsole +56 -0
- data/lib/mongomapper.rb +70 -0
- data/lib/mongomapper/associations.rb +84 -0
- data/lib/mongomapper/associations/base.rb +69 -0
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongomapper/associations/belongs_to_proxy.rb +22 -0
- data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
- data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
- data/lib/mongomapper/associations/many_embedded_proxy.rb +17 -0
- data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongomapper/associations/many_proxy.rb +6 -0
- data/lib/mongomapper/associations/proxy.rb +63 -0
- data/lib/mongomapper/callbacks.rb +106 -0
- data/lib/mongomapper/document.rb +337 -0
- data/lib/mongomapper/dynamic_finder.rb +38 -0
- data/lib/mongomapper/embedded_document.rb +267 -0
- data/lib/mongomapper/finder_options.rb +85 -0
- data/lib/mongomapper/key.rb +76 -0
- data/lib/mongomapper/observing.rb +50 -0
- data/lib/mongomapper/pagination.rb +52 -0
- data/lib/mongomapper/rails_compatibility/document.rb +15 -0
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
- data/lib/mongomapper/save_with_validation.rb +19 -0
- data/lib/mongomapper/serialization.rb +55 -0
- data/lib/mongomapper/serializers/json_serializer.rb +92 -0
- data/lib/mongomapper/support.rb +30 -0
- data/lib/mongomapper/validations.rb +61 -0
- data/mongomapper.gemspec +142 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
- data/test/functional/associations/test_many_proxy.rb +295 -0
- data/test/functional/test_associations.rb +47 -0
- data/test/functional/test_callbacks.rb +85 -0
- data/test/functional/test_document.rb +952 -0
- data/test/functional/test_pagination.rb +81 -0
- data/test/functional/test_rails_compatibility.rb +30 -0
- data/test/functional/test_validations.rb +172 -0
- data/test/models.rb +139 -0
- data/test/test_helper.rb +67 -0
- data/test/unit/serializers/test_json_serializer.rb +157 -0
- data/test/unit/test_association_base.rb +144 -0
- data/test/unit/test_document.rb +123 -0
- data/test/unit/test_embedded_document.rb +526 -0
- data/test/unit/test_finder_options.rb +183 -0
- data/test/unit/test_key.rb +247 -0
- data/test/unit/test_mongomapper.rb +28 -0
- data/test/unit/test_observing.rb +101 -0
- data/test/unit/test_pagination.rb +113 -0
- data/test/unit/test_rails_compatibility.rb +34 -0
- data/test/unit/test_serializations.rb +52 -0
- data/test/unit/test_validations.rb +500 -0
- metadata +189 -0
@@ -0,0 +1 @@
|
|
1
|
+
I am doing my best to keep unit and functional tests separate. As I see them, functional tests hit the database and should never care about internals. Unit tests do not hit the database.
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default to nil" do
|
10
|
+
status = Status.new
|
11
|
+
status.target.should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to replace the association" do
|
15
|
+
status = Status.new
|
16
|
+
project = Project.new(:name => "mongomapper")
|
17
|
+
status.target = project
|
18
|
+
status.save.should be_true
|
19
|
+
|
20
|
+
from_db = Status.find(status.id)
|
21
|
+
from_db.target.should_not be_nil
|
22
|
+
from_db.target_id.should == project.id
|
23
|
+
from_db.target_type.should == "Project"
|
24
|
+
from_db.target.name.should == "mongomapper"
|
25
|
+
end
|
26
|
+
|
27
|
+
should "unset the association" do
|
28
|
+
status = Status.new
|
29
|
+
project = Project.new(:name => "mongomapper")
|
30
|
+
status.target = project
|
31
|
+
status.save.should be_true
|
32
|
+
|
33
|
+
from_db = Status.find(status.id)
|
34
|
+
from_db.target = nil
|
35
|
+
from_db.target_type.should be_nil
|
36
|
+
from_db.target_id.should be_nil
|
37
|
+
from_db.target.should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
context "association id set but document not found" do
|
41
|
+
setup do
|
42
|
+
@status = Status.new
|
43
|
+
project = Project.new(:name => "mongomapper")
|
44
|
+
@status.target = project
|
45
|
+
@status.save.should be_true
|
46
|
+
project.destroy
|
47
|
+
end
|
48
|
+
|
49
|
+
should "return nil instead of raising error" do
|
50
|
+
@status.target.should be_nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default to nil" do
|
10
|
+
status = Status.new
|
11
|
+
status.project.should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to replace the association" do
|
15
|
+
status = Status.new
|
16
|
+
project = Project.new(:name => "mongomapper")
|
17
|
+
status.project = project
|
18
|
+
status.save.should be_true
|
19
|
+
|
20
|
+
from_db = Status.find(status.id)
|
21
|
+
from_db.project.should_not be_nil
|
22
|
+
from_db.project.name.should == "mongomapper"
|
23
|
+
end
|
24
|
+
|
25
|
+
should "unset the association" do
|
26
|
+
status = Status.new
|
27
|
+
project = Project.new(:name => "mongomapper")
|
28
|
+
status.project = project
|
29
|
+
status.save.should be_true
|
30
|
+
|
31
|
+
from_db = Status.find(status.id)
|
32
|
+
from_db.project = nil
|
33
|
+
from_db.project.should be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
context "association id set but document not found" do
|
37
|
+
setup do
|
38
|
+
@status = Status.new(:name => 'Foo', :project_id => '1234')
|
39
|
+
end
|
40
|
+
|
41
|
+
should "return nil instead of raising error" do
|
42
|
+
@status.project.should be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default reader to empty array" do
|
10
|
+
catalog = Catalog.new
|
11
|
+
catalog.medias.should == []
|
12
|
+
end
|
13
|
+
|
14
|
+
should "allow adding to association like it was an array" do
|
15
|
+
catalog = Catalog.new
|
16
|
+
catalog.medias << Video.new
|
17
|
+
catalog.medias.push Video.new
|
18
|
+
catalog.medias.size.should == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
should "be able to replace the association" do
|
22
|
+
catalog = Catalog.new
|
23
|
+
catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)]
|
24
|
+
catalog.save.should be_true
|
25
|
+
|
26
|
+
from_db = Catalog.find(catalog.id)
|
27
|
+
from_db.medias.size.should == 1
|
28
|
+
from_db.medias[0].file.should == "video.mpg"
|
29
|
+
end
|
30
|
+
|
31
|
+
should "store different associations" do
|
32
|
+
catalog = Catalog.new
|
33
|
+
catalog.medias = [
|
34
|
+
Video.new("file" => "video.mpg", "length" => 3600),
|
35
|
+
Music.new("file" => "music.mp3", "bitrate" => "128kbps"),
|
36
|
+
Image.new("file" => "image.png", "width" => 800, "height" => 600)
|
37
|
+
]
|
38
|
+
catalog.save.should be_true
|
39
|
+
|
40
|
+
from_db = Catalog.find(catalog.id)
|
41
|
+
from_db.medias.size.should == 3
|
42
|
+
from_db.medias[0].file.should == "video.mpg"
|
43
|
+
from_db.medias[0].length.should == 3600
|
44
|
+
from_db.medias[1].file.should == "music.mp3"
|
45
|
+
from_db.medias[1].bitrate.should == "128kbps"
|
46
|
+
from_db.medias[2].file.should == "image.png"
|
47
|
+
from_db.medias[2].width.should == 800
|
48
|
+
from_db.medias[2].height.should == 600
|
49
|
+
end
|
50
|
+
|
51
|
+
context "With modularized models" do
|
52
|
+
should "set associations correctly" do
|
53
|
+
fleet_attributes = {
|
54
|
+
"name" => "My Fleet",
|
55
|
+
"transports" => [
|
56
|
+
{"_type" => "TrModels::Ambulance", "license_plate" => "GGG123", "icu" => true},
|
57
|
+
{"_type" => "TrModels::Car", "license_plate" => "ABC123", "model" => "VW Golf", "year" => 2001},
|
58
|
+
{"_type" => "TrModels::Car", "license_plate" => "DEF123", "model" => "Honda Accord", "year" => 2008},
|
59
|
+
]
|
60
|
+
}
|
61
|
+
|
62
|
+
fleet = TrModels::Fleet.new(fleet_attributes)
|
63
|
+
fleet.transports.size.should == 3
|
64
|
+
fleet.transports[0].class.should == TrModels::Ambulance
|
65
|
+
fleet.transports[0].license_plate.should == "GGG123"
|
66
|
+
fleet.transports[0].icu.should be_true
|
67
|
+
fleet.transports[1].class.should == TrModels::Car
|
68
|
+
fleet.transports[1].license_plate.should == "ABC123"
|
69
|
+
fleet.transports[1].model.should == "VW Golf"
|
70
|
+
fleet.transports[1].year.should == 2001
|
71
|
+
fleet.transports[2].class.should == TrModels::Car
|
72
|
+
fleet.transports[2].license_plate.should == "DEF123"
|
73
|
+
fleet.transports[2].model.should == "Honda Accord"
|
74
|
+
fleet.transports[2].year.should == 2008
|
75
|
+
fleet.save.should be_true
|
76
|
+
|
77
|
+
from_db = TrModels::Fleet.find(fleet.id)
|
78
|
+
from_db.transports.size.should == 3
|
79
|
+
from_db.transports[0].license_plate.should == "GGG123"
|
80
|
+
from_db.transports[0].icu.should be_true
|
81
|
+
from_db.transports[1].license_plate.should == "ABC123"
|
82
|
+
from_db.transports[1].model.should == "VW Golf"
|
83
|
+
from_db.transports[1].year.should == 2001
|
84
|
+
from_db.transports[2].license_plate.should == "DEF123"
|
85
|
+
from_db.transports[2].model.should == "Honda Accord"
|
86
|
+
from_db.transports[2].year.should == 2008
|
87
|
+
end
|
88
|
+
|
89
|
+
should "default reader to empty array" do
|
90
|
+
fleet = TrModels::Fleet.new
|
91
|
+
fleet.transports.should == []
|
92
|
+
end
|
93
|
+
|
94
|
+
should "allow adding to association like it was an array" do
|
95
|
+
fleet = TrModels::Fleet.new
|
96
|
+
fleet.transports << TrModels::Car.new
|
97
|
+
fleet.transports.push TrModels::Bus.new
|
98
|
+
fleet.transports.size.should == 2
|
99
|
+
end
|
100
|
+
|
101
|
+
should "be able to replace the association" do
|
102
|
+
fleet = TrModels::Fleet.new
|
103
|
+
fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")]
|
104
|
+
fleet.save.should be_true
|
105
|
+
|
106
|
+
from_db = TrModels::Fleet.find(fleet.id)
|
107
|
+
from_db.transports.size.should == 1
|
108
|
+
from_db.transports[0].license_plate.should == "DCU2013"
|
109
|
+
end
|
110
|
+
|
111
|
+
should "store different associations" do
|
112
|
+
fleet = TrModels::Fleet.new
|
113
|
+
fleet.transports = [
|
114
|
+
TrModels::Car.new("license_plate" => "ABC1223", "model" => "Honda Civic", "year" => 2003),
|
115
|
+
TrModels::Bus.new("license_plate" => "XYZ9090", "max_passengers" => 51),
|
116
|
+
TrModels::Ambulance.new("license_plate" => "HDD3030", "icu" => true)
|
117
|
+
]
|
118
|
+
fleet.save.should be_true
|
119
|
+
|
120
|
+
from_db = TrModels::Fleet.find(fleet.id)
|
121
|
+
from_db.transports.size.should == 3
|
122
|
+
from_db.transports[0].license_plate.should == "ABC1223"
|
123
|
+
from_db.transports[0].model.should == "Honda Civic"
|
124
|
+
from_db.transports[0].year.should == 2003
|
125
|
+
from_db.transports[1].license_plate.should == "XYZ9090"
|
126
|
+
from_db.transports[1].max_passengers.should == 51
|
127
|
+
from_db.transports[2].license_plate.should == "HDD3030"
|
128
|
+
from_db.transports[2].icu.should == true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default reader to empty array" do
|
10
|
+
Project.new.addresses.should == []
|
11
|
+
end
|
12
|
+
|
13
|
+
should "allow adding to association like it was an array" do
|
14
|
+
project = Project.new
|
15
|
+
project.addresses << Address.new
|
16
|
+
project.addresses.push Address.new
|
17
|
+
project.addresses.size.should == 2
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be embedded in document on save" do
|
21
|
+
sb = Address.new(:city => 'South Bend', :state => 'IN')
|
22
|
+
chi = Address.new(:city => 'Chicago', :state => 'IL')
|
23
|
+
project = Project.new
|
24
|
+
project.addresses << sb
|
25
|
+
project.addresses << chi
|
26
|
+
project.save
|
27
|
+
|
28
|
+
from_db = Project.find(project.id)
|
29
|
+
from_db.addresses.size.should == 2
|
30
|
+
from_db.addresses[0].should == sb
|
31
|
+
from_db.addresses[1].should == chi
|
32
|
+
end
|
33
|
+
|
34
|
+
should "allow embedding arbitrarily deep" do
|
35
|
+
@document = Class.new do
|
36
|
+
include MongoMapper::Document
|
37
|
+
key :person, Person
|
38
|
+
end
|
39
|
+
@document.collection.clear
|
40
|
+
|
41
|
+
meg = Person.new(:name => "Meg")
|
42
|
+
meg.child = Person.new(:name => "Steve")
|
43
|
+
meg.child.child = Person.new(:name => "Linda")
|
44
|
+
|
45
|
+
doc = @document.new(:person => meg)
|
46
|
+
doc.save
|
47
|
+
|
48
|
+
from_db = @document.find(doc.id)
|
49
|
+
from_db.person.name.should == 'Meg'
|
50
|
+
from_db.person.child.name.should == 'Steve'
|
51
|
+
from_db.person.child.child.name.should == 'Linda'
|
52
|
+
end
|
53
|
+
|
54
|
+
should "allow assignment of 'many' embedded documents using a hash" do
|
55
|
+
person_attributes = {
|
56
|
+
"name" => "Mr. Pet Lover",
|
57
|
+
"pets" => [
|
58
|
+
{"name" => "Jimmy", "species" => "Cocker Spainel"},
|
59
|
+
{"name" => "Sasha", "species" => "Siberian Husky"},
|
60
|
+
]
|
61
|
+
}
|
62
|
+
|
63
|
+
pet_lover = RealPerson.new(person_attributes)
|
64
|
+
pet_lover.name.should == "Mr. Pet Lover"
|
65
|
+
pet_lover.pets[0].name.should == "Jimmy"
|
66
|
+
pet_lover.pets[0].species.should == "Cocker Spainel"
|
67
|
+
pet_lover.pets[1].name.should == "Sasha"
|
68
|
+
pet_lover.pets[1].species.should == "Siberian Husky"
|
69
|
+
pet_lover.save.should be_true
|
70
|
+
|
71
|
+
from_db = RealPerson.find(pet_lover.id)
|
72
|
+
from_db.name.should == "Mr. Pet Lover"
|
73
|
+
from_db.pets[0].name.should == "Jimmy"
|
74
|
+
from_db.pets[0].species.should == "Cocker Spainel"
|
75
|
+
from_db.pets[1].name.should == "Sasha"
|
76
|
+
from_db.pets[1].species.should == "Siberian Husky"
|
77
|
+
end
|
78
|
+
|
79
|
+
should "allow saving embedded documents in 'many' embedded documents" do
|
80
|
+
@document = Class.new do
|
81
|
+
include MongoMapper::Document
|
82
|
+
many :people
|
83
|
+
end
|
84
|
+
@document.collection.clear
|
85
|
+
|
86
|
+
meg = Person.new(:name => "Meg")
|
87
|
+
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
88
|
+
koda = Pet.new(:name => "Koda", :species => "Dog")
|
89
|
+
|
90
|
+
doc = @document.new
|
91
|
+
|
92
|
+
meg.pets << sparky
|
93
|
+
meg.pets << koda
|
94
|
+
|
95
|
+
doc.people << meg
|
96
|
+
doc.save
|
97
|
+
|
98
|
+
from_db = @document.find(doc.id)
|
99
|
+
from_db.people.first.name.should == "Meg"
|
100
|
+
from_db.people.first.pets.should_not == []
|
101
|
+
from_db.people.first.pets.first.name.should == "Sparky"
|
102
|
+
from_db.people.first.pets.first.species.should == "Dog"
|
103
|
+
from_db.people.first.pets[1].name.should == "Koda"
|
104
|
+
from_db.people.first.pets[1].species.should == "Dog"
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default reader to empty array" do
|
10
|
+
Room.new.messages.should == []
|
11
|
+
end
|
12
|
+
|
13
|
+
should "add type key to polymorphic class base" do
|
14
|
+
Message.keys.keys.should include('_type')
|
15
|
+
end
|
16
|
+
|
17
|
+
should "allow adding to assiciation like it was an array" do
|
18
|
+
room = Room.new
|
19
|
+
room.messages << Enter.new
|
20
|
+
room.messages.push Exit.new
|
21
|
+
room.messages.concat Exit.new
|
22
|
+
room.messages.size.should == 3
|
23
|
+
end
|
24
|
+
|
25
|
+
should "be able to replace the association" do
|
26
|
+
room = Room.create(:name => 'Lounge')
|
27
|
+
|
28
|
+
lambda {
|
29
|
+
room.messages = [
|
30
|
+
Enter.new(:body => 'John entered room', :position => 1),
|
31
|
+
Chat.new(:body => 'Heyyyoooo!', :position => 2),
|
32
|
+
Exit.new(:body => 'John exited room', :position => 3)
|
33
|
+
]
|
34
|
+
}.should change { Message.count }.by(3)
|
35
|
+
|
36
|
+
from_db = Room.find(room.id)
|
37
|
+
messages = from_db.messages.all :order => "position"
|
38
|
+
messages.size.should == 3
|
39
|
+
messages[0].body.should == 'John entered room'
|
40
|
+
messages[1].body.should == 'Heyyyoooo!'
|
41
|
+
messages[2].body.should == 'John exited room'
|
42
|
+
end
|
43
|
+
|
44
|
+
should "correctly store type when using <<, push and concat" do
|
45
|
+
room = Room.new
|
46
|
+
room.messages << Enter.new(:body => 'John entered the room', :position => 1)
|
47
|
+
room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
|
48
|
+
room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
|
49
|
+
|
50
|
+
from_db = Room.find(room.id)
|
51
|
+
messages = from_db.messages.all :order => "position"
|
52
|
+
messages[0]._type.should == 'Enter'
|
53
|
+
messages[1]._type.should == 'Exit'
|
54
|
+
messages[2]._type.should == 'Chat'
|
55
|
+
end
|
56
|
+
|
57
|
+
context "build" do
|
58
|
+
should "assign foreign key" do
|
59
|
+
room = Room.create
|
60
|
+
message = room.messages.build
|
61
|
+
message.room_id.should == room.id
|
62
|
+
end
|
63
|
+
|
64
|
+
should "assign _type" do
|
65
|
+
room = Room.create
|
66
|
+
message = room.messages.build
|
67
|
+
message._type.should == 'Message'
|
68
|
+
end
|
69
|
+
|
70
|
+
should "allow assigning attributes" do
|
71
|
+
room = Room.create
|
72
|
+
message = room.messages.build(:body => 'Foo!')
|
73
|
+
message.body.should == 'Foo!'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "create" do
|
78
|
+
should "assign foreign key" do
|
79
|
+
room = Room.create
|
80
|
+
message = room.messages.create
|
81
|
+
message.room_id.should == room.id
|
82
|
+
end
|
83
|
+
|
84
|
+
should "assign _type" do
|
85
|
+
room = Room.create
|
86
|
+
message = room.messages.create
|
87
|
+
message._type.should == 'Message'
|
88
|
+
end
|
89
|
+
|
90
|
+
should "save record" do
|
91
|
+
room = Room.create
|
92
|
+
lambda {
|
93
|
+
room.messages.create
|
94
|
+
}.should change { Message.count }
|
95
|
+
end
|
96
|
+
|
97
|
+
should "allow passing attributes" do
|
98
|
+
room = Room.create
|
99
|
+
message = room.messages.create(:body => 'Foo!')
|
100
|
+
message.body.should == 'Foo!'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "count" do
|
105
|
+
should "work scoped to association" do
|
106
|
+
room = Room.create
|
107
|
+
3.times { room.messages.create }
|
108
|
+
|
109
|
+
other_room = Room.create
|
110
|
+
2.times { other_room.messages.create }
|
111
|
+
|
112
|
+
room.messages.count.should == 3
|
113
|
+
other_room.messages.count.should == 2
|
114
|
+
end
|
115
|
+
|
116
|
+
should "work with conditions" do
|
117
|
+
room = Room.create
|
118
|
+
room.messages.create(:body => 'Foo')
|
119
|
+
room.messages.create(:body => 'Other 1')
|
120
|
+
room.messages.create(:body => 'Other 2')
|
121
|
+
|
122
|
+
room.messages.count(:body => 'Foo').should == 1
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "Finding scoped to association" do
|
127
|
+
setup do
|
128
|
+
@lounge = Room.create(:name => 'Lounge')
|
129
|
+
@lm1 = Message.create(:body => 'Loungin!', :position => 1)
|
130
|
+
@lm2 = Message.create(:body => 'I love loungin!', :position => 2)
|
131
|
+
@lounge.messages = [@lm1, @lm2]
|
132
|
+
@lounge.save
|
133
|
+
|
134
|
+
@hall = Room.create(:name => 'Hall')
|
135
|
+
@hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
|
136
|
+
@hm2 = Message.create(:body => 'Hall the king!', :position => 2)
|
137
|
+
@hm3 = Message.create(:body => 'Loungin!', :position => 3)
|
138
|
+
@hall.messages = [@hm1, @hm2, @hm3]
|
139
|
+
@hall.save
|
140
|
+
end
|
141
|
+
|
142
|
+
context "with :all" do
|
143
|
+
should "work" do
|
144
|
+
@lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
|
145
|
+
end
|
146
|
+
|
147
|
+
should "work with conditions" do
|
148
|
+
messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
|
149
|
+
messages.should == [@lm1]
|
150
|
+
end
|
151
|
+
|
152
|
+
should "work with order" do
|
153
|
+
messages = @lounge.messages.find(:all, :order => 'position desc')
|
154
|
+
messages.should == [@lm2, @lm1]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "with #all" do
|
159
|
+
should "work" do
|
160
|
+
@lounge.messages.all(:order => "position").should == [@lm1, @lm2]
|
161
|
+
end
|
162
|
+
|
163
|
+
should "work with conditions" do
|
164
|
+
messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
|
165
|
+
messages.should == [@lm1]
|
166
|
+
end
|
167
|
+
|
168
|
+
should "work with order" do
|
169
|
+
messages = @lounge.messages.all(:order => 'position desc')
|
170
|
+
messages.should == [@lm2, @lm1]
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "with :first" do
|
175
|
+
should "work" do
|
176
|
+
@lounge.messages.find(:first, :order => "position asc").should == @lm1
|
177
|
+
end
|
178
|
+
|
179
|
+
should "work with conditions" do
|
180
|
+
message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
181
|
+
message.should == @lm2
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "with #first" do
|
186
|
+
should "work" do
|
187
|
+
@lounge.messages.first(:order => "position asc").should == @lm1
|
188
|
+
end
|
189
|
+
|
190
|
+
should "work with conditions" do
|
191
|
+
message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
192
|
+
message.should == @lm2
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context "with :last" do
|
197
|
+
should "work" do
|
198
|
+
@lounge.messages.find(:last, :order => "position asc").should == @lm2
|
199
|
+
end
|
200
|
+
|
201
|
+
should "work with conditions" do
|
202
|
+
message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
|
203
|
+
message.should == @lm1
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "with #last" do
|
208
|
+
should "work" do
|
209
|
+
@lounge.messages.last(:order => "position asc").should == @lm2
|
210
|
+
end
|
211
|
+
|
212
|
+
should "work with conditions" do
|
213
|
+
message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
|
214
|
+
message.should == @lm1
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "with one id" do
|
219
|
+
should "work for id in association" do
|
220
|
+
@lounge.messages.find(@lm2.id).should == @lm2
|
221
|
+
end
|
222
|
+
|
223
|
+
should "not work for id not in association" do
|
224
|
+
lambda {
|
225
|
+
@lounge.messages.find(@hm2.id)
|
226
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "with multiple ids" do
|
231
|
+
should "work for ids in association" do
|
232
|
+
messages = @lounge.messages.find(@lm1.id, @lm2.id)
|
233
|
+
messages.should == [@lm1, @lm2]
|
234
|
+
end
|
235
|
+
|
236
|
+
should "not work for ids not in association" do
|
237
|
+
lambda {
|
238
|
+
@lounge.messages.find(@lm1.id, @lm2.id, @hm2.id)
|
239
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "with #paginate" do
|
244
|
+
setup do
|
245
|
+
@messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
|
246
|
+
end
|
247
|
+
|
248
|
+
should "return total pages" do
|
249
|
+
@messages.total_pages.should == 2
|
250
|
+
end
|
251
|
+
|
252
|
+
should "return total entries" do
|
253
|
+
@messages.total_entries.should == 3
|
254
|
+
end
|
255
|
+
|
256
|
+
should "return the subject" do
|
257
|
+
@messages.should == [@hm1, @hm2]
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|