mongo_mapper 0.12.0 → 0.13.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
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,134 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ module IdentityMapSpec
5
+ describe "IdentityMapMiddleware" do
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ @app ||= Rack::Builder.new do
10
+ use MongoMapper::Middleware::IdentityMap
11
+
12
+ map "/" do
13
+ run lambda {|env| [200, {}, []] }
14
+ end
15
+
16
+ map "/fail" do
17
+ run lambda {|env| raise "FAIL!" }
18
+ end
19
+ end.to_app
20
+ end
21
+
22
+ context "" do
23
+ before do
24
+ @enabled = MongoMapper::Plugins::IdentityMap.enabled
25
+ MongoMapper::Plugins::IdentityMap.enabled = false
26
+ end
27
+
28
+ after do
29
+ MongoMapper::Plugins::IdentityMap.enabled = @enabled
30
+ end
31
+
32
+ it "should delegate" do
33
+ called = false
34
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
35
+ called = true
36
+ [200, {}, nil]
37
+ }
38
+ mw.call({})
39
+ called.should be_true
40
+ end
41
+
42
+ it "should enable identity map during delegation" do
43
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
44
+ MongoMapper::Plugins::IdentityMap.should be_enabled
45
+ [200, {}, nil]
46
+ }
47
+ mw.call({})
48
+ end
49
+
50
+ class Enum < Struct.new(:iter)
51
+ def each(&b)
52
+ iter.call(&b)
53
+ end
54
+ end
55
+
56
+ it "should enable IM for body each" do
57
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
58
+ [200, {}, Enum.new(lambda { |&b|
59
+ MongoMapper::Plugins::IdentityMap.should be_enabled
60
+ b.call "hello"
61
+ })]
62
+ }
63
+ body = mw.call({}).last
64
+ body.each { |x| x.should eql('hello') }
65
+ end
66
+
67
+ it "should disable IM after body close" do
68
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
69
+ body = mw.call({}).last
70
+ MongoMapper::Plugins::IdentityMap.should be_enabled
71
+ body.close
72
+ MongoMapper::Plugins::IdentityMap.should_not be_enabled
73
+ end
74
+
75
+ it "should clear IM after body close" do
76
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
77
+ body = mw.call({}).last
78
+
79
+ MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
80
+ MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
81
+
82
+ body.close
83
+
84
+ MongoMapper::Plugins::IdentityMap.repository.should be_empty
85
+ end
86
+
87
+ context "with a successful request" do
88
+ it "should clear the identity map" do
89
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
90
+ get '/'
91
+ end
92
+ end
93
+
94
+ context "when the request raises an error" do
95
+ it "should clear the identity map" do
96
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).once
97
+ get '/fail' rescue nil
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ # describe "IdentityMapMiddleware" do
104
+ # include Rack::Test::Methods
105
+
106
+ # def app
107
+ # @app ||= Rack::Builder.new do
108
+ # use MongoMapper::Middleware::IdentityMap
109
+ # map "/" do
110
+ # run lambda {|env| [200, {}, []] }
111
+ # end
112
+ # map "/fail" do
113
+ # run lambda {|env| raise "FAIL!" }
114
+ # end
115
+ # end.to_app
116
+ # end
117
+
118
+ # context "with a successful request" do
119
+ # it "should clear the identity map" do
120
+ # MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
121
+ # get '/'
122
+ # end
123
+ # end
124
+
125
+ # context "when the request raises an error" do
126
+ # it "should clear the identity map" do
127
+ # MongoMapper::Plugins::IdentityMap.should_receive(:clear).twice
128
+ # get '/fail' rescue nil
129
+ # end
130
+ # end
131
+
132
+
133
+ # end
134
+ end
@@ -1,8 +1,8 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class InspectTest < Test::Unit::TestCase
3
+ describe "Inspect" do
4
4
  context "#inspect" do
5
- setup do
5
+ before do
6
6
  @document = Doc('User') do
7
7
  key :name, String
