| 1 |
require File.dirname(__FILE__) + '/../test_helper' |
|---|
| 2 |
|
|---|
| 3 |
class AttachmentTest < Test::Unit::TestCase |
|---|
| 4 |
|
|---|
| 5 |
def setup |
|---|
| 6 |
setup_settings |
|---|
| 7 |
|
|---|
| 8 |
user = create_user('test') |
|---|
| 9 |
user_group = UserGroup.create!(:name => 'Test', :info => 'test') |
|---|
| 10 |
user_group.add_user(user) |
|---|
| 11 |
UserAccount.current_user = user |
|---|
| 12 |
@root_asset = create_root_asset |
|---|
| 13 |
@root_for_user = add_user_group_to_asset(user_group, @root_asset, true, true, true, true) |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
def test_Attachment_find_without_data |
|---|
| 17 |
assert_equal 0, Attachment.count |
|---|
| 18 |
attachment = create_attachment(@root_asset, true) |
|---|
| 19 |
|
|---|
| 20 |
x = Attachment.find_without(:data, attachment.id) |
|---|
| 21 |
Attachment.column_names.each { |c| |
|---|
| 22 |
if c == 'data' |
|---|
| 23 |
assert_raise(ActiveRecord::MissingAttributeError) { x.send(c) } |
|---|
| 24 |
else |
|---|
| 25 |
assert_equal true, x.respond_to?(c) |
|---|
| 26 |
assert_equal attachment.send(c).to_s, x.send(c).to_s |
|---|
| 27 |
end |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
# Just check that there is no typos in other finders |
|---|
| 31 |
assert [x], Attachment.find_authorized_to_read_without(:data, :all) |
|---|
| 32 |
assert Attachment.find_authorized_to_edit_without(:data, :all).empty? |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
# Testing type casting and insertion of binary data to database. |
|---|
| 36 |
def test_binary_column |
|---|
| 37 |
dir = File.dirname(__FILE__) |
|---|
| 38 |
filenames = [] |
|---|
| 39 |
['/../fixtures/test_files', |
|---|
| 40 |
'/../fixtures/open_document_files' |
|---|
| 41 |
].each {|rel_dir| |
|---|
| 42 |
Dir.foreach(dir + rel_dir) { |f| |
|---|
| 43 |
filenames << dir + rel_dir + '/' + f |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
data_hash = {} |
|---|
| 48 |
data_attribute_hash = {} |
|---|
| 49 |
|
|---|
| 50 |
filenames.map {|path| File.new(path, 'rb') }.each { |file| |
|---|
| 51 |
name = File.basename(file.path) |
|---|
| 52 |
next unless name == 'README' or ['.png', '.jpg', '.odt', '.pdf'].include?(File.extname(file.path).downcase) |
|---|
| 53 |
data = file.read |
|---|
| 54 |
file.close |
|---|
| 55 |
x = Attachment.new |
|---|
| 56 |
x.asset = @root_asset |
|---|
| 57 |
x.filename = name |
|---|
| 58 |
x.content_type = 'something' |
|---|
| 59 |
x.data = data |
|---|
| 60 |
#### DEBUG |
|---|
| 61 |
puts "\n" + 'File: ' + name |
|---|
| 62 |
#x.save! |
|---|
| 63 |
#f=File.new('tmp/%s_before' % name, 'wb') |
|---|
| 64 |
#f.write(data) |
|---|
| 65 |
#f.close |
|---|
| 66 |
#f=File.new('tmp/%s_after' % name, 'wb') |
|---|
| 67 |
#f.write(x.data) |
|---|
| 68 |
#f.close |
|---|
| 69 |
#### |
|---|
| 70 |
# Test that the binary data was not corrupted when it was |
|---|
| 71 |
# type casted or when it was saved/inserted to database or |
|---|
| 72 |
# when it is fetched from database. |
|---|
| 73 |
x.save! |
|---|
| 74 |
assert_equal data.size, x.data.size |
|---|
| 75 |
assert x.data == data |
|---|
| 76 |
#x.save! |
|---|
| 77 |
read_data = x.class.find(x.id).data |
|---|
| 78 |
assert_equal data.size, read_data.size |
|---|
| 79 |
assert read_data == data |
|---|
| 80 |
} |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
end |
|---|