mongo_mapper 0.12.0 → 0.13.0.beta1

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 (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. data/test/unit/test_serialization.rb +0 -166
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Documents with the Rails plugin" do
4
+ let(:doc) { Doc {
5
+ key :foo, String
6
+ key :long_field, String, :alias => "lf"
7
+ }}
8
+
9
+ context "with values from the DB" do
10
+ subject { doc.create(:foo => "bar", :long_field => "long value") }
11
+ it "should have x_before_type_cast" do
12
+ subject.foo_before_type_cast.should == "bar"
13
+ end
14
+
15
+ it "should have x_before_type_cast for aliased fields" do
16
+ subject.long_field_before_type_cast.should == "long value"
17
+ end
18
+
19
+ it "should honor app-set values over DB-set values" do
20
+ subject.foo = nil
21
+ subject.foo_before_type_cast.should == nil
22
+ end
23
+ end
24
+
25
+ context "when blank" do
26
+ subject { doc.create() }
27
+ it "should have x_before_type_cast" do
28
+ subject.foo_before_type_cast.should == nil
29
+ end
30
+
31
+ it "should honor app-set values over DB-set values" do
32
+ subject.foo = nil
33
+ subject.foo_before_type_cast.should == nil
34
+
35
+ subject.foo = :baz
36
+ subject.foo_before_type_cast.should == :baz
37
+
38
+ subject.save
39
+ subject.reload.foo_before_type_cast.should == "baz"
40
+ end
41
+ end
42
+
43
+ context "#has_one" do
44
+ subject do
45
+ Doc do
46
+ has_one :foo
47
+ end
48
+ end
49
+
50
+ it "should create a one association" do
51
+ subject.associations.should have_key :foo
52
+ subject.associations[:foo].should be_a MongoMapper::Plugins::Associations::OneAssociation
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,163 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Safe" do
4
+ context "A Document" do
5
+ it "should default safe to off" do
6
+ Doc().should_not be_safe
7
+ end
8
+
9
+ it "should allow turning safe on" do
10
+ Doc() { safe }.should be_safe
11
+ end
12
+
13
+ context "inherited with safe setting on" do
14
+ it "should set subclass safe setting on" do
15
+ inherited = Class.new(Doc() { safe })
16
+ inherited.should be_safe
17
+ inherited.safe_options.should == true
18
+ end
19
+
20
+ it "should set subclass safe setting to same options hash as superclass" do
21
+ inherited = Class.new(Doc() { safe(:j => true) })
22
+ inherited.should be_safe
23
+ inherited.safe_options.should == {:j => true}
24
+ end
25
+ end
26
+
27
+ context "inherited with safe setting off" do
28
+ it "should leave subclass safe setting off" do
29
+ inherited = Class.new(Doc())
30
+ inherited.should_not be_safe
31
+ end
32
+ end
33
+ end
34
+
35
+ context "An unsafe document" do
36
+ before do
37
+ @klass = Doc do
38
+ safe(:w => 0)
39
+ end
40
+ end
41
+ after { drop_indexes(@klass) }
42
+
43
+ it "should not raise an error on duplicate IDs" do
44
+ k = @klass.create
45
+ expect { j = @klass.create(:_id => k.id) }.to_not raise_error
46
+ end
47
+ end
48
+
49
+ context "A safe document" do
50
+ before do
51
+ @klass = Doc() do
52
+ safe
53
+ end
54
+ end
55
+ after { drop_indexes(@klass) }
56
+
57
+ context "#save" do
58
+ before do
59
+ @klass.ensure_index :email, :unique => true
60
+ end
61
+
62
+ it "should raise an error on duplicate IDs" do
63
+ k = @klass.create
64
+ expect { j = @klass.create(:_id => k.id) }.to raise_error(Mongo::OperationFailure)
65
+ end
66
+
67
+ context "using safe setting from class" do
68
+ it "should pass :w => 1 option to save" do
69
+ instance = @klass.new(:email => 'john@doe.com')
70
+ Mongo::Collection.any_instance.should_receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:w => 1})
71
+ instance.save!
72
+ end
73
+
74
+ it "should work fine when all is well" do
75
+ expect {
76
+ @klass.new(:email => 'john@doe.com').save
77
+ }.to_not raise_error
78
+ end
79
+
80
+ it "should raise error when operation fails" do
81
+ expect {
82
+ 2.times do
83
+ @klass.new(:email => 'john@doe.com').save
84
+ end
85
+ }.to raise_error(Mongo::OperationFailure)
86
+ end
87
+ end
88
+
89
+ context "overriding safe setting" do
90
+ it "should raise error if safe is true" do
91
+ expect {
92
+ 2.times do
93
+ @klass.new(:email => 'john@doe.com').save(:safe => true)
94
+ end
95
+ }.to raise_error(Mongo::OperationFailure)
96
+ end
97
+
98
+ it "should not raise error if safe is false" do
99
+ expect {
100
+ 2.times do
101
+ @klass.new(:email => 'john@doe.com').save(:safe => false)
102
+ end
103
+ }.to_not raise_error
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ context "a safe document with options hash" do
110
+ before do
111
+ @klass = Doc() do
112
+ safe(:j => true)
113
+ end
114
+ end
115
+ after { drop_indexes(@klass) }
116
+
117
+ context "#save" do
118
+ before do
119
+ @klass.ensure_index :email, :unique => true
120
+ end
121
+
122
+ context "using safe setting from class" do
123
+ it "should pass :safe => options_hash to save" do
124
+ instance = @klass.new(:email => 'john@doe.com')
125
+ Mongo::Collection.any_instance.should_receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:j => true})
126
+ instance.save!
127
+ end
128
+
129
+ it "should work fine when all is well" do
130
+ expect {
131
+ @klass.new(:email => 'john@doe.com').save
132
+ }.to_not raise_error
133
+ end
134
+
135
+ it "should raise error when operation fails" do
136
+ expect {
137
+ 2.times do
138
+ @klass.new(:email => 'john@doe.com').save
139
+ end
140
+ }.to raise_error(Mongo::OperationFailure)
141
+ end
142
+ end
143
+
144
+ context "overriding safe setting" do
145
+ it "should raise error if safe is true" do
146
+ expect {
147
+ 2.times do
148
+ @klass.new(:email => 'john@doe.com').save(:safe => true)
149
+ end
150
+ }.to raise_error(Mongo::OperationFailure)
151
+ end
152
+
153
+ it "should not raise error if safe is false" do
154
+ expect {
155
+ 2.times do
156
+ @klass.new(:email => 'john@doe.com').save(:safe => false)
157
+ end
158
+ }.to_not raise_error
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -1,8 +1,28 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
+
3
+ describe "Single collection inheritance (document)" do
4
+ context "without a connection", :without_connection => true do
5
+ it "should attempt to create a connection during inheritance" do
6
+ Mongo::MongoClient.should_not_receive(:new)
7
+ doc = Class.new
8
+ doc.send(:include, MongoMapper::Document)
9
+ expect {
10
+ Class.new(doc)
11
+ }.to_not raise_error
12
+ end
2
13
 
