mongo_mapper 0.14.0 → 0.15.4

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 (120) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +72 -0
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +19 -17
  10. data/lib/mongo_mapper/connection.rb +16 -38
  11. data/lib/mongo_mapper/extensions/array.rb +1 -1
  12. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  13. data/lib/mongo_mapper/extensions/date.rb +1 -1
  14. data/lib/mongo_mapper/extensions/float.rb +1 -1
  15. data/lib/mongo_mapper/extensions/hash.rb +1 -1
  16. data/lib/mongo_mapper/extensions/nil_class.rb +2 -2
  17. data/lib/mongo_mapper/extensions/object.rb +1 -1
  18. data/lib/mongo_mapper/extensions/object_id.rb +6 -2
  19. data/lib/mongo_mapper/extensions/set.rb +1 -1
  20. data/lib/mongo_mapper/extensions/string.rb +1 -1
  21. data/lib/mongo_mapper/plugins/accessible.rb +1 -1
  22. data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
  23. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
  24. data/lib/mongo_mapper/plugins/associations/many_association.rb +6 -5
  25. data/lib/mongo_mapper/plugins/associations/{belongs_to_polymorphic_proxy.rb → proxy/belongs_to_polymorphic_proxy.rb} +0 -0
  26. data/lib/mongo_mapper/plugins/associations/{belongs_to_proxy.rb → proxy/belongs_to_proxy.rb} +6 -0
  27. data/lib/mongo_mapper/plugins/associations/proxy/collection.rb +55 -0
  28. data/lib/mongo_mapper/plugins/associations/{embedded_collection.rb → proxy/embedded_collection.rb} +0 -0
  29. data/lib/mongo_mapper/plugins/associations/{in_array_proxy.rb → proxy/in_array_proxy.rb} +36 -6
  30. data/lib/mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy.rb +136 -0
  31. data/lib/mongo_mapper/plugins/associations/{many_documents_as_proxy.rb → proxy/many_documents_as_proxy.rb} +0 -0
  32. data/lib/mongo_mapper/plugins/associations/{many_documents_proxy.rb → proxy/many_documents_proxy.rb} +0 -4
  33. data/lib/mongo_mapper/plugins/associations/{many_embedded_polymorphic_proxy.rb → proxy/many_embedded_polymorphic_proxy.rb} +0 -0
  34. data/lib/mongo_mapper/plugins/associations/{many_embedded_proxy.rb → proxy/many_embedded_proxy.rb} +0 -0
  35. data/lib/mongo_mapper/plugins/associations/{many_polymorphic_proxy.rb → proxy/many_polymorphic_proxy.rb} +0 -0
  36. data/lib/mongo_mapper/plugins/associations/{one_as_proxy.rb → proxy/one_as_proxy.rb} +0 -0
  37. data/lib/mongo_mapper/plugins/associations/{one_embedded_polymorphic_proxy.rb → proxy/one_embedded_polymorphic_proxy.rb} +0 -0
  38. data/lib/mongo_mapper/plugins/associations/{one_embedded_proxy.rb → proxy/one_embedded_proxy.rb} +3 -1
  39. data/lib/mongo_mapper/plugins/associations/{one_proxy.rb → proxy/one_proxy.rb} +0 -0
  40. data/lib/mongo_mapper/plugins/associations/proxy/proxy.rb +164 -0
  41. data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -13
  42. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  43. data/lib/mongo_mapper/plugins/document.rb +1 -1
  44. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
  45. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
  46. data/lib/mongo_mapper/plugins/embedded_document.rb +2 -2
  47. data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
  48. data/lib/mongo_mapper/plugins/indexes.rb +13 -6
  49. data/lib/mongo_mapper/plugins/keys.rb +12 -7
  50. data/lib/mongo_mapper/plugins/keys/key.rb +21 -13
  51. data/lib/mongo_mapper/plugins/modifiers.rb +39 -14
  52. data/lib/mongo_mapper/plugins/persistence.rb +6 -2
  53. data/lib/mongo_mapper/plugins/querying.rb +9 -3
  54. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +6 -6
  55. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  56. data/lib/mongo_mapper/plugins/scopes.rb +19 -3
  57. data/lib/mongo_mapper/plugins/stats.rb +1 -3
  58. data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
  59. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  60. data/lib/mongo_mapper/railtie.rb +1 -0
  61. data/lib/mongo_mapper/utils.rb +2 -2
  62. data/lib/mongo_mapper/version.rb +1 -1
  63. data/spec/examples.txt +1731 -0
  64. data/spec/functional/accessible_spec.rb +7 -1
  65. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
  66. data/spec/functional/associations/belongs_to_proxy_spec.rb +55 -5
  67. data/spec/functional/associations/in_array_proxy_spec.rb +149 -14
  68. data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
  69. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  70. data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
  71. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
  72. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  73. data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
  74. data/spec/functional/associations/one_embedded_proxy_spec.rb +28 -0
  75. data/spec/functional/associations/one_proxy_spec.rb +19 -9
  76. data/spec/functional/associations_spec.rb +3 -3
  77. data/spec/functional/binary_spec.rb +2 -2
  78. data/spec/functional/caching_spec.rb +15 -22
  79. data/spec/functional/callbacks_spec.rb +2 -2
  80. data/spec/functional/counter_cache_spec.rb +10 -10
  81. data/spec/functional/dirty_spec.rb +48 -10
  82. data/spec/functional/dirty_with_callbacks_spec.rb +59 -0
  83. data/spec/functional/document_spec.rb +5 -8
  84. data/spec/functional/dumpable_spec.rb +1 -1
  85. data/spec/functional/embedded_document_spec.rb +5 -5
  86. data/spec/functional/identity_map_spec.rb +8 -8
  87. data/spec/functional/indexes_spec.rb +19 -18
  88. data/spec/functional/keys_spec.rb +64 -33
  89. data/spec/functional/logger_spec.rb +2 -2
  90. data/spec/functional/modifiers_spec.rb +81 -19
  91. data/spec/functional/partial_updates_spec.rb +8 -8
  92. data/spec/functional/protected_spec.rb +1 -1
  93. data/spec/functional/querying_spec.rb +70 -22
  94. data/spec/functional/safe_spec.rb +23 -27
  95. data/spec/functional/sci_spec.rb +7 -7
  96. data/spec/functional/scopes_spec.rb +89 -1
  97. data/spec/functional/static_keys_spec.rb +2 -2
  98. data/spec/functional/stats_spec.rb +28 -12
  99. data/spec/functional/strong_parameters_spec.rb +49 -0
  100. data/spec/functional/validations_spec.rb +8 -16
  101. data/spec/quality_spec.rb +1 -1
  102. data/spec/spec_helper.rb +39 -8
  103. data/spec/support/matchers.rb +1 -1
  104. data/spec/unit/associations/proxy_spec.rb +18 -10
  105. data/spec/unit/clone_spec.rb +1 -1
  106. data/spec/unit/document_spec.rb +3 -3
  107. data/spec/unit/embedded_document_spec.rb +4 -5
  108. data/spec/unit/extensions_spec.rb +12 -7
  109. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  110. data/spec/unit/inspect_spec.rb +1 -1
  111. data/spec/unit/key_spec.rb +23 -18
  112. data/spec/unit/keys_spec.rb +17 -8
  113. data/spec/unit/mongo_mapper_spec.rb +41 -88
  114. data/spec/unit/rails_spec.rb +2 -2
  115. data/spec/unit/validations_spec.rb +18 -18
  116. metadata +70 -38
  117. data/README.rdoc +0 -56
  118. data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
  119. data/lib/mongo_mapper/plugins/associations/collection.rb +0 -29
  120. data/lib/mongo_mapper/plugins/associations/proxy.rb +0 -141
