forked from jnunemaker/joint
/
test_joint.rb
executable file
·554 lines (461 loc) · 15.4 KB
/
test_joint.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
require 'helper'
class JointTest < Test::Unit::TestCase
include JointTestHelpers
def setup
super
@file = open_file('unixref.pdf')
@image = open_file('mr_t.jpg')
@image2 = open_file('harmony.png')
@test1 = open_file('test1.txt')
@test2 = open_file('test2.txt')
end
def teardown
all_files.each { |file| file.close }
end
context "Using Joint plugin" do
should "add each attachment to attachment_names" do
Asset.attachment_names.should == Set.new([:image, :file])
EmbeddedAsset.attachment_names.should == Set.new([:image, :file])
end
should "add keys for each attachment" do
key_names.each do |key|
Asset.keys.should include("image_#{key}")
Asset.keys.should include("file_#{key}")
EmbeddedAsset.keys.should include("image_#{key}")
EmbeddedAsset.keys.should include("file_#{key}")
end
end
should "add memoized accessors module" do
Asset.attachment_accessor_module.should be_instance_of(Module)
EmbeddedAsset.attachment_accessor_module.should be_instance_of(Module)
end
context "with inheritance" do
should "add attachment to attachment_names" do
BaseModel.attachment_names.should == Set.new([:file])
end
should "inherit attachments from superclass, but not share other inherited class attachments" do
Image.attachment_names.should == Set.new([:file, :image])
Video.attachment_names.should == Set.new([:file, :video])
end
should "add inherit keys from superclass" do
key_names.each do |key|
BaseModel.keys.should include("file_#{key}")
Image.keys.should include("file_#{key}")
Image.keys.should include("image_#{key}")
Video.keys.should include("file_#{key}")
Video.keys.should include("video_#{key}")
end
end
end
end
context "Assigning new attachments to document" do
setup do
@doc = Asset.create(:image => @image, :file => @file)
rewind_files
end
subject { @doc }
should "assign GridFS content_type" do
grid.get(subject.image_id).content_type.should == 'image/jpeg'
grid.get(subject.file_id).content_type.should == 'application/pdf'
end
should "assign joint keys" do
subject.image_size.should == 13661
subject.file_size.should == 68926
subject.image_type.should == "image/jpeg"
subject.file_type.should == "application/pdf"
subject.image_id.should_not be_nil
subject.file_id.should_not be_nil
subject.image_id.should be_instance_of(BSON::ObjectId)
subject.file_id.should be_instance_of(BSON::ObjectId)
end
should "allow accessing keys through attachment proxy" do
subject.image.size.should == 13661
subject.file.size.should == 68926
subject.image.type.should == "image/jpeg"
subject.file.type.should == "application/pdf"
subject.image.id.should_not be_nil
subject.file.id.should_not be_nil
subject.image.id.should be_instance_of(BSON::ObjectId)
subject.file.id.should be_instance_of(BSON::ObjectId)
end
should "proxy unknown methods to GridIO object" do
subject.image.files_id.should == subject.image_id
subject.image.content_type.should == 'image/jpeg'
subject.image.filename.should == 'mr_t.jpg'
subject.image.file_length.should == 13661
end
should "assign file name from path if original file name not available" do
subject.image_name.should == 'mr_t.jpg'
subject.file_name.should == 'unixref.pdf'
end
should "save attachment contents correctly" do
subject.file.read.should == @file.read
subject.image.read.should == @image.read
end
should "know that attachment exists" do
subject.image?.should be(true)
subject.file?.should be(true)
end
should "respond with false when asked if the attachment is blank?" do
subject.image.blank?.should be(false)
subject.file.blank?.should be(false)
end
should "clear assigned attachments so they don't get uploaded twice" do
Mongo::Grid.any_instance.expects(:put).never
subject.save
end
end
context "Assigning new attachments to embedded document" do
setup do
@asset = Asset.new
@doc = @asset.embedded_assets.build(:image => @image, :file => @file)
@asset.save!
rewind_files
end
subject { @doc }
should "assign GridFS content_type" do
grid.get(subject.image_id).content_type.should == 'image/jpeg'
grid.get(subject.file_id).content_type.should == 'application/pdf'
end
should "assign joint keys" do
subject.image_size.should == 13661
subject.file_size.should == 68926
subject.image_type.should == "image/jpeg"
subject.file_type.should == "application/pdf"
subject.image_id.should_not be_nil
subject.file_id.should_not be_nil
subject.image_id.should be_instance_of(BSON::ObjectId)
subject.file_id.should be_instance_of(BSON::ObjectId)
end
should "allow accessing keys through attachment proxy" do
subject.image.size.should == 13661
subject.file.size.should == 68926
subject.image.type.should == "image/jpeg"
subject.file.type.should == "application/pdf"
subject.image.id.should_not be_nil
subject.file.id.should_not be_nil
subject.image.id.should be_instance_of(BSON::ObjectId)
subject.file.id.should be_instance_of(BSON::ObjectId)
end
should "proxy unknown methods to GridIO object" do
subject.image.files_id.should == subject.image_id
subject.image.content_type.should == 'image/jpeg'
subject.image.filename.should == 'mr_t.jpg'
subject.image.file_length.should == 13661
end
should "assign file name from path if original file name not available" do
subject.image_name.should == 'mr_t.jpg'
subject.file_name.should == 'unixref.pdf'
end
should "save attachment contents correctly" do
subject.file.read.should == @file.read
subject.image.read.should == @image.read
end
should "know that attachment exists" do
subject.image?.should be(true)
subject.file?.should be(true)
end
should "respond with false when asked if the attachment is blank?" do
subject.image.blank?.should be(false)
subject.file.blank?.should be(false)
end
should "clear assigned attachments so they don't get uploaded twice" do
Mongo::Grid.any_instance.expects(:put).never
subject.save
end
end
context "Updating existing attachment" do
setup do
@doc = Asset.create(:file => @test1)
assert_no_grid_difference do
@doc.file = @test2
@doc.save!
end
rewind_files
end
subject { @doc }
should "not change attachment id" do
subject.file_id_changed?.should be(false)
end
should "update keys" do
subject.file_name.should == 'test2.txt'
subject.file_type.should == "text/plain"
subject.file_size.should == 5
end
should "update GridFS" do
grid.get(subject.file_id).filename.should == 'test2.txt'
grid.get(subject.file_id).content_type.should == 'text/plain'
grid.get(subject.file_id).file_length.should == 5
grid.get(subject.file_id).read.should == @test2.read
end
end
context "Updating existing attachment in embedded document" do
setup do
@asset = Asset.new
@doc = @asset.embedded_assets.build(:file => @test1)
@asset.save!
assert_no_grid_difference do
@doc.file = @test2
@doc.save!
end
rewind_files
end
subject { @doc }
should "update keys" do
subject.file_name.should == 'test2.txt'
subject.file_type.should == "text/plain"
subject.file_size.should == 5
end
should "update GridFS" do
grid.get(subject.file_id).filename.should == 'test2.txt'
grid.get(subject.file_id).content_type.should == 'text/plain'
grid.get(subject.file_id).file_length.should == 5
grid.get(subject.file_id).read.should == @test2.read
end
end
context "Updating document but not attachments" do
setup do
@doc = Asset.create(:image => @image)
@doc.update_attributes(:title => 'Updated')
@doc.reload
rewind_files
end
subject { @doc }
should "not affect attachment" do
subject.image.read.should == @image.read
end
should "update document attributes" do
subject.title.should == 'Updated'
end
end
context "Updating embedded document but not attachments" do
setup do
@asset = Asset.new
@doc = @asset.embedded_assets.build(:image => @image)
@doc.update_attributes(:title => 'Updated')
@asset.reload
@doc = @asset.embedded_assets.first
rewind_files
end
subject { @doc }
should "not affect attachment" do
subject.image.read.should == @image.read
end
should "update document attributes" do
subject.title.should == 'Updated'
end
end
context "Assigning file where file pointer is not at beginning" do
setup do
@image.read
@doc = Asset.create(:image => @image)
@doc.reload
rewind_files
end
subject { @doc }
should "rewind and correctly store contents" do
subject.image.read.should == @image.read
end
end
context "Setting attachment to nil" do
setup do
@doc = Asset.create(:image => @image)
rewind_files
end
subject { @doc }
should "delete attachment after save" do
assert_no_grid_difference { subject.image = nil }
assert_grid_difference(-1) { subject.save }
end
should "know that the attachment has been nullified" do
subject.image = nil
subject.image?.should be(false)
end
should "respond with true when asked if the attachment is nil?" do
subject.image = nil
subject.image.nil?.should be(true)
end
should "respond with true when asked if the attachment is blank?" do
subject.image = nil
subject.image.blank?.should be(true)
end
should "clear nil attachments after save and not attempt to delete again" do
Mongo::Grid.any_instance.expects(:delete).once
subject.image = nil
subject.save
Mongo::Grid.any_instance.expects(:delete).never
subject.save
end
should "clear id, name, type, size" do
subject.image = nil
subject.save
assert_nil subject.image_id
assert_nil subject.image_name
assert_nil subject.image_type
assert_nil subject.image_size
subject.reload
assert_nil subject.image_id
assert_nil subject.image_name
assert_nil subject.image_type
assert_nil subject.image_size
end
end
context "Setting attachment to nil on embedded document" do
setup do
@asset = Asset.new
@doc = @asset.embedded_assets.build(:image => @image)
@asset.save!
rewind_files
end
subject { @doc }
should "delete attachment after save" do
assert_no_grid_difference { subject.image = nil }
assert_grid_difference(-1) { subject.save }
end
should "know that the attachment has been nullified" do
subject.image = nil
subject.image?.should be(false)
end
should "respond with true when asked if the attachment is nil?" do
subject.image = nil
subject.image.nil?.should be(true)
end
should "respond with true when asked if the attachment is blank?" do
subject.image = nil
subject.image.blank?.should be(true)
end
should "clear nil attachments after save and not attempt to delete again" do
Mongo::Grid.any_instance.expects(:delete).once
subject.image = nil
subject.save
Mongo::Grid.any_instance.expects(:delete).never
subject.save
end
should "clear id, name, type, size" do
subject.image = nil
subject.save
assert_nil subject.image_id
assert_nil subject.image_name
assert_nil subject.image_type
assert_nil subject.image_size
s = subject._root_document.reload.embedded_assets.first
assert_nil s.image_id
assert_nil s.image_name
assert_nil s.image_type
assert_nil s.image_size
end
end
context "Retrieving attachment that does not exist" do
setup do
@doc = Asset.create
rewind_files
end
subject { @doc }
should "know that the attachment is not present" do
subject.image?.should be(false)
end
should "respond with true when asked if the attachment is nil?" do
subject.image.nil?.should be(true)
end
should "raise Mongo::GridFileNotFound" do
assert_raises(Mongo::GridFileNotFound) { subject.image.read }
end
end
context "Destroying a document" do
setup do
@doc = Asset.create(:image => @image)
rewind_files
end
subject { @doc }
should "remove files from grid fs as well" do
assert_grid_difference(-1) { subject.destroy }
end
end
context "Destroying an embedded document's _root_document" do
setup do
@asset = Asset.new
@doc = @asset.embedded_assets.build(:image => @image)
@doc.save!
rewind_files
end
subject { @doc }
should "remove files from grid fs as well" do
assert_grid_difference(-1) { subject._root_document.destroy }
end
end
# What about when an embedded document is removed?
context "Assigning file name" do
should "default to path" do
Asset.create(:image => @image).image.name.should == 'mr_t.jpg'
end
should "use original_filename if available" do
def @image.original_filename
'testing.txt'
end
doc = Asset.create(:image => @image)
assert_equal 'testing.txt', doc.image_name
end
end
context "Validating attachment presence" do
setup do
@model_class = Class.new do
include MongoMapper::Document
plugin Joint
attachment :file, :required => true
def self.name; "Foo"; end
end
end
should "work" do
model = @model_class.new
model.should_not be_valid
model.file = @file
model.should be_valid
model.file = nil
model.should_not be_valid
model.file = @image
model.should be_valid
end
end
context "Assigning joint io instance" do
setup do
io = Joint::IO.new({
:name => 'foo.txt',
:type => 'plain/text',
:content => 'This is my stuff'
})
@asset = Asset.create(:file => io)
end
should "work" do
@asset.file_name.should == 'foo.txt'
@asset.file_size.should == 16
@asset.file_type.should == 'plain/text'
@asset.file.read.should == 'This is my stuff'
end
end
context "A font file" do
setup do
@file = open_file('font.eot')
@doc = Asset.create(:file => @file)
end
subject { @doc }
should "assign joint keys" do
subject.file_size.should == 17610
subject.file_type.should == "application/octet-stream"
subject.file_id.should_not be_nil
subject.file_id.should be_instance_of(BSON::ObjectId)
end
end
context "A music file" do
setup do
@file = open_file('example.m4r')
@doc = Asset.create(:file => @file)
end
subject { @doc }
should "assign joint keys" do
subject.file_size.should == 50790
subject.file_type.should == "audio/mp4"
subject.file_id.should_not be_nil
subject.file_id.should be_instance_of(BSON::ObjectId)
end
end
end