3
- class SciTest < Test::Unit::TestCase
4
- context "Single collection inheritance (document)" do
5
- setup do
14
+ it "should pick up a connection if one wasn't set" do
15
+ doc = Class.new
16
+ doc.send(:include, MongoMapper::Document)
17
+ klass = Class.new(doc)
18
+ klass.connection.should be_nil
19
+ MongoMapper.connection
20
+ klass.connection.should be_a Mongo::MongoClient
21
+ end
22
+ end
23
+
24
+ context "with a connection" do
25
+ before do
6
26
  class ::DocParent
7
27
  include MongoMapper::Document
8
28
  key :name, String
@@ -20,7 +40,7 @@ class SciTest < Test::Unit::TestCase
20
40
  @daughter = DocDaughter.new({:name => "Little Orphan Annie"})
21
41
  end
22
42
 
23
- teardown do
43
+ after do
24
44
  Object.send :remove_const, 'DocParent' if defined?(::DocParent)
25
45
  Object.send :remove_const, 'DocDaughter' if defined?(::DocDaughter)
26
46
  Object.send :remove_const, 'DocSon' if defined?(::DocSon)
@@ -28,15 +48,55 @@ class SciTest < Test::Unit::TestCase
28
48
  Object.send :remove_const, 'DocGrandGrandSon' if defined?(::DocGrandGrandSon)