data/spec/quality_spec.rb CHANGED
@@ -46,6 +46,6 @@ describe "The library itself" do
46
46
  error_messages << check_for_extra_spaces(filename)
47
47
  end
48
48
  end
49
- expect(error_messages.compact).to be_well_formed
49
+ error_messages.compact.should be_well_formed
50
50
  end
51
51
  end
data/spec/spec_helper.rb CHANGED
@@ -2,10 +2,21 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
2
2
 
3
3
  require 'rubygems'
4
4
  require 'bundler/setup'
5
+
6
+ # workaround for https://github.com/jruby/jruby/issues/6547
7
+ if RUBY_PLATFORM == 'java'
8
+ require 'i18n/backend'
9
+ require 'i18n/backend/simple'
10
+ end
11
+
12
+ Bundler.require(:default)
5
13
  require 'fileutils'
6
14
  require 'timecop'
7
15
  require "generator_spec/test_case"
8
- # require 'ruby-debug'
16
+
17
+ if RUBY_ENGINE == "ruby" && RUBY_VERSION >= '2.3'
18
+ require 'byebug'
19
+ end
9
20
 
10
21
  if RUBY_PLATFORM != "java"
11
22
  if ENV['TRAVIS']
@@ -40,7 +51,7 @@ def Doc(name='Class', &block)
40
51
  end