8
8
  key :age, Integer
@@ -12,19 +12,19 @@ class InspectTest < Test::Unit::TestCase
12
12
  @doc = @document.new(:name => 'John', :age => 29)
13
13
  end
14
14
 
15
- should "print out non-nil attributes in alpha sorted order" do
15
+ it "should print out non-nil attributes in alpha sorted order" do
16
16
  @doc.inspect.should =~ /_id:.*, age: 29, name: "John"/
17
17
  end
18
18
 
19
- should "print out all attributes when (optional) include_super argument is true" do
19
+ it "should print out all attributes when (optional) include_super argument is true" do
20
20
  @doc.inspect(true).should =~ /_id:.*, age: 29, email: nil, name: "John"/
21
21
  end
22
22
 
23
- should "include class name" do
23
+ it "should include class name" do
24
24
  @doc.inspect.should =~ /^#<User/
25
25
  end
26
26
 
27
- should "include embedded documents" do
27
+ it "should include embedded documents" do
28
28
  klass = Doc()
29
29
  pets = EDoc()
30
30
 
@@ -34,7 +34,7 @@ class InspectTest < Test::Unit::TestCase
34
34
  doc.inspect.should =~ /_id:.*, pets: \[.*_id.*, name: "Kitten".*\]/
35
35
  end
36
36
 
37
- should "include embedded document" do
37
+ it "should include embedded document" do
38
38
  klass = Doc()
39
39
  pet = EDoc()
40
40
 
@@ -1,5 +1,5 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
2
+
3
3
 
4
4
  class FooType < Struct.new(:bar)
5
5
  def self.to_mongo(value)
@@ -11,147 +11,159 @@ class FooType < Struct.new(:bar)
11
11
  end
12
12
  end
13
13
 
14
- class KeyTest < Test::Unit::TestCase
14
+ describe "Key" do
15
15
  Key = MongoMapper::Plugins::Keys::Key
16
16
 
17
17
  context "Initializing a new key" do
18
- should "allow setting the name" do
18
+ it "should allow setting the name" do
19
19
  Key.new(:foo, String).name.should == 'foo'
20
20
  end
21
21
 
22
- should "allow setting the type" do
22
+ it "should allow setting the type" do
23
23
  Key.new(:foo, Integer).type.should be(Integer)
24
24
  end
25
25
 
26
- should "allow setting options" do
26
+ it "should allow setting options" do
27
27
  Key.new(:foo, Integer, :required => true).options[:required].should be(true)
28
28
  end
29
29
 
30
- should "default options to {}" do
30
+ it "should default options to {}" do
31
31
  Key.new(:foo, Integer, nil).options.should == {}
32
32
  end
33
33
 
34
- should "symbolize option keys" do
34
+ it "should symbolize option keys" do
35
35
  Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
36
36
  end
37
37
 
38
- should "work with just name" do
38
+ it "should work with just name" do
39
39
  key = Key.new(:foo)
40
40
  key.name.should == 'foo'
41
41
  end
42
42
 
43
- should "work with name and type" do
43
+ it "should work with name and type" do
44
44
  key = Key.new(:foo, String)
45
45
  key.name.should == 'foo'
46
46
  key.type.should == String
47
47
  end
48
48
 
49
- should "work with name, type, and options" do
49
+ it "should work with name, type, and options" do
50
50
  key = Key.new(:foo, String, :required => true)
51
51
  key.name.should == 'foo'
52
52
  key.type.should == String
53
53
  key.options[:required].should be_true
54
54
  end
55
55
 
56
- should "work with name and options" do
56
+ it "should work with name and options" do
57
57
  key = Key.new(:foo, :required => true)
58
58
  key.name.should == 'foo'
59
59
  key.options[:required].should be_true
60
60
  end