29
49
  end
30
50
 
31
- should "automatically add _type key to store class" do
51
+ it "should automatically add _type key to store class" do
32
52
  DocParent.key?(:_type).should be_true
33
53
  end
34
54
 
35
- should "use the same collection in the subclass" do
55
+ it "should use modifiers properly" do
56
+ DocDaughter.increment({:title => 'Home'}, {:day_count => 1}, :upsert => true)
57
+ DocDaughter.first.should_not be_nil
58
+ DocDaughter.first._type.should == "DocDaughter"
59
+ end
60
+
61
+ it "should use the same connection in the subclass" do
62
+ parent_class = Class.new do
63
+ include MongoMapper::Document
64
+ connection Mongo::MongoClient.new
65
+ end
66
+
67
+ child_class = Class.new(parent_class) do
68
+ include MongoMapper::Document
69
+ end
70
+
71
+ child_class.connection.should == child_class.connection
72
+ end
73
+
74
+ it "should use the same database in the subclass" do
75
+ parent_class = Class.new do
76
+ include MongoMapper::Document
77
+ set_database_name 'something'
78
+ end
79
+
80
+ child_class = Class.new(parent_class) do
81
+ include MongoMapper::Document
82
+ end
83
+
84
+ child_class.database.name.should == 'something'
85
+ end
86
+
87
+ it "should use the same collection in the subclass" do
36
88
  DocDaughter.collection.name.should == DocParent.collection.name
37
89
  end
38
90
 
39
- should "know single_collection_parent" do
91
+ it "should negate SCI if the subclass changes its collection" do
92
+ klass = Class.new(DocParent) do
93
+ set_collection_name "foobars"
94
+ end
95
+ klass.collection.name.should == "foobars"
96
+ klass.should_not be_single_collection_inherited
97
+ end
98
+
99
+ it "should know single_collection_parent" do
40
100
  DocParent.single_collection_parent.should be_nil
41
101
  DocDaughter.single_collection_parent.should == DocParent
42
102
  DocSon.single_collection_parent.should == DocParent
@@ -44,7 +104,7 @@ class SciTest < Test::Unit::TestCase
44
104
  DocGrandGrandSon.single_collection_parent.should == DocGrandSon
45
105
  end
46
106
 
47
- should "know single_collection_root" do
107
+ it "should know single_collection_root" do
48
108
  DocParent.single_collection_root.should == DocParent
49
109
  DocDaughter.single_collection_root.should == DocParent
50
110
  DocSon.single_collection_root.should == DocParent
@@ -53,29 +113,29 @@ class SciTest < Test::Unit::TestCase
53
113
  end
54
114
 
55
115
  context ".single_collection_inherited?" do
56
- should "be false if has not inherited" do
116
+ it "should be false if has not inherited" do
57
117
  DocParent.should_not be_single_collection_inherited
58
118
  end
59
119
 
60
- should "be true if inherited" do
120
+ it "should be true if inherited" do
61
121
  DocDaughter.should be_single_collection_inherited
62
122
  DocSon.should be_single_collection_inherited
63
123
  DocGrandSon.should be_single_collection_inherited
64
124
  end
65
125
  end
66
126
 
67
- should "set _type on initialize" do
127
+ it "should set _type on initialize" do
68
128
  DocDaughter.new._type.should == 'DocDaughter'
69
129
  DocSon.new._type.should == 'DocSon'
70
130
  DocGrandSon.new._type.should == 'DocGrandSon'
