mongo_mapper_ign 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +35 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +313 -0
  8. data/lib/mongo_mapper/embedded_document.rb +70 -0
  9. data/lib/mongo_mapper/plugins.rb +35 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +345 -0
  33. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  34. data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +91 -0
  41. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
  42. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +214 -0
  52. data/mongo_mapper_ign.gemspec +217 -0
  53. data/performance/read_write.rb +52 -0
  54. data/specs.watchr +51 -0
  55. data/test/NOTE_ON_TESTING +1 -0
  56. data/test/active_model_lint_test.rb +13 -0
  57. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  58. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  59. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  60. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  61. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  62. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  63. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  64. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  65. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  66. data/test/functional/associations/test_one_proxy.rb +196 -0
  67. data/test/functional/test_associations.rb +44 -0
  68. data/test/functional/test_binary.rb +27 -0
  69. data/test/functional/test_callbacks.rb +151 -0
  70. data/test/functional/test_dirty.rb +163 -0
  71. data/test/functional/test_document.rb +1219 -0
  72. data/test/functional/test_embedded_document.rb +210 -0
  73. data/test/functional/test_identity_map.rb +507 -0
  74. data/test/functional/test_indexing.rb +44 -0
  75. data/test/functional/test_logger.rb +20 -0
  76. data/test/functional/test_modifiers.rb +394 -0
  77. data/test/functional/test_pagination.rb +93 -0
  78. data/test/functional/test_protected.rb +163 -0
  79. data/test/functional/test_string_id_compatibility.rb +67 -0
  80. data/test/functional/test_timestamps.rb +64 -0
  81. data/test/functional/test_userstamps.rb +28 -0
  82. data/test/functional/test_validations.rb +342 -0
  83. data/test/models.rb +227 -0
  84. data/test/support/custom_matchers.rb +37 -0
  85. data/test/support/timing.rb +16 -0
  86. data/test/test_helper.rb +64 -0
  87. data/test/unit/associations/test_base.rb +212 -0
  88. data/test/unit/associations/test_proxy.rb +105 -0
  89. data/test/unit/serializers/test_json_serializer.rb +202 -0
  90. data/test/unit/test_descendant_appends.rb +71 -0
  91. data/test/unit/test_document.rb +225 -0
  92. data/test/unit/test_dynamic_finder.rb +123 -0
  93. data/test/unit/test_embedded_document.rb +657 -0
  94. data/test/unit/test_keys.rb +185 -0
  95. data/test/unit/test_mongo_mapper.rb +118 -0
  96. data/test/unit/test_pagination.rb +160 -0
  97. data/test/unit/test_plugins.rb +50 -0
  98. data/test/unit/test_query.rb +374 -0
  99. data/test/unit/test_rails.rb +181 -0
  100. data/test/unit/test_rails_compatibility.rb +52 -0
  101. data/test/unit/test_serialization.rb +51 -0
  102. data/test/unit/test_support.rb +382 -0
  103. data/test/unit/test_time_zones.rb +39 -0
  104. data/test/unit/test_validations.rb +544 -0
  105. metadata +327 -0
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class OneEmbeddedProxyTest < Test::Unit::TestCase
4
+ def setup
5
+ @post_class = Doc('Post') do
6
+ key :title, String
7
+ end
8
+ @author_class = EDoc('Author') do
9
+ key :name, String
10
+ embedded_in :post
11
+ end
12
+ end
13
+
14
+ should "default to nil" do
15
+ @post_class.one :author, :class => @author_class
16
+ @post_class.new.author.should be_nil
17
+ end
18
+
19
+ should "be able to build" do
20
+ @post_class.one :author, :class => @author_class
21
+
22
+ post = @post_class.create
23
+ author = post.author.build(:name => "John")
24
+ post.author.should be_instance_of(@author_class)
25
+ post.author.should be_new
26
+ post.author.name.should == 'John'
27
+ post.author.should == author
28
+ post.author.post.should == post
29
+ end
30
+
31
+ should "send object id to target" do
32
+ @post_class.one :author, :class => @author_class
33
+
34
+ post = @post_class.new
35
+ author = @author_class.new(:name => 'Frank')
36
+ post.author = author
37
+
38
+ post.author.object_id.should == post.author.target.object_id
39
+ end
40
+
41
+ should "be able to replace the association" do
42
+ @post_class.one :author, :class => @author_class
43
+
44
+ post = @post_class.new
45
+ author = @author_class.new(:name => 'Frank')
46
+ post.author = author
47
+ post.save
48
+ post.reload
49
+
50
+ post.author.should == author
51
+ post.author.nil?.should be_false
52
+
53
+ new_author = @author_class.new(:name => 'Emily')
54
+ post.author = new_author
55
+ post.author.should == new_author
56
+ end
57
+
58
+ should "have boolean method for testing presence" do
59
+ @post_class.one :author, :class => @author_class
60
+
61
+ post = @post_class.new
62
+ post.author?.should be_false
63
+
64
+ post.author = @author_class.new(:name => 'Frank')
65
+ post.author?.should be_true
66
+ end
67
+
68
+ end
@@ -0,0 +1,196 @@
1
+ require 'test_helper'
2
+
3
+ class OneProxyTest < Test::Unit::TestCase
4
+ def setup
5
+ @post_class = Doc('Post')
6
+ @author_class = Doc do
7
+ key :post_id, ObjectId
8
+ end
9
+ end
10
+
11
+ should "default to nil" do
12
+ @post_class.one :author, :class => @author_class
13
+ @post_class.new.author.nil?.should be_true
14
+ end
15
+
16
+ should "send object id to target" do
17
+ @post_class.one :author, :class => @author_class
18
+
19
+ post = @post_class.new
20
+ author = @author_class.new(:name => 'Frank')
21
+ post.author = author
22
+ author.save.should be_true
23
+ post.save.should be_true
24
+
25
+ post.author.object_id.should == post.author.target.object_id
26
+ end
27
+
28
+ should "allow assignment of associated document using a hash" do
29
+ @post_class.one :author, :class => @author_class
30
+
31
+ post = @post_class.new('author' => { 'name' => 'Frank' })
32
+ post.author.name.should == 'Frank'
33
+
34
+ post.save.should be_true
35
+ post.reload
36
+
37
+ post.author.name.should == 'Frank'
38
+ end
39
+
40
+ context "replacing the association" do
41
+ context "with an object of the class" do
42
+ should "work" do
43
+ @post_class.one :author, :class => @author_class
44
+
45
+ post = @post_class.new
46
+ author = @author_class.new(:name => 'Frank')
47
+ post.author = author
48
+ post.reload
49
+
50
+ post.author.should == author
51
+ post.author.nil?.should be_false
52
+
53
+ new_author = @author_class.new(:name => 'Emily')
54
+ post.author = new_author
55
+ post.author.should == new_author
56
+ end
57
+ end
58
+
59
+ context "with a Hash" do
60
+ should "convert to an object of the class and work" do
61
+ @post_class.one :author, :class => @author_class
62
+
63
+ post = @post_class.new
64
+ author = { 'name' => 'Frank' }
65
+ post.author = author
66
+ post.reload
67
+
68
+ post.author.name.should == 'Frank'
69
+ post.author.nil?.should be_false
70
+
71
+ new_author = { 'name' => 'Emily' }
72
+ post.author = new_author
73
+ post.author.name.should == 'Emily'
74
+ end
75
+ end
76
+ end
77
+
78
+ should "have boolean method for testing presence" do
79
+ @post_class.one :author, :class => @author_class
80
+
81
+ post = @post_class.new
82
+ post.author?.should be_false
83
+
84
+ post.author = @author_class.new(:name => 'Frank')
85
+ post.author?.should be_true
86
+ end
87
+
88
+ should "work with criteria" do
89
+ @post_class.one :primary_author, :class => @author_class, :primary => true
90
+ @post_class.one :author, :class => @author_class
91
+
92
+ post = @post_class.create
93
+ author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
94
+ primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
95
+ post.reload
96
+ post.author.should == author
97
+ post.primary_author.should == primary
98
+ end
99
+
100
+ should "unset the association" do
101
+ @post_class.one :author, :class => @author_class
102
+ post = @post_class.new
103
+ author = @author_class.new
104
+ post.author = author
105
+ post.reload
106
+
107
+ post.author = nil
108
+ post.author.nil?.should be_false
109
+ end
110
+
111
+ should "work with :dependent delete" do
112
+ @post_class.one :author, :class => @author_class, :dependent => :delete
113
+
114
+ post = @post_class.create
115
+ author = @author_class.new
116
+ post.author = author
117
+ post.reload
118
+
119
+ @author_class.any_instance.expects(:delete).once
120
+ post.author = @author_class.new
121
+ end
122
+
123
+ should "work with :dependent destroy" do
124
+ @post_class.one :author, :class => @author_class, :dependent => :destroy
125
+
126
+ post = @post_class.create
127
+ author = @author_class.new
128
+ post.author = author
129
+ post.reload
130
+
131
+ @author_class.any_instance.expects(:destroy).once
132
+ post.author = @author_class.new
133
+ end
134
+
135
+ should "work with :dependent nullify" do
136
+ @post_class.one :author, :class => @author_class, :dependent => :nullify
137
+
138
+ post = @post_class.create
139
+ author = @author_class.new
140
+ post.author = author
141
+ post.reload
142
+
143
+ post.author = @author_class.new
144
+
145
+ author.reload
146
+ author.post_id.should be_nil
147
+ end
148
+
149
+ should "be able to build" do
150
+ @post_class.one :author, :class => @author_class
151
+
152
+ post = @post_class.create
153
+ author = post.author.build(:name => 'John')
154
+ post.author.should be_instance_of(@author_class)
155
+ post.author.should be_new
156
+ post.author.name.should == 'John'
157
+ post.author.should == author
158
+ post.author.post_id.should == post.id
159
+ end
160
+
161
+ should "be able to create" do
162
+ @post_class.one :author, :class => @author_class
163
+
164
+ post = @post_class.create
165
+ author = post.author.create(:name => 'John')
166
+ post.author.should be_instance_of(@author_class)
167
+ post.author.should_not be_new
168
+ post.author.name.should == 'John'
169
+ post.author.should == author
170
+ post.author.post_id.should == post.id
171
+ end
172
+
173
+ context "#create!" do
174
+ setup do
175
+ @author_class.key :name, String, :required => true
176
+ @post_class.one :author, :class => @author_class
177
+ end
178
+
179
+ should "raise exception if invalid" do
180
+ post = @post_class.create
181
+ assert_raises(MongoMapper::DocumentNotValid) do
182
+ post.author.create!
183
+ end
184
+ end
185
+
186
+ should "work if valid" do
187
+ post = @post_class.create
188
+ author = post.author.create!(:name => 'John')
189
+ post.author.should be_instance_of(@author_class)
190
+ post.author.should_not be_new
191
+ post.author.name.should == 'John'
192
+ post.author.should == author
193
+ post.author.post_id.should == post.id
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class AssociationsTest < Test::Unit::TestCase
5
+ should "allow changing class names" do
6
+ class AwesomeUser
7
+ include MongoMapper::Document
8
+
9
+ many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
10
+ end
11
+ AwesomeUser.collection.remove
12
+
13
+ class AwesomeTag
14
+ include MongoMapper::EmbeddedDocument
15
+
16
+ key :name, String
17
+ key :post_id, ObjectId
18
+
19
+ belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
20
+ end
21
+
22
+ class AwesomePost
23
+ include MongoMapper::Document
24
+
25
+ key :creator_id, ObjectId
26
+
27
+ belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
28
+ many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
29
+ end
30
+
31
+ AwesomeUser.collection.remove
32
+ AwesomePost.collection.remove
33
+
34
+ user = AwesomeUser.create
35
+ tag1 = AwesomeTag.new(:name => 'awesome')
36
+ tag2 = AwesomeTag.new(:name => 'grand')
37
+ post1 = AwesomePost.create(:creator => user, :tags => [tag1])
38
+ post2 = AwesomePost.create(:creator => user, :tags => [tag2])
39
+ user.posts.should == [post1, post2]
40
+
41
+ post1 = post1.reload
42
+ post1.tags.should == [tag1]
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class BinaryTest < Test::Unit::TestCase
4
+ should "serialize and deserialize correctly" do
5
+ klass = Doc do
6
+ key :contents, Binary
7
+ end
8
+
9
+ doc = klass.new(:contents => '010101')
10
+ doc.save
11
+
12
+ doc = doc.reload
13
+ doc.contents.to_s.should == BSON::ByteBuffer.new('010101').to_s
14
+ end
15
+
16
+ context "Saving a document with a blank binary value" do
17
+ setup do
18
+ @document = Doc do
19
+ key :file, Binary
20
+ end
21
+ end
22
+
23
+ should "not fail" do
24
+ assert_nothing_raised { @document.new(:file => nil).save }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,151 @@
1
+ require 'test_helper'
2
+
3
+ module CallbacksSupport
4
+ def self.included base
5
+ base.key :name, String
6
+
7
+ [ :before_validation_on_create, :before_validation_on_update,
8
+ :before_validation, :after_validation,
9
+ :before_create, :after_create,
10
+ :before_update, :after_update,
11
+ :before_save, :after_save,
12
+ :before_destroy, :after_destroy].each do |callback|
13
+ callback_method = "#{callback}_callback"
14
+ base.send(callback, callback_method)
15
+ define_method(callback_method) do
16
+ history << callback.to_sym
17
+ end
18
+ end
19
+ end
20
+
21
+ def history
22
+ @history ||= []
23
+ end
24
+
25
+ def clear_history
26
+ embedded_associations.each { |a| self.send(a.name).each(&:clear_history) }
27
+ @history = nil
28
+ end
29
+ end
30
+
31
+ class CallbacksTest < Test::Unit::TestCase
32
+ CreateCallbackOrder = [:before_validation, :before_validation_on_create, :after_validation, :before_save, :before_create, :after_create, :after_save]
33
+ UpdateCallbackOrder = [:before_validation, :before_validation_on_update, :after_validation, :before_save, :before_update, :after_update, :after_save]
34
+
35
+ context "Defining and running callbacks on documents" do
36
+ setup do
37
+ @document = Doc { include CallbacksSupport }
38
+ end
39
+
40
+ should "get the order right for creating documents" do
41
+ doc = @document.create(:name => 'John Nunemaker')
42
+ doc.history.should == CreateCallbackOrder
43
+ end
44
+
45
+ should "get the order right for updating documents" do
46
+ doc = @document.create(:name => 'John Nunemaker')
47
+ doc.clear_history
48
+ doc.name = 'John'
49
+ doc.save
50
+ doc.history.should == UpdateCallbackOrder
51
+ end
52
+
53
+ should "work for before and after validation" do
54
+ doc = @document.new(:name => 'John Nunemaker')
55
+ doc.valid?
56
+ doc.history.should include(:before_validation)
57
+ doc.history.should include(:after_validation)
58
+ end
59
+
60
+ should "work for before and after create" do
61
+ doc = @document.create(:name => 'John Nunemaker')
62
+ doc.history.should include(:before_create)
63
+ doc.history.should include(:after_create)
64
+ end
65
+
66
+ should "work for before and after update" do
67
+ doc = @document.create(:name => 'John Nunemaker')
68
+ doc.name = 'John Doe'
69
+ doc.save
70
+ doc.history.should include(:before_update)
71
+ doc.history.should include(:after_update)
72
+ end
73
+
74
+ should "work for before and after save" do
75
+ doc = @document.new
76
+ doc.name = 'John Doe'
77
+ doc.save
78
+ doc.history.should include(:before_save)
79
+ doc.history.should include(:after_save)
80
+ end
81
+
82
+ should "work for before and after destroy" do
83
+ doc = @document.create(:name => 'John Nunemaker')
84
+ doc.destroy
85
+ doc.history.should include(:before_destroy)
86
+ doc.history.should include(:after_destroy)
87
+ end
88
+ end
89
+
90
+ context "Defining and running callbacks on many embedded documents" do
91
+ setup do
92
+ @root_class = Doc { include CallbacksSupport }
93
+ @child_class = EDoc { include CallbacksSupport }
94
+ @grand_child_class = EDoc { include CallbacksSupport }
95
+
96
+ @root_class.many :children, :class => @child_class
97
+ @child_class.many :children, :class => @grand_child_class
98
+ end
99
+
100
+ should "get the order right based on root document creation" do
101
+ grand = @grand_child_class.new(:name => 'Grand Child')
102
+ child = @child_class.new(:name => 'Child', :children => [grand])
103
+ root = @root_class.create(:name => 'Parent', :children => [child])
104
+
105
+ child = root.children.first
106
+ child.history.should == CreateCallbackOrder
107
+
108
+ grand = root.children.first.children.first
109
+ grand.history.should == CreateCallbackOrder
110
+ end
111
+
112
+ should "get the order right based on root document updating" do
113
+ grand = @grand_child_class.new(:name => 'Grand Child')
114
+ child = @child_class.new(:name => 'Child', :children => [grand])
115
+ root = @root_class.create(:name => 'Parent', :children => [child])
116
+ root.clear_history
117
+ root.update_attributes(:name => 'Updated Parent')
118
+
119
+ child = root.children.first
120
+ child.history.should == UpdateCallbackOrder
121
+
122
+ grand = root.children.first.children.first
123
+ grand.history.should == UpdateCallbackOrder
124
+ end
125
+
126
+ should "work for before and after destroy" do
127
+ grand = @grand_child_class.new(:name => 'Grand Child')
128
+ child = @child_class.new(:name => 'Child', :children => [grand])
129
+ root = @root_class.create(:name => 'Parent', :children => [child])
130
+ root.destroy
131
+ child = root.children.first
132
+ child.history.should include(:before_destroy)
133
+ child.history.should include(:after_destroy)
134
+
135
+ grand = root.children.first.children.first
136
+ grand.history.should include(:before_destroy)
137
+ grand.history.should include(:after_destroy)
138
+ end
139
+
140
+ should "not attempt to run callback defined on root that is not defined on embedded association" do
141
+ @root_class.define_callbacks :after_publish
142
+ @root_class.after_save { |d| d.run_callbacks(:after_publish) }
143
+
144
+ assert_nothing_raised do
145
+ child = @child_class.new(:name => 'Child')
146
+ root = @root_class.create(:name => 'Parent', :children => [child])
147
+ child.history.should_not include(:after_publish)
148
+ end
149
+ end
150
+ end
151
+ end