mrkurt-mongo_mapper 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +139 -0
  8. data/lib/mongo_mapper/associations.rb +72 -0
  9. data/lib/mongo_mapper/associations/base.rb +113 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
  12. data/lib/mongo_mapper/associations/collection.rb +19 -0
  13. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
  14. data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
  15. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
  16. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
  17. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  18. data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
  19. data/lib/mongo_mapper/associations/proxy.rb +111 -0
  20. data/lib/mongo_mapper/callbacks.rb +61 -0
  21. data/lib/mongo_mapper/dirty.rb +117 -0
  22. data/lib/mongo_mapper/document.rb +496 -0
  23. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  24. data/lib/mongo_mapper/embedded_document.rb +380 -0
  25. data/lib/mongo_mapper/finder_options.rb +145 -0
  26. data/lib/mongo_mapper/key.rb +36 -0
  27. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  28. data/lib/mongo_mapper/pagination.rb +66 -0
  29. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  30. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +28 -0
  31. data/lib/mongo_mapper/serialization.rb +54 -0
  32. data/lib/mongo_mapper/serializers/json_serializer.rb +48 -0
  33. data/lib/mongo_mapper/support.rb +192 -0
  34. data/lib/mongo_mapper/validations.rb +39 -0
  35. data/mongo_mapper.gemspec +173 -0
  36. data/specs.watchr +30 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_proxy.rb +91 -0
  40. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  41. data/test/functional/associations/test_many_documents_proxy.rb +477 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  45. data/test/functional/associations/test_one_proxy.rb +131 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +33 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +159 -0
  50. data/test/functional/test_document.rb +1198 -0
  51. data/test/functional/test_embedded_document.rb +135 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_modifiers.rb +242 -0
  54. data/test/functional/test_pagination.rb +95 -0
  55. data/test/functional/test_rails_compatibility.rb +25 -0
  56. data/test/functional/test_string_id_compatibility.rb +72 -0
  57. data/test/functional/test_validations.rb +361 -0
  58. data/test/models.rb +271 -0
  59. data/test/support/custom_matchers.rb +55 -0
  60. data/test/support/timing.rb +16 -0
  61. data/test/test_helper.rb +27 -0
  62. data/test/unit/associations/test_base.rb +182 -0
  63. data/test/unit/associations/test_proxy.rb +91 -0
  64. data/test/unit/serializers/test_json_serializer.rb +189 -0
  65. data/test/unit/test_document.rb +236 -0
  66. data/test/unit/test_dynamic_finder.rb +125 -0
  67. data/test/unit/test_embedded_document.rb +709 -0
  68. data/test/unit/test_finder_options.rb +325 -0
  69. data/test/unit/test_key.rb +172 -0
  70. data/test/unit/test_mongo_mapper.rb +65 -0
  71. data/test/unit/test_pagination.rb +119 -0
  72. data/test/unit/test_rails_compatibility.rb +52 -0
  73. data/test/unit/test_serializations.rb +52 -0
  74. data/test/unit/test_support.rb +346 -0
  75. data/test/unit/test_time_zones.rb +40 -0
  76. data/test/unit/test_validations.rb +503 -0
  77. metadata +239 -0