61
+
62
+ it "should not permit reserved names" do
63
+ expect { Key.new(:id) }.to raise_error(/reserved/)
64
+ end
65
+
66
+ it "should not permit bad names" do
67
+ expect { Key.new(:"id.bar") }.to raise_error(/must match/)
68
+ end
69
+
70
+ it "should permit bad names if __dynamic" do
71
+ expect { Key.new(:"id.bar", :__dynamic => true) }.to_not raise_error
72
+ end
61
73
  end
62
74
 
63
75
  context "A key" do
64
- should "be equal to another key with same name and type" do
76
+ it "should be equal to another key with same name and type" do
65
77
  Key.new(:name, String).should == Key.new(:name, String)
66
78
  end
67
79
 
68
- should "not be equal to another key with different name" do
80
+ it "should not be equal to another key with different name" do
69
81
  Key.new(:name, String).should_not == Key.new(:foo, String)
70
82
  end
71
83
 
72
- should "not be equal to another key with different type" do
84
+ it "should not be equal to another key with different type" do
73
85
  Key.new(:name, String).should_not == Key.new(:name, Integer)
74
86
  end
75
87
 
76
- should "know if it is a embedded_document" do
88
+ it "should know if it is a embedded_document" do
77
89
  Key.new(:name, EDoc()).embeddable?.should be_true
78
90
  end
79
91
 
80
- should "know if it is not a embedded_document" do
92
+ it "should know if it is not a embedded_document" do
81
93
  Key.new(:name, String).embeddable?.should be_false
82
94
  end
83
95
 
84
- should "know if it is a number" do
96
+ it "should know if it is a number" do
85
97
  Key.new(:age, Integer).number?.should be_true
86
98
  Key.new(:age, Float).number?.should be_true
87
99
  end
88
100
 
89
- should "know if it is not a number" do
101
+ it "should know if it is not a number" do
90
102
  Key.new(:age, String).number?.should be_false
91
103
  end
92
104
  end
93
105
 
94
106
  context "for an array with :typecast option" do
95
- setup { @key = Key.new(:user_ids, Array, :typecast => 'ObjectId') }
107
+ before { @key = Key.new(:user_ids, Array, :typecast => 'ObjectId') }
96
108
  subject { @key }
97
109
 
98
- should "cast each element correctly" do
110
+ it "should cast each element correctly" do
99
111
  ids = [BSON::ObjectId.new, BSON::ObjectId.new, BSON::ObjectId.new.to_s, BSON::ObjectId.new.to_s]
100
112
  subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
101
113
  end
102
114
  end
103
115
 
104
116
  context "for an array with :typecast option of Date" do
105
- setup { @key = Key.new(:dates, Array, :typecast => 'Date') }
117
+ before { @key = Key.new(:dates, Array, :typecast => 'Date') }
106
118
  subject { @key }
107
119
 
108
- should "cast each element correctly when get" do
120
+ it "should cast each element correctly when get" do
109
121
  dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
110
122
  subject.get(dates).should == dates.map { |date| Date.from_mongo(date) }
111
123
  end
112
124
 
113
- should "cast each element correctly when set" do
125
+ it "should cast each element correctly when set" do
114
126
  dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
115
127
  subject.set(dates).should == dates.map { |date| Date.to_mongo(date) }
116
128
  end
117
129
  end
118
130
 
119
131
  context "for a set with :typecast option" do
