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,155 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe "Key" do
5
+ context ".new with no id and _id of type integer" do
6
+ it "should not error" do
7
+ lambda {
8
+ klass = Doc() do
9
+ key :_id, Integer
10
+ end
11
+ # No sensible default id for integer, people better pass them in if they user this
12
+ silence_stderr { klass.new.id.should be_nil }
13
+ }.should_not raise_error
14
+ end
15
+ end
16
+
17
+ context ".key?(:symbol)" do
18
+ it "should be true if document has key" do
19
+ Address.key?(:city).should be_true
20
+ end
21
+
22
+ it "should be false if document does not have key" do
23
+ Address.key?(:foo).should be_false
24
+ end
25
+ end
26
+
27
+ context "#assign" do
28
+ it "should raise a deprecation warning" do
29
+ klass = Doc() do
30
+ key :_id, Integer
31
+ end
32
+ doc = klass.new
33
+ doc.should_receive(:warn).once
34
+ doc.assign({:x => :y})
35
+ end
36
+ end
37
+
38
+ # TODO: Are these methods deprecated?
39
+ context "#embedded and #non_embedded_keys" do
40
+ EmbeddableThingie = EDoc {
41
+ key :whiz, String
42
+ }
43
+
44
+ let(:klass) do
45
+ Doc do
46
+ key :foo, String
47
+ key :embeddable_thingie, EmbeddableThingie
48
+ end
49
+ end
50
+
51
+ it "should get non-embeddable keys" do
52
+ klass.new.non_embedded_keys.map(&:name).should =~ %w(_id foo)
53
+ end
54
+
55
+ it "should get embeddable keys" do
56
+ klass.new.embedded_keys.map(&:name).should == %w(embeddable_thingie)
57
+ end
58
+ end
59
+
60
+ context ".key?('string')" do
61
+ it "should be true if document has key" do
62
+ Address.key?('city').should be_true
63
+ end
64
+
65
+ it "should be false if document does not have key" do
66
+ Address.key?('foo').should be_false
67
+ end
68
+ end
69
+
70
+ context ".new (from database)" do
71
+ before do
72
+ @klass = Doc do
73
+ key :user, Hash
74
+
75
+ def user=(user)
76
+ super(:id => user.id, :name => user.name)
77
+ end
78
+ end
79
+
80
+ user_class = Struct.new(:id, :name)
81
+ @klass.create(:user => user_class.new(1, 'John Nunemaker'))
82
+ end
83
+
84
+ it "should use []= for keys instead of public writer" do
85
+ expect {
86
+ doc = @klass.first
87
+ doc.user['id'].should == 1
88
+ doc.user['name'].should == 'John Nunemaker'
89
+ }.to_not raise_error
90
+ end
91
+ end
92
+
93
+ context ".load" do
94
+ it "should return nil if argument is nil" do
95
+ Doc().load(nil).should be_nil
96
+ end
97
+ end
98
+
99
+ context "default values" do
100
+ before do
101
+ @klass = Doc do
102
+ key :value, Integer, :default => 1
103
+ end
104
+ end
105
+
106
+ it "should initialize default value" do
107
+ @klass.new.value.should == 1
108
+ end
109
+
110
+ it "should allow overriding default value" do
111
+ @klass.new(:value => 2).value.should == 2
112
+ end
113
+
114
+ it "should allow re-setting a value that is defaulted" do
115
+ instance = @klass.new
116
+ instance.value = 2
117
+ instance.value.should == 2
118
+ instance.value = nil
119
+ instance.value.should == nil
120
+ end
121
+
122
+ context "for _id" do
123
+ before do
124
+ @klass.class_eval do
125
+ key :_id, Integer, :default => lambda { 12345 }
126
+ end
127
+ end
128
+
129
+ it "should work" do
130
+ @klass.new._id.should == 12345
131
+ end
132
+ end
133
+ end
134
+
135
+ context "when loading from the database" do
136
+ it "should not set default values for keys that already exist" do
137
+ counter = 0
138
+ instance = nil
139
+
140
+ klass = Doc do
141
+ key :value, Integer, :default => lambda { counter += 1 }
142
+ end
143
+
144
+ expect { instance = klass.create }.to change { counter }.by(1)
145
+ expect {
146
+ instance.reload.value.should == 1
147
+
148
+ instance.value = 10
149
+ instance.save
150
+
151
+ instance.reload.value.should == 10
152
+ }.to_not change { counter }
153
+ end
154
+ end
155
+ end # KeyTest
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'rails/generators'
3
+ require 'rails/generators/test_case'
4
+ require 'rails/generators/mongo_mapper/model/model_generator'
5
+
6
+ describe MongoMapper::Generators::ModelGenerator do
7
+ include GeneratorSpec::TestCase
8
+ destination File.expand_path('../../tmp', File.dirname(__FILE__))
9
+
10
+ before do
11
+ prepare_destination
12
+ end
13
+
14
+ it 'help shows MongoMapper options' do
15
+ pending "Rails 4 breaks Kernel#capture on JRuby", :if => (Rails::VERSION::MAJOR >= 4 && RUBY_PLATFORM == "java") do
16
+ content = run_generator ['--help']
17
+ assert_match(/rails generate mongo_mapper:model/, content)
18
+ end
19
+ end
20
+
21
+ it 'model are properly created' do
22
+ run_generator ['Color']
23
+ assert_file 'app/models/color.rb', /class Color/
24
+ assert_file 'app/models/color.rb', /include MongoMapper::Document/
25
+ end
26
+
27
+ it 'model are properly created with attributes' do
28
+ run_generator ['Color', 'name:string', 'saturation:integer']
29
+ assert_file 'app/models/color.rb', /class Color/
30
+ assert_file 'app/models/color.rb', /include MongoMapper::Document/
31
+ assert_file 'app/models/color.rb', /key :name, String/
32
+ assert_file 'app/models/color.rb', /key :saturation, Integer/
33
+ end
34
+
35
+ it 'model are properly created with timestamps option' do
36
+ run_generator ['Color', '--timestamps']
37
+ assert_file 'app/models/color.rb', /class Color/
38
+ assert_file 'app/models/color.rb', /include MongoMapper::Document/
39
+ assert_file 'app/models/color.rb', /timestamps/
40
+ end
41
+
42
+ it 'model are properly created with parent option' do
43
+ run_generator ['Green', '--parent', 'Color']
44
+ assert_file 'app/models/green.rb', /class Green < Color/
45
+ end
46
+
47
+ end
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ class Address; end
4
+
5
+ describe "MongoMapper" do
6
+ it "should be able to write and read connection" do
7
+ conn = Mongo::MongoClient.new
8
+ MongoMapper.connection = conn
9
+ MongoMapper.connection.should == conn
10
+ end
11
+
12
+ it "should default connection to new mongo ruby driver" do
13
+ MongoMapper.connection = nil
14
+ MongoMapper.connection.should be_instance_of(Mongo::MongoClient)
15
+ end
16
+
17
+ it "should be able to write and read default database" do
18
+ MongoMapper.database = 'test'
19
+ MongoMapper.database.should be_instance_of(Mongo::DB)
20
+ MongoMapper.database.name.should == 'test'
21
+ end
22
+
23
+ it "should have document not found error" do
24
+ lambda {
25
+ MongoMapper::DocumentNotFound
26
+ }.should_not raise_error
27
+ end
28
+
29
+ it "should be able to read/write config" do
30
+ config = {
31
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'},
32
+ 'production' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test-prod'}
33
+ }
34
+ MongoMapper.config = config
35
+ MongoMapper.config.should == config
36
+ end
37
+
38
+ context "connecting to environment from config" do
39
+ it "should work without authentication" do
40
+ MongoMapper.config = {
41
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
42
+ }
43
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, {})
44
+ MongoMapper.should_receive(:database=).with('test')
45
+ Mongo::DB.any_instance.should_receive(:authenticate).never
46
+ MongoMapper.connect('development')
47
+ end
48
+
49
+ it "should work without authentication using uri" do
50
+ MongoMapper.config = {
51
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
52
+ }
53
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, {})
54
+ MongoMapper.should_receive(:database=).with('test')
55
+ Mongo::DB.any_instance.should_receive(:authenticate).never
56
+ MongoMapper.connect('development')
57
+ end
58
+
59
+ it "should work with sinatra environment symbol" do
60
+ MongoMapper.config = {
61
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
+ }
63
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, {})
64
+ MongoMapper.should_receive(:database=).with('test')
65
+ Mongo::DB.any_instance.should_receive(:authenticate).never
66
+ MongoMapper.connect(:development)
67
+ end
68
+
69
+ it "should work with options" do
70
+ MongoMapper.config = {
71
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
72
+ }
73
+ connection, logger = double('connection'), double('logger')
74
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, :logger => logger)
75
+ MongoMapper.connect('development', :logger => logger)
76
+ end
77
+
78
+ it "should pass along ssl when true" do
79
+ MongoMapper.config = {
80
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'ssl' => true}
81
+ }
82
+ connection, logger = double('connection'), double('logger')
83
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, :logger => logger, :ssl => true)
84
+ MongoMapper.connect('development', :logger => logger)
85
+ end
86
+
87
+ it "should pass along ssl when false" do
88
+ MongoMapper.config = {
89
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'ssl' => false}
90
+ }
91
+ connection, logger = double('connection'), double('logger')
92
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, :logger => logger, :ssl => false)
93
+ MongoMapper.connect('development', :logger => logger)
94
+ end
95
+
96
+ it "should convert read preferences to symbols" do
97
+ MongoMapper.config = {
98
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'options' => {'read' => 'primary'}}
99
+ }
100
+ connection, logger = double('connection'), double('logger')
101
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, :logger => logger, :read => :primary)
102
+ MongoMapper.connect('development', :logger => logger)
103
+ end
104
+
105
+ it "should work with options from config" do
106
+ MongoMapper.config = {
107
+ 'development' => {'host' => '192.168.1.1', 'port' => 2222, 'database' => 'test', 'options' => {'safe' => true}}
108
+ }
109
+ connection, logger = double('connection'), double('logger')
110
+ Mongo::MongoClient.should_receive(:new).with('192.168.1.1', 2222, :logger => logger, :safe => true)
111
+ MongoMapper.connect('development', :logger => logger)
112
+ end
113
+
114
+ it "should work with options using uri" do
115
+ MongoMapper.config = {
116
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
117
+ }
118
+ connection, logger = double('connection'), double('logger')
119
+ Mongo::MongoClient.should_receive(:new).with('127.0.0.1', 27017, :logger => logger)
120
+ MongoMapper.connect('development', :logger => logger)
121
+ end
122
+
123
+ it "should work with authentication" do
124
+ MongoMapper.config = {
125
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
126
+ }
127
+ Mongo::DB.any_instance.should_receive(:authenticate).with('john', 'secret')
128
+ MongoMapper.connect('development')
129
+ end
130
+
131
+ it "should work with authentication using uri" do
132
+ MongoMapper.config = {
133
+ 'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
134
+ }
135
+ Mongo::DB.any_instance.should_receive(:authenticate).with('john', 'secret')
136
+ MongoMapper.connect('development')
137
+ end
138
+
139
+ it "should raise error for invalid scheme" do
140
+ MongoMapper.config = {
141
+ 'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
142
+ }
143
+ expect { MongoMapper.connect('development') }.to raise_error(MongoMapper::InvalidScheme)
144
+ end
145
+
146
+ it "should create a replica set connection if config contains multiple hosts in the old format" do
147
+ MongoMapper.config = {
148
+ 'development' => {
149
+ 'hosts' => [ ['127.0.0.1', 27017], ['localhost', 27017] ],
150
+ 'database' => 'test'
151
+ }
152
+ }
153
+
154
+ Mongo::MongoReplicaSetClient.should_receive(:new).with( ['127.0.0.1', 27017], ['localhost', 27017], {'read_secondary' => true} )
155
+ MongoMapper.should_receive(:database=).with('test')
156
+ Mongo::DB.any_instance.should_receive(:authenticate).never
157
+ MongoMapper.connect('development', 'read_secondary' => true)
158
+ end
159
+
160
+ it "should create a replica set connection if config contains multiple hosts in the new format" do
161
+ MongoMapper.config = {
162
+ 'development' => {
163
+ 'hosts' => ['127.0.0.1:27017', 'localhost:27017'],
164
+ 'database' => 'test'
165
+ }
166
+ }
167
+
168
+ Mongo::MongoReplicaSetClient.should_receive(:new).with( ['127.0.0.1:27017', 'localhost:27017'], {'read_secondary' => true} )
169
+ MongoMapper.should_receive(:database=).with('test')
170
+ Mongo::DB.any_instance.should_receive(:authenticate).never
171
+ MongoMapper.connect('development', 'read_secondary' => true)
172
+ end
173
+ end
174
+
175
+ context "setup" do
176
+ it "should work as shortcut for setting config, environment and options" do
177
+ config, logger = double('config'), double('logger')
178
+ MongoMapper.should_receive(:config=).with(config)
179
+ MongoMapper.should_receive(:connect).with('development', :logger => logger)
180
+ MongoMapper.should_receive(:handle_passenger_forking).once
181
+ MongoMapper.setup(config, 'development', :logger => logger)
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Pagination" do
4
+ it "should default per_page to 25" do
5
+ Doc().per_page.should == 25
6
+ end
7
+
8
+ it "should allow overriding per_page" do
9
+ Doc() { def self.per_page; 1 end }.per_page.should == 1
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class PluginsTest < Test::Unit::TestCase
4
- should "default plugins to empty array" do
3
+ describe "Plugins" do
4
+ it "should default plugins to empty array" do
5
5
  Class.new { extend MongoMapper::Plugins }.plugins.should == []
