jonbell-mongo_mapper 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +33 -0
  3. data/UPGRADES +7 -0
  4. data/bin/mmconsole +60 -0
  5. data/examples/attr_accessible.rb +22 -0
  6. data/examples/attr_protected.rb +22 -0
  7. data/examples/cache_key.rb +24 -0
  8. data/examples/custom_types.rb +24 -0
  9. data/examples/identity_map.rb +33 -0
  10. data/examples/identity_map/automatic.rb +8 -0
  11. data/examples/identity_map/middleware.rb +14 -0
  12. data/examples/keys.rb +40 -0
  13. data/examples/modifiers/set.rb +25 -0
  14. data/examples/plugins.rb +41 -0
  15. data/examples/querying.rb +35 -0
  16. data/examples/safe.rb +43 -0
  17. data/examples/scopes.rb +52 -0
  18. data/examples/validating/embedded_docs.rb +29 -0
  19. data/lib/mongo_mapper.rb +79 -0
  20. data/lib/mongo_mapper/connection.rb +83 -0
  21. data/lib/mongo_mapper/document.rb +41 -0
  22. data/lib/mongo_mapper/embedded_document.rb +31 -0
  23. data/lib/mongo_mapper/exceptions.rb +27 -0
  24. data/lib/mongo_mapper/extensions/array.rb +19 -0
  25. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  26. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  27. data/lib/mongo_mapper/extensions/date.rb +25 -0
  28. data/lib/mongo_mapper/extensions/float.rb +14 -0
  29. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  30. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  31. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  32. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  33. data/lib/mongo_mapper/extensions/object.rb +27 -0
  34. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  35. data/lib/mongo_mapper/extensions/set.rb +20 -0
  36. data/lib/mongo_mapper/extensions/string.rb +18 -0
  37. data/lib/mongo_mapper/extensions/time.rb +29 -0
  38. data/lib/mongo_mapper/plugins.rb +15 -0
  39. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  40. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  41. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  42. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  43. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  44. data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
  45. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  46. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  47. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  48. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -0
  49. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  50. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  51. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  52. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  53. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  54. data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
  55. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  56. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  57. data/lib/mongo_mapper/plugins/clone.rb +22 -0
  58. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  59. data/lib/mongo_mapper/plugins/dirty.rb +124 -0
  60. data/lib/mongo_mapper/plugins/document.rb +41 -0
  61. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  62. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  63. data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
  64. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  65. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  66. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  67. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  68. data/lib/mongo_mapper/plugins/keys.rb +313 -0
  69. data/lib/mongo_mapper/plugins/keys/key.rb +59 -0
  70. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  71. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  72. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  73. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  74. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  75. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  76. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  77. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  78. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  79. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  80. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  81. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  82. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  83. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  84. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  85. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  86. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  87. data/lib/mongo_mapper/version.rb +4 -0
  88. data/rails/init.rb +19 -0
  89. data/test/_NOTE_ON_TESTING +1 -0
  90. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  91. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  92. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  93. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  94. data/test/functional/associations/test_many_documents_proxy.rb +615 -0
  95. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  96. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  97. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  98. data/test/functional/associations/test_one_embedded_proxy.rb +81 -0
  99. data/test/functional/associations/test_one_proxy.rb +181 -0
  100. data/test/functional/test_accessible.rb +168 -0
  101. data/test/functional/test_associations.rb +44 -0
  102. data/test/functional/test_binary.rb +27 -0
  103. data/test/functional/test_caching.rb +76 -0
  104. data/test/functional/test_callbacks.rb +151 -0
  105. data/test/functional/test_dirty.rb +163 -0
  106. data/test/functional/test_document.rb +253 -0
  107. data/test/functional/test_dynamic_querying.rb +75 -0
  108. data/test/functional/test_embedded_document.rb +210 -0
  109. data/test/functional/test_identity_map.rb +514 -0
  110. data/test/functional/test_indexes.rb +42 -0
  111. data/test/functional/test_logger.rb +20 -0
  112. data/test/functional/test_modifiers.rb +416 -0
  113. data/test/functional/test_pagination.rb +91 -0
  114. data/test/functional/test_protected.rb +175 -0
  115. data/test/functional/test_querying.rb +873 -0
  116. data/test/functional/test_safe.rb +76 -0
  117. data/test/functional/test_sci.rb +230 -0
  118. data/test/functional/test_scopes.rb +171 -0
  119. data/test/functional/test_string_id_compatibility.rb +67 -0
  120. data/test/functional/test_timestamps.rb +62 -0
  121. data/test/functional/test_userstamps.rb +27 -0
  122. data/test/functional/test_validations.rb +342 -0
  123. data/test/models.rb +233 -0
  124. data/test/test_active_model_lint.rb +13 -0
  125. data/test/test_helper.rb +100 -0
  126. data/test/unit/associations/test_base.rb +212 -0
  127. data/test/unit/associations/test_proxy.rb +105 -0
  128. data/test/unit/serializers/test_json_serializer.rb +217 -0
  129. data/test/unit/test_clone.rb +69 -0
  130. data/test/unit/test_descendant_appends.rb +71 -0
  131. data/test/unit/test_document.rb +213 -0
  132. data/test/unit/test_dynamic_finder.rb +125 -0
  133. data/test/unit/test_embedded_document.rb +644 -0
  134. data/test/unit/test_extensions.rb +376 -0
  135. data/test/unit/test_key.rb +185 -0
  136. data/test/unit/test_keys.rb +89 -0
  137. data/test/unit/test_mongo_mapper.rb +110 -0
  138. data/test/unit/test_pagination.rb +11 -0
  139. data/test/unit/test_plugins.rb +50 -0
  140. data/test/unit/test_rails.rb +181 -0
  141. data/test/unit/test_rails_compatibility.rb +52 -0
  142. data/test/unit/test_serialization.rb +51 -0
  143. data/test/unit/test_time_zones.rb +39 -0
  144. data/test/unit/test_validations.rb +564 -0
  145. metadata +337 -0
