aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/mysql/consistency_test.rb
blob: 743f6436e466a79d00d0c2fca4990534817d631a (plain) (blame)
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
require "cases/helper"

class MysqlConsistencyTest < ActiveRecord::MysqlTestCase
  self.use_transactional_tests = false

  class Consistency < ActiveRecord::Base
    self.table_name = "mysql_consistency"
  end

  setup do
    @old_emulate_booleans = ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans
    ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false

    @connection = ActiveRecord::Base.connection
    @connection.clear_cache!
    @connection.create_table("mysql_consistency") do |t|
      t.boolean "a_bool"
      t.string "a_string"
    end
    Consistency.reset_column_information
    Consistency.create!
  end

  teardown do
    ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = @old_emulate_booleans
    @connection.drop_table "mysql_consistency"
  end

  test "boolean columns with random value type cast to 0 when emulate_booleans is false" do
    with_new = Consistency.new
    with_last = Consistency.last
    with_new.a_bool = 'wibble'
    with_last.a_bool = 'wibble'

    assert_equal 0, with_new.a_bool
    assert_equal 0, with_last.a_bool
  end

  test "string columns call #to_s" do
    with_new = Consistency.new
    with_last = Consistency.last
    thing = Object.new
    with_new.a_string = thing
    with_last.a_string = thing

    assert_equal thing.to_s, with_new.a_string
    assert_equal thing.to_s, with_last.a_string
  end
end