6
6
  end
7
7
 
@@ -24,41 +24,41 @@ class PluginsTest < Test::Unit::TestCase
24
24
  end
25
25
  end
26
26
 
27
- setup do
27
+ before do
28
28
  @document = Class.new do
29
29
  extend MongoMapper::Plugins
30
30
  plugin MyConcern
31
31
  end
32
32
  end
33
33
 
34
- should "include instance methods" do
34
+ it "should include instance methods" do
35
35
  @document.new.instance_foo.should == 'instance_foo'
36
36
  end
37
37
 
38
- should "extend class methods" do
38
+ it "should extend class methods" do
39
39
  @document.class_foo.should == 'class_foo'
40
40
  end
41
41
 
42
- should "pass model to configure" do
42
+ it "should pass model to configure" do
43
43
  @document.new.should respond_to(:from_concern)
44
44
  end
45
45
 
46
- should "add plugin to plugins" do
46
+ it "should add plugin to plugins" do
47
47
  @document.plugins.should include(MyConcern)
48
48
  end
49
49
 
50
50
  context "Document" do
51
- setup do
51
+ before do
52
52
  MongoMapper::Document.plugins.delete(MyConcern)
53
53
  end
54
54
 
55
- should 'allow plugins on Document' do
55
+ it 'should allow plugins on Document' do
56
56
  MongoMapper::Document.plugin(MyConcern)
