aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/custom_properties_test.rb
blob: 047c1b9b7444cc8dac3770ae8ea67a594f7477c2 (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
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
require 'cases/helper'

class OverloadedType < ActiveRecord::Base
  property :overloaded_float, Type::Integer.new
  property :overloaded_string_with_limit, Type::String.new(limit: 50)
  property :non_existent_decimal, Type::Decimal.new
end

class ChildOfOverloadedType < OverloadedType
end

class GrandchildOfOverloadedType < ChildOfOverloadedType
  property :overloaded_float, Type::Float.new
end

class UnoverloadedType < ActiveRecord::Base
  self.table_name = 'overloaded_types'
end

module ActiveRecord
  class CustomPropertiesTest < ActiveRecord::TestCase
    def test_overloading_types
      data = OverloadedType.new

      data.overloaded_float = "1.1"
      data.unoverloaded_float = "1.1"

      assert_equal 1, data.overloaded_float
      assert_equal 1.1, data.unoverloaded_float
    end

    def test_overloaded_properties_save
      data = OverloadedType.new

      data.overloaded_float = "2.2"
      data.save!
      data.reload

      assert_equal 2, data.overloaded_float
      assert_kind_of Fixnum, OverloadedType.last.overloaded_float
      assert_equal 2.0, UnoverloadedType.last.overloaded_float
      assert_kind_of Float, UnoverloadedType.last.overloaded_float
    end

    def test_properties_assigned_in_constructor
      data = OverloadedType.new(overloaded_float: '3.3')

      assert_equal 3, data.overloaded_float
    end

    def test_overloaded_properties_with_limit
      assert_equal 50, OverloadedType.columns_hash['overloaded_string_with_limit'].limit
      assert_equal 255, UnoverloadedType.columns_hash['overloaded_string_with_limit'].limit
    end

    def test_nonexistent_property
      data = OverloadedType.new(non_existent_decimal: 1)

      assert_equal BigDecimal.new(1), data.non_existent_decimal
      assert_raise ActiveRecord::UnknownAttributeError do
        UnoverloadedType.new(non_existent_decimal: 1)
      end
    end

    def test_overloaded_properties_have_no_default
      data = OverloadedType.new
      unoverloaded_data = UnoverloadedType.new

      assert_nil data.overloaded_float
      assert unoverloaded_data.overloaded_float
    end

    def test_children_inherit_custom_properties
      data = ChildOfOverloadedType.new(overloaded_float: '4.4')

      assert_equal 4, data.overloaded_float
    end

    def test_children_can_override_parents
      data = GrandchildOfOverloadedType.new(overloaded_float: '4.4')

      assert_equal 4.4, data.overloaded_float
    end

    def test_overloading_properties_does_not_change_column_order
      column_names = OverloadedType.column_names
      assert_equal %w(id overloaded_float unoverloaded_float overloaded_string_with_limit non_existent_decimal), column_names
    end
  end
end