data/test/models.rb ADDED
@@ -0,0 +1,271 @@
1
+ # custom type
2
+ class WindowSize
3
+ attr_reader :width, :height
4
+
5
+ def self.to_mongo(value)
6
+ value.to_a
7
+ end
8
+
9
+ def self.from_mongo(value)
10
+ value.is_a?(self) ? value : WindowSize.new(value)
11
+ end
12
+
13
+ def initialize(*args)
14
+ @width, @height = args.flatten
15
+ end
16
+
17
+ def to_a
18
+ [width, height]
19
+ end
20
+
21
+ def ==(other)
22
+ other.is_a?(self.class) && other.width == width && other.height == height
23
+ end
24
+ end
25
+
26
+ module AccountsExtensions
27
+ def inactive
28
+ all(:last_logged_in => nil)
29
+ end
30
+ end
31
+
32
+ class Post
33
+ include MongoMapper::Document
34
+
35
+ key :title, String
36
+ key :body, String
37
+
38
+ many :comments, :as => :commentable, :class_name => 'PostComment'
39
+
40
+ timestamps!
41
+ end
42
+
43
+ class PostComment
44
+ include MongoMapper::Document
45
+
46
+ key :username, String, :default => 'Anonymous'
47
+ key :body, String
48
+
49
+ key :commentable_id, ObjectId
50
+ key :commentable_type, String
51
+ belongs_to :commentable, :polymorphic => true
52
+
53
+ timestamps!
54
+ end
55
+
56
+ class Address
57
+ include MongoMapper::EmbeddedDocument
58
+
59
+ key :address, String
60
+ key :city, String
61
+ key :state, String
62
+ key :zip, Integer
63
+ end
64
+
65
+ class Message
66
+ include MongoMapper::Document
67
+
68
+ key :body, String
69
+ key :position, Integer
70
+ key :_type, String
71
+ key :room_id, ObjectId
72
+
73
+ belongs_to :room
74
+ end
75
+
76
+ class Enter < Message; end
77
+ class Exit < Message; end
78
+ class Chat < Message; end
79
+
80
+ class Room
81
+ include MongoMapper::Document
82
+
83
+ key :name, String
84
+ many :messages, :polymorphic => true, :order => 'position' do
85
+ def older
86
+ all(:position => {'$gt' => 5})
87
+ end
88
+ end
89
+ many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
90
+
91
+ many :accounts, :polymorphic => true, :extend => AccountsExtensions
92
+ end
93
+
94
+ class Account
95
+ include MongoMapper::Document
96
+
97
+ key :_type, String
98
+ key :room_id, ObjectId
99
+ key :last_logged_in, Time
100
+
101
+ belongs_to :room
102
+ end
103
+ class User < Account; end
104
+ class Bot < Account; end
105
+
106
+ class Answer
107
+ include MongoMapper::Document
108
+
109
+ key :body, String
110
+ end
111
+
112
+ module PeopleExtensions
113
+ def find_by_name(name)
114
+ detect { |p| p.name == name }
115
+ end
116
+ end
117
+
118
+ module CollaboratorsExtensions
119
+ def top
120
+ first
121
+ end
122
+ end
123
+
124
+ class Project
125
+ include MongoMapper::Document
126
+
127
+ key :name, String
128
+
129
+ many :people, :extend => PeopleExtensions
130
+ many :collaborators, :extend => CollaboratorsExtensions
131
+
132
+ many :statuses, :order => 'position' do
133
+ def open
134
+ all(:name => %w(New Assigned))
135
+ end
136
+ end
137
+
138
+ many :addresses do
139
+ def find_all_by_state(state)
140
+ # can't use select here for some reason
141
+ find_all { |a| a.state == state }
142
+ end
143
+ end
144
+ end
145
+
146
+ class Collaborator
147
+ include MongoMapper::Document
148
+ key :project_id, ObjectId
149
+ key :name, String
150
+ belongs_to :project
151
+ end
152
+
153
+ class Status
154
+ include MongoMapper::Document
155
+
156
+ key :project_id, ObjectId
157
+ key :target_id, ObjectId
158
+ key :target_type, String
159
+ key :name, String, :required => true
160
+ key :position, Integer
161
+
162
+ belongs_to :project
163
+ belongs_to :target, :polymorphic => true
164
+ end
165
+
166
+ class RealPerson
167
+ include MongoMapper::Document
168
+
169
+ key :room_id, ObjectId
170
+ key :name, String
171
+
172
+ belongs_to :room
173
+
174
+ many :pets
175
+
176
+ def realname=(n)
177
+ self.name = n
178
+ end
179
+ end
180
+
181
+ class Person
182
+ include MongoMapper::EmbeddedDocument
183
+
184
+ key :name, String
185
+ key :child, Person
186
+
187
+ many :pets
188
+ end
189
+
190
+ class Pet
191
+ include MongoMapper::EmbeddedDocument
192
+
193
+ key :name, String
194
+ key :species, String
195
+ end
196
+
197
+ class Media
198
+ include MongoMapper::EmbeddedDocument
199
+
200
+ key :_type, String
201
+ key :file, String
202
+
203
+ key :visible, Boolean
204
+ end
205
+
206
+ class Video < Media
207
+ key :length, Integer
208
+ end
209
+
210
+ class Image < Media
211
+ key :width, Integer
212
+ key :height, Integer
213
+ end
214
+
215
+ class Music < Media
216
+ key :bitrate, String
217
+ end
218
+
219
+ class Catalog
220
+ include MongoMapper::Document
221
+
222
+ many :medias, :polymorphic => true do
223
+ def visible
224
+ # for some reason we can't use select here
225
+ find_all { |m| m.visible? }
226
+ end
227
+ end
228
+ end
229
+
230
+ module TrModels
231
+ class Transport
232
+ include MongoMapper::EmbeddedDocument
233
+
234
+ key :_type, String
235
+ key :license_plate, String
236
+ key :purchased_on, Date
237
+ end
238
+
239
+ class Car < TrModels::Transport
240
+ include MongoMapper::EmbeddedDocument
241
+
242
+ key :model, String
243
+ key :year, Integer
244
+ end
245
+
246
+ class Bus < TrModels::Transport
247
+ include MongoMapper::EmbeddedDocument
248
+
249
+ key :max_passengers, Integer
250
+ end
251
+
252
+ class Ambulance < TrModels::Transport
253
+ include MongoMapper::EmbeddedDocument
254
+
255
+ key :icu, Boolean
256
+ end
257
+
258
+ class Fleet
259
+ include MongoMapper::Document
260
+
261
+ module TransportsExtension
262
+ def to_be_replaced
263
+ # for some reason we can't use select
264
+ find_all { |t| t.purchased_on < 2.years.ago.to_date }
265
+ end
266
+ end
267
+
268
+ many :transports, :polymorphic => true, :class_name => "TrModels::Transport", :extend => TransportsExtension
269
+ key :name, String
270
+ end
271
+ end
@@ -0,0 +1,55 @@
1
+ module CustomMatchers
2
+ custom_matcher :be_nil do |receiver, matcher, args|
3
+ matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
4
+ matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
5
+ receiver.nil?
6
+ end
7
+
8
+ custom_matcher :be_blank do |receiver, matcher, args|
9
+ matcher.positive_failure_message = "Expected #{receiver} to be blank but it wasn't"
10
+ matcher.negative_failure_message = "Expected #{receiver} not to be blank but it was"
11
+ receiver.blank?
12
+ end
13
+
14
+ custom_matcher :be_true do |receiver, matcher, args|
15
+ matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
16
+ matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
17
+ receiver.eql?(true)
18
+ end
19
+
20
+ custom_matcher :be_false do |receiver, matcher, args|
21
+ matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
22
+ matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
23
+ receiver.eql?(false)
24
+ end
25
+
26
+ custom_matcher :be_valid do |receiver, matcher, args|
27
+ matcher.positive_failure_message = "Expected to be valid but it was invalid #{receiver.errors.inspect}"
28
+ matcher.negative_failure_message = "Expected to be invalid but it was valid #{receiver.errors.inspect}"
29
+ receiver.valid?
30
+ end
31
+
32
+ custom_matcher :have_error_on do |receiver, matcher, args|
33
+ receiver.valid?
34
+ attribute = args[0]
35
+ expected_message = args[1]
36
+
37
+ if expected_message.nil?
38
+ matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
39
+ matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
40
+ !receiver.errors.on(attribute).blank?
41
+ else
42
+ actual = receiver.errors.on(attribute)
43
+ matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
44
+ matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
45
+ actual == expected_message
46
+ end
47
+ end
48
+
49
+ custom_matcher :have_index do |receiver, matcher, args|
50
+ index_name = args[0]
51
+ matcher.positive_failure_message = "#{receiver} does not have index named #{index_name}, but should"
52
+ matcher.negative_failure_message = "#{receiver} does have index named #{index_name}, but should not"
53
+ !receiver.collection.index_information.detect { |index| index[0] == index_name }.nil?
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ class Test::Unit::TestCase
2
+ def run_with_test_timing(*args, &block)
3
+ begin_time = Time.now
4
+ run_without_test_timing(*args, &block)
5
+ end_time = Time.now
6
+
7
+ duration = end_time - begin_time
8
+ threshold = 1.0
9
+
10
+ if duration > threshold
11
+ puts "\nSLOW TEST: #{duration} - #{self.name}"
12
+ end
13
+ end
14
+
15
+ alias_method_chain :run, :test_timing unless method_defined?(:run_without_test_timing)
16
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/mongo_mapper')
2
+
3
+ gem 'jnunemaker-matchy', '0.4.0'
4
+ gem 'shoulda', '2.10.2'
5
+ gem 'timecop', '0.3.1'
6
+ gem 'mocha', '0.9.8'
7
+
8
+ require 'matchy'
9
+ require 'shoulda'
10
+ require 'timecop'
11
+ require 'mocha'
12
+ require 'pp'
13
+
14
+ require 'support/custom_matchers'
15
+ require 'support/timing'
16
+
17
+ class Test::Unit::TestCase
18
+ include CustomMatchers
19
+ end
20
+
21
+ test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
22
+ FileUtils.mkdir_p(test_dir) unless File.exist?(test_dir)
23
+
24
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {
25
+ :logger => Logger.new(test_dir + '/test.log')
26
+ })
27
+ MongoMapper.database = 'test'
@@ -0,0 +1,182 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class FooMonster; end
5
+
6
+ class AssociationBaseTest < Test::Unit::TestCase
7
+ include MongoMapper::Associations
8
+
9
+ should "initialize with type and name" do
10
+ base = Base.new(:many, :foos)
11
+ base.type.should == :many
12
+ base.name.should == :foos
13
+ end
14
+
15
+ should "also allow options when initializing" do
16
+ base = Base.new(:many, :foos, :polymorphic => true)
17
+ base.options[:polymorphic].should be_true
18
+ end
19
+
20
+ context "class_name" do
21
+ should "work for belongs_to" do
22
+ Base.new(:belongs_to, :user).class_name.should == 'User'
23
+ end
24
+
25
+ should "work for many" do
26
+ Base.new(:many, :smart_people).class_name.should == 'SmartPerson'
27
+ end
28
+
29
+ should "be changeable using class_name option" do
30
+ base = Base.new(:many, :smart_people, :class_name => 'IntelligentPerson')
31
+ base.class_name.should == 'IntelligentPerson'
32
+ end
33
+ end
34
+
35
+ context "klass" do
36
+ should "default to class_name constantized" do
37
+ Base.new(:belongs_to, :foo_monster).klass.should == FooMonster
38
+ end
39
+
40
+ should "be the specified class" do
41
+ anonnymous_class = Class.new
42
+ Base.new(:belongs_to, :foo_monster, :class => anonnymous_class).klass.should == anonnymous_class
43
+ end
44
+ end
45
+
46
+ context "many?" do
47
+ should "be true if many" do
48
+ Base.new(:many, :foos).many?.should be_true
49
+ end
50
+
51
+ should "be false if not many" do
52
+ Base.new(:belongs_to, :foo).many?.should be_false
53
+ Base.new(:one, :foo).many?.should be_false
54
+ end
55
+ end
56
+
57
+ context "one?" do
58
+ should "be true if one" do
59
+ Base.new(:one, :foo).one?.should be_true
60
+ end
61
+
62
+ should "be false if not one" do
63
+ Base.new(:many, :foo).one?.should be_false
64
+ end
65
+ end
66
+
67
+ context "finder_options" do
68
+ should "default to empty hash" do
69
+ base = Base.new(:many, :foos)
70
+ base.finder_options.should == {}
71
+ end
72
+
73
+ should "work with order" do
74
+ base = Base.new(:many, :foos, :order => 'position')
75
+ base.finder_options.should == {:order => 'position'}
76
+ end
77
+
78
+ should "correctly parse from options" do
79
+ base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
80
+ base.finder_options.should == {:order => 'position', :somekey => 'somevalue'}
81
+ end
82
+ end
83
+
84
+ context "belongs_to?" do
85
+ should "be true if belongs_to" do
86
+ Base.new(:belongs_to, :foo).belongs_to?.should be_true
87
+ end
88
+
89
+ should "be false if not belongs_to" do
90
+ Base.new(:many, :foos).belongs_to?.should be_false
91
+ end
92
+ end
93
+
94
+ context "polymorphic?" do
95
+ should "be true if polymorphic" do
96
+ Base.new(:many, :foos, :polymorphic => true).polymorphic?.should be_true
97
+ end
98
+
99
+ should "be false if not polymorphic" do
100
+ Base.new(:many, :bars).polymorphic?.should be_false
101
+ end
102
+ end
103
+
104
+ context "type_key_name" do
105
+ should "be _type for many" do
106
+ Base.new(:many, :foos).type_key_name.should == '_type'
107
+ end
108
+
109
+ should "be association name _ type for belongs_to" do
110
+ Base.new(:belongs_to, :foo).type_key_name.should == 'foo_type'
111
+ end
112
+ end
113
+
114
+ context "foreign_key" do
115
+ should "default to assocation name _id for belongs to" do
116
+ base = Base.new(:belongs_to, :foo)
117
+ base.foreign_key.should == 'foo_id'
118
+ end
119
+
120
+ should "be overridable with :foreign_key option" do
121
+ base = Base.new(:belongs_to, :foo, :foreign_key => 'foobar_id')
122
+ base.foreign_key.should == 'foobar_id'
123
+ end
124
+ end
125
+
126
+ should "have ivar that is association name" do
127
+ Base.new(:belongs_to, :foo).ivar.should == '@_foo'
128
+ end
129
+
130
+ context "embeddable?" do
131
+ should "be true if class is embeddable" do
132
+ base = Base.new(:many, :medias)
133
+ base.embeddable?.should be_true
134
+ end
135
+
136
+ should "be false if class is not embeddable" do
137
+ base = Base.new(:many, :statuses)
138
+ base.embeddable?.should be_false
139
+
140
+ base = Base.new(:belongs_to, :project)
141
+ base.embeddable?.should be_false
142
+ end
143
+ end
144
+
145
+ context "proxy_class" do
146
+ should "be ManyDocumentsProxy for many" do
147
+ base = Base.new(:many, :statuses)
148
+ base.proxy_class.should == ManyDocumentsProxy
149
+ end
150
+
151
+ should "be ManyPolymorphicProxy for polymorphic many" do
152
+ base = Base.new(:many, :messages, :polymorphic => true)
153
+ base.proxy_class.should == ManyPolymorphicProxy
154
+ end
155
+
156
+ should "be ManyEmbeddedProxy for many embedded" do
157
+ base = Base.new(:many, :medias)
158
+ base.proxy_class.should == ManyEmbeddedProxy
159
+ end
160
+
161
+ should "be ManyEmbeddedPolymorphicProxy for polymorphic many embedded" do
162
+ base = Base.new(:many, :medias, :polymorphic => true)
163
+ base.proxy_class.should == ManyEmbeddedPolymorphicProxy
164
+ end
165
+
166
+ should "be BelongsToProxy for belongs_to" do
167
+ base = Base.new(:belongs_to, :project)
168
+ base.proxy_class.should == BelongsToProxy
169
+ end
170
+
171
+ should "be BelongsToPolymorphicProxy for polymorphic belongs_to" do
172
+ base = Base.new(:belongs_to, :target, :polymorphic => true)
173
+ base.proxy_class.should == BelongsToPolymorphicProxy
174
+ end
175
+
176
+ should "be OneProxy for one" do
177
+ base = Base.new(:one, :target, :polymorphic => true)
178
+ base.proxy_class.should == OneProxy
179
+ end
180
+ end
181
+
182
+ end