jnunemaker-mongomapper 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/History +11 -0
  2. data/VERSION +1 -1
  3. data/lib/mongomapper/associations/base.rb +5 -2
  4. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +1 -2
  5. data/lib/mongomapper/associations/belongs_to_proxy.rb +3 -3
  6. data/lib/mongomapper/associations/many_documents_proxy.rb +85 -0
  7. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +2 -3
  8. data/lib/mongomapper/associations/many_embedded_proxy.rb +2 -4
  9. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  10. data/lib/mongomapper/associations/many_proxy.rb +1 -50
  11. data/lib/mongomapper/associations/proxy.rb +1 -1
  12. data/lib/mongomapper/document.rb +5 -12
  13. data/lib/mongomapper/embedded_document.rb +21 -2
  14. data/lib/mongomapper/key.rb +2 -1
  15. data/lib/mongomapper/serializers/json_serializer.rb +15 -0
  16. data/lib/mongomapper.rb +17 -10
  17. data/mongomapper.gemspec +17 -4
  18. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +39 -0
  19. data/test/functional/associations/test_belongs_to_proxy.rb +35 -0
  20. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  21. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  22. data/test/functional/associations/test_many_polymorphic_proxy.rb +259 -0
  23. data/test/functional/associations/test_many_proxy.rb +236 -0
  24. data/test/functional/test_associations.rb +22 -467
  25. data/test/functional/test_document.rb +76 -19
  26. data/test/functional/test_pagination.rb +2 -3
  27. data/test/models.rb +16 -0
  28. data/test/test_helper.rb +1 -1
  29. data/test/unit/serializers/test_json_serializer.rb +69 -16
  30. data/test/unit/test_association_base.rb +5 -0
  31. data/test/unit/test_document.rb +14 -4
  32. data/test/unit/test_embedded_document.rb +46 -21
  33. data/test/unit/test_key.rb +5 -0
  34. data/test/unit/test_mongo_id.rb +2 -2
  35. data/test/unit/test_rails_compatibility.rb +3 -3
  36. metadata +16 -3
  37. data/lib/mongomapper/associations/array_proxy.rb +0 -6