41
52
 
42
53
  klass.class_eval(&block) if block_given?
43
- klass.collection.remove
54
+ klass.collection.drop
44
55
  klass
45
56
  end
46
57
 
@@ -59,21 +70,28 @@ def EDoc(name='Class', &block)
59
70
  end
60
71
 
61
72
  def drop_indexes(klass)
62
- klass.collection.drop_indexes if klass.database.collection_names.include?(klass.collection.name)
73
+ klass.collection.indexes.drop_all if klass.database.collection_names.include?(klass.collection.name)
63
74
  end
64
75
 
65
76
  log_dir = File.expand_path('../../log', __FILE__)
66
77
  FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
67
78
  logger = Logger.new(log_dir + '/test.log')
68
79
 
69
- MongoMapper.connection = Mongo::MongoClient.new('127.0.0.1', 27017, :logger => logger)
70
- MongoMapper.database = "test"
71
- MongoMapper.database.collections.each { |c| c.drop_indexes }
80
+ Mongo::Logger.logger = logger
81
+
82
+ MongoMapper.connection = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
83
+ MongoMapper.database.collections.each { |c| c.indexes.drop_all }
72
84
  Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
73
85
 
74
86
  RSpec.configure do |config|
87
+ config.example_status_persistence_file_path = "./spec/examples.txt"
88
+
75
89
  config.expect_with :rspec do |c|
76
- c.syntax = [:should, :expect]
90
+ c.syntax = :should
91
+ end
92
+
93
+ config.mock_with :rspec do |mocks|
94
+ mocks.syntax = :should
77
95
  end
78
96
 
79
97
  config.around(:each, :without_connection) do |example|
@@ -81,5 +99,18 @@ RSpec.configure do |config|
81
99
  example.run
82
100
  MongoMapper.connection = old
83
101
  end
84
- end
85
102
 
103
+ def suppress_stderr
104
+ begin
105
+ original_stderr = $stderr.clone
106
+ $stderr.reopen(File.new('/dev/null', 'w'))
107
+ retval = yield
108
+ rescue Exception => e
109
+ $stderr.reopen(original_stderr)
110
+ raise e
111
+ ensure
112
+ $stderr.reopen(original_stderr)
113
+ end
114
+ retval
115
+ end
116
+ end
@@ -10,6 +10,6 @@ end
10
10
 
11
11
  RSpec::Matchers.define :have_index do |index_name|
12
12
  match do |model|
13
- model.collection.index_information.detect { |index| index[0] == index_name }.present?
13
+ model.collection.indexes.get(index_name).present?
14
14
  end
15
15
  end
@@ -1,19 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class FakeNilProxy < MongoMapper::Plugins::Associations::Proxy
4
- def find_target; nil end
4
+ def load_target; @target ||= nil end
5
5
  end
6
6
 
7
7
  class FakeBlankProxy < MongoMapper::Plugins::Associations::Proxy
8
- def find_target; '' end
8
+ def load_target; @target ||= '' end
9
9
  end
10
10
 
11
11
  class FakeNumberProxy < MongoMapper::Plugins::Associations::Proxy
12
- def find_target; 17 end
12
+ def load_target; @target ||= 17 end
13
13
  end
14
14
 
15
15
  class FakeProxy < MongoMapper::Plugins::Associations::Proxy
16
- def find_target; [1, 2] end
16
+ def load_target; @target ||= [1, 2] end
17
17
  end
18
18
 
19
19
  describe "Proxy" do
@@ -32,7 +32,7 @@ describe "Proxy" do
32
32
  end
33
33
 
34
34
  it "should be able to inspect the proxy" do
35
- @proxy.inspect.should == '[1, 2]'
35
+ @proxy.inspect.should == "#<FakeProxy:#{@proxy.object_id} [1, 2]>"
36
36
  end
37
37
 
38
38
  context "nil?" do
@@ -77,10 +77,6 @@ describe "Proxy" do
77
77
  @proxy.proxy_owner.should == @owner
78
78
  end
79
79
 
80
- it "should alias proxy target to target" do
81
- @proxy.proxy_target.should == @target
82
- end
83
-
84
80
  context "send" do
85
81
  it "should work if proxy responds to method" do
86
82
  @proxy.send(:reset)
@@ -92,12 +88,24 @@ describe "Proxy" do
92
88
  end
93
89
 
94
90
  it "should not work if neither the proxy or target respond to method" do
95
- lambda { @proxy.send(:gsub) }.should raise_error
91
+ lambda { @proxy.send(:gsub) }.should raise_error(NoMethodError)
96
92
  end
