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
@@ -1,132 +0,0 @@
1
- require 'test_helper'
2
- require 'rack/test'
3
-
4
- class IdentityMapMiddlewareTest < Test::Unit::TestCase
5
- include Rack::Test::Methods
6
-
7
- def app
8
- @app ||= Rack::Builder.new do
9
- use MongoMapper::Middleware::IdentityMap
10
-
11
- map "/" do
12
- run lambda {|env| [200, {}, []] }
13
- end
14
-
15
- map "/fail" do
16
- run lambda {|env| raise "FAIL!" }
17
- end
18
- end.to_app
19
- end
20
-
21
- context "" do
22
- setup do
23
- @enabled = MongoMapper::Plugins::IdentityMap.enabled
24
- MongoMapper::Plugins::IdentityMap.enabled = false
25
- end
26
-
27
- teardown do
28
- MongoMapper::Plugins::IdentityMap.enabled = @enabled
29
- end
30
-
31
- 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_true
39
- end
40
-
41
- 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
48
-
49
- class Enum < Struct.new(:iter)
50
- def each(&b)
51
- iter.call(&b)
52
- end
53
- end
54
-
55
- 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
65
-
66
- 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
73
-
74
- should "clear IM after body close" do
75
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
76
- body = mw.call({}).last
77
-
78
- MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
79
- MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
80
-
81
- body.close
82
-
83
- MongoMapper::Plugins::IdentityMap.repository.should be_empty
84
- end
85
-
86
- context "with a successful request" do
87
- should "clear the identity map" do
88
- MongoMapper::Plugins::IdentityMap.expects(:clear).twice
89
- get '/'
90
- end
91
- end
92
-
93
- context "when the request raises an error" do
94
- should "clear the identity map" do
95
- MongoMapper::Plugins::IdentityMap.expects(:clear).once
96
- get '/fail' rescue nil
97
- end
98
- end
99
- end
100
- end
101
-
102
- # class IdentityMapMiddlewareTest < Test::Unit::TestCase
103
- # include Rack::Test::Methods
104
-
105
- # def app
106
- # @app ||= Rack::Builder.new do
107
- # use MongoMapper::Middleware::IdentityMap
108
- # map "/" do
109
- # run lambda {|env| [200, {}, []] }
110
- # end
111
- # map "/fail" do
112
- # run lambda {|env| raise "FAIL!" }
113
- # end
114
- # end.to_app
115
- # end
116
-
117
- # context "with a successful request" do
118
- # should "clear the identity map" do
119
- # MongoMapper::Plugins::IdentityMap.expects(:clear).twice
120
- # get '/'
121
- # end
122
- # end
123
-
124
- # context "when the request raises an error" do
125
- # should "clear the identity map" do
126
- # MongoMapper::Plugins::IdentityMap.expects(:clear).twice
127
- # get '/fail' rescue nil
128
- # end
129
- # end
130
-
131
-
132
- # end
@@ -1,65 +0,0 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class KeyTest < Test::Unit::TestCase
5
- context ".new with no id and _id of type integer" do
6
- 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
- should "be true if document has key" do
19
- Address.key?(:city).should be_true
20
- end
21
-
22
- should "be false if document does not have key" do
23
- Address.key?(:foo).should be_false
24
- end
25
- end
26
-
27
- context ".key?('string')" do
28
- should "be true if document has key" do
29
- Address.key?('city').should be_true
30
- end
31
-
32
- should "be false if document does not have key" do
33
- Address.key?('foo').should be_false
34
- end
35
- end
36
-
37
- context ".new (from database)" do
38
- setup do
39
- @klass = Doc do
40
- key :user, Hash
41
-
42
- def user=(user)
43
- super(:id => user.id, :name => user.name)
44
- end
45
- end
46
-
47
- user_class = Struct.new(:id, :name)
48
- @klass.create(:user => user_class.new(1, 'John Nunemaker'))
49
- end
50
-
51
- should "use []= for keys instead of public writer" do
52
- assert_nothing_raised do
53
- doc = @klass.first
54
- doc.user['id'].should == 1
55
- doc.user['name'].should == 'John Nunemaker'
56
- end
57
- end
58
- end
59
-
60
- context ".load" do
61
- should "return nil if argument is nil" do
62
- Doc().load(nil).should be_nil
63
- end
64
- end
65
- end # KeyTest
@@ -1,157 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Address; end
4
-
5
- class MongoMapperTest < Test::Unit::TestCase
6
- should "be able to write and read connection" do
7
- conn = Mongo::Connection.new
8
- MongoMapper.connection = conn
9
- MongoMapper.connection.should == conn
10
- end
11
-
12
- should "default connection to new mongo ruby driver" do
13
- MongoMapper.connection = nil
14
- MongoMapper.connection.should be_instance_of(Mongo::Connection)
15
- end
16
-
17
- 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
- should "have document not found error" do
24
- lambda {
25
- MongoMapper::DocumentNotFound
26
- }.should_not raise_error
27
- end
28
-
29
- 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
- should "work without authentication" do
40
- MongoMapper.config = {
41
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
42
- }
43
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
44
- MongoMapper.expects(:database=).with('test')
45
- Mongo::DB.any_instance.expects(:authenticate).never
46
- MongoMapper.connect('development')
47
- end
48
-
49
- should "work without authentication using uri" do
50
- MongoMapper.config = {
51
- 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
52
- }
53
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
54
- MongoMapper.expects(:database=).with('test')
55
- Mongo::DB.any_instance.expects(:authenticate).never
56
- MongoMapper.connect('development')
57
- end
58
-
59
- should "work with sinatra environment symbol" do
60
- MongoMapper.config = {
61
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
- }
63
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
64
- MongoMapper.expects(:database=).with('test')
65
- Mongo::DB.any_instance.expects(:authenticate).never
66
- MongoMapper.connect(:development)
67
- end
68
-
69
- should "work with options" do
70
- MongoMapper.config = {
71
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
72
- }
73
- connection, logger = mock('connection'), mock('logger')
74
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
75
- MongoMapper.connect('development', :logger => logger)
76
- end
77
-
78
- should "work with options from config" do
79
- MongoMapper.config = {
80
- 'development' => {'host' => '192.168.1.1', 'port' => 2222, 'database' => 'test', 'options' => {'safe' => true}}
81
- }
82
- connection, logger = mock('connection'), mock('logger')
83
- Mongo::Connection.expects(:new).with('192.168.1.1', 2222, :logger => logger, :safe => true)
84
- MongoMapper.connect('development', :logger => logger)
85
- end
86
-
87
- should "work with options using uri" do
88
- MongoMapper.config = {
89
- 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
90
- }
91
- connection, logger = mock('connection'), mock('logger')
92
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
93
- MongoMapper.connect('development', :logger => logger)
94
- end
95
-
96
- should "work with authentication" do
97
- MongoMapper.config = {
98
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
99
- }
100
- Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
101
- MongoMapper.connect('development')
102
- end
103
-
104
- should "work with authentication using uri" do
105
- MongoMapper.config = {
106
- 'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
107
- }
108
- Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
109
- MongoMapper.connect('development')
110
- end
111
-
112
- should "raise error for invalid scheme" do
113
- MongoMapper.config = {
114
- 'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
115
- }
116
- assert_raises(MongoMapper::InvalidScheme) { MongoMapper.connect('development') }
117
- end
118
-
119
- should "create a replica set connection if config contains multiple hosts in the old format" do
120
- MongoMapper.config = {
121
- 'development' => {
122
- 'hosts' => [ ['127.0.0.1', 27017], ['localhost', 27017] ],
123
- 'database' => 'test'
124
- }
125
- }
126
-
127
- Mongo::ReplSetConnection.expects(:new).with( ['127.0.0.1', 27017], ['localhost', 27017], {'read_secondary' => true} )
128
- MongoMapper.expects(:database=).with('test')
129
- Mongo::DB.any_instance.expects(:authenticate).never
130
- MongoMapper.connect('development', 'read_secondary' => true)
131
- end
132
-
133
- should "create a replica set connection if config contains multiple hosts in the new format" do
134
- MongoMapper.config = {
135
- 'development' => {
136
- 'hosts' => ['127.0.0.1:27017', 'localhost:27017'],
137
- 'database' => 'test'
138
- }
139
- }
140
-
141
- Mongo::ReplSetConnection.expects(:new).with( ['127.0.0.1:27017', 'localhost:27017'], {'read_secondary' => true} )
142
- MongoMapper.expects(:database=).with('test')
143
- Mongo::DB.any_instance.expects(:authenticate).never
144
- MongoMapper.connect('development', 'read_secondary' => true)
145
- end
146
- end
147
-
148
- context "setup" do
149
- should "work as shortcut for setting config, environment and options" do
150
- config, logger = mock('config'), mock('logger')
151
- MongoMapper.expects(:config=).with(config)
152
- MongoMapper.expects(:connect).with('development', :logger => logger)
153
- MongoMapper.expects(:handle_passenger_forking).once
154
- MongoMapper.setup(config, 'development', :logger => logger)
155
- end
156
- end
157
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- class PaginationTest < Test::Unit::TestCase
4
- should "default per_page to 25" do
5
- Doc().per_page.should == 25
6
- end
7
-
8
- should "allow overriding per_page" do
9
- Doc() { def self.per_page; 1 end }.per_page.should == 1
10
- end
11
- end
@@ -1,38 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestRailsCompatibility < Test::Unit::TestCase
4
- class Item
5
- include MongoMapper::EmbeddedDocument
6
- key :for_all, String
7
- end
8
-
9
- class FirstItem < Item
10
- key :first_only, String
11
- many :second_items
12
- end
13
-
14
- class SecondItem < Item
15
- key :second_only, String
16
- end
17
-
18
- context "EmbeddedDocument" do
19
- should "alias many to has_many" do
20
- FirstItem.should respond_to(:has_many)
21
- end
22
-
23
- should "alias one to has_one" do
24
- FirstItem.should respond_to(:has_one)
25
- end
26
-
27
- should "have column names" do
28
- Item.column_names.sort.should == ['_id', '_type', 'for_all']
29
- FirstItem.column_names.sort.should == ['_id', '_type', 'first_only', 'for_all']
30
- SecondItem.column_names.sort.should == ['_id', '_type', 'for_all', 'second_only']
31
- end
32
-
33
- should "alias new to new_record?" do
34
- instance = Item.new
35
- instance.new_record?.should == instance.new?
36
- end
37
- end
38
- end
@@ -1,166 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SerializationTest < Test::Unit::TestCase
4
- def setup
5
- @document = EDoc do
6
- key :name, String
7
- key :age, Integer
8
- key :awesome, Boolean
9
- key :preferences, Hash
10
- key :created_at, Time
11
- end
12
-
13
- @instance = @document.new(
14
- :name => 'John Doe',
15
- :age => 25,
16
- :awesome => true,
17
- :preferences => {:language => 'Ruby'},
18
- :created_at => Time.now.change(:usec => 0)
19
- )
20
- end
21
-
22
- context "#serializable_hash" do
23
- class List
24
- include MongoMapper::Document
25
- key :name
26
- many :items, :class_name => 'SerializationTest::Item'
27
- belongs_to :creator, :class_name => 'SerializationTest::User'
28
- end
29
-
30
- class Item
31
- include MongoMapper::Document
32
-
33
- key :title
34
- key :description
35
- many :assignments, :class_name => 'SerializationTest::Assignment'
36
-
37
- def a_method
38
- 1
39
- end
40
- end
41
-
42
- class Assignment
43
- include MongoMapper::EmbeddedDocument
44
- belongs_to :assigned_by, :class_name => 'SerializationTest::User'
45
- belongs_to :user, :class_name => 'SerializationTest::User'
46
-
47
- def serializable_hash(options = {})
48
- super({:only => :user_id}.merge(options))
49
- end
50
- end
51
-
52
- class User
53
- include MongoMapper::Document
54
- key :name, String
55
- end
56
-
57
- setup do
58
- @user1 = User.new(:name => 'Brandon')
59
- @user2 = User.new(:name => 'John')
60
- @item = Item.new(
61
- :title => 'Serialization',
62
- :description => 'Make it work like magic!',
63
- :assignments => [
64
- Assignment.new(:assigned_by => @user1, :user => @user2)
65
- ]
66
- )
67
- end
68
-
69
- should "only include specified attributes with :only option" do
70
- @item.serializable_hash(:only => :title).should == {'title' => 'Serialization'}
71
- end
72
-
73
- should "exclude attributes specified with :except option" do
74
- hash = @item.serializable_hash(:except => :description)
75
- hash['title'].should_not be_nil
76
- hash['description'].should be_nil
77
- end
78
-
79
- should "add :methods with :only option" do
80
- @item.serializable_hash(:only => :title, :methods => :a_method).should == {
81
- 'title' => 'Serialization',
82
- 'a_method' => 1
83
- }
84
- end
85
-
86
- should "call #serializable_hash on embedded many docs" do
87
- @item.serializable_hash.should == {
88
- 'id' => @item.id,
89
- 'title' => 'Serialization',
90
- 'description' => 'Make it work like magic!',
91
- 'assignments' => [{'user_id' => @user2.id}]
92
- }
93
- end
94
-
95
- context "with :include" do
96
- setup do
97
- @list = List.new(:title => 'MongoMapper', :items => [@item], :creator => @user1)
98
- end
99
-
100
- should "add many association" do
101
- hash = @list.serializable_hash(:include => :items)
102
- hash['items'].should be_instance_of(Array)
103
- hash['items'].first['title'].should == 'Serialization'
104
- end
105
-
106
- should "add belongs_to association" do
107
- hash = @list.serializable_hash(:include => :creator)
108
- hash['creator'].should == @user1.serializable_hash
109
- end
110
-
111
- should "add one association" do
112
- author_class = Doc do
113
- key :post_id, ObjectId
114
- end
115
- post_class = Doc('Post') do
116
- one :author, :class => author_class
117
- end
118
-
119
- author = author_class.new
120
- hash = post_class.new(:author => author).serializable_hash(:include => :author)
121
- hash['author'].should == author.serializable_hash
122
- end
123
-
124
- should "include multiple associations" do
125
- hash = @list.serializable_hash(:include => [:items, :creator])
126
- hash['items'].should be_instance_of(Array)
127
- hash['creator'].should == @user1.serializable_hash
128
- end
129
-
130
- should "include multiple associations with options" do
131
- hash = @list.serializable_hash(:include => {:creator => {:only => :name}})
132
- hash['creator'].should == @user1.serializable_hash(:only => :name)
133
- end
134
- end
135
- end
136
-
137
- [:json, :xml].each do |format|
138
- context format do
139
- should "be reversable" do
140
- serialized = @instance.send("to_#{format}")
141
- unserialized = @document.send("from_#{format}", serialized)
142
-
143
- assert_equal @instance, unserialized
144
- end
145
-
146
- should "allow attribute only filtering" do
147
- serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
148
- unserialized = @document.send("from_#{format}", serialized)
149
-
150
- assert_equal @instance.name, unserialized.name
151
- assert_equal @instance.age, unserialized.age
152
- assert ! unserialized.awesome
153
- assert_nil unserialized.created_at
154
- end
155
-
156
- should "allow attribute except filtering" do
157
- serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
158
- unserialized = @document.send("from_#{format}", serialized)
159
-
160
- assert_nil unserialized.name
161
- assert_nil unserialized.age
162
- assert_equal @instance.awesome, unserialized.awesome
163
- end
164
- end
165
- end
166
- end