forked from railsadminteam/rails_admin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongoid_spec.rb
569 lines (503 loc) · 20.2 KB
/
mongoid_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
require 'spec_helper'
require 'timecop'
require 'rails_admin/adapters/mongoid'
describe RailsAdmin::Adapters::Mongoid do
describe '#associations' do
before :all do
RailsAdmin::AbstractModel.reset_polymorphic_parents
class MongoBlog
include Mongoid::Document
references_many :mongo_posts
references_many :mongo_comments, :as => :commentable
end
class MongoPost
include Mongoid::Document
referenced_in :mongo_blog
has_and_belongs_to_many :mongo_categories
references_many :mongo_comments, :as => :commentable
end
class MongoCategory
include Mongoid::Document
has_and_belongs_to_many :mongo_posts
end
class MongoUser
include Mongoid::Document
references_one :mongo_profile
field :name, :type => String
field :message, :type => String
field :short_text, :type => String
validates :short_text, :length => {:maximum => 255}
end
class MongoProfile
include Mongoid::Document
referenced_in :mongo_user
end
class MongoComment
include Mongoid::Document
referenced_in :commentable, :polymorphic => true
end
@blog = RailsAdmin::AbstractModel.new(MongoBlog)
@post = RailsAdmin::AbstractModel.new(MongoPost)
@category = RailsAdmin::AbstractModel.new(MongoCategory)
@user = RailsAdmin::AbstractModel.new(MongoUser)
@profile = RailsAdmin::AbstractModel.new(MongoProfile)
@comment = RailsAdmin::AbstractModel.new(MongoComment)
end
after :all do
RailsAdmin::AbstractModel.reset_polymorphic_parents
end
it 'lists associations' do
@post.associations.map{|a|a[:name]}.should == [:mongo_blog, :mongo_categories, :mongo_comments]
end
it 'reads correct and know types in [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]' do
(@post.associations + @blog.associations + @user.associations).map{|a|a[:type]}.uniq.sort.should == [:belongs_to, :has_and_belongs_to_many, :has_many, :has_one]
end
it "has correct parameter of belongs_to association" do
param = @post.associations.select{|a| a[:name] == :mongo_blog}.first
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_blog,
:pretty_name=>"Mongo blog",
:type=>:belongs_to,
:foreign_key=>:mongo_blog_id,
:foreign_type=>nil,
:as=>nil,
:polymorphic=>false,
:inverse_of=>nil,
:read_only=>nil,
:nested_form=>nil
}
param[:primary_key_proc].call.should == :_id
param[:model_proc].call.should == MongoBlog
end
it "has correct parameter of has_many association" do
param = @blog.associations.select{|a| a[:name] == :mongo_posts}.first
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_posts,
:pretty_name=>"Mongo posts",
:type=>:has_many,
:foreign_key=>:mongo_blog_id,
:foreign_type=>nil,
:as=>nil,
:polymorphic=>false,
:inverse_of=>nil,
:read_only=>nil,
:nested_form=>nil
}
param[:primary_key_proc].call.should == :_id
param[:model_proc].call.should == MongoPost
end
it "has correct parameter of has_and_belongs_to_many association" do
param = @post.associations.select{|a| a[:name] == :mongo_categories}.first
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:mongo_categories,
:pretty_name=>"Mongo categories",
:type=>:has_and_belongs_to_many,
:foreign_key=>:mongo_category_ids,
:foreign_type=>nil,
:as=>nil,
:polymorphic=>false,
:inverse_of=>nil,
:read_only=>nil,
:nested_form=>nil
}
param[:primary_key_proc].call.should == :_id
param[:model_proc].call.should == MongoCategory
end
it "has correct parameter of polymorphic belongs_to association" do
RailsAdmin::Config.stub!(:models_pool).and_return(["MongoBlog", "MongoPost", "MongoCategory", "MongoUser", "MongoProfile", "MongoComment"])
param = @comment.associations.select{|a| a[:name] == :commentable}.first
param.reject{|k, v| [:primary_key_proc, :model_proc].include? k }.should == {
:name=>:commentable,
:pretty_name=>"Commentable",
:type=>:belongs_to,
:foreign_key=>:commentable_id,
:foreign_type=>:commentable_type,
:as=>nil,
:polymorphic=>true,
:inverse_of=>nil,
:read_only=>nil,
:nested_form=>nil
}
param[:primary_key_proc].call.should == :_id
param[:model_proc].call.should == [MongoBlog, MongoPost]
end
end
describe "#properties" do
before :all do
@abstract_model = RailsAdmin::AbstractModel.new(MongoidFieldTest)
end
it "maps Mongoid column types to RA types" do
@abstract_model.properties.sort{|a,b| a[:name] <=> b[:name] }.should == [
{ :name => :_id,
:pretty_name => "Id",
:nullable? => true,
:serial? => false,
:type => :bson_object_id,
:length => nil },
{ :name => :_type,
:pretty_name => "Type",
:nullable? => true,
:serial? => false,
:type => :mongoid_type,
:length => 1024 },
{ :name => :array_field,
:pretty_name => "Array field",
:nullable? => true,
:serial? => false,
:type => :serialized,
:length => nil },
{ :name => :big_decimal_field,
:pretty_name => "Big decimal field",
:nullable? => true,
:serial? => false,
:type => :string,
:length => 1024 },
{ :name => :boolean_field,
:pretty_name => "Boolean field",
:nullable? => true,
:serial? => false,
:type => :boolean,
:length => nil },
{ :name => :bson_object_id_field,
:pretty_name => "Bson object id field",
:nullable? => true,
:serial? => false,
:type => :bson_object_id,
:length => nil },
{ :name => :date_field,
:pretty_name => "Date field",
:nullable? => true,
:serial? => false,
:type => :date,
:length => nil },
{ :name => :date_time_field,
:pretty_name => "Date time field",
:nullable? => true,
:serial? => false,
:type => :datetime,
:length => nil },
{ :name => :description,
:pretty_name => "Description",
:nullable? => true,
:serial? => false,
:type => :text,
:length => nil },
{ :name => :float_field,
:pretty_name => "Float field",
:nullable? => true,
:serial? => false,
:type => :float,
:length => nil },
{ :name => :hash_field,
:pretty_name => "Hash field",
:nullable? => true,
:serial? => false,
:type => :serialized,
:length => nil },
{ :name => :integer_field,
:pretty_name => "Integer field",
:nullable? => true,
:serial? => false,
:type => :integer,
:length => nil },
{ :name => :name,
:pretty_name => "Name",
:nullable? => true,
:serial? => false,
:type => :string,
:length => 255 },
{ :name => :object_field,
:pretty_name => "Object field",
:nullable? => true,
:serial? => false,
:type => :bson_object_id,
:length => nil },
{ :name => :short_text,
:pretty_name => "Short text",
:nullable? => true,
:serial? => false,
:type => :string,
:length => 255 },
{ :name => :subject,
:pretty_name => "Subject",
:nullable? => true,
:serial? => false,
:type => :string,
:length => 255 },
{ :name => :time_field,
:pretty_name => "Time field",
:nullable? => true,
:serial? => false,
:type => :datetime,
:length => nil },
{ :name => :title,
:pretty_name => "Title",
:nullable? => true,
:serial? => false,
:type => :string,
:length => 255 }
]
end
end
describe "data access method" do
before do
@articles = FactoryGirl.create_list(:article, 3)
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "#new returns instance of AbstractObject" do
@abstract_model.new.object.should be_instance_of(Article)
end
it "#get returns instance of AbstractObject" do
@abstract_model.get(@articles.first.id.to_s).object.should == @articles.first
end
it "#get returns nil when id does not exist" do
@abstract_model.get('4f4f0824dcf2315093000000').should be_nil
end
it "#first returns first item" do
@abstract_model.first.should == @articles.first
end
it "#count returns count of items" do
@abstract_model.count.should == @articles.count
end
it "#destroy destroys multiple items" do
@abstract_model.destroy(@articles[0..1])
Article.all.should == @articles[2..2]
end
describe "#all" do
it "works without options" do
@abstract_model.all.sort.should == @articles.sort
end
it "supports eager loading" do
@abstract_model.all(:include => :author).inclusions.map{|i| i.class_name}.should == ["Author"]
end
it "supports limiting" do
@abstract_model.all(:limit => 2).to_a.count.should == 2
end
it "supports retrieval by bulk_ids" do
@abstract_model.all(:bulk_ids => @articles[0..1].map{|article| article.id.to_s }).
sort.should == @articles[0..1].sort
end
it "supports pagination" do
@abstract_model.all(:sort => :_id, :page => 2, :per => 1).to_a.should == @articles[1..1]
# To prevent RSpec matcher to call Mongoid::Criteria#== method,
# (we want to test equality of query result, not of Mongoid criteria)
# to_a is added to invoke Mongoid query
end
it "supports ordering" do
@abstract_model.all(:sort => :_id, :sort_reverse => false).to_a.should == @articles.sort
@abstract_model.all(:sort => :_id, :sort_reverse => true).to_a.should == @articles.sort.reverse
end
it "supports querying" do
@abstract_model.all(:query => @articles[1].title).should == @articles[1..1]
end
it "supports filtering" do
@abstract_model.all(:filters => {"title" => {"0000" => {:o=>"is", :v=>@articles[1].title}}}).should == @articles[1..1]
end
it "ignores non-existent field name on filtering" do
lambda{ @abstract_model.all(:filters => {"dummy" => {"0000" => {:o=>"is", :v=>@articles[1].title}}}) }.should_not raise_error
end
end
end
describe "searching on association" do
describe "whose type is belongs_to" do
before do
RailsAdmin.config Article do
field :author do
queryable true
end
end
@articles = FactoryGirl.create_list(:article, 3)
@author = FactoryGirl.create :author, :name => 'foobar'
@author.articles << @articles[1]
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "supports querying" do
@abstract_model.all(:query => 'foobar').to_a.should == @articles[1..1]
end
it "supports filtering" do
@abstract_model.all(:filters => {"author" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @articles[1..1]
end
end
describe "whose type is has_many" do
before do
RailsAdmin.config Author do
field :articles do
queryable true
searchable :all
end
end
@authors = FactoryGirl.create_list(:author, 3)
@articles = [{:author => @authors[1]},
{:author => @authors[1], :title => 'foobar'},
{:author => @authors[2]}].map{|h| FactoryGirl.create :article, h}
@abstract_model = RailsAdmin::AbstractModel.new('Author')
end
it "supports querying" do
@abstract_model.all(:query => 'foobar').to_a.should == @authors[1..1]
end
it "supports filtering" do
@abstract_model.all(:filters => {"articles" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @authors[1..1]
end
end
describe "whose type is has_and_belongs_to_many" do
before do
RailsAdmin.config Article do
field :tags do
queryable true
searchable :all
end
end
@articles = FactoryGirl.create_list(:article, 3)
@tags = [{}, {:name => 'foobar'}, {}].map{|h| FactoryGirl.create :tag, h}
@articles[1].tags = [@tags[0], @tags[1]]
@articles[2].tags << @tags[2]
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "supports querying" do
@abstract_model.all(:query => 'foobar').to_a.should == @articles[1..1]
end
it "supports filtering" do
@abstract_model.all(:filters => {"tags" => {"0000" => {:o=>"is", :v=>'foobar'}}}).to_a.should == @articles[1..1]
end
end
end
describe "#query_conditions" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "returns query statement" do
@abstract_model.send(:query_conditions, "word").should ==
{"$or"=>[{"title"=>/word/}, {"body"=>/word/}]}
end
end
describe "#filter_conditions" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "returns filter statement" do
@author = FactoryGirl.create :author, :name => 'Author 1'
@abstract_model.send(
:filter_conditions,
{"title" => {"0000" => {:o=>"is", :v=>"foo"}},
"author" => {"0001" => {:o=>"like", :v=>"1"}}}
).should == {"$and"=>[{"title"=>"foo"}, {"author_id"=>{"$in"=>[@author.id]}}]}
end
end
describe "#build_statement" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "ignores '_discard' operator or value" do
[["_discard", ""], ["", "_discard"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should be_nil
end
end
it "supports '_blank' operator" do
[["_blank", ""], ["", "_blank"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$in"=>[nil, ""]}}
end
end
it "supports '_present' operator" do
[["_present", ""], ["", "_present"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$nin"=>[nil, ""]}}
end
end
it "supports '_null' operator" do
[["_null", ""], ["", "_null"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>nil}
end
end
it "supports '_not_null' operator" do
[["_not_null", ""], ["", "_not_null"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$ne"=>nil}}
end
end
it "supports '_empty' operator" do
[["_empty", ""], ["", "_empty"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>""}
end
end
it "supports '_not_empty' operator" do
[["_not_empty", ""], ["", "_not_empty"]].each do |value, operator|
@abstract_model.send(:build_statement, :name, :string, value, operator).should == {:name=>{"$ne"=>""}}
end
end
it "supports boolean type query" do
['false', 'f', '0'].each do |value|
@abstract_model.send(:build_statement, :field, :boolean, value, nil).should == {:field => false}
end
['true', 't', '1'].each do |value|
@abstract_model.send(:build_statement, :field, :boolean, value, nil).should == {:field => true}
end
@abstract_model.send(:build_statement, :field, :boolean, 'word', nil).should be_nil
end
it "supports integer type query" do
@abstract_model.send(:build_statement, :field, :integer, "1", nil).should == {:field => 1}
@abstract_model.send(:build_statement, :field, :integer, 'word', nil).should be_nil
end
it "supports string type query" do
@abstract_model.send(:build_statement, :field, :string, "", nil).should be_nil
@abstract_model.send(:build_statement, :field, :string, "foo", "was").should be_nil
@abstract_model.send(:build_statement, :field, :string, "foo", "default").should == {:field=>/foo/}
@abstract_model.send(:build_statement, :field, :string, "foo", "like").should == {:field=>/foo/}
@abstract_model.send(:build_statement, :field, :string, "foo", "starts_with").should == {:field=>/^foo/}
@abstract_model.send(:build_statement, :field, :string, "foo", "ends_with").should == {:field=>/foo$/}
@abstract_model.send(:build_statement, :field, :string, "foo", "is").should == {:field=>'foo'}
end
[:datetime, :timestamp, :date].each do |type|
it "supports #{type} query" do
@abstract_model.send(:build_statement, :field, type, "", "default").should be_nil
@abstract_model.send(:build_statement, :field, type, "", "is").should be_nil
Timecop.freeze(Time.utc(2012,1,15,12,0,0)) do
@abstract_model.send(:build_statement, :field, type, "", "today").to_s.should ==
'{:field=>{"$gte"=>Sun, 15 Jan 2012 00:00:00 UTC +00:00, "$lte"=>Sun, 15 Jan 2012 23:59:59 UTC +00:00}}'
@abstract_model.send(:build_statement, :field, type, "", "yesterday").to_s.should ==
'{:field=>{"$gte"=>Sat, 14 Jan 2012 00:00:00 UTC +00:00, "$lte"=>Sat, 14 Jan 2012 23:59:59 UTC +00:00}}'
@abstract_model.send(:build_statement, :field, type, "", "this_week").to_s.should ==
'{:field=>{"$gte"=>Mon, 09 Jan 2012 00:00:00 UTC +00:00, "$lte"=>Sun, 15 Jan 2012 23:59:59 UTC +00:00}}'
@abstract_model.send(:build_statement, :field, type, "", "last_week").to_s.should ==
'{:field=>{"$gte"=>Mon, 02 Jan 2012 00:00:00 UTC +00:00, "$lte"=>Sun, 08 Jan 2012 23:59:59 UTC +00:00}}'
@abstract_model.send(:build_statement, :field, type, "", "less_than").should be_nil
@abstract_model.send(:build_statement, :field, type, "1", "less_than").to_s.should ==
'{:field=>{"$gte"=>Sat, 14 Jan 2012 12:00:00 UTC +00:00}}'
@abstract_model.send(:build_statement, :field, type, "1", "more_than").to_s.should ==
'{:field=>{"$lte"=>Sat, 14 Jan 2012 12:00:00 UTC +00:00}}'
end
@abstract_model.send(:build_statement, :field, type, "", "mmddyyyy").should be_nil
@abstract_model.send(:build_statement, :field, type, "201105", "mmddyyyy").should be_nil
@abstract_model.send(:build_statement, :field, type, "12312011", "mmddyyyy").to_s.should ==
'{:field=>{"$gte"=>Sat, 31 Dec 2011 00:00:00 UTC +00:00, "$lte"=>Sat, 31 Dec 2011 23:59:59 UTC +00:00}}'
end
end
it "supports enum type query" do
@abstract_model.send(:build_statement, :field, :enum, "1", nil).should == {:field => {"$in" => ["1"]}}
end
end
describe "model attribute method" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('Article')
end
it "#scoped returns relation object" do
@abstract_model.scoped.should be_instance_of(Mongoid::Criteria)
end
it "#table_name works" do
@abstract_model.table_name.should == 'articles'
end
end
describe "serialization" do
before do
@abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
@controller = RailsAdmin::MainController.new
end
it "accepts array value" do
params = {:array_field => '[1,3]'}
@controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
params[:array_field].should == [1, 3]
end
it "accepts hash value" do
params = {:hash_field => '{a: 1, b: 3}'}
@controller.send(:sanitize_params_for!, 'create', @abstract_model.config, params)
params[:hash_field].should == {"a"=>1, "b"=>3}
end
end
end