class PostgresqlByteaTest

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 13
def setup
  @connection = ActiveRecord::Base.connection
  begin
    @connection.transaction do
      @connection.create_table("bytea_data_type") do |t|
        t.binary "payload"
        t.binary "serialized"
      end
    end
  end
  @column = ByteaDataType.columns_hash["payload"]
  @type = ByteaDataType.type_for_attribute("payload")
end
test_binary_columns_are_limitless_the_upper_limit_is_one_GB() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 36
def test_binary_columns_are_limitless_the_upper_limit_is_one_GB
  assert_equal "bytea", @connection.type_to_sql(:binary, limit: 100_000)
  assert_raise ActiveRecord::ActiveRecordError do
    @connection.type_to_sql(:binary, limit: 4294967295)
  end
end
test_column() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 31
def test_column
  assert @column.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLColumn)
  assert_equal :binary, @column.type
end
test_read_nil_value() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 68
def test_read_nil_value
  @connection.execute "insert into bytea_data_type (payload) VALUES (null)"
  record = ByteaDataType.first
  assert_nil(record.payload)
  record.delete
end
test_read_value() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 60
def test_read_value
  data = "\u001F"
  @connection.execute "insert into bytea_data_type (payload) VALUES ('#{data}')"
  record = ByteaDataType.first
  assert_equal(data, record.payload)
  record.delete
end
test_schema_dumping() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 132
def test_schema_dumping
  output = dump_table_schema("bytea_data_type")
  assert_match %r{t\.binary\s+"payload"$}, output
  assert_match %r{t\.binary\s+"serialized"$}, output
end
test_serialize() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 121
def test_serialize
  klass = Class.new(ByteaDataType) {
    serialize :serialized, Serializer.new
  }
  obj = klass.new
  obj.serialized = "hello world"
  obj.save!
  obj.reload
  assert_equal "hello world", obj.serialized
end
test_type_case_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 56
def test_type_case_nil
  assert_nil(@type.deserialize(nil))
end
test_type_cast_binary_converts_the_encoding() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 43
def test_type_cast_binary_converts_the_encoding
  assert @column

  data = "\u001F\x8B"
  assert_equal("UTF-8", data.encoding.name)
  assert_equal("ASCII-8BIT", @type.deserialize(data).encoding.name)
end
test_type_cast_binary_value() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 51
def test_type_cast_binary_value
  data = "\u001F\x8B".dup.force_encoding("BINARY")
  assert_equal(data, @type.deserialize(data))
end
test_via_to_sql() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 82
def test_via_to_sql
  data = "'\u001F\\"
  ByteaDataType.create(payload: data)
  sql = ByteaDataType.where(payload: data).select(:payload).to_sql
  result = @connection.query(sql)
  assert_equal([[data]], result)
end
test_via_to_sql_with_complicating_connection() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 90
def test_via_to_sql_with_complicating_connection
  Thread.new do
    other_conn = ActiveRecord::Base.connection
    other_conn.execute("SET standard_conforming_strings = off")
    other_conn.execute("SET escape_string_warning = off")
  end.join

  test_via_to_sql
end
test_write_binary() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 100
def test_write_binary
  data = File.read(File.join(__dir__, "..", "..", "..", "assets", "example.log"))
  assert(data.size > 1)
  record = ByteaDataType.create(payload: data)
  assert_not record.new_record?
  assert_equal(data, record.payload)
  assert_equal(data, ByteaDataType.where(id: record.id).first.payload)
end
test_write_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 109
def test_write_nil
  record = ByteaDataType.create(payload: nil)
  assert_not record.new_record?
  assert_nil(record.payload)
  assert_nil(ByteaDataType.where(id: record.id).first.payload)
end
test_write_value() click to toggle source
# File activerecord/test/cases/adapters/postgresql/bytea_test.rb, line 75
def test_write_value
  data = "\u001F"
  record = ByteaDataType.create(payload: data)
  assert_not record.new_record?
  assert_equal(data, record.payload)
end