2011-04-13 18:35:53 -04:00
|
|
|
# encoding: utf-8
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2005-01-01 13:34:39 -05:00
|
|
|
|
2007-11-21 20:29:19 -05:00
|
|
|
# Without using prepared statements, it makes no sense to test
|
|
|
|
# BLOB data with DB2 or Firebird, because the length of a statement
|
|
|
|
# is limited to 32KB.
|
2008-11-19 11:09:44 -05:00
|
|
|
unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter)
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/binary'
|
2005-09-26 16:29:06 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class BinaryTest < ActiveRecord::TestCase
|
2012-01-31 00:26:41 -05:00
|
|
|
FIXTURES = %w(flowers.jpg example.log test.txt)
|
2005-07-01 13:20:04 -04:00
|
|
|
|
2011-04-13 18:35:53 -04:00
|
|
|
def test_mixed_encoding
|
|
|
|
str = "\x80"
|
2011-12-25 06:34:58 -05:00
|
|
|
str.force_encoding('ASCII-8BIT')
|
2011-04-13 18:35:53 -04:00
|
|
|
|
|
|
|
binary = Binary.new :name => 'いただきます!', :data => str
|
|
|
|
binary.save!
|
|
|
|
binary.reload
|
|
|
|
assert_equal str, binary.data
|
|
|
|
|
|
|
|
name = binary.name
|
|
|
|
|
|
|
|
# Mysql adapter doesn't properly encode things, so we have to do it
|
|
|
|
if current_adapter?(:MysqlAdapter)
|
2013-01-28 02:59:50 -05:00
|
|
|
name.force_encoding(Encoding::UTF_8)
|
2011-04-13 18:35:53 -04:00
|
|
|
end
|
|
|
|
assert_equal 'いただきます!', name
|
|
|
|
end
|
|
|
|
|
2005-09-26 16:29:06 -04:00
|
|
|
def test_load_save
|
2007-11-21 20:29:19 -05:00
|
|
|
Binary.delete_all
|
|
|
|
|
|
|
|
FIXTURES.each do |filename|
|
2008-04-01 02:58:52 -04:00
|
|
|
data = File.read(ASSETS_ROOT + "/#{filename}")
|
2011-12-25 06:34:58 -05:00
|
|
|
data.force_encoding('ASCII-8BIT')
|
2008-04-01 02:58:52 -04:00
|
|
|
data.freeze
|
2007-11-21 20:29:19 -05:00
|
|
|
|
|
|
|
bin = Binary.new(:data => data)
|
|
|
|
assert_equal data, bin.data, 'Newly assigned data differs from original'
|
2005-01-01 13:34:39 -05:00
|
|
|
|
2007-11-21 20:29:19 -05:00
|
|
|
bin.save!
|
|
|
|
assert_equal data, bin.data, 'Data differs from original after save'
|
2005-01-01 13:34:39 -05:00
|
|
|
|
2007-11-21 20:29:19 -05:00
|
|
|
assert_equal data, bin.reload.data, 'Reloaded data differs from original'
|
|
|
|
end
|
2005-09-26 16:29:06 -04:00
|
|
|
end
|
2005-01-01 13:34:39 -05:00
|
|
|
end
|
2005-01-03 18:00:44 -05:00
|
|
|
end
|