djsun-mongomapper 0.3.5.5 → 0.4.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.rdoc +38 -38
  2. data/Rakefile +87 -73
  3. data/VERSION +1 -1
  4. data/lib/mongomapper.rb +67 -71
  5. data/lib/mongomapper/associations.rb +86 -84
  6. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
  7. data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
  8. data/lib/mongomapper/associations/proxy.rb +74 -73
  9. data/lib/mongomapper/document.rb +342 -348
  10. data/lib/mongomapper/embedded_document.rb +354 -274
  11. data/lib/mongomapper/finder_options.rb +84 -84
  12. data/lib/mongomapper/key.rb +32 -76
  13. data/lib/mongomapper/rails_compatibility/document.rb +14 -14
  14. data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
  15. data/lib/mongomapper/support.rb +156 -29
  16. data/lib/mongomapper/validations.rb +69 -47
  17. data/test/custom_matchers.rb +48 -0
  18. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
  19. data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
  20. data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
  21. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
  22. data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
  23. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
  24. data/test/functional/test_binary.rb +21 -0
  25. data/test/functional/test_document.rb +946 -952
  26. data/test/functional/test_embedded_document.rb +98 -0
  27. data/test/functional/test_pagination.rb +87 -80
  28. data/test/functional/test_rails_compatibility.rb +29 -29
  29. data/test/functional/test_validations.rb +262 -172
  30. data/test/models.rb +169 -169
  31. data/test/test_helper.rb +28 -66
  32. data/test/unit/serializers/test_json_serializer.rb +193 -193
  33. data/test/unit/test_document.rb +161 -123
  34. data/test/unit/test_embedded_document.rb +643 -547
  35. data/test/unit/test_finder_options.rb +183 -183
  36. data/test/unit/test_key.rb +175 -247
  37. data/test/unit/test_rails_compatibility.rb +38 -33
  38. data/test/unit/test_serializations.rb +52 -52
  39. data/test/unit/test_support.rb +268 -0
  40. data/test/unit/test_time_zones.rb +40 -0
  41. data/test/unit/test_validations.rb +499 -258
  42. metadata +22 -12
  43. data/History +0 -76
  44. data/mongomapper.gemspec +0 -145
