mongo_mapper-unstable 2010.2.28 → 2010.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -5
- data/VERSION +1 -1
- data/lib/mongo_mapper/document.rb +3 -72
- data/lib/mongo_mapper/plugins/callbacks.rb +14 -3
- data/lib/mongo_mapper/plugins/keys.rb +9 -4
- data/lib/mongo_mapper/plugins/modifiers.rb +87 -0
- data/lib/mongo_mapper/plugins/rails.rb +16 -8
- data/lib/mongo_mapper/plugins/serialization.rb +51 -81
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
- data/lib/mongo_mapper/plugins.rb +3 -0
- data/lib/mongo_mapper.rb +2 -2
- data/test/active_model_lint_test.rb +11 -0
- data/test/functional/test_document.rb +0 -116
- data/test/functional/test_embedded_document.rb +17 -12
- data/test/functional/test_indexing.rb +44 -0
- data/test/functional/test_modifiers.rb +297 -227
- data/test/functional/test_timestamps.rb +64 -0
- data/test/functional/test_userstamps.rb +28 -0
- data/test/support/timing.rb +1 -1
- data/test/test_helper.rb +0 -4
- data/test/unit/serializers/test_json_serializer.rb +30 -17
- data/test/unit/test_serialization.rb +3 -3
- metadata +80 -35
@@ -1069,89 +1069,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
1069
1069
|
end
|
1070
1070
|
end
|
1071
1071
|
|
1072
|
-
context "timestamping" do
|
1073
|
-
setup do
|
1074
|
-
@klass = Doc do
|
1075
|
-
set_collection_name 'users'
|
1076
|
-
|
1077
|
-
key :first_name, String
|
1078
|
-
key :last_name, String
|
1079
|
-
key :age, Integer
|
1080
|
-
key :date, Date
|
1081
|
-
end
|
1082
|
-
@klass.timestamps!
|
1083
|
-
end
|
1084
|
-
|
1085
|
-
should "set created_at and updated_at on create" do
|
1086
|
-
doc = @klass.new(:first_name => 'John', :age => 27)
|
1087
|
-
doc.created_at.should be(nil)
|
1088
|
-
doc.updated_at.should be(nil)
|
1089
|
-
doc.save
|
1090
|
-
doc.created_at.should_not be(nil)
|
1091
|
-
doc.updated_at.should_not be(nil)
|
1092
|
-
end
|
1093
|
-
|
1094
|
-
should "not overwrite created_at if it already exists" do
|
1095
|
-
original_created_at = 1.month.ago
|
1096
|
-
doc = @klass.new(:first_name => 'John', :age => 27, :created_at => original_created_at)
|
1097
|
-
doc.created_at.to_i.should == original_created_at.to_i
|
1098
|
-
doc.updated_at.should be_nil
|
1099
|
-
doc.save
|
1100
|
-
doc.created_at.to_i.should == original_created_at.to_i
|
1101
|
-
doc.updated_at.should_not be_nil
|
1102
|
-
end
|
1103
|
-
|
1104
|
-
should "set updated_at on field update but leave created_at alone" do
|
1105
|
-
doc = @klass.create(:first_name => 'John', :age => 27)
|
1106
|
-
old_created_at = doc.created_at
|
1107
|
-
old_updated_at = doc.updated_at
|
1108
|
-
doc.first_name = 'Johnny'
|
1109
|
-
|
1110
|
-
Timecop.freeze(Time.now + 5.seconds) do
|
1111
|
-
doc.save
|
1112
|
-
end
|
1113
|
-
|
1114
|
-
doc.created_at.should == old_created_at
|
1115
|
-
doc.updated_at.should_not == old_updated_at
|
1116
|
-
end
|
1117
|
-
|
1118
|
-
should "set updated_at on document update but leave created_at alone" do
|
1119
|
-
doc = @klass.create(:first_name => 'John', :age => 27)
|
1120
|
-
old_created_at = doc.created_at
|
1121
|
-
old_updated_at = doc.updated_at
|
1122
|
-
|
1123
|
-
Timecop.freeze(Time.now + 5.seconds) do
|
1124
|
-
@klass.update(doc._id, { :first_name => 'Johnny' })
|
1125
|
-
end
|
1126
|
-
|
1127
|
-
doc = doc.reload
|
1128
|
-
doc.created_at.should == old_created_at
|
1129
|
-
doc.updated_at.should_not == old_updated_at
|
1130
|
-
end
|
1131
|
-
end
|
1132
|
-
|
1133
|
-
context "userstamping" do
|
1134
|
-
setup do
|
1135
|
-
@document.userstamps!
|
1136
|
-
end
|
1137
|
-
|
1138
|
-
should "add creator_id key" do
|
1139
|
-
@document.keys.keys.should include('creator_id')
|
1140
|
-
end
|
1141
|
-
|
1142
|
-
should "add updater_id key" do
|
1143
|
-
@document.keys.keys.should include('updater_id')
|
1144
|
-
end
|
1145
|
-
|
1146
|
-
should "add belongs_to creator" do
|
1147
|
-
@document.associations.keys.should include('creator')
|
1148
|
-
end
|
1149
|
-
|
1150
|
-
should "add belongs_to updater" do
|
1151
|
-
@document.associations.keys.should include('updater')
|
1152
|
-
end
|
1153
|
-
end
|
1154
|
-
|
1155
1072
|
context "#exists?" do
|
1156
1073
|
setup do
|
1157
1074
|
@doc = @document.create(:first_name => "James", :age => 27)
|
@@ -1245,37 +1162,4 @@ class DocumentTest < Test::Unit::TestCase
|
|
1245
1162
|
doc.skills.should == ['ruby', 'rails', 'javascript', 'xhtml', 'css']
|
1246
1163
|
end
|
1247
1164
|
end
|
1248
|
-
|
1249
|
-
context "Indexing" do
|
1250
|
-
setup do
|
1251
|
-
drop_indexes(@document)
|
1252
|
-
end
|
1253
|
-
|
1254
|
-
should "allow creating index for a key" do
|
1255
|
-
@document.ensure_index :first_name
|
1256
|
-
@document.should have_index('first_name_1')
|
1257
|
-
end
|
1258
|
-
|
1259
|
-
should "allow creating unique index for a key" do
|
1260
|
-
@document.ensure_index :first_name, :unique => true
|
1261
|
-
@document.should have_index('first_name_1')
|
1262
|
-
end
|
1263
|
-
|
1264
|
-
should "allow creating index on multiple keys" do
|
1265
|
-
@document.ensure_index [[:first_name, 1], [:last_name, -1]]
|
1266
|
-
|
1267
|
-
# order is different for different versions of ruby so instead of
|
1268
|
-
# just checking have_index('first_name_1_last_name_-1') I'm checking
|
1269
|
-
# the values of the indexes to make sure the index creation was successful
|
1270
|
-
@document.collection.index_information.detect do |index|
|
1271
|
-
keys = index[1]
|
1272
|
-
keys.include?(['first_name', 1]) && keys.include?(['last_name', -1])
|
1273
|
-
end.should_not be_nil
|
1274
|
-
end
|
1275
|
-
|
1276
|
-
should "work with :index shortcut when defining key" do
|
1277
|
-
@document.key :father, String, :index => true
|
1278
|
-
@document.should have_index('father_1')
|
1279
|
-
end
|
1280
|
-
end
|
1281
1165
|
end
|
@@ -3,18 +3,17 @@ require 'models'
|
|
3
3
|
|
4
4
|
class EmbeddedDocumentTest < Test::Unit::TestCase
|
5
5
|
def setup
|
6
|
-
@klass = Doc do
|
7
|
-
key :
|
8
|
-
key :last_name, String
|
6
|
+
@klass = Doc('Person') do
|
7
|
+
key :name, String
|
9
8
|
end
|
10
9
|
|
11
|
-
@pet_klass = EDoc do
|
10
|
+
@pet_klass = EDoc('Pet') do
|
12
11
|
key :name, String
|
13
12
|
end
|
14
13
|
|
15
14
|
@klass.many :pets, :class => @pet_klass
|
16
15
|
|
17
|
-
@address_class = EDoc do
|
16
|
+
@address_class = EDoc('Address') do
|
18
17
|
key :city, String
|
19
18
|
key :state, String
|
20
19
|
end
|
@@ -23,20 +22,26 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
23
22
|
context "Saving a document with a key that is an embedded document" do
|
24
23
|
setup do
|
25
24
|
@klass.key :foo, @address_class
|
26
|
-
|
27
|
-
@address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
28
|
-
@doc = @klass.new(:foo => @address)
|
29
25
|
end
|
30
26
|
|
31
27
|
should "embed embedded document" do
|
32
|
-
@
|
33
|
-
@
|
34
|
-
|
28
|
+
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
29
|
+
doc = @klass.create(:foo => address)
|
30
|
+
doc.foo.city.should == 'South Bend'
|
31
|
+
doc.foo.state.should == 'IN'
|
35
32
|
|
36
|
-
doc =
|
33
|
+
doc = doc.reload
|
37
34
|
doc.foo.city.should == 'South Bend'
|
38
35
|
doc.foo.state.should == 'IN'
|
39
36
|
end
|
37
|
+
|
38
|
+
should "assign _parent_document and _root_document" do
|
39
|
+
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
40
|
+
address._parent_document.should be_nil
|
41
|
+
doc = @klass.create(:foo => address)
|
42
|
+
address._parent_document.should be(doc)
|
43
|
+
address._root_document.should be(doc)
|
44
|
+
end
|
40
45
|
end
|
41
46
|
|
42
47
|
should "correctly instantiate single collection inherited embedded documents" do
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IndexingTest < Test::Unit::TestCase
|
4
|
+
context "Indexing" do
|
5
|
+
setup do
|
6
|
+
@document = Doc do
|
7
|
+
set_collection_name 'users'
|
8
|
+
|
9
|
+
key :first_name, String
|
10
|
+
key :last_name, String
|
11
|
+
key :age, Integer
|
12
|
+
key :date, Date
|
13
|
+
end
|
14
|
+
drop_indexes(@document)
|
15
|
+
end
|
16
|
+
|
17
|
+
should "allow creating index for a key" do
|
18
|
+
@document.ensure_index :first_name
|
19
|
+
@document.should have_index('first_name_1')
|
20
|
+
end
|
21
|
+
|
22
|
+
should "allow creating unique index for a key" do
|
23
|
+
@document.ensure_index :first_name, :unique => true
|
24
|
+
@document.should have_index('first_name_1')
|
25
|
+
end
|
26
|
+
|
27
|
+
should "allow creating index on multiple keys" do
|
28
|
+
@document.ensure_index [[:first_name, 1], [:last_name, -1]]
|
29
|
+
|
30
|
+
# order is different for different versions of ruby so instead of
|
31
|
+
# just checking have_index('first_name_1_last_name_-1') I'm checking
|
32
|
+
# the values of the indexes to make sure the index creation was successful
|
33
|
+
@document.collection.index_information.detect do |index|
|
34
|
+
keys = index[1]
|
35
|
+
keys.include?(['first_name', 1]) && keys.include?(['last_name', -1])
|
36
|
+
end.should_not be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
should "work with :index shortcut when defining key" do
|
40
|
+
@document.key :father, String, :index => true
|
41
|
+
@document.should have_index('father_1')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|