mongo_mapper-unstable 2010.3.8 → 2010.06.23
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.
- data/README.rdoc +4 -8
- data/bin/mmconsole +1 -1
- data/examples/keys.rb +37 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +11 -329
- data/lib/mongo_mapper/embedded_document.rb +9 -38
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
- data/lib/mongo_mapper/plugins/associations.rb +14 -22
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
- data/lib/mongo_mapper/plugins/clone.rb +10 -4
- data/lib/mongo_mapper/plugins/descendants.rb +3 -2
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -10
- data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/keys.rb +85 -110
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
- data/lib/mongo_mapper/plugins/pagination.rb +5 -15
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +9 -1
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/rails.rb +6 -1
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +9 -5
- data/lib/mongo_mapper/plugins.rb +1 -20
- data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/mongo_mapper.rb +71 -128
- data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
- data/test/functional/associations/test_in_array_proxy.rb +7 -9
- data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
- data/test/functional/associations/test_many_documents_proxy.rb +186 -64
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
- data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
- data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +70 -49
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +11 -11
- data/test/functional/test_binary.rb +5 -5
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_dirty.rb +16 -16
- data/test/functional/test_document.rb +12 -924
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +88 -8
- data/test/functional/test_identity_map.rb +41 -43
- data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
- data/test/functional/test_logger.rb +1 -1
- data/test/functional/test_modifiers.rb +275 -181
- data/test/functional/test_pagination.rb +13 -15
- data/test/functional/test_protected.rb +25 -11
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +11 -11
- data/test/functional/test_timestamps.rb +0 -2
- data/test/functional/test_userstamps.rb +0 -1
- data/test/functional/test_validations.rb +44 -31
- data/test/models.rb +18 -17
- data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
- data/test/test_helper.rb +59 -16
- data/test/unit/associations/test_base.rb +47 -42
- data/test/unit/associations/test_proxy.rb +15 -15
- data/test/unit/serializers/test_json_serializer.rb +29 -29
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +3 -3
- data/test/unit/test_document.rb +49 -67
- data/test/unit/test_dynamic_finder.rb +53 -51
- data/test/unit/test_embedded_document.rb +19 -38
- data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +29 -147
- data/test/unit/test_mongo_mapper.rb +3 -48
- data/test/unit/test_pagination.rb +1 -150
- data/test/unit/test_rails.rb +77 -19
- data/test/unit/test_rails_compatibility.rb +12 -12
- data/test/unit/test_serialization.rb +5 -5
- data/test/unit/test_time_zones.rb +9 -9
- data/test/unit/test_validations.rb +46 -46
- metadata +157 -155
- data/.gitignore +0 -10
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
- data/lib/mongo_mapper/query.rb +0 -130
- data/lib/mongo_mapper/support.rb +0 -215
- data/mongo_mapper.gemspec +0 -196
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/support/custom_matchers.rb +0 -55
- data/test/support/timing.rb +0 -16
- data/test/unit/test_query.rb +0 -340
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ValidationsTest < Test::Unit::TestCase
|
3
|
+
class ValidationsTest < Test::Unit::TestCase
|
4
4
|
context "Saving a new document that is invalid" do
|
5
5
|
setup do
|
6
6
|
@document = Doc do
|
7
7
|
key :name, String, :required => true
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
should "not insert document" do
|
12
12
|
doc = @document.new
|
13
13
|
doc.save
|
14
14
|
@document.count.should == 0
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
should "populate document's errors" do
|
18
18
|
doc = @document.new
|
19
19
|
doc.errors.size.should == 0
|
@@ -28,7 +28,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
28
28
|
key :name, String, :required => true
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
should "raise error" do
|
33
33
|
doc = @document.new
|
34
34
|
lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
@@ -41,7 +41,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
41
41
|
key :name, String, :required => true
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
should "raise error" do
|
46
46
|
lambda { @document.create! }.should raise_error(MongoMapper::DocumentNotValid)
|
47
47
|
end
|
@@ -51,29 +51,29 @@ class ValidationsTest < Test::Unit::TestCase
|
|
51
51
|
instance.new_record?.should be_false
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
context "Saving an existing document that is invalid" do
|
56
56
|
setup do
|
57
57
|
@document = Doc do
|
58
58
|
key :name, String, :required => true
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
@doc = @document.create(:name => 'John Nunemaker')
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
should "not update document" do
|
65
65
|
@doc.name = nil
|
66
66
|
@doc.save
|
67
67
|
@doc.reload.name.should == 'John Nunemaker'
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
should "populate document's errors" do
|
71
71
|
@doc.name = nil
|
72
72
|
@doc.save
|
73
73
|
@doc.errors.full_messages.should == ["Name can't be empty"]
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
context "Adding validation errors" do
|
78
78
|
setup do
|
79
79
|
@document = Doc do
|
@@ -83,38 +83,38 @@ class ValidationsTest < Test::Unit::TestCase
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
should "work with validate_on_create callback" do
|
88
88
|
@document.validate_on_create :action_present
|
89
|
-
|
89
|
+
|
90
90
|
doc = @document.new
|
91
91
|
doc.action = nil
|
92
92
|
doc.should have_error_on(:action)
|
93
|
-
|
93
|
+
|
94
94
|
doc.action = 'kick'
|
95
95
|
doc.should_not have_error_on(:action)
|
96
96
|
doc.save
|
97
|
-
|
97
|
+
|
98
98
|
doc.action = nil
|
99
99
|
doc.should_not have_error_on(:action)
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
should "work with validate_on_update callback" do
|
103
103
|
@document.validate_on_update :action_present
|
104
|
-
|
104
|
+
|
105
105
|
doc = @document.new
|
106
106
|
doc.action = nil
|
107
107
|
doc.should_not have_error_on(:action)
|
108
108
|
doc.save
|
109
|
-
|
109
|
+
|
110
110
|
doc.action = nil
|
111
111
|
doc.should have_error_on(:action)
|
112
|
-
|
112
|
+
|
113
113
|
doc.action = 'kick'
|
114
114
|
doc.should_not have_error_on(:action)
|
115
115
|
end
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
context "validating uniqueness of" do
|
119
119
|
setup do
|
120
120
|
@document = Doc do
|
@@ -167,13 +167,13 @@ class ValidationsTest < Test::Unit::TestCase
|
|
167
167
|
doc2 = @document.new("name" => "joe")
|
168
168
|
doc2.should have_error_on(:name)
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
171
|
should "allow multiple blank entries if :allow_blank => true" do
|
172
172
|
document = Doc do
|
173
173
|
key :name
|
174
174
|
validates_uniqueness_of :name, :allow_blank => :true
|
175
175
|
end
|
176
|
-
|
176
|
+
|
177
177
|
doc = document.new("name" => "")
|
178
178
|
doc.save.should be_true
|
179
179
|
|
@@ -219,7 +219,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
219
219
|
validates_uniqueness_of :name, :case_sensitive => false
|
220
220
|
end
|
221
221
|
end
|
222
|
-
|
222
|
+
|
223
223
|
should "fail on entries that differ only in case" do
|
224
224
|
doc = @document.new("name" => "BLAMMO")
|
225
225
|
doc.save.should be_true
|
@@ -232,6 +232,19 @@ class ValidationsTest < Test::Unit::TestCase
|
|
232
232
|
doc = @document.new("name" => nil)
|
233
233
|
lambda { doc.valid? }.should_not raise_error
|
234
234
|
end
|
235
|
+
|
236
|
+
should "not raise an error if special Regexp characters used" do
|
237
|
+
doc = @document.new("name" => '?')
|
238
|
+
lambda { doc.valid? }.should_not raise_error
|
239
|
+
end
|
240
|
+
|
241
|
+
should "check for uniqueness using entire string" do
|
242
|
+
doc = @document.new("name" => "John Doe")
|
243
|
+
doc.save.should be_true
|
244
|
+
|
245
|
+
doc2 = @document.new("name" => "John")
|
246
|
+
doc2.valid?.should be_true
|
247
|
+
end
|
235
248
|
end
|
236
249
|
|
237
250
|
context "scoped by a single attribute" do
|
@@ -246,7 +259,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
246
259
|
should "fail if the same name exists in the scope" do
|
247
260
|
doc = @document.new("name" => "joe", "scope" => "one")
|
248
261
|
doc.save.should be_true
|
249
|
-
|
262
|
+
|
250
263
|
@document \
|
251
264
|
.stubs(:first) \
|
252
265
|
.with(:name => 'joe', :scope => "one") \
|
@@ -279,11 +292,11 @@ class ValidationsTest < Test::Unit::TestCase
|
|
279
292
|
validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
|
280
293
|
end
|
281
294
|
end
|
282
|
-
|
295
|
+
|
283
296
|
should "fail if the same name exists in the scope" do
|
284
297
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
285
298
|
doc.save.should be_true
|
286
|
-
|
299
|
+
|
287
300
|
@document \
|
288
301
|
.stubs(:first) \
|
289
302
|
.with(:name => 'joe', :first_scope => 'one', :second_scope => 'two') \
|
@@ -292,11 +305,11 @@ class ValidationsTest < Test::Unit::TestCase
|
|
292
305
|
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
293
306
|
doc2.should have_error_on(:name)
|
294
307
|
end
|
295
|
-
|
308
|
+
|
296
309
|
should "pass if the same name exists in a different scope" do
|
297
310
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
298
311
|
doc.save.should be_true
|
299
|
-
|
312
|
+
|
300
313
|
@document \
|
301
314
|
.stubs(:first) \
|
302
315
|
.with(:name => 'joe', :first_scope => 'one', :second_scope => 'one') \
|
@@ -307,21 +320,21 @@ class ValidationsTest < Test::Unit::TestCase
|
|
307
320
|
end
|
308
321
|
end
|
309
322
|
end
|
310
|
-
|
323
|
+
|
311
324
|
context "validates uniqueness of with :unique shortcut" do
|
312
325
|
should "work" do
|
313
326
|
@document = Doc do
|
314
327
|
key :name, String, :unique => true
|
315
328
|
end
|
316
|
-
|
329
|
+
|
317
330
|
doc = @document.create(:name => 'John')
|
318
331
|
doc.should_not have_error_on(:name)
|
319
|
-
|
332
|
+
|
320
333
|
@document \
|
321
334
|
.stubs(:first) \
|
322
335
|
.with(:name => 'John') \
|
323
336
|
.returns(doc)
|
324
|
-
|
337
|
+
|
325
338
|
second_john = @document.create(:name => 'John')
|
326
339
|
second_john.should have_error_on(:name, 'has already been taken')
|
327
340
|
end
|
data/test/models.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# custom type
|
2
2
|
class WindowSize
|
3
3
|
attr_reader :width, :height
|
4
|
-
|
4
|
+
|
5
5
|
def self.to_mongo(value)
|
6
6
|
value.to_a
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def self.from_mongo(value)
|
10
10
|
value.is_a?(self) ? value : WindowSize.new(value)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def initialize(*args)
|
14
14
|
@width, @height = args.flatten
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def to_a
|
18
18
|
[width, height]
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def ==(other)
|
22
22
|
other.is_a?(self.class) && other.width == width && other.height == height
|
23
23
|
end
|
@@ -67,7 +67,6 @@ class Message
|
|
67
67
|
|
68
68
|
key :body, String
|
69
69
|
key :position, Integer
|
70
|
-
key :_type, String
|
71
70
|
key :room_id, ObjectId
|
72
71
|
|
73
72
|
belongs_to :room
|
@@ -87,17 +86,16 @@ class Room
|
|
87
86
|
end
|
88
87
|
end
|
89
88
|
many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
|
90
|
-
|
89
|
+
|
91
90
|
many :accounts, :polymorphic => true, :extend => AccountsExtensions
|
92
91
|
end
|
93
92
|
|
94
93
|
class Account
|
95
94
|
include MongoMapper::Document
|
96
|
-
|
97
|
-
key :_type, String
|
95
|
+
|
98
96
|
key :room_id, ObjectId
|
99
97
|
key :last_logged_in, Time
|
100
|
-
|
98
|
+
|
101
99
|
belongs_to :room
|
102
100
|
end
|
103
101
|
class AccountUser < Account; end
|
@@ -119,14 +117,14 @@ class Project
|
|
119
117
|
include MongoMapper::Document
|
120
118
|
|
121
119
|
key :name, String
|
122
|
-
|
120
|
+
|
123
121
|
many :collaborators, :extend => CollaboratorsExtensions
|
124
122
|
many :statuses, :order => 'position' do
|
125
123
|
def open
|
126
124
|
all(:name => %w(New Assigned))
|
127
125
|
end
|
128
126
|
end
|
129
|
-
|
127
|
+
|
130
128
|
many :addresses do
|
131
129
|
def find_all_by_state(state)
|
132
130
|
# can't use select here for some reason
|
@@ -145,6 +143,12 @@ end
|
|
145
143
|
class Status
|
146
144
|
include MongoMapper::Document
|
147
145
|
|
146
|
+
scope :complete, where(:name => 'Complete')
|
147
|
+
|
148
|
+
def self.by_position(position)
|
149
|
+
where(:position => position)
|
150
|
+
end
|
151
|
+
|
148
152
|
key :project_id, ObjectId
|
149
153
|
key :target_id, ObjectId
|
150
154
|
key :target_type, String
|
@@ -158,9 +162,7 @@ end
|
|
158
162
|
class Media
|
159
163
|
include MongoMapper::EmbeddedDocument
|
160
164
|
|
161
|
-
key :_type, String
|
162
165
|
key :file, String
|
163
|
-
|
164
166
|
key :visible, Boolean
|
165
167
|
end
|
166
168
|
|
@@ -179,7 +181,7 @@ end
|
|
179
181
|
|
180
182
|
class Catalog
|
181
183
|
include MongoMapper::Document
|
182
|
-
|
184
|
+
|
183
185
|
many :medias, :polymorphic => true do
|
184
186
|
def visible
|
185
187
|
# for some reason we can't use select here
|
@@ -192,7 +194,6 @@ module TrModels
|
|
192
194
|
class Transport
|
193
195
|
include MongoMapper::EmbeddedDocument
|
194
196
|
|
195
|
-
key :_type, String
|
196
197
|
key :license_plate, String
|
197
198
|
key :purchased_on, Date
|
198
199
|
end
|
@@ -225,7 +226,7 @@ module TrModels
|
|
225
226
|
find_all { |t| t.purchased_on < 2.years.ago.to_date }
|
226
227
|
end
|
227
228
|
end
|
228
|
-
|
229
|
+
|
229
230
|
many :transports, :polymorphic => true, :class_name => "TrModels::Transport", :extend => TransportsExtension
|
230
231
|
key :name, String
|
231
232
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
# For testing against edge rails also.
|
3
|
+
# $:.unshift '/Users/jnunemaker/dev/ruby/rails/activemodel/lib'
|
2
4
|
require 'active_model'
|
3
5
|
require 'models'
|
4
6
|
|
5
7
|
class ActiveModelLintTest < ActiveModel::TestCase
|
6
8
|
include ActiveModel::Lint::Tests
|
7
|
-
|
9
|
+
|
8
10
|
def setup
|
9
11
|
@model = Post.new
|
10
12
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,26 +1,29 @@
|
|
1
|
-
require
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'activesupport', ENV['ACTIVE_SUPPORT_VERSION']
|
3
|
+
gem 'json', '~> 1.2.3'
|
4
|
+
gem 'jnunemaker-matchy', '~> 0.4.0'
|
5
|
+
gem 'shoulda', '~> 2.10.2'
|
6
|
+
gem 'timecop', '~> 0.3.1'
|
7
|
+
gem 'mocha', '~> 0.9.8'
|
2
8
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
9
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
10
|
+
require 'mongo_mapper'
|
11
|
+
require 'fileutils'
|
12
|
+
require 'ostruct'
|
13
|
+
require 'pp'
|
7
14
|
|
15
|
+
require 'active_support/version'
|
16
|
+
require 'json'
|
8
17
|
require 'matchy'
|
9
18
|
require 'shoulda'
|
10
19
|
require 'timecop'
|
11
20
|
require 'mocha'
|
12
|
-
require 'pp'
|
13
|
-
|
14
|
-
require 'support/custom_matchers'
|
15
|
-
require 'support/timing'
|
16
21
|
|
17
22
|
class Test::Unit::TestCase
|
18
|
-
include CustomMatchers
|
19
|
-
|
20
23
|
def Doc(name=nil, &block)
|
21
24
|
klass = Class.new do
|
22
25
|
include MongoMapper::Document
|
23
|
-
set_collection_name
|
26
|
+
set_collection_name :test
|
24
27
|
|
25
28
|
if name
|
26
29
|
class_eval "def self.name; '#{name}' end"
|
@@ -52,10 +55,50 @@ class Test::Unit::TestCase
|
|
52
55
|
klass.collection.drop_indexes
|
53
56
|
end
|
54
57
|
end
|
58
|
+
|
59
|
+
custom_matcher :be_true do |receiver, matcher, args|
|
60
|
+
matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
|
61
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
|
62
|
+
receiver.eql?(true)
|
63
|
+
end
|
64
|
+
|
65
|
+
custom_matcher :be_false do |receiver, matcher, args|
|
66
|
+
matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
|
67
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
|
68
|
+
receiver.eql?(false)
|
69
|
+
end
|
70
|
+
|
71
|
+
custom_matcher :have_error_on do |receiver, matcher, args|
|
72
|
+
receiver.valid?
|
73
|
+
attribute = args[0]
|
74
|
+
expected_message = args[1]
|
75
|
+
|
76
|
+
if expected_message.nil?
|
77
|
+
matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
|
78
|
+
matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
|
79
|
+
!receiver.errors.on(attribute).blank?
|
80
|
+
else
|
81
|
+
actual = receiver.errors.on(attribute)
|
82
|
+
matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
|
83
|
+
matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
|
84
|
+
actual == expected_message
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
custom_matcher :have_index do |receiver, matcher, args|
|
89
|
+
index_name = args[0]
|
90
|
+
matcher.positive_failure_message = "#{receiver} does not have index named #{index_name}, but should"
|
91
|
+
matcher.negative_failure_message = "#{receiver} does have index named #{index_name}, but should not"
|
92
|
+
!receiver.collection.index_information.detect { |index| index[0] == index_name }.nil?
|
93
|
+
end
|
55
94
|
end
|
56
95
|
|
57
|
-
|
58
|
-
FileUtils.mkdir_p(
|
96
|
+
log_dir = File.expand_path('../../log', __FILE__)
|
97
|
+
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
98
|
+
logger = Logger.new(log_dir + '/test.log')
|
99
|
+
|
100
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
|
101
|
+
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
102
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
59
103
|
|
60
|
-
|
61
|
-
MongoMapper.database = 'test'
|
104
|
+
puts "\n--- Active Support Version: #{ActiveSupport::VERSION::STRING} ---\n"
|