71
131
  end
72
132
 
73
- should "set _type based on class and ignore assigned values" do
133
+ it "should set _type based on class and ignore assigned values" do
74
134
  DocSon.new(:_type => 'DocDaughter')._type.should == 'DocSon'
75
135
  end
76
136
 
77
137
  context "loading" do
78
- should "be based on _type" do
138
+ it "should be based on _type" do
79
139
  @parent.save
80
140
  @daughter.save
81
141
 
@@ -87,7 +147,7 @@ class SciTest < Test::Unit::TestCase
87
147
  collection.last.name.should == "Little Orphan Annie"
88
148
  end
89
149
 
90
- should "gracefully handle when _type cannot be constantized" do
150
+ it "should gracefully handle when _type cannot be constantized" do
91
151
  doc = DocParent.new(:name => 'Nunes')
92
152
  doc._type = 'FoobarBaz'
93
153
  doc.save
@@ -99,7 +159,7 @@ class SciTest < Test::Unit::TestCase
99
159
  end
100
160
 
101
161
  context "querying" do
102
- should "find scoped to class" do
162
+ it "should find scoped to class" do
103
163
  john = DocSon.create(:name => 'John')
104
164
  steve = DocSon.create(:name => 'Steve')
105
165
  steph = DocDaughter.create(:name => 'Steph')
@@ -119,13 +179,13 @@ class SciTest < Test::Unit::TestCase
119
179
  DocParent.all(:order => 'name').should == [boris, carrie, john, sigmund, steph, steve]
120
180
  end
121
181
 
122
- should "work with nested hash conditions" do
182
+ it "should work with nested hash conditions" do
123
183
  john = DocSon.create(:name => 'John')
124
184
  steve = DocSon.create(:name => 'Steve')
125
185
  DocSon.all(:name => {'$ne' => 'Steve'}).should == [john]
126
186
  end
127
187
 
128
- should "raise error if not found scoped to class" do
188
+ it "should raise error if not found scoped to class" do
129
189
  john = DocSon.create(:name => 'John')
130
190
  steph = DocDaughter.create(:name => 'Steph')
131
191
 
@@ -134,13 +194,13 @@ class SciTest < Test::Unit::TestCase
134
194
  }.should raise_error(MongoMapper::DocumentNotFound)
135
195
  end
136
196
 
137
- should "not raise error for find with parent" do
197
+ it "should not raise error for find with parent" do
138
198
  john = DocSon.create(:name => 'John')
139
199
 
140
200
  DocParent.find!(john._id).should == john
141
201
  end
142
202
 
143
- should "count scoped to class" do
203
+ it "should count scoped to class" do
144
204
  john = DocSon.create(:name => 'John')
145
205
  steve = DocSon.create(:name => 'Steve')
146
206
  steph = DocDaughter.create(:name => 'Steph')
@@ -152,7 +212,7 @@ class SciTest < Test::Unit::TestCase
152
212
  DocParent.count.should == 4
153
213
  end
154
214
 
155
- should "not be able to destroy each other" do
215
+ it "should not be able to destroy each other" do
156
216
  john = DocSon.create(:name => 'John')
157
217
  steph = DocDaughter.create(:name => 'Steph')
158
218
 
@@ -161,7 +221,7 @@ class SciTest < Test::Unit::TestCase
161
221
  }.should raise_error(MongoMapper::DocumentNotFound)
162
222
  end
163
223
 
164
- should "not be able to delete each other" do
224
+ it "should not be able to delete each other" do
165
225
  john = DocSon.create(:name => 'John')
166
226
  steph = DocDaughter.create(:name => 'Steph')
167
227
 
@@ -170,7 +230,7 @@ class SciTest < Test::Unit::TestCase
170
230
  }.should_not change { DocParent.count }
171
231
  end
172
232
 
173
- should "be able to destroy using parent" do
233
+ it "should be able to destroy using parent" do
174
234
  john = DocSon.create(:name => 'John')