@@ -1,131 +1,131 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
5
- def setup
6
- clear_all_collections
7
- end
8
-
9
- should "default reader to empty array" do
10
- Catalog.new.medias.should == []
11
- Catalog.new.medias.inspect.should == "[]"
12
- end
13
-
14
- should "allow adding to association like it was an array" do
15
- catalog = Catalog.new
16
- catalog.medias << Video.new
17
- catalog.medias.push Video.new
18
- catalog.medias.size.should == 2
19
- end
20
-
21
- should "be able to replace the association" do
22
- catalog = Catalog.new
23
- catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)]
24
- catalog.save.should be_true
25
-
26
- from_db = Catalog.find(catalog.id)
27
- from_db.medias.size.should == 1
28
- from_db.medias[0].file.should == "video.mpg"
29
- end
30
-
31
- should "store different associations" do
32
- catalog = Catalog.new
33
- catalog.medias = [
34
- Video.new("file" => "video.mpg", "length" => 3600),
35
- Music.new("file" => "music.mp3", "bitrate" => "128kbps"),
36
- Image.new("file" => "image.png", "width" => 800, "height" => 600)
37
- ]
38
- catalog.save.should be_true
39
-
40
- from_db = Catalog.find(catalog.id)
41
- from_db.medias.size.should == 3
42
- from_db.medias[0].file.should == "video.mpg"
43
- from_db.medias[0].length.should == 3600
44
- from_db.medias[1].file.should == "music.mp3"
45
- from_db.medias[1].bitrate.should == "128kbps"
46
- from_db.medias[2].file.should == "image.png"
47
- from_db.medias[2].width.should == 800
48
- from_db.medias[2].height.should == 600
49
- end
50
-
51
- context "With modularized models" do
52
- should "set associations correctly" do
53
- fleet_attributes = {
54
- "name" => "My Fleet",
55
- "transports" => [
56
- {"_type" => "TrModels::Ambulance", "license_plate" => "GGG123", "icu" => true},
57
- {"_type" => "TrModels::Car", "license_plate" => "ABC123", "model" => "VW Golf", "year" => 2001},
58
- {"_type" => "TrModels::Car", "license_plate" => "DEF123", "model" => "Honda Accord", "year" => 2008},
59
- ]
60
- }
61
-
62
- fleet = TrModels::Fleet.new(fleet_attributes)
63
- fleet.transports.size.should == 3
64
- fleet.transports[0].class.should == TrModels::Ambulance
65
- fleet.transports[0].license_plate.should == "GGG123"
66
- fleet.transports[0].icu.should be_true
67
- fleet.transports[1].class.should == TrModels::Car
68
- fleet.transports[1].license_plate.should == "ABC123"
69
- fleet.transports[1].model.should == "VW Golf"
70
- fleet.transports[1].year.should == 2001
71
- fleet.transports[2].class.should == TrModels::Car
72
- fleet.transports[2].license_plate.should == "DEF123"
73
- fleet.transports[2].model.should == "Honda Accord"
74
- fleet.transports[2].year.should == 2008
75
- fleet.save.should be_true
76
-
77
- from_db = TrModels::Fleet.find(fleet.id)
78
- from_db.transports.size.should == 3
79
- from_db.transports[0].license_plate.should == "GGG123"
80
- from_db.transports[0].icu.should be_true
81
- from_db.transports[1].license_plate.should == "ABC123"
82
- from_db.transports[1].model.should == "VW Golf"
83
- from_db.transports[1].year.should == 2001
84
- from_db.transports[2].license_plate.should == "DEF123"
85
- from_db.transports[2].model.should == "Honda Accord"
86
- from_db.transports[2].year.should == 2008
87
- end
88
-
89
- should "default reader to empty array" do
90
- TrModels::Fleet.new.transports.should == []
91
- TrModels::Fleet.new.transports.inspect.should == "[]"
92
- end
93
-
94
- should "allow adding to association like it was an array" do
95
- fleet = TrModels::Fleet.new
96
- fleet.transports << TrModels::Car.new
97
- fleet.transports.push TrModels::Bus.new
98
- fleet.transports.size.should == 2
99
- end
100
-
101
- should "be able to replace the association" do
102
- fleet = TrModels::Fleet.new
103
- fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")]
104
- fleet.save.should be_true
105
-
106
- from_db = TrModels::Fleet.find(fleet.id)
107
- from_db.transports.size.should == 1
108
- from_db.transports[0].license_plate.should == "DCU2013"
109
- end
110
-
111
- should "store different associations" do
112
- fleet = TrModels::Fleet.new
113
- fleet.transports = [
114
- TrModels::Car.new("license_plate" => "ABC1223", "model" => "Honda Civic", "year" => 2003),
115
- TrModels::Bus.new("license_plate" => "XYZ9090", "max_passengers" => 51),
116
- TrModels::Ambulance.new("license_plate" => "HDD3030", "icu" => true)
117
- ]
118
- fleet.save.should be_true
119
-
120
- from_db = TrModels::Fleet.find(fleet.id)
121
- from_db.transports.size.should == 3
122
- from_db.transports[0].license_plate.should == "ABC1223"
123
- from_db.transports[0].model.should == "Honda Civic"
124
- from_db.transports[0].year.should == 2003
125
- from_db.transports[1].license_plate.should == "XYZ9090"
126
- from_db.transports[1].max_passengers.should == 51
127
- from_db.transports[2].license_plate.should == "HDD3030"
128
- from_db.transports[2].icu.should == true
129
- end
130
- end
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ clear_all_collections
7
+ end
8
+
9
+ should "default reader to empty array" do
10
+ catalog = Catalog.new
11
+ catalog.medias.should == []
12
+ end
13
+
14
+ should "allow adding to association like it was an array" do
15
+ catalog = Catalog.new
16
+ catalog.medias << Video.new
17
+ catalog.medias.push Video.new
18
+ catalog.medias.size.should == 2
19
+ end
20
+
21
+ should "be able to replace the association" do
22
+ catalog = Catalog.new
23
+ catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)]
24
+ catalog.save.should be_true
25
+
26
+ from_db = Catalog.find(catalog.id)
27
+ from_db.medias.size.should == 1
28
+ from_db.medias[0].file.should == "video.mpg"
29
+ end
30
+
31
+ should "store different associations" do
32
+ catalog = Catalog.new
33
+ catalog.medias = [
34
+ Video.new("file" => "video.mpg", "length" => 3600),
35
+ Music.new("file" => "music.mp3", "bitrate" => "128kbps"),
36
+ Image.new("file" => "image.png", "width" => 800, "height" => 600)
37
+ ]
38
+ catalog.save.should be_true
39
+
40
+ from_db = Catalog.find(catalog.id)
41
+ from_db.medias.size.should == 3
42
+ from_db.medias[0].file.should == "video.mpg"
43
+ from_db.medias[0].length.should == 3600
44
+ from_db.medias[1].file.should == "music.mp3"
45
+ from_db.medias[1].bitrate.should == "128kbps"
46
+ from_db.medias[2].file.should == "image.png"
47
+ from_db.medias[2].width.should == 800
48
+ from_db.medias[2].height.should == 600
49
+ end
50
+
51
+ context "With modularized models" do
52
+ should "set associations correctly" do
53
+ fleet_attributes = {
54
+ "name" => "My Fleet",
55
+ "transports" => [
56
+ {"_type" => "TrModels::Ambulance", "license_plate" => "GGG123", "icu" => true},
57
+ {"_type" => "TrModels::Car", "license_plate" => "ABC123", "model" => "VW Golf", "year" => 2001},
58
+ {"_type" => "TrModels::Car", "license_plate" => "DEF123", "model" => "Honda Accord", "year" => 2008},
59
+ ]
60
+ }
61
+
62
+ fleet = TrModels::Fleet.new(fleet_attributes)
63
+ fleet.transports.size.should == 3
64
+ fleet.transports[0].class.should == TrModels::Ambulance
65
+ fleet.transports[0].license_plate.should == "GGG123"
66
+ fleet.transports[0].icu.should be_true
67
+ fleet.transports[1].class.should == TrModels::Car
68
+ fleet.transports[1].license_plate.should == "ABC123"
69
+ fleet.transports[1].model.should == "VW Golf"
70
+ fleet.transports[1].year.should == 2001
71
+ fleet.transports[2].class.should == TrModels::Car
72
+ fleet.transports[2].license_plate.should == "DEF123"
73
+ fleet.transports[2].model.should == "Honda Accord"
74
+ fleet.transports[2].year.should == 2008
75
+ fleet.save.should be_true
76
+
77
+ from_db = TrModels::Fleet.find(fleet.id)
78
+ from_db.transports.size.should == 3
79
+ from_db.transports[0].license_plate.should == "GGG123"
80
+ from_db.transports[0].icu.should be_true
81
+ from_db.transports[1].license_plate.should == "ABC123"
82
+ from_db.transports[1].model.should == "VW Golf"
83
+ from_db.transports[1].year.should == 2001
84
+ from_db.transports[2].license_plate.should == "DEF123"
85
+ from_db.transports[2].model.should == "Honda Accord"
86
+ from_db.transports[2].year.should == 2008
87
+ end
88
+
89
+ should "default reader to empty array" do
90
+ fleet = TrModels::Fleet.new
91
+ fleet.transports.should == []
92
+ end
93
+
94
+ should "allow adding to association like it was an array" do
95
+ fleet = TrModels::Fleet.new
96
+ fleet.transports << TrModels::Car.new
97
+ fleet.transports.push TrModels::Bus.new
98
+ fleet.transports.size.should == 2
99
+ end
100
+
101
+ should "be able to replace the association" do
102
+ fleet = TrModels::Fleet.new
103
+ fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")]
104
+ fleet.save.should be_true
105
+
106
+ from_db = TrModels::Fleet.find(fleet.id)
107
+ from_db.transports.size.should == 1
108
+ from_db.transports[0].license_plate.should == "DCU2013"
109
+ end
110
+
111
+ should "store different associations" do
112
+ fleet = TrModels::Fleet.new
113
+ fleet.transports = [
114
+ TrModels::Car.new("license_plate" => "ABC1223", "model" => "Honda Civic", "year" => 2003),
115
+ TrModels::Bus.new("license_plate" => "XYZ9090", "max_passengers" => 51),
116
+ TrModels::Ambulance.new("license_plate" => "HDD3030", "icu" => true)
117
+ ]
118
+ fleet.save.should be_true
119
+
120
+ from_db = TrModels::Fleet.find(fleet.id)
121
+ from_db.transports.size.should == 3
122
+ from_db.transports[0].license_plate.should == "ABC1223"
123
+ from_db.transports[0].model.should == "Honda Civic"
124
+ from_db.transports[0].year.should == 2003
125
+ from_db.transports[1].license_plate.should == "XYZ9090"
126
+ from_db.transports[1].max_passengers.should == 51
127
+ from_db.transports[2].license_plate.should == "HDD3030"
128
+ from_db.transports[2].icu.should == true
129
+ end
130
+ end
131
131
  end
