pwnash-mongo_mapper 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/lib/mongo_mapper.rb +6 -8
  2. data/lib/mongo_mapper/document.rb +6 -281
  3. data/lib/mongo_mapper/embedded_document.rb +5 -45
  4. data/lib/mongo_mapper/extensions.rb +190 -0
  5. data/lib/mongo_mapper/plugins.rb +27 -18
  6. data/lib/mongo_mapper/plugins/associations.rb +7 -6
  7. data/lib/mongo_mapper/plugins/associations/base.rb +1 -0
  8. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +1 -0
  9. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +1 -0
  10. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  11. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -0
  12. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +2 -1
  13. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +1 -0
  14. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +4 -4
  15. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  16. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -0
  17. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  18. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +1 -0
  19. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +1 -0
  20. data/lib/mongo_mapper/plugins/associations/proxy.rb +1 -0
  21. data/lib/mongo_mapper/plugins/callbacks.rb +4 -3
  22. data/lib/mongo_mapper/plugins/clone.rb +1 -0
  23. data/lib/mongo_mapper/plugins/descendants.rb +1 -0
  24. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  25. data/lib/mongo_mapper/plugins/document.rb +40 -0
  26. data/lib/mongo_mapper/plugins/dynamic_querying.rb +42 -0
  27. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +2 -36
  28. data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +1 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +8 -11
  31. data/lib/mongo_mapper/plugins/indexes.rb +11 -0
  32. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  33. data/lib/mongo_mapper/plugins/keys.rb +7 -9
  34. data/lib/mongo_mapper/plugins/keys/key.rb +12 -2
  35. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  36. data/lib/mongo_mapper/plugins/modifiers.rb +3 -2
  37. data/lib/mongo_mapper/plugins/pagination.rb +1 -0
  38. data/lib/mongo_mapper/plugins/pagination/proxy.rb +1 -0
  39. data/lib/mongo_mapper/plugins/persistence.rb +1 -0
  40. data/lib/mongo_mapper/plugins/protected.rb +1 -0
  41. data/lib/mongo_mapper/plugins/query_logger.rb +5 -10
  42. data/lib/mongo_mapper/plugins/querying.rb +236 -0
  43. data/lib/mongo_mapper/plugins/rails.rb +1 -0
  44. data/lib/mongo_mapper/plugins/sci.rb +31 -0
  45. data/lib/mongo_mapper/plugins/serialization.rb +1 -0
  46. data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
  47. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  48. data/lib/mongo_mapper/plugins/validations.rb +5 -1
  49. data/lib/mongo_mapper/query.rb +53 -120
  50. data/lib/mongo_mapper/support.rb +2 -213
  51. data/lib/mongo_mapper/support/descendant_appends.rb +3 -6
  52. data/lib/mongo_mapper/version.rb +2 -1
  53. metadata +156 -190
  54. data/.gitignore +0 -10
  55. data/Rakefile +0 -37
  56. data/mongo_mapper.gemspec +0 -216
  57. data/performance/read_write.rb +0 -52
  58. data/specs.watchr +0 -51
  59. data/test/NOTE_ON_TESTING +0 -1
  60. data/test/active_model_lint_test.rb +0 -13
  61. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
  62. data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
  63. data/test/functional/associations/test_in_array_proxy.rb +0 -325
  64. data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
  65. data/test/functional/associations/test_many_documents_proxy.rb +0 -536
  66. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
  67. data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
  68. data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
  69. data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
  70. data/test/functional/associations/test_one_proxy.rb +0 -196
  71. data/test/functional/test_associations.rb +0 -44
  72. data/test/functional/test_binary.rb +0 -27
  73. data/test/functional/test_callbacks.rb +0 -151
  74. data/test/functional/test_dirty.rb +0 -163
  75. data/test/functional/test_document.rb +0 -1219
  76. data/test/functional/test_embedded_document.rb +0 -210
  77. data/test/functional/test_identity_map.rb +0 -507
  78. data/test/functional/test_indexing.rb +0 -44
  79. data/test/functional/test_logger.rb +0 -20
  80. data/test/functional/test_modifiers.rb +0 -416
  81. data/test/functional/test_pagination.rb +0 -93
  82. data/test/functional/test_protected.rb +0 -163
  83. data/test/functional/test_string_id_compatibility.rb +0 -67
  84. data/test/functional/test_timestamps.rb +0 -64
  85. data/test/functional/test_userstamps.rb +0 -28
  86. data/test/functional/test_validations.rb +0 -342
  87. data/test/models.rb +0 -227
  88. data/test/support/custom_matchers.rb +0 -37
  89. data/test/support/timing.rb +0 -16
  90. data/test/test_helper.rb +0 -64
  91. data/test/unit/associations/test_base.rb +0 -212
  92. data/test/unit/associations/test_proxy.rb +0 -105
  93. data/test/unit/serializers/test_json_serializer.rb +0 -202
  94. data/test/unit/test_descendant_appends.rb +0 -71
  95. data/test/unit/test_document.rb +0 -225
  96. data/test/unit/test_dynamic_finder.rb +0 -123
  97. data/test/unit/test_embedded_document.rb +0 -657
  98. data/test/unit/test_keys.rb +0 -216
  99. data/test/unit/test_mongo_mapper.rb +0 -118
  100. data/test/unit/test_pagination.rb +0 -160
  101. data/test/unit/test_plugins.rb +0 -50
  102. data/test/unit/test_query.rb +0 -374
  103. data/test/unit/test_rails.rb +0 -181
  104. data/test/unit/test_rails_compatibility.rb +0 -52
  105. data/test/unit/test_serialization.rb +0 -51
  106. data/test/unit/test_support.rb +0 -390
  107. data/test/unit/test_time_zones.rb +0 -39
  108. data/test/unit/test_validations.rb +0 -544