175
235
  steph = DocDaughter.create(:name => 'Steph')
176
236
 
@@ -179,7 +239,7 @@ class SciTest < Test::Unit::TestCase
179
239
  }.should change { DocParent.count }.by(-2)
180
240
  end
181
241
 
182
- should "be able to delete using parent" do
242
+ it "should be able to delete using parent" do
183
243
  john = DocSon.create(:name => 'John')
184
244
  steph = DocDaughter.create(:name => 'Steph')
185
245
 
@@ -189,7 +249,7 @@ class SciTest < Test::Unit::TestCase
189
249
  end
190
250
  end
191
251
 
192
- should "be able to reload single collection inherited parent class" do
252
+ it "should be able to reload single collection inherited parent class" do
193
253
  brian = DocParent.create(:name => 'Brian')
194
254
  brian.name = 'B-Dawg'
195
255
  brian.reload
@@ -197,8 +257,8 @@ class SciTest < Test::Unit::TestCase
197
257
  end
198
258
  end
199
259
 
200
- context "Single collection inheritance (embedded document)" do
201
- setup do
260
+ describe "Single collection inheritance (embedded document)" do
261
+ before do
202
262
  class ::Grandparent
203
263
  include MongoMapper::EmbeddedDocument
204
264
  key :grandparent, String
@@ -220,37 +280,66 @@ class SciTest < Test::Unit::TestCase
220
280
  end
221
281
  end
222
282
 
223
- teardown do
283
+ after do
224
284
  Object.send :remove_const, 'Grandparent' if defined?(::Grandparent)
225
285
  Object.send :remove_const, 'Parent' if defined?(::Parent)
226
286
  Object.send :remove_const, 'Child' if defined?(::Child)
227
287
  Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
228
288
  end
229
289
 
230
- should "automatically add _type key" do
290
+ it "should automatically add _type key" do
231
291
  Grandparent.key?(:_type).should be_true
232
292
  end
233
293
 
234
294
  context ".single_collection_inherited?" do
235
- should "be false if has not inherited" do
295
+ it "should be false if has not inherited" do
236
296
  Grandparent.should_not be_single_collection_inherited
237
297
  end
238
298
 
239
- should "be true if inherited" do
299
+ it "should be true if inherited" do
240
300
  Parent.should be_single_collection_inherited
241
301
  Child.should be_single_collection_inherited
242
302
  OtherChild.should be_single_collection_inherited
243
303
  end
244
304
  end
245
305
 
246
- should "set _type on initialize" do
306
+ it "should set _type on initialize" do
247
307
  Parent.new._type.should == 'Parent'
248
308
  Child.new._type.should == 'Child'
249
309
  OtherChild.new._type.should == 'OtherChild'
250
310
  end
251
311
 
252
- should "set _type based on class and ignore assigned values" do
312
+ it "should set _type based on class and ignore assigned values" do
253
313
  Child.new(:_type => 'OtherChild')._type.should == 'Child'
254
314
  end
255
315
  end
256
- end
316
+
317
+ describe "With polymorphism" do
318
+ before :all do
319
+ class SciPolymorphicPost
320
+ include MongoMapper::Document
321
+ belongs_to :article_parent, :polymorphic => true
322
+ end
323
+
324
+ class GalleryItem
325
+ include MongoMapper::Document
326
+ belongs_to :gallery_album
327
+ key :text, Hash
328
+ timestamps!
329
+ end
330
+
331
+ class TextGalleryItem < GalleryItem;
332
+ many :sci_polymorphic_posts, :as => :article_parent
333
+ end
334
+ end
335
+
336
+ it "should find polymorphic SCI items" do
337
+ item = TextGalleryItem.new()
338
+ p = SciPolymorphicPost.create(:article_parent => item)
339
+ p.article_parent_id.should be_a BSON::ObjectId
340
+ p.article_parent_type.should == "TextGalleryItem"
341
+
342
+ p.reload.article_parent.sci_polymorphic_posts.all.should include(p)
343
+ end
344
+ end
345
+ end