120
- setup { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') }
132
+ before { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') }
121
133
  subject { @key }
122
134
 
123
- should "cast each element correctly" do
135
+ it "should cast each element correctly" do
124
136
  ids = [BSON::ObjectId.new, BSON::ObjectId.new, BSON::ObjectId.new.to_s, BSON::ObjectId.new.to_s]
125
137
  subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
126
138
  end
127
139
  end
128
140
 
129
141
  context "setting a value with a custom type" do
130
- should "correctly typecast" do
142
+ it "should correctly typecast" do
131
143
  key = Key.new(:foo, FooType)
132
144
  key.set("something").should == 'to_mongo'
133
145
  end
134
146
 
135
- should "correctly typecast if object of that type is given" do
147
+ it "should correctly typecast if object of that type is given" do
136
148
  key = Key.new(:foo, FooType)
137
149
  key.set(FooType.new('something')).should == 'to_mongo'
138
150
  end
139
151
  end
140
152
 
141
153
  context "getting a value with a custom type" do
142
- should "use #from_mongo to convert back to custom type" do
154
+ it "should use #from_mongo to convert back to custom type" do
143
155
  key = Key.new(:foo, FooType)
144
156
  key.get('something').should == 'from_mongo'
145
157
  end
146
158
  end
147
159
 
148
160
  context "getting a value" do
149
- should "work with a type" do
161
+ it "should work with a type" do
150
162
  key = Key.new(:foo, String)
151
163
  key.get('bar').should == 'bar'
152
164
  end
153
165
 
154
- should "work without type" do
166
+ it "should work without type" do
155
167
  key = Key.new(:foo)
156
168
  key.get([1, '2']).should == [1, '2']
157
169
  key.get(false).should == false
@@ -159,12 +171,12 @@ class KeyTest < Test::Unit::TestCase
159
171
  end
160
172
 
161
173
  context "for a embedded_document" do
162
- should "default to nil" do
174
+ it "should default to nil" do
163
175
  key = Key.new(:foo, Address)
164
176
  key.get(nil).should be_nil
165
177
  end
166
178
 
167
- should "return instance if instance" do
179
+ it "should return instance if instance" do
168
180
  address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
169
181
  key = Key.new(:foo, Address)
170
182
  key.get(address).should == address
@@ -172,34 +184,52 @@ class KeyTest < Test::Unit::TestCase
172
184
  end
173
185
  end
174
186
 
175
- context "getting a value with a default set" do
176
- setup do
187
+ context "with a default set" do
188
+ before do
177
189
  @key = Key.new(:foo, String, :default => 'baz')
178
190
  end
179
191
 
180
- should "return default value if value nil" do
181
- @key.get(nil).should == 'baz'
182
- end
192
+ context "#get" do
193
+ it "should return nil" do
194
+ @key.get(nil).should == nil
195
+ end
183
196
 
184
- should "return a dup of the default value" do
185
- @key.get(nil).replace('bar')
186
- @key.get(nil).should == 'baz'
187
- end
197
+ it "should return value if not blank" do
198
+ @key.get('foobar').should == 'foobar'
199
+ end
188
200
 
189
- should "return value if not blank" do
190
- @key.get('foobar').should == 'foobar'
201
+ it "should return default value if name is _id and value nil" do
202
+ id = BSON::ObjectId.new
203
+ key = Key.new(:_id, ObjectId, :default => lambda { id })
204
+ key.get(nil).should == id
205
+ end
191
206
  end
192
207
 
193
- should "work with Boolean type and false value" do
194
- Key.new(:active, Boolean, :default => false).get(nil).should be_false
195
- end
208
+ context "#default_value" do
209
+ it "should return default value" do
210
+ @key.default_value.should == 'baz'
211
+ end
196
212
 
197
- should "work with Boolean type and true value" do
198
- Key.new(:active, Boolean, :default => true).get(nil).should be_true
199
- end
213
+ it "should return a dup of the default value" do
214
+ @key.default_value.replace('bar')
215
+ @key.default_value.should == 'baz'
216
+ end
217
+
218
+ it "should work with Boolean type and false value" do
219
+ Key.new(:active, Boolean, :default => false).default_value.should be_false
220
+ end
200
221
 
201
- should "work with procs" do
202
- Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
222
+ it "should work with Boolean type and true value" do
223
+ Key.new(:active, Boolean, :default => true).default_value.should be_true
224
+ end
225
+
226
+ it "should work with Array values" do
227
+ Key.new(:active, Array, :default => []).default_value.should == []
228
+ end
229
+
230
+ it "should work with procs" do
231
+ Key.new(:foo, String, :default => lambda { return 'hello world' }).default_value.should == "hello world"
232
+ end
203
233
  end
204
234
  end
205
235
  end # KeyTest