@@ -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::Binary.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,76 @@
1
+ require 'test_helper'
2
+
3
+ class CachingTest < Test::Unit::TestCase
4
+ context "Caching" do
5
+ setup do
6
+ @klass = Class.new do
7
+ extend MongoMapper::Plugins
8
+ plugin MongoMapper::Plugins::Caching
9
+ end
10
+ @klass.stubs(:name).returns('Post')
11
+ @klass.any_instance.stubs(:[]).returns(nil)
12
+ @klass.any_instance.stubs(:[]=).returns(nil)
13
+ end
14
+
15
+ context "new" do
16
+ setup do
17
+ @doc = @klass.new
18
+ @doc.stubs(:new?).returns(true)
19
+ end
20
+
21
+ should "be class/new" do
22
+ @doc.cache_key.should == 'Post/new'
23
+ end
24
+
25
+ should "work with suffix" do
26
+ @doc.cache_key(:foo).
27
+ should == 'Post/new/foo'
28
+
29
+ @doc.cache_key(:foo, :bar).
30
+ should == 'Post/new/foo/bar'
31
+ end
32
+ end
33
+
34
+ context "not new" do
35
+ setup do
36
+ @object_id = BSON::ObjectId.new
37
+ @doc = @klass.new
38
+ @doc.stubs(:new?).returns(false)
39
+ @doc.stubs(:id).returns(@object_id)
40
+ end
41
+
42
+ context "with updated_at" do
43
+ setup do
44
+ time = Time.utc(2010, 6, 20, 8, 10, 7)
45
+ @doc.stubs(:[]).with(:updated_at).returns(time)
46
+ end
47
+
48
+ should "be class/id-timestamp" do
49
+ @doc.cache_key.should == "Post/#{@object_id}-20100620081007"
50
+ end
51
+
52
+ should "work with suffix" do
53
+ @doc.cache_key(:foo).
54
+ should == "Post/#{@object_id}-20100620081007/foo"
55
+
56
+ @doc.cache_key(:foo, :bar).
57
+ should == "Post/#{@object_id}-20100620081007/foo/bar"
58
+ end
59
+ end
60
+
61
+ context "without updated_at" do
62
+ should "be class/id" do
63
+ @doc.cache_key.should == "Post/#{@object_id}"
64
+ end
65
+
66
+ should "work with suffix" do
67
+ @doc.cache_key(:foo).
68
+ should == "Post/#{@object_id}/foo"
69
+
70
+ @doc.cache_key(:foo, :bar, :baz).
71
+ should == "Post/#{@object_id}/foo/bar/baz"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ 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
@@ -0,0 +1,163 @@
1
+ require 'test_helper'
2
+
3
+ class DirtyTest < Test::Unit::TestCase
4
+ def setup
5
+ @document = Doc { key :phrase, String }
6
+ end
7
+
8
+ context "marking changes" do
9
+ should "not happen if there are none" do
10
+ doc = @document.new
11
+ doc.phrase_changed?.should be_false
12
+ doc.phrase_change.should be_nil
13
+ end
14
+
15
+ should "happen when change happens" do
16
+ doc = @document.new
17
+ doc.phrase = 'Golly Gee Willikers Batman'
18
+ doc.phrase_changed?.should be_true
19
+ doc.phrase_was.should be_nil
20
+ doc.phrase_change.should == [nil, 'Golly Gee Willikers Batman']
21
+ end
22
+
23
+ should "happen when initializing" do
24
+ doc = @document.new(:phrase => 'Foo')
25
+ doc.changed?.should be_true
26
+ end
27
+
28
+ should "clear changes on save" do
29
+ doc = @document.new
30
+ doc.phrase = 'Golly Gee Willikers Batman'
31
+ doc.phrase_changed?.should be_true
32
+ doc.save
33
+ doc.phrase_changed?.should_not be_true
34
+ doc.phrase_change.should be_nil
35
+ end
36
+
37
+ should "clear changes on save!" do
38
+ doc = @document.new
39
+ doc.phrase = 'Golly Gee Willikers Batman'
40
+ doc.phrase_changed?.should be_true
41
+ doc.save!
42
+ doc.phrase_changed?.should_not be_true
43
+ doc.phrase_change.should be_nil
44
+ end
45
+
46
+ should "not happen when loading from database" do
47
+ doc = @document.create(:phrase => 'Foo')
48
+ doc = @document.find(doc.id)
49
+
50
+ doc.changed?.should be_false
51
+ doc.phrase = 'Fart'
52
+ doc.changed?.should be_true
53
+ doc.reload
54
+ doc.changed?.should be_false
55
+ end
56
+
57
+ should "happen if changed after loading from database" do
58
+ doc = @document.create(:phrase => 'Foo')
59
+ doc.reload
60
+ doc.changed?.should be_false
61
+ doc.phrase = 'Bar'
62
+ doc.changed?.should be_true
63
+ end
64
+ end
65
+
66
+ context "blank new value and type integer" do
67
+ should "not mark changes" do
68
+ @document.key :age, Integer
69
+
70
+ [nil, ''].each do |value|
71
+ doc = @document.new
72
+ doc.age = value
73
+ doc.age_changed?.should be_false
74
+ doc.age_change.should be_nil
75
+ end
76
+ end
77
+ end
78
+
79
+ context "blank new value and type float" do
80
+ should "not mark changes" do
81
+ @document.key :amount, Float
82
+
83
+ [nil, ''].each do |value|
84
+ doc = @document.new
85
+ doc.amount = value
86
+ doc.amount_changed?.should be_false
87
+ doc.amount_change.should be_nil
88
+ end
89
+ end
90
+ end
91
+
92
+ context "changed?" do
93
+ should "be true if key changed" do
94
+ doc = @document.new
95
+ doc.phrase = 'A penny saved is a penny earned.'
96
+ doc.changed?.should be_true
97
+ end
98
+
99
+ should "be false if no keys changed" do
100
+ @document.new.changed?.should be_false
101
+ end
102
+ end
103
+
104
+ context "changes" do
105
+ should "be empty hash if no changes" do
106
+ @document.new.changes.should == {}
107
+ end
108
+
109
+ should "be hash of keys with values of changes if there are changes" do
110
+ doc = @document.new
111
+ doc.phrase = 'A penny saved is a penny earned.'
112
+ doc.changes['phrase'].should == [nil, 'A penny saved is a penny earned.']
113
+ end
114
+ end
115
+
116
+ context "changed" do
117
+ should "be empty array if no changes" do
118
+ @document.new.changed.should == []
119
+ end
120
+
121
+ should "be array of keys that have changed if there are changes" do
122
+ doc = @document.new
123
+ doc.phrase = 'A penny saved is a penny earned.'
124
+ doc.changed.should == ['phrase']
125
+ end
126
+ end
127
+
128
+ context "will_change!" do
129
+ should "mark changes" do
130
+ doc = @document.create(:phrase => 'Foo')
131
+
132
+ doc.phrase << 'bar'
133
+ doc.phrase_changed?.should be_false
134
+
135
+ doc.phrase_will_change!
136
+ doc.phrase_changed?.should be_true
137
+ doc.phrase_change.should == ['Foobar', 'Foobar']
138
+
139
+ doc.phrase << '!'
140
+ doc.phrase_changed?.should be_true
141
+ doc.phrase_change.should == ['Foobar', 'Foobar!']
142
+ end
143
+ end
144
+
145
+ context "changing a foreign key through association" do
146
+ should "mark changes" do
147
+ project_class = Doc do
148
+ key :name, String
149
+ end
150
+
151
+ milestone_class = Doc do
152
+ key :project_id, ObjectId
153
+ key :name, String
154
+ end
155
+ milestone_class.belongs_to :project, :class => project_class
156
+
157
+ milestone = milestone_class.create(:name => 'Launch')
158
+ milestone.project = project_class.create(:name => 'Harmony')
159
+ milestone.changed?.should be_true
160
+ milestone.changed.should == %w(project_id)
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,253 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class DocumentTest < Test::Unit::TestCase
5
+ def setup
6
+ @document = Doc do
7
+ key :first_name, String
8
+ key :last_name, String
9
+ key :age, Integer
10
+ key :date, Date
11
+ end
12
+ end
13
+
14
+ context "array key" do
15
+ setup do
16
+ @document.key :tags, Array
17
+ end
18
+
19
+ should "give correct default" do
20
+ doc = @document.new
21
+ doc.tags.should == []
22
+ end
23
+
24
+ should "work with assignment" do
25
+ doc = @document.new
26
+ doc.tags = %w(foo bar)
27
+ doc.tags.should == %w(foo bar)
28
+ end
29
+
30
+ should "work with assignment after saving" do
31
+ doc = @document.new
32
+ doc.tags = %w(foo bar)
33
+ doc.save
34
+ doc.tags.should == %w(foo bar)
35
+ doc.reload.tags.should == %w(foo bar)
36
+ end
37
+
38
+ should "work with assignment then <<" do
39
+ doc = @document.new
40
+ doc.tags = []
41
+ doc.tags << "foo"
42
+ doc.tags.should == ["foo"]
43
+ end
44
+
45
+ should "work with <<" do
46
+ doc = @document.new
47
+ doc.tags << "foo"
48
+ doc.tags.should == ["foo"]
49
+ end
50
+
51
+ should "work with << then save" do
52
+ doc = @document.new
53
+ doc.tags << "foo"
54
+ doc.tags << "bar"
55
+ doc.save
56
+ doc.tags.should == %w(foo bar)
57
+ doc.reload.tags.should == %w(foo bar)
58
+ end
59
+ end
60
+
61
+ context "hash key" do
62
+ setup do
63
+ @document.key :foo, Hash
64
+ end
65
+
66
+ should "give correct default" do
67
+ doc = @document.new
68
+ doc.foo.should == {}
69
+ end
70
+
71
+ should "work with []=" do
72
+ doc = @document.new
73
+ doc.foo["quux"] = "bar"
74
+ doc.foo["quux"].should == "bar"
75
+ doc.foo.should == { "quux" => "bar" }
76
+ end
77
+
78
+ should "work with indifferent access" do
79
+ doc = @document.new
80
+ doc.foo = {:baz => 'bar'}
81
+ doc.foo[:baz].should == 'bar'
82
+ doc.foo['baz'].should == 'bar'
83
+ end
84
+
85
+ should "work with indifferent access after save" do
86
+ doc = @document.new
87
+ doc.foo = {:baz => 'bar'}
88
+ doc.save
89
+
90
+ doc = doc.reload
91
+ doc.foo[:baz].should == 'bar'
92
+ doc.foo['baz'].should == 'bar'
93
+ end
94
+ end
95
+
96
+ context "custom type key with default" do
97
+ setup do
98
+ @document.key :window, WindowSize, :default => WindowSize.new(600, 480)
99
+ end
100
+
101
+ should "default to default" do
102
+ doc = @document.new
103
+ doc.window.should == WindowSize.new(600, 480)
104
+
105
+ end
106
+
107
+ should "save and load from mongo" do
108
+ doc = @document.new
109
+ doc.save
110
+
111
+ doc = doc.reload
112
+ doc.window.should == WindowSize.new(600, 480)
113
+ end
114
+ end
115
+
116
+ context "key with proc default value" do
117
+ setup do
118
+ @document.key :proc_default, String, :default => lambda { return 'string' }
119
+ end
120
+
121
+ should "detect and run proc default" do
122
+ doc = @document.new
123
+ doc.proc_default.should == 'string'
124
+ end
125
+
126
+ should "save and load from mongo" do
127
+ doc = @document.create
128
+ doc = doc.reload
129
+ doc.proc_default.should == 'string'
130
+ end
131
+ end
132
+
133
+ should "have instance method for collection" do
134
+ @document.new.collection.name.should == @document.collection.name
135
+ end
136
+
137
+ should "have instance method for database" do
138
+ @document.new.database.should == @document.database
139
+ end
140
+
141
+ context "#destroyed?" do
142
+ setup do
143
+ @doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
144
+ end
145
+
146
+ should "be true if deleted" do
147
+ @doc1.delete
148
+ assert @doc1.destroyed?
149
+ end
150
+
151
+ should "be true if destroyed" do
152
+ @doc1.destroy
153
+ assert @doc1.destroyed?
154
+ end
155
+
156
+ should "be false if not deleted or destroyed" do
157
+ assert ! @doc1.destroyed?
158
+ end
159
+ end
160
+
161
+ context "#persisted?" do
162
+ setup do
163
+ @doc = @document.new(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
164
+ end
165
+
166
+ should "be false if new" do
167
+ @doc.should_not be_persisted
168
+ end
169
+
170
+ should "be false if destroyed" do
171
+ @doc.save
172
+ @doc.destroy
173
+ @doc.should be_destroyed
174
+ @doc.should_not be_persisted
175
+ end
176
+
177
+ should "be true if not new or destroyed" do
178
+ @doc.save
179
+ @doc.should be_persisted
180
+ end
181
+ end
182
+
183
+ context "#reload" do
184
+ setup do
185
+ @foo_class = Doc do
186
+ key :name
187
+ end
188
+
189
+ @bar_class = EDoc do
190
+ key :name
191
+ end
192
+
193
+ @document.many :foos, :class => @foo_class
194
+ @document.many :bars, :class => @bar_class
195
+
196
+ @instance = @document.create({
197
+ :age => 39,
198
+ :foos => [@foo_class.new(:name => '1')],
199
+ :bars => [@bar_class.new(:name => '1')],
200
+ })
201
+ end
202
+
203
+ should "reload keys from the database" do
204
+ @instance.age = 37
205
+ @instance.age.should == 37
206
+ @instance.reload
207
+ @instance.age.should == 39
208
+ end
209
+
210
+ should "reset all associations" do
211
+ @instance.foos.expects(:reset).at_least_once
212
+ @instance.bars.expects(:reset).at_least_once
213
+ @instance.reload
214
+ end
215
+
216
+ should "reinstantiate embedded associations" do
217
+ @instance.reload
218
+ @instance.bars.first.name.should == '1'
219
+ end
220
+
221
+ should "return self" do
222
+ @instance.reload.object_id.should == @instance.object_id
223
+ end
224
+
225
+ should "raise DocumentNotFound if not found" do
226
+ @instance.destroy
227
+ assert_raises(MongoMapper::DocumentNotFound) { @instance.reload }
228
+ end
229
+ end
230
+
231
+ context "database has keys not defined in model" do
232
+ setup do
233
+ @id = BSON::ObjectId.new
234
+ @document.collection.insert({
235
+ :_id => @id,
236
+ :first_name => 'John',
237
+ :last_name => 'Nunemaker',
238
+ :age => 27,
239
+ :favorite_color => 'red',
240
+ :skills => ['ruby', 'rails', 'javascript', 'xhtml', 'css']
241
+ })
242
+ end
243
+
244
+ should "assign all keys from database" do
245
+ doc = @document.find(@id)
246
+ doc.first_name.should == 'John'
247
+ doc.last_name.should == 'Nunemaker'
248
+ doc.age.should == 27
249
+ doc.favorite_color.should == 'red'
250
+ doc.skills.should == ['ruby', 'rails', 'javascript', 'xhtml', 'css']
251
+ end
252
+ end
253
+ end