@@ -0,0 +1,236 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class ManyProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ clear_all_collections
7
+ end
8
+
9
+ should "default reader to empty array" do
10
+ project = Project.new
11
+ project.statuses.should == []
12
+ end
13
+
14
+ should "allow adding to association like it was an array" do
15
+ project = Project.new
16
+ project.statuses << Status.new
17
+ project.statuses.push Status.new
18
+ project.statuses.concat Status.new
19
+ project.statuses.size.should == 3
20
+ end
21
+
22
+ should "be able to replace the association" do
23
+ project = Project.new
24
+ project.statuses = [Status.new("name" => "ready")]
25
+ project.save.should be_true
26
+
27
+ from_db = Project.find(project.id)
28
+ from_db.statuses.size.should == 1
29
+ from_db.statuses[0].name.should == "ready"
30
+ end
31
+
32
+ should "correctly assign foreign key when using <<, push and concat" do
33
+ project = Project.new
34
+ project.statuses << Status.new(:name => '<<')
35
+ project.statuses.push Status.new(:name => 'push')
36
+ project.statuses.concat Status.new(:name => 'concat')
37
+
38
+ from_db = Project.find(project.id)
39
+ from_db.statuses[0].project_id.should == project.id
40
+ from_db.statuses[1].project_id.should == project.id
41
+ from_db.statuses[2].project_id.should == project.id
42
+ end
43
+
44
+ context "build" do
45
+ should "assign foreign key" do
46
+ project = Project.create
47
+ status = project.statuses.build
48
+ status.project_id.should == project.id
49
+ end
50
+
51
+ should "allow assigning attributes" do
52
+ project = Project.create
53
+ status = project.statuses.build(:name => 'Foo')
54
+ status.name.should == 'Foo'
55
+ end
56
+ end
57
+
58
+ context "create" do
59
+ should "assign foreign key" do
60
+ project = Project.create
61
+ status = project.statuses.create
62
+ status.project_id.should == project.id
63
+ end
64
+
65
+ should "save record" do
66
+ project = Project.create
67
+ lambda {
68
+ project.statuses.create
69
+ }.should change { Status.count }
70
+ end
71
+
72
+ should "allow passing attributes" do
73
+ project = Project.create
74
+ status = project.statuses.create(:name => 'Foo!')
75
+ status.name.should == 'Foo!'
76
+ end
77
+ end
78
+
79
+ context "count" do
80
+ should "work scoped to association" do
81
+ project = Project.create
82
+ 3.times { project.statuses.create }
83
+
84
+ other_project = Project.create
85
+ 2.times { other_project.statuses.create }
86
+
87
+ project.statuses.count.should == 3
88
+ other_project.statuses.count.should == 2
89
+ end
90
+
91
+ should "work with conditions" do
92
+ project = Project.create
93
+ project.statuses.create(:name => 'Foo')
94
+ project.statuses.create(:name => 'Other 1')
95
+ project.statuses.create(:name => 'Other 2')
96
+
97
+ project.statuses.count(:name => 'Foo').should == 1
98
+ end
99
+ end
100
+
101
+ context "Finding scoped to association" do
102
+ setup do
103
+ @project1 = Project.new(:name => 'Project 1')
104
+ @brand_new = Status.create(:name => 'New')
105
+ @complete = Status.create(:name => 'Complete')
106
+ @project1.statuses = [@brand_new, @complete]
107
+ @project1.save
108
+
109
+ @project2 = Project.create(:name => 'Project 2')
110
+ @in_progress = Status.create(:name => 'In Progress')
111
+ @archived = Status.create(:name => 'Archived')
112
+ @another_complete = Status.create(:name => 'Complete')
113
+ @project2.statuses = [@in_progress, @archived, @another_complete]
114
+ @project2.save
115
+ end
116
+
117
+ context "with :all" do
118
+ should "work" do
119
+ @project1.statuses.find(:all).should == [@brand_new, @complete]
120
+ end
121
+
122
+ should "work with conditions" do
123
+ statuses = @project1.statuses.find(:all, :conditions => {'name' => 'Complete'})
124
+ statuses.should == [@complete]
125
+ end
126
+
127
+ should "work with order" do
128
+ statuses = @project1.statuses.find(:all, :order => 'name asc')
129
+ statuses.should == [@complete, @brand_new]
130
+ end
131
+ end
132
+
133
+ context "with #all" do
134
+ should "work" do
135
+ @project1.statuses.all.should == [@brand_new, @complete]
136
+ end
137
+
138
+ should "work with conditions" do
139
+ statuses = @project1.statuses.all(:conditions => {'name' => 'Complete'})
140
+ statuses.should == [@complete]
141
+ end
142
+
143
+ should "work with order" do
144
+ statuses = @project1.statuses.all(:order => 'name asc')
145
+ statuses.should == [@complete, @brand_new]
146
+ end
147
+ end
148
+
149
+ context "with :first" do
150
+ should "work" do
151
+ @project1.statuses.find(:first).should == @brand_new
152
+ end
153
+
154
+ should "work with conditions" do
155
+ status = @project1.statuses.find(:first, :conditions => {:name => 'Complete'})
156
+ status.should == @complete
157
+ end
158
+ end
159
+
160
+ context "with #first" do
161
+ should "work" do
162
+ @project1.statuses.first.should == @brand_new
163
+ end
164
+
165
+ should "work with conditions" do
166
+ status = @project1.statuses.first(:conditions => {:name => 'Complete'})
167
+ status.should == @complete
168
+ end
169
+ end
170
+
171
+ context "with :last" do
172
+ should "work" do
173
+ @project1.statuses.find(:last).should == @complete
174
+ end
175
+
176
+ should "work with conditions" do
177
+ status = @project1.statuses.find(:last, :conditions => {:name => 'New'})
178
+ status.should == @brand_new
179
+ end
180
+ end
181
+
182
+ context "with #last" do
183
+ should "work" do
184
+ @project1.statuses.last.should == @complete
185
+ end
186
+
187
+ should "work with conditions" do
188
+ status = @project1.statuses.last(:conditions => {:name => 'New'})
189
+ status.should == @brand_new
190
+ end
191
+ end
192
+
193
+ context "with one id" do
194
+ should "work for id in association" do
195
+ @project1.statuses.find(@complete.id).should == @complete
196
+ end
197
+
198
+ should "not work for id not in association" do
199
+ lambda {
200
+ @project1.statuses.find(@archived.id)
201
+ }.should raise_error(MongoMapper::DocumentNotFound)
202
+ end
203
+ end
204
+
205
+ context "with multiple ids" do
206
+ should "work for ids in association" do
207
+ statuses = @project1.statuses.find(@brand_new.id, @complete.id)
208
+ statuses.should == [@brand_new, @complete]
209
+ end
210
+
211
+ should "not work for ids not in association" do
212
+ lambda {
213
+ @project1.statuses.find(@brand_new.id, @complete.id, @archived.id)
214
+ }.should raise_error(MongoMapper::DocumentNotFound)
215
+ end
216
+ end
217
+
218
+ context "with #paginate" do
219
+ setup do
220
+ @statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => '$natural asc')
221
+ end
222
+
223
+ should "return total pages" do
224
+ @statuses.total_pages.should == 2
225
+ end
226
+
227
+ should "return total entries" do
228
+ @statuses.total_entries.should == 3
229
+ end
230
+
231
+ should "return the subject" do
232
+ @statuses.should == [@in_progress, @archived]
233
+ end
234
+ end
235
+ end
236
+ end