@@ -1,216 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Address
4
- include MongoMapper::EmbeddedDocument
5
-
6
- key :address, String
7
- key :city, String
8
- key :state, String
9
- key :zip, Integer
10
- end
11
-
12
- class FooType < Struct.new(:bar)
13
- def self.to_mongo(value)
14
- 'to_mongo'
15
- end
16
-
17
- def self.from_mongo(value)
18
- 'from_mongo'
19
- end
20
- end
21
-
22
- class KeyTest < Test::Unit::TestCase
23
- include MongoMapper::Plugins::Keys
24
-
25
- context ".key?(:symbol)" do
26
- should "be true if document has key" do
27
- Address.key?(:city).should be_true
28
- end
29
-
30
- should "be false if document does not have key" do
31
- Address.key?(:foo).should be_false
32
- end
33
- end
34
-
35
- context ".key?('string')" do
36
- should "be true if document has key" do
37
- Address.key?('city').should be_true
38
- end
39
-
40
- should "be false if document does not have key" do
41
- Address.key?('foo').should be_false
42
- end
43
- end
44
-
45
- context ".new (from database)" do
46
- setup do
47
- @klass = Doc do
48
- key :user, Hash
49
-
50
- def user=(user)
51
- super(:id => user.id, :name => user.name)
52
- end
53
- end
54
-
55
- user_class = Struct.new(:id, :name)
56
- @klass.create(:user => user_class.new(1, 'John Nunemaker'))
57
- end
58
-
59
- should "use []= for keys instead of public writer" do
60
- assert_nothing_raised do
61
- doc = @klass.first
62
- doc.user['id'].should == 1
63
- doc.user['name'].should == 'John Nunemaker'
64
- end
65
- end
66
- end
67
-
68
- context "Initializing a new key" do
69
- should "allow setting the name" do
70
- Key.new(:foo, String).name.should == 'foo'
71
- end
72
-
73
- should "allow setting the type" do
74
- Key.new(:foo, Integer).type.should be(Integer)
75
- end
76
-
77
- should "allow setting options" do
78
- Key.new(:foo, Integer, :required => true).options[:required].should be(true)
79
- end
80
-
81
- should "default options to {}" do
82
- Key.new(:foo, Integer, nil).options.should == {}
83
- end
84
-
85
- should "symbolize option keys" do
86
- Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
87
- end
88
-
89
- should "work with just name" do
90
- key = Key.new(:foo)
91
- key.name.should == 'foo'
92
- end
93
-
94
- should "work with name and type" do
95
- key = Key.new(:foo, String)
96
- key.name.should == 'foo'
97
- key.type.should == String
98
- end
99
-
100
- should "work with name, type, and options" do
101
- key = Key.new(:foo, String, :required => true)
102
- key.name.should == 'foo'
103
- key.type.should == String
104
- key.options[:required].should be_true
105
- end
106
-
107
- should "work with name and options" do
108
- key = Key.new(:foo, :required => true)
109
- key.name.should == 'foo'
110
- key.options[:required].should be_true
111
- end
112
- end
113
-
114
- context "A key" do
115
- should "be equal to another key with same name and type" do
116
- Key.new(:name, String).should == Key.new(:name, String)
117
- end
118
-
119
- should "not be equal to another key with different name" do
120
- Key.new(:name, String).should_not == Key.new(:foo, String)
121
- end
122
-
123
- should "not be equal to another key with different type" do
124
- Key.new(:name, String).should_not == Key.new(:name, Integer)
125
- end
126
-
127
- should "know if it is a embedded_document" do
128
- Key.new(:name, EDoc()).embeddable?.should be_true
129
- end
130
-
131
- should "know if it is not a embedded_document" do
132
- Key.new(:name, String).embeddable?.should be_false
133
- end
134
-
135
- should "know if it is a number" do
136
- Key.new(:age, Integer).number?.should be_true
137
- Key.new(:age, Float).number?.should be_true
138
- end
139
-
140
- should "know if it is not a number" do
141
- Key.new(:age, String).number?.should be_false
142
- end
143
- end
144
-
145
- context "setting a value with a custom type" do
146
- should "correctly typecast" do
147
- key = Key.new(:foo, FooType)
148
- key.set("something").should == 'to_mongo'
149
- end
150
-
151
- should "correctly typecast if object of that type is given" do
152
- key = Key.new(:foo, FooType)
153
- key.set(FooType.new('something')).should == 'to_mongo'
154
- end
155
- end
156
-
157
- context "getting a value with a custom type" do
158
- should "use #from_mongo to convert back to custom type" do
159
- key = Key.new(:foo, FooType)
160
- key.get('something').should == 'from_mongo'
161
- end
162
- end
163
-
164
- context "getting a value" do
165
- should "work with a type" do
166
- key = Key.new(:foo, String)
167
- key.get('bar').should == 'bar'
168
- end
169
-
170
- should "work without type" do
171
- key = Key.new(:foo)
172
- key.get([1, '2']).should == [1, '2']
173
- key.get(false).should == false
174
- key.get({}).should == {}
175
- end
176
-
177
- context "for a embedded_document" do
178
- should "default to nil" do
179
- key = Key.new(:foo, Address)
180
- key.get(nil).should be_nil
181
- end
182
-
183
- should "return instance if instance" do
184
- address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
185
- key = Key.new(:foo, Address)
186
- key.get(address).should == address
187
- end
188
- end
189
- end
190
-
191
- context "getting a value with a default set" do
192
- setup do
193
- @key = Key.new(:foo, String, :default => 'baz')
194
- end
195
-
196
- should "return default value if value nil" do
197
- @key.get(nil).should == 'baz'
198
- end
199
-
200
- should "return value if not blank" do
201
- @key.get('foobar').should == 'foobar'
202
- end
203
-
204
- should "work with Boolean type and false value" do
205
- Key.new(:active, Boolean, :default => false).get(nil).should be_false
206
- end
207
-
208
- should "work with Boolean type and true value" do
209
- Key.new(:active, Boolean, :default => true).get(nil).should be_true
210
- end
211
-
212
- should "work with procs" do
213
- Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
214
- end
215
- end
216
- end # KeyTest
@@ -1,118 +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 options" do
60
- MongoMapper.config = {
61
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
- }
63
- connection, logger = mock('connection'), mock('logger')
64
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
65
- MongoMapper.connect('development', :logger => logger)
66
- end
67
-
68
- should "work with options using uri" do
69
- MongoMapper.config = {
70
- 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
71
- }
72
- connection, logger = mock('connection'), mock('logger')
73
- Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
74
- MongoMapper.connect('development', :logger => logger)
75
- end
76
-
77
- should "work with authentication" do
78
- MongoMapper.config = {
79
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
80
- }
81
- Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
82
- MongoMapper.connect('development')
83
- end
84
-
85
- should "work with authentication using uri" do
86
- MongoMapper.config = {
87
- 'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
88
- }
89
- Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
90
- MongoMapper.connect('development')
91
- end
92
-
93
- should "raise error for invalid scheme" do
94
- MongoMapper.config = {
95
- 'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
96
- }
97
- assert_raises(MongoMapper::InvalidScheme) { MongoMapper.connect('development') }
98
- end
99
- end
100
-
101
- context "setup" do
102
- should "work as shortcut for setting config, environment and options" do
103
- config, logger = mock('config'), mock('logger')
104
- MongoMapper.expects(:config=).with(config)
105
- MongoMapper.expects(:connect).with('development', :logger => logger)
106
- MongoMapper.expects(:handle_passenger_forking).never
107
- MongoMapper.setup(config, 'development', :logger => logger)
108
- end
109
-
110
- should "handle passenger if option present" do
111
- config, logger = mock('config'), mock('logger')
112
- MongoMapper.expects(:config=).with(config)
113
- MongoMapper.expects(:connect).with('development', :logger => logger)
114
- MongoMapper.expects(:handle_passenger_forking)
115
- MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
116
- end
117
- end
118
- end
@@ -1,160 +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
-
12
- context "Pagination proxy" do
13
- include MongoMapper::Plugins::Pagination
14
-
15
- should "respond_to? correctly on proxy readers" do
16
- proxy = Proxy.new(25, 10, 4)
17
- proxy.respond_to?(:subject).should be_true
18
- proxy.respond_to?(:total_entries).should be_true
19
- proxy.respond_to?(:per_page).should be_true
20
- proxy.respond_to?(:current_page).should be_true
21
- proxy.respond_to?(:limit).should be_true
22
- proxy.respond_to?(:total_pages).should be_true
23
- proxy.respond_to?(:out_of_bounds?).should be_true
24
- proxy.respond_to?(:previous_page).should be_true
25
- proxy.respond_to?(:next_page).should be_true
26
- proxy.respond_to?(:skip).should be_true
27
- proxy.respond_to?(:offset).should be_true
28
-
29
- # make sure it doesnt respond true to everything
30
- proxy.respond_to?(:blahblahblah).should be_false
31
- end
32
-
33
- should "respond_to? correctly on proxy writers" do
34
- proxy = Proxy.new(25, 10, 4)
35
- proxy.respond_to?(:subject=).should be_true
36
- end
37
-
38
- should "should have accessors for subject" do
39
- subject = [1,2,3,4,5]
40
- collection = Proxy.new(25, 2)
41
- collection.subject = subject
42
- collection.subject.should == subject
43
- end
44
-
45
- should "delegate any methods not defined to the subject" do
46
- subject = [1,2,3,4,5]
47
- collection = Proxy.new(25, 2, 10)
48
- collection.subject = subject
49
- collection.size.should == 5
50
- collection.each_with_index do |value, i|
51
- value.should == subject[i]
52
- end
53
- collection[0..2].should == [1,2,3]
54
- collection.class.should == Array
55
- end
56
-
57
- should "should respond_to? correctly for methods defined on the subject" do
58
- subject = [1,2,3,4,5]
59
- def subject.blahblah
60
- "BLAHBLAH"
61
- end
62
- collection = Proxy.new(25, 2, 10)
63
- collection.subject = subject
64
- collection.respond_to?(:blahblah).should be_true
65
- end
66
-
67
- should "return correct value for total_entries" do
68
- Proxy.new(25, 2, 10).total_entries.should == 25
69
- Proxy.new('25', 2, 10).total_entries.should == 25
70
- end
71
-
72
- should "return correct value for per_page" do
73
- Proxy.new(25, 2, 10).per_page.should == 10
74
- Proxy.new(25, 2, '10').per_page.should == 10
75
- end
76
-
77
- should "alias limit to per_page" do
78
- Proxy.new(100, 1, 300).limit.should == 300
79
- end
80
-
81
- should "set per_page to 25 if nil or blank" do
82
- Proxy.new(25, 2).per_page.should == 25
83
- Proxy.new(25, 2, '').per_page.should == 25
84
- end
85
-
86
- should "return correct value for current_page" do
87
- Proxy.new(25, 2, 10).current_page.should == 2
88
- Proxy.new(25, '2', 10).current_page.should == 2
89
- end
90
-
91
- should "not allow value less than 1 for current page" do
92
- Proxy.new(25, -1).current_page.should == 1
93
- end
94
-
95
- should "automatically calculate total_pages from total_entries and per page" do
96
- Proxy.new(25, 2, 10).total_pages.should == 3
97
- end
98
-
99
- should "know how many records to skip" do
100
- Proxy.new(25, 2, 10).skip.should == 10
101
- end
102
-
103
- should "alias offset to skip" do
104
- Proxy.new(25, 2, 10).offset.should == 10
105
- end
106
-
107
- should "return true for === Array" do
108
- collection = Proxy.new(25, 2, 10)
109
- collection.subject = [1, 2]
110
- collection.should === Array
111
- end
112
-
113
- context "previous_page" do
114
- should "be nil if current page 1" do
115
- Proxy.new(25, 1, 10).previous_page.should be_nil
116
- end
117
-
118
- should "be one less than current page if current is > 1" do
119
- Proxy.new(25, 2, 10).previous_page.should == 1
120
- end
121
- end
122
-
123
- context "next_page" do
124
- should "be nil if current page is last page" do
125
- Proxy.new(25, 3, 10).next_page.should be_nil
126
- end
127
-
128
- should "work for any page that is not last" do
129
- Proxy.new(25, 1, 10).next_page.should == 2
130
- Proxy.new(25, 2, 10).next_page.should == 3
131
- end
132
- end
133
-
134
- context "previous_page" do
135
- should "be nil if current page is first page" do
136
- Proxy.new(25, 1, 10).previous_page.should be_nil
137
- end
138
-
139
- should "work for any page other than first" do
140
- Proxy.new(25, 2, 10).previous_page.should == 1
141
- Proxy.new(25, 3, 10).previous_page.should == 2
142
- end
143
- end
144
-
145
- context "out_of_bounds?" do
146
- should "be true if current_page is greater than total_pages" do
147
- Proxy.new(25, 10, 4).out_of_bounds?.should be_true
148
- end
149
-
150
- should "be false if current_page is less than total_pages" do
151
- Proxy.new(25, 10, 1).out_of_bounds?.should be_false
152
- Proxy.new(25, 2, 10).out_of_bounds?.should be_false
153
- end
154
-
155
- should "be false if current_page is equal to total_pages" do
156
- Proxy.new(25, 3, 10).out_of_bounds?.should be_false
157
- end
158
- end
159
- end
160
- end