57
57
  Doc().should respond_to(:class_foo)
58
58
  Doc().new.should respond_to(:instance_foo)
59
59
  end
60
60
 
61
- should 'add plugins to classes that include Document before they are added' do
61
+ it 'should add plugins to classes that include Document before they are added' do
62
62
  article = Doc()
63
63
  MongoMapper::Document.plugin(MyConcern)
64
64
  article.should respond_to(:class_foo)
@@ -67,18 +67,18 @@ class PluginsTest < Test::Unit::TestCase
67
67
  end
68
68
 
69
69
  context "EmbeddedDocument" do
70
- setup do
70
+ before do
71
71
  MongoMapper::EmbeddedDocument.plugins.delete(MyConcern)
72
72
  end
73
73
 
74
- should 'allow plugins on EmbeddedDocument' do
74
+ it 'should allow plugins on EmbeddedDocument' do
75
75
  MongoMapper::EmbeddedDocument.plugin(MyConcern)
76
76
  article = EDoc()
77
77
  article.should respond_to(:class_foo)
78
78
  article.new.should respond_to(:instance_foo)
79
79
  end
80
80
 
81
- should 'add plugins to classes that include EmbeddedDocument before they are added' do
81
+ it 'should add plugins to classes that include EmbeddedDocument before they are added' do
82
82
  article = EDoc()
83
83
  MongoMapper::EmbeddedDocument.plugin(MyConcern)
84
84
  article.should respond_to(:class_foo)