97
93
 
98
94
  it "should work if a proc is passed" do
99
95
  p = Proc.new {|x| x+1}
100
96
  @proxy.send(:collect, &p).should == [2,3]
101
97
  end
98
+
99
+ it "should not respond to private method" do
100
+ @proxy.reload # To load @proxy.target
101
+ @proxy.target.extend(Module.new do
102
+ private
103
+
104
+ def private_foo
105
+ end
106
+ end)
107
+
108
+ lambda { @proxy.private_foo }.should raise_error(NoMethodError, /private method `private_foo' called/)
109
+ end
102
110
  end
103
111
  end
@@ -45,7 +45,7 @@ describe "Clone" do
45
45
  end
46
46
 
47
47
  it "should clone a cloned document" do
48
- expect { @doc.clone.clone }.to_not raise_error
48
+ lambda { @doc.clone.clone }.should_not raise_error
49
49
  @doc.clone.clone.id.should be_a BSON::ObjectId
50
50
  end
51
51
  end
@@ -21,11 +21,11 @@ describe "Document" do
21
21
  end
22
22
 
23
23
  it "should have a connection" do
24
- @document.connection.should be_instance_of(Mongo::MongoClient)
24
+ @document.connection.should be_instance_of(Mongo::Client)
25
25
  end
26
26
 
27
27
  it "should allow setting different connection without affecting the default" do
28
- conn = Mongo::MongoClient.new
28
+ conn = Mongo::Client.new(['127.0.0.1:27001'])
29
29
  @document.connection conn
30
30
  @document.connection.should == conn
31
31
  @document.connection.should_not == MongoMapper.connection
@@ -152,7 +152,7 @@ describe "Document" do
152
152
 
153
153
  it "should be true if id but using custom id and not saved yet" do
154
154
  @document.key :_id, String
155
- doc = silence_stderr { @document.new }
155
+ doc = suppress_stderr { @document.new }
156
156
  doc.id = '1234'
157
157
  doc.new?.should be_truthy
158
158
  end
@@ -210,7 +210,7 @@ describe "EmbeddedDocument" do
210
210
  end
211
211
 
212
212
  it "should not include descendant keys" do
213
- lambda { Parent.new.other_child }.should raise_error
213
+ lambda { Parent.new.other_child }.should raise_error(NoMethodError)
214
214
  end
215
215
  end
216
216
 
@@ -308,7 +308,7 @@ describe "EmbeddedDocument" do
308
308
  end
309
309
 
310
310
  it "should not throw error if initialized with nil" do
311
- expect { @document.new(nil) }.to_not raise_error
311
+ lambda { @document.new(nil) }.should_not raise_error
312
312
  end
313
313
  end
314
314
 
@@ -661,16 +661,15 @@ describe "EmbeddedDocument" do
661
661
 
662
662
  it "should should raise errors when invalid" do
663
663
  @doc.name = ''
664
- expect { @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
664
+ lambda { @doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
665
665
  end
666
666
 
667
667
  it "should should raise errors when root document is invalid" do
668
668
  @root.name = ''
669
669
  @root.save(:validate => false)
670
- expect{ @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
670
+ lambda { @doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
671
671
  end
672
672
  end
673
673
  end # instance of a embedded document
674
674
  end
675
675
  end
676
-
@@ -192,22 +192,28 @@ describe "Support" do
192
192
  end
193
193
  end
194
194
 
195
- context "NilClass#from_mongo" do
195
+ context "NilClass.from_mongo" do
196
196
  it "should return nil" do
197
- nil.from_mongo(nil).should be_nil
197
+ NilClass.from_mongo(nil).should be_nil
198
+ end
199
+ end
200
+
201
+ context "NilClass.to_mongo" do
202
+ it "should return nil" do
203
+ NilClass.to_mongo(nil).should be_nil
198
204
  end
199
205
  end
200
206
 
201
207
  context "NilClass#to_mongo" do
202
208
  it "should return nil" do
203
- nil.to_mongo(nil).should be_nil
209
+ nil.to_mongo.should be_nil
204
210
  end
205
211
  end
206
212
 
207
213
  context "ObjectId#to_mongo" do
208
214
  it "should call class to_mongo with self" do
209
215
  object = Object.new
210
- expect(object.class).to receive(:to_mongo).with(object)
216
+ object.class.should_receive(:to_mongo).with(object)
211
217
  object.to_mongo
212
218
  end
213
219
  end
@@ -300,8 +306,7 @@ describe "Support" do
300
306
  end
301
307
 
302
308
  it "should be time in utc if time" do
303
- utc_str = RUBY_PLATFORM != "java" ? 'UTC' : "GMT"
304
- Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == utc_str
309
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
305
310
  end
306
311
 
307
312
  it "should be nil if blank string" do
@@ -388,7 +393,7 @@ describe "Support" do
388
393
 
389
394
  it "should support ruby driver syntax also" do
390
395
  id = BSON::ObjectId.new
391
- id.original_to_json.should == %Q({"$oid": "#{id}"})
396
+ id.original_to_json.should == %Q({"$oid":"#{id}"})
392
397
  end
393
398
  end
394
399
  end
@@ -19,116 +19,85 @@ module IdentityMapSpec
19
19
  end.to_app
20
20
  end
21
21
 
22
- context "" do
23
- before do
24
- @enabled = MongoMapper::Plugins::IdentityMap.enabled
25
- MongoMapper::Plugins::IdentityMap.enabled = false
26
- end
22
+ before do
23
+ @enabled = MongoMapper::Plugins::IdentityMap.enabled
24
+ MongoMapper::Plugins::IdentityMap.enabled = false
25
+ end
27
26
 
28
- after do
29
- MongoMapper::Plugins::IdentityMap.enabled = @enabled
30
- end
27
+ after do
28
+ MongoMapper::Plugins::IdentityMap.enabled = @enabled
29
+ end
31
30
 
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_truthy
40
- end
31
+ it "should delegate" do
32
+ called = false
33
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
34
+ called = true
35
+ [200, {}, nil]
36
+ }
37
+ mw.call({})
38
+ called.should be_truthy
39
+ end
41
40
 
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
41
+ it "should enable identity map during delegation" do
42
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
43
+ MongoMapper::Plugins::IdentityMap.should be_enabled
44
+ [200, {}, nil]
45
+ }
46
+ mw.call({})
47
+ end
49
48
 
50
- class Enum < Struct.new(:iter)
51
- def each(&b)
52
- iter.call(&b)
53
- end
49
+ class Enum < Struct.new(:iter)
50
+ def each(&b)
51
+ iter.call(&b)
54
52
  end
53
+ end
55
54
 
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
55
+ it "should enable IM for body each" do
56
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
57
+ [200, {}, Enum.new(lambda { |&b|
58
+ MongoMapper::Plugins::IdentityMap.should be_enabled
59
+ b.call "hello"
60
+ })]
61
+ }
62
+ body = mw.call({}).last
63
+ body.each { |x| x.should eql('hello') }
64
+ end
66
65
 
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
66
+ it "should disable IM after body close" do
67
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
68
+ body = mw.call({}).last
69
+ MongoMapper::Plugins::IdentityMap.should be_enabled
70
+ body.close
71
+ MongoMapper::Plugins::IdentityMap.should_not be_enabled
72
+ end
74
73
 
75
- it "should clear IM after body close" do
76
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
77
- body = mw.call({}).last
74
+ it "should clear IM after body close" do
75
+ mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
76
+ body = mw.call({}).last
78
77
 
79
- MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
80
- MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
78
+ MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
79
+ MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
81
80
 
82
- body.close
81
+ body.close
83
82
 
84
- MongoMapper::Plugins::IdentityMap.repository.should be_empty
85
- end
83
+ MongoMapper::Plugins::IdentityMap.repository.should be_empty
84
+ end
86
85
 
87
- context "with a successful request" do
88
- it "should clear the identity map" do
89
- expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).twice
90
- get '/'
91
- end
86
+ context "with a successful request" do
87
+ it "should clear the identity map" do
88
+ # for some reason, body.close gets called twice - once in rack-2.2.3/lib/rack/response.rb:281
89
+ # the other time rack-test-0.8.3/lib/rack/mock_session.rb:32
90
+ # use at_least(:twice) here to handle the error happening more than twice
91
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).at_least(:twice)
92
+ get '/'
92
93
  end
94
+ end
93
95
 
94
- context "when the request raises an error" do
95
- it "should clear the identity map" do
96
- expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).once
97
- get '/fail' rescue nil
98
- end
96
+ context "when the request raises an error" do
97
+ it "should clear the identity map" do
98
+ MongoMapper::Plugins::IdentityMap.should_receive(:clear).once
99
+ get '/fail' rescue nil
99
100
  end
100
101
  end
101
102
  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
- # expect(MongoMapper::Plugins::IdentityMap).to 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
- # expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).twice
128
- # get '/fail' rescue nil
129
- # end
130
- # end
131
-
132
-
133
- # end
134
- end
103
+ end