@@ -1,107 +1,169 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class ManyEmbeddedProxyTest < Test::Unit::TestCase
5
- def setup
6
- clear_all_collections
7
- end
8
-
9
- should "default reader to empty array" do
10
- Project.new.addresses.should == []
11
- Project.new.addresses.inspect.should == "[]"
12
- end
13
-
14
- should "allow adding to association like it was an array" do
15
- project = Project.new
16
- project.addresses << Address.new
17
- project.addresses.push Address.new
18
- project.addresses.size.should == 2
19
- end
20
-
21
- should "be embedded in document on save" do
22
- sb = Address.new(:city => 'South Bend', :state => 'IN')
23
- chi = Address.new(:city => 'Chicago', :state => 'IL')
24
- project = Project.new
25
- project.addresses << sb
26
- project.addresses << chi
27
- project.save
28
-
29
- from_db = Project.find(project.id)
30
- from_db.addresses.size.should == 2
31
- from_db.addresses[0].should == sb
32
- from_db.addresses[1].should == chi
33
- end
34
-
35
- should "allow embedding arbitrarily deep" do
36
- @document = Class.new do
37
- include MongoMapper::Document
38
- key :person, Person
39
- end
40
- @document.collection.clear
41
-
42
- meg = Person.new(:name => "Meg")
43
- meg.child = Person.new(:name => "Steve")
44
- meg.child.child = Person.new(:name => "Linda")
45
-
46
- doc = @document.new(:person => meg)
47
- doc.save
48
-
49
- from_db = @document.find(doc.id)
50
- from_db.person.name.should == 'Meg'
51
- from_db.person.child.name.should == 'Steve'
52
- from_db.person.child.child.name.should == 'Linda'
53
- end
54
-
55
- should "allow assignment of 'many' embedded documents using a hash" do
56
- person_attributes = {
57
- "name" => "Mr. Pet Lover",
58
- "pets" => [
59
- {"name" => "Jimmy", "species" => "Cocker Spainel"},
60
- {"name" => "Sasha", "species" => "Siberian Husky"},
61
- ]
62
- }
63
-
64
- pet_lover = RealPerson.new(person_attributes)
65
- pet_lover.name.should == "Mr. Pet Lover"
66
- pet_lover.pets[0].name.should == "Jimmy"
67
- pet_lover.pets[0].species.should == "Cocker Spainel"
68
- pet_lover.pets[1].name.should == "Sasha"
69
- pet_lover.pets[1].species.should == "Siberian Husky"
70
- pet_lover.save.should be_true
71
-
72
- from_db = RealPerson.find(pet_lover.id)
73
- from_db.name.should == "Mr. Pet Lover"
74
- from_db.pets[0].name.should == "Jimmy"
75
- from_db.pets[0].species.should == "Cocker Spainel"
76
- from_db.pets[1].name.should == "Sasha"
77
- from_db.pets[1].species.should == "Siberian Husky"
78
- end
79
-
80
- should "allow saving embedded documents in 'many' embedded documents" do
81
- @document = Class.new do
82
- include MongoMapper::Document
83
- many :people
84
- end
85
- @document.collection.clear
86
-
87
- meg = Person.new(:name => "Meg")
88
- sparky = Pet.new(:name => "Sparky", :species => "Dog")
89
- koda = Pet.new(:name => "Koda", :species => "Dog")
90
-
91
- doc = @document.new
92
-
93
- meg.pets << sparky
94
- meg.pets << koda
95
-
96
- doc.people << meg
97
- doc.save
98
-
99
- from_db = @document.find(doc.id)
100
- from_db.people.first.name.should == "Meg"
101
- from_db.people.first.pets.should_not == []
102
- from_db.people.first.pets.first.name.should == "Sparky"
103
- from_db.people.first.pets.first.species.should == "Dog"
104
- from_db.people.first.pets[1].name.should == "Koda"
105
- from_db.people.first.pets[1].species.should == "Dog"
106
- end
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class ManyEmbeddedProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ clear_all_collections
7
+ end
8
+
9
+ should "default reader to empty array" do
10
+ Project.new.addresses.should == []
11
+ end
12
+
13
+ should "allow adding to association like it was an array" do
14
+ project = Project.new
15
+ project.addresses << Address.new
16
+ project.addresses.push Address.new
17
+ project.addresses.size.should == 2
18
+ end
19
+
20
+ should "allow finding :all embedded documents" do
21
+ project = Project.new
22
+ project.addresses << Address.new
23
+ project.addresses << Address.new
24
+ project.save
25
+ end
26
+
27
+ should "be embedded in document on save" do
28
+ sb = Address.new(:city => 'South Bend', :state => 'IN')
29
+ chi = Address.new(:city => 'Chicago', :state => 'IL')
30
+ project = Project.new
31
+ project.addresses << sb
32
+ project.addresses << chi
33
+ project.save
34
+
35
+ from_db = Project.find(project.id)
36
+ from_db.addresses.size.should == 2
37
+ from_db.addresses[0].should == sb
38
+ from_db.addresses[1].should == chi
39
+ end
40
+
41
+ should "allow embedding arbitrarily deep" do
42
+ @document = Class.new do
43
+ include MongoMapper::Document
44
+ key :person, Person
45
+ end
46
+
47
+ meg = Person.new(:name => "Meg")
48
+ meg.child = Person.new(:name => "Steve")
49
+ meg.child.child = Person.new(:name => "Linda")
50
+
51
+ doc = @document.new(:person => meg)
52
+ doc.save
53
+
54
+ from_db = @document.find(doc.id)
55
+ from_db.person.name.should == 'Meg'
56
+ from_db.person.child.name.should == 'Steve'
57
+ from_db.person.child.child.name.should == 'Linda'
58
+ end
59
+
60
+ should "allow assignment of 'many' embedded documents using a hash" do
61
+ person_attributes = {
62
+ "name" => "Mr. Pet Lover",
63
+ "pets" => [
64
+ {"name" => "Jimmy", "species" => "Cocker Spainel"},
65
+ {"name" => "Sasha", "species" => "Siberian Husky"},
66
+ ]
67
+ }
68
+
69
+ pet_lover = RealPerson.new(person_attributes)
70
+ pet_lover.name.should == "Mr. Pet Lover"
71
+ pet_lover.pets[0].name.should == "Jimmy"
72
+ pet_lover.pets[0].species.should == "Cocker Spainel"
73
+ pet_lover.pets[1].name.should == "Sasha"
74
+ pet_lover.pets[1].species.should == "Siberian Husky"
75
+ pet_lover.save.should be_true
76
+
77
+ from_db = RealPerson.find(pet_lover.id)
78
+ from_db.name.should == "Mr. Pet Lover"
79
+ from_db.pets[0].name.should == "Jimmy"
80
+ from_db.pets[0].species.should == "Cocker Spainel"
81
+ from_db.pets[1].name.should == "Sasha"
82
+ from_db.pets[1].species.should == "Siberian Husky"
83
+ end
84
+
85
+ context "embedding many embedded documents" do
86
+ setup do
87
+ @document = Class.new do
88
+ include MongoMapper::Document
89
+ many :people
90
+ end
91
+ end
92
+
93
+ should "persist all embedded documents" do
94
+ meg = Person.new(:name => "Meg")
95
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
96
+ koda = Pet.new(:name => "Koda", :species => "Dog")
97
+
98
+ doc = @document.new
99
+
100
+ meg.pets << sparky
101
+ meg.pets << koda
102
+
103
+ doc.people << meg
104
+ doc.save
105
+
106
+ from_db = @document.find(doc.id)
107
+ from_db.people.first.name.should == "Meg"
108
+ from_db.people.first.pets.should_not == []
109
+ from_db.people.first.pets.first.name.should == "Sparky"
110
+ from_db.people.first.pets.first.species.should == "Dog"
111
+ from_db.people.first.pets[1].name.should == "Koda"
112
+ from_db.people.first.pets[1].species.should == "Dog"
113
+ end
114
+
115
+ should "create a reference to the root document for all embedded documents before save" do
116
+ meg = Person.new(:name => "Meg")
117
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
118
+
119
+ doc = @document.new
120
+
121
+ doc.people << meg
122
+ meg.pets << sparky
123
+
124
+ doc.people.first._root_document.should == doc
125
+ doc.people.first.pets.first._root_document.should == doc
126
+ end
127
+
128
+ should "create properly-named reference to parent document when building off association proxy" do
129
+ person = RealPerson.new
130
+ pet = person.pets.build
131
+ person.should == pet.real_person
132
+ end
133
+
134
+
135
+ should "create a reference to the root document for all embedded documents" do
136
+ meg = Person.new(:name => "Meg")
137
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
138
+
139
+ doc = @document.new
140
+
141
+ meg.pets << sparky
142
+
143
+ doc.people << meg
144
+ doc.save
145
+
146
+ from_db = @document.find(doc.id)
147
+ from_db.people.first._root_document.should == doc
148
+ from_db.people.first.pets.first._root_document.should == doc
149
+ end
150
+ end
151
+
152
+ should "allow retrieval via find(:all)" do
153
+ meg = Person.new(:name => "Meg")
154
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
155
+
156
+ meg.pets << sparky
157
+
158
+ meg.pets.find(:all).should include(sparky)
159
+ end
160
+
161
+ should "allow retrieval via find(id)" do
162
+ meg = Person.new(:name => "Meg")
163
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
164
+
165
+ meg.pets << sparky
166
+
167
+ meg.pets.find(sparky.id).should == sparky
168
+ end
107
169
  end