mrkurt-mongo_mapper 0.6.8

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.
Files changed (77) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +139 -0
  8. data/lib/mongo_mapper/associations.rb +72 -0
  9. data/lib/mongo_mapper/associations/base.rb +113 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
  12. data/lib/mongo_mapper/associations/collection.rb +19 -0
  13. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
  14. data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
  15. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
  16. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
  17. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  18. data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
  19. data/lib/mongo_mapper/associations/proxy.rb +111 -0
  20. data/lib/mongo_mapper/callbacks.rb +61 -0
  21. data/lib/mongo_mapper/dirty.rb +117 -0
  22. data/lib/mongo_mapper/document.rb +496 -0
  23. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  24. data/lib/mongo_mapper/embedded_document.rb +380 -0
  25. data/lib/mongo_mapper/finder_options.rb +145 -0
  26. data/lib/mongo_mapper/key.rb +36 -0
  27. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  28. data/lib/mongo_mapper/pagination.rb +66 -0
  29. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  30. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +28 -0
  31. data/lib/mongo_mapper/serialization.rb +54 -0
  32. data/lib/mongo_mapper/serializers/json_serializer.rb +48 -0
  33. data/lib/mongo_mapper/support.rb +192 -0
  34. data/lib/mongo_mapper/validations.rb +39 -0
  35. data/mongo_mapper.gemspec +173 -0
  36. data/specs.watchr +30 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_proxy.rb +91 -0
  40. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  41. data/test/functional/associations/test_many_documents_proxy.rb +477 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  45. data/test/functional/associations/test_one_proxy.rb +131 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +33 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +159 -0
  50. data/test/functional/test_document.rb +1198 -0
  51. data/test/functional/test_embedded_document.rb +135 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_modifiers.rb +242 -0
  54. data/test/functional/test_pagination.rb +95 -0
  55. data/test/functional/test_rails_compatibility.rb +25 -0
  56. data/test/functional/test_string_id_compatibility.rb +72 -0
  57. data/test/functional/test_validations.rb +361 -0
  58. data/test/models.rb +271 -0
  59. data/test/support/custom_matchers.rb +55 -0
  60. data/test/support/timing.rb +16 -0
  61. data/test/test_helper.rb +27 -0
  62. data/test/unit/associations/test_base.rb +182 -0
  63. data/test/unit/associations/test_proxy.rb +91 -0
  64. data/test/unit/serializers/test_json_serializer.rb +189 -0
  65. data/test/unit/test_document.rb +236 -0
  66. data/test/unit/test_dynamic_finder.rb +125 -0
  67. data/test/unit/test_embedded_document.rb +709 -0
  68. data/test/unit/test_finder_options.rb +325 -0
  69. data/test/unit/test_key.rb +172 -0
  70. data/test/unit/test_mongo_mapper.rb +65 -0
  71. data/test/unit/test_pagination.rb +119 -0
  72. data/test/unit/test_rails_compatibility.rb +52 -0
  73. data/test/unit/test_serializations.rb +52 -0
  74. data/test/unit/test_support.rb +346 -0
  75. data/test/unit/test_time_zones.rb +40 -0
  76. data/test/unit/test_validations.rb +503 -0
  77. metadata +239 -0
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class EmbeddedDocumentTest < Test::Unit::TestCase
5
+ def setup
6
+ @document = Class.new do
7
+ include MongoMapper::Document
8
+ set_collection_name 'users'
9
+
10
+ key :first_name, String
11
+ key :last_name, String
12
+ end
13
+ @document.collection.remove
14
+ end
15
+
16
+ context "Saving a document with an embedded document" do
17
+ setup do
18
+ @document.class_eval do
19
+ key :foo, Address
20
+ end
21
+
22
+ @address = Address.new(:city => 'South Bend', :state => 'IN')
23
+ @doc = @document.new(:foo => @address)
24
+ end
25
+
26
+ should "embed embedded document" do
27
+ @doc.save
28
+ @doc.foo.city.should == 'South Bend'
29
+ @doc.foo.state.should == 'IN'
30
+
31
+ doc = @doc.reload
32
+ doc.foo.city.should == 'South Bend'
33
+ doc.foo.state.should == 'IN'
34
+ end
35
+ end
36
+
37
+ context "Instantiating single collection inherited embedded documents" do
38
+ setup do
39
+ @document = Class.new do
40
+ include MongoMapper::Document
41
+ key :message, Message
42
+ end
43
+ end
44
+
45
+ should "work" do
46
+ doc1 = @document.create(:message => Enter.new)
47
+ doc2 = @document.create(:message => Exit.new)
48
+ doc3 = @document.create(:message => Chat.new)
49
+
50
+ doc1.reload.message.class.should be(Enter)
51
+ doc2.reload.message.class.should be(Exit)
52
+ doc3.reload.message.class.should be(Chat)
53
+ end
54
+ end
55
+
56
+ context "new?" do
57
+ setup do
58
+ @document.class_eval do
59
+ key :foo, Address
60
+ end
61
+ end
62
+
63
+ should "be new until document is saved" do
64
+ address = Address.new(:city => 'South Bend', :state => 'IN')
65
+ doc = @document.new(:foo => address)
66
+ address.new?.should == true
67
+ end
68
+
69
+ should "not be new after document is saved" do
70
+ address = Address.new(:city => 'South Bend', :state => 'IN')
71
+ doc = @document.new(:foo => address)
72
+ doc.save
73
+ doc.foo.new?.should == false
74
+ end
75
+
76
+ should "not be new when document is read back" do
77
+ address = Address.new(:city => 'South Bend', :state => 'IN')
78
+ doc = @document.new(:foo => address)
79
+ doc.save
80
+
81
+ doc = doc.reload
82
+ doc.foo.new?.should == false
83
+ end
84
+ end
85
+
86
+ context "save" do
87
+ should "save the root document" do
88
+ person = RealPerson.create
89
+
90
+ pet = Pet.new :name => 'sparky'
91
+ person.pets << pet
92
+ pet.save
93
+
94
+ person = person.reload
95
+ person.pets.first.should == pet
96
+ end
97
+
98
+ should "save new keys" do
99
+ person = RealPerson.new
100
+ person[:new_attribute] = 'foobar'
101
+ person.save
102
+
103
+ person = person.reload
104
+ person.new_attribute.should == 'foobar'
105
+ end
106
+ end
107
+
108
+ context "update_attributes" do
109
+ should "save the root document" do
110
+ person = RealPerson.create
111
+
112
+ pet = Pet.new(:name => 'sparky')
113
+ person.pets << pet
114
+ pet.save
115
+
116
+ person = person.reload
117
+ pet = person.pets.first
118
+ pet.update_attributes :name => 'koda'
119
+
120
+ person = person.reload
121
+ person.pets.first._id.should == pet._id
122
+ person.pets.first.name.should == 'koda'
123
+ end
124
+ end
125
+
126
+ context "update_attributes!" do
127
+ should "pass the attributes to self.attributes" do
128
+ person = RealPerson.create
129
+ attributes = { :foo => 'bar' }
130
+ person.expects(:attributes=).with(attributes)
131
+ person.expects(:save!)
132
+ person.update_attributes!(attributes)
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ context "with connection that has logger" do
5
+ setup do
6
+ @output = StringIO.new
7
+ @logger = Logger.new(@output)
8
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => @logger)
9
+ end
10
+
11
+ should "be able to get access to that logger" do
12
+ MongoMapper.logger.should == @logger
13
+ end
14
+
15
+ should "be able to log messages" do
16
+ MongoMapper.logger.debug 'testing'
17
+ @output.string.include?('testing').should be_true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,242 @@
1
+ require 'test_helper'
2
+
3
+ class ModifierTest < Test::Unit::TestCase
4
+ def setup
5
+ @page_class = Class.new do
6
+ include MongoMapper::Document
7
+
8
+ key :title, String
9
+ key :day_count, Integer, :default => 0
10
+ key :week_count, Integer, :default => 0
11
+ key :month_count, Integer, :default => 0
12
+ key :tags, Array
13
+ end
14
+
15
+ @page_class.collection.remove
16
+ end
17
+
18
+ def assert_page_counts(page, day_count, week_count, month_count)
19
+ page.reload
20
+ page.day_count.should == day_count
21
+ page.week_count.should == week_count
22
+ page.month_count.should == month_count
23
+ end
24
+
25
+ should "be able to increment with criteria and modifier hashes" do
26
+ page = @page_class.create(:title => 'Home')
27
+ page2 = @page_class.create(:title => 'Home')
28
+
29
+ @page_class.increment({:title => 'Home'}, {
30
+ :day_count => 1, :week_count => 2, :month_count => 3
31
+ })
32
+
33
+ assert_page_counts page, 1, 2, 3
34
+ assert_page_counts page2, 1, 2, 3
35
+ end
36
+
37
+ should "be able to increment with ids and modifier hash" do
38
+ page = @page_class.create(:title => 'Home')
39
+ page2 = @page_class.create(:title => 'Home')
40
+
41
+ @page_class.increment(page.id, page2.id, {
42
+ :day_count => 1, :week_count => 2, :month_count => 3
43
+ })
44
+
45
+ assert_page_counts page, 1, 2, 3
46
+ assert_page_counts page2, 1, 2, 3
47
+ end
48
+
49
+ should "be able to decrement with criteria and modifier hashes" do
50
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
51
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
52
+
53
+ @page_class.decrement({:title => 'Home'}, {
54
+ :day_count => 1, :week_count => 2, :month_count => 3
55
+ })
56
+
57
+ assert_page_counts page, 0, 0, 0
58
+ assert_page_counts page2, 0, 0, 0
59
+ end
60
+
61
+ should "be able to decrement with ids and modifier hash" do
62
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
63
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
64
+
65
+ @page_class.decrement(page.id, page2.id, {
66
+ :day_count => 1, :week_count => 2, :month_count => 3
67
+ })
68
+
69
+ assert_page_counts page, 0, 0, 0
70
+ assert_page_counts page2, 0, 0, 0
71
+ end
72
+
73
+ should "always decrement when decrement is called whether number is positive or negative" do
74
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
75
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
76
+
77
+ @page_class.decrement(page.id, page2.id, {
78
+ :day_count => -1, :week_count => 2, :month_count => -3
79
+ })
80
+
81
+ assert_page_counts page, 0, 0, 0
82
+ assert_page_counts page2, 0, 0, 0
83
+ end
84
+
85
+ should "be able to set with criteria and modifier hashes" do
86
+ page = @page_class.create(:title => 'Home')
87
+ page2 = @page_class.create(:title => 'Home')
88
+
89
+ @page_class.set({:title => 'Home'}, :title => 'Home Revised')
90
+
91
+ page.reload
92
+ page.title.should == 'Home Revised'
93
+
94
+ page2.reload
95
+ page2.title.should == 'Home Revised'
96
+ end
97
+
98
+ should "be able to set with ids and modifier hash" do
99
+ page = @page_class.create(:title => 'Home')
100
+ page2 = @page_class.create(:title => 'Home')
101
+
102
+ @page_class.set(page.id, page2.id, :title => 'Home Revised')
103
+
104
+ page.reload
105
+ page.title.should == 'Home Revised'
106
+
107
+ page2.reload
108
+ page2.title.should == 'Home Revised'
109
+ end
110
+
111
+ should "be able to push with criteria and modifier hashes" do
112
+ page = @page_class.create(:title => 'Home')
113
+ page2 = @page_class.create(:title => 'Home')
114
+
115
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
116
+
117
+ page.reload
118
+ page.tags.should == %w(foo)
119
+
120
+ page2.reload
121
+ page.tags.should == %w(foo)
122
+ end
123
+
124
+ should "be able to push with ids and modifier hash" do
125
+ page = @page_class.create(:title => 'Home')
126
+ page2 = @page_class.create(:title => 'Home')
127
+
128
+ @page_class.push(page.id, page2.id, :tags => 'foo')
129
+
130
+ page.reload
131
+ page.tags.should == %w(foo)
132
+
133
+ page2.reload
134
+ page.tags.should == %w(foo)
135
+ end
136
+
137
+ should "be able to push all with criteria and modifier hashes" do
138
+ page = @page_class.create(:title => 'Home')
139
+ page2 = @page_class.create(:title => 'Home')
140
+ tags = %w(foo bar)
141
+
142
+ @page_class.push_all({:title => 'Home'}, :tags => tags)
143
+
144
+ page.reload
145
+ page.tags.should == tags
146
+
147
+ page2.reload
148
+ page.tags.should == tags
149
+ end
150
+
151
+ should "be able to push all with ids and modifier hash" do
152
+ page = @page_class.create(:title => 'Home')
153
+ page2 = @page_class.create(:title => 'Home')
154
+ tags = %w(foo bar)
155
+
156
+ @page_class.push_all(page.id, page2.id, :tags => tags)
157
+
158
+ page.reload
159
+ page.tags.should == tags
160
+
161
+ page2.reload
162
+ page.tags.should == tags
163
+ end
164
+
165
+ should "be able to pull with criteria and modifier hashes" do
166
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
167
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
168
+
169
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
170
+
171
+ page.reload
172
+ page.tags.should == %w(bar)
173
+
174
+ page2.reload
175
+ page.tags.should == %w(bar)
176
+ end
177
+
178
+ should "be able to pull with ids and modifier hash" do
179
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
180
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
181
+
182
+ @page_class.pull(page.id, page2.id, :tags => 'foo')
183
+
184
+ page.reload
185
+ page.tags.should == %w(bar)
186
+
187
+ page2.reload
188
+ page.tags.should == %w(bar)
189
+ end
190
+
191
+ should "be able to pull all with criteria and modifier hashes" do
192
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
193
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
194
+
195
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
196
+
197
+ page.reload
198
+ page.tags.should == %w(baz)
199
+
200
+ page2.reload
201
+ page.tags.should == %w(baz)
202
+ end
203
+
204
+ should "be able to pull all with ids and modifier hash" do
205
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
206
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
207
+
208
+ @page_class.pull_all(page.id, page2.id, :tags => %w(foo bar))
209
+
210
+ page.reload
211
+ page.tags.should == %w(baz)
212
+
213
+ page2.reload
214
+ page.tags.should == %w(baz)
215
+ end
216
+
217
+ should "be able to push uniq with criteria and modifier hash" do
218
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
219
+ page2 = @page_class.create(:title => 'Home')
220
+
221
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
222
+
223
+ page.reload
224
+ page.tags.should == %w(foo)
225
+
226
+ page2.reload
227
+ page.tags.should == %w(foo)
228
+ end
229
+
230
+ should "be able to push uniq with ids and modifier hash" do
231
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
232
+ page2 = @page_class.create(:title => 'Home')
233
+
234
+ @page_class.push_uniq(page.id, page2.id, :tags => 'foo')
235
+
236
+ page.reload
237
+ page.tags.should == %w(foo)
238
+
239
+ page2.reload
240
+ page.tags.should == %w(foo)
241
+ end
242
+ end
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ context "Paginating" do
5
+ setup do
6
+ @document = Class.new do
7
+ include MongoMapper::Document
8
+ set_collection_name 'users'
9
+
10
+ key :first_name, String
11
+ key :last_name, String
12
+ key :age, Integer
13
+
14
+ def self.per_page; 1 end
15
+ end
16
+ @document.collection.remove
17
+
18
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
19
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
20
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
21
+ end
22
+
23
+ should "return the total pages" do
24
+ result = @document.paginate(:per_page => 2, :page => 1)
25
+ result.total_pages.should == 2
26
+ end
27
+
28
+ should "return the total pages when defaulting to the document class per_page" do
29
+ result = @document.paginate(:page => 1)
30
+ result.total_pages.should == 3
31
+ end
32
+
33
+ should "return the total of records" do
34
+ result = @document.paginate(:per_page => 2, :page => 1)
35
+ result.total_entries.should == 3
36
+ end
37
+
38
+ should "return the items" do
39
+ result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
40
+ result.size.should == 2
41
+ result.should == [@doc1, @doc3]
42
+ end
43
+
44
+ should "accept conditions" do
45
+ result = @document.paginate({
46
+ :last_name => 'Nunemaker',
47
+ :order => "age DESC",
48
+ :per_page => 2,
49
+ :page => 1,
50
+ })
51
+ result.should == [@doc1, @doc3]
52
+ result.first.age.should == 27
53
+
54
+ result = @document.paginate({
55
+ :conditions => {:last_name => 'Nunemaker'},
56
+ :order => "age DESC",
57
+ :per_page => 2,
58
+ :page => 1} )
59
+ result.should == [@doc1, @doc3]
60
+ result.first.age.should == 27
61
+ end
62
+
63
+ should "withstand rigor" do
64
+ result = @document.paginate({
65
+ :per_page => 1,
66
+ :page => 1,
67
+ :order => 'age desc',
68
+ :last_name => 'Nunemaker'
69
+ })
70
+ result.should == [@doc1]
71
+ result.total_entries.should == 2
72
+ result.total_pages.should == 2
73
+
74
+ result = @document.paginate({
75
+ :per_page => 1,
76
+ :page => 2,
77
+ :order => 'age desc',
78
+ :last_name => 'Nunemaker'
79
+ })
80
+ result.should == [@doc3]
81
+ result.total_entries.should == 2
82
+ result.total_pages.should == 2
83
+
84
+ result = @document.paginate({
85
+ :per_page => 2,
86
+ :page => 1,
87
+ :order => 'age desc',
88
+ :last_name => 'Nunemaker'
89
+ })
90
+ result.should == [@doc1, @doc3]
91
+ result.total_entries.should == 2
92
+ result.total_pages.should == 1
93
+ end
94
+ end
95
+ end