generated_attribute_test.rb 4.1 KB
Newer Older
1 2 3 4 5 6
require 'generators/generators_test_helper'
require 'rails/generators/generated_attribute'

class GeneratedAttributeTest < Rails::Generators::TestCase
  include GeneratorsTestHelper

7 8 9 10
  def test_field_type_returns_number_field
    assert_field_type :integer, :number_field
  end

11
  def test_field_type_returns_text_field
12
    %w(float decimal string).each do |attribute_type|
13
      assert_field_type attribute_type, :text_field
14 15 16 17
    end
  end

  def test_field_type_returns_datetime_select
18 19
    %w(datetime timestamp).each do |attribute_type|
      assert_field_type attribute_type, :datetime_select
20 21 22 23
    end
  end

  def test_field_type_returns_time_select
24
    assert_field_type :time, :time_select
25 26 27
  end

  def test_field_type_returns_date_select
28
    assert_field_type :date, :date_select
29 30 31
  end

  def test_field_type_returns_text_area
32
    assert_field_type :text, :text_area
33 34 35
  end

  def test_field_type_returns_check_box
36
    assert_field_type :boolean, :check_box
37 38 39
  end

  def test_field_type_with_unknown_type_returns_text_field
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
    %w(foo bar baz).each do |attribute_type|
      assert_field_type attribute_type, :text_field
    end
  end

  def test_default_value_is_integer
    assert_field_default_value :integer, 1
  end

  def test_default_value_is_float
    assert_field_default_value :float, 1.5
  end

  def test_default_value_is_decimal
    assert_field_default_value :decimal, '9.99'
  end

  def test_default_value_is_datetime
    %w(datetime timestamp time).each do |attribute_type|
      assert_field_default_value attribute_type, Time.now.to_s(:db)
    end
  end

  def test_default_value_is_date
    assert_field_default_value :date, Date.today.to_s(:db)
  end

  def test_default_value_is_string
    assert_field_default_value :string, 'MyString'
  end

71
  def test_default_value_for_type
J
José Valim 已提交
72
    att = Rails::Generators::GeneratedAttribute.parse("type:string")
73 74 75
    assert_equal("", att.default)
  end

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  def test_default_value_is_text
    assert_field_default_value :text, 'MyText'
  end

  def test_default_value_is_boolean
    assert_field_default_value :boolean, false
  end

  def test_default_value_is_nil
    %w(references belongs_to).each do |attribute_type|
      assert_field_default_value attribute_type, nil
    end
  end

  def test_default_value_is_empty_string
    %w(foo bar baz).each do |attribute_type|
      assert_field_default_value attribute_type, ''
    end
  end

  def test_human_name
    assert_equal(
      'Full name',
      create_generated_attribute(:string, 'full_name').human_name
    )
  end

  def test_reference_is_true
    %w(references belongs_to).each do |attribute_type|
105
      assert create_generated_attribute(attribute_type).reference?
106 107 108 109 110
    end
  end

  def test_reference_is_false
    %w(foo bar baz).each do |attribute_type|
111
      assert !create_generated_attribute(attribute_type).reference?
112 113
    end
  end
114

115 116 117 118 119
  def test_polymorphic_reference_is_true
    %w(references belongs_to).each do |attribute_type|
      assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
    end
  end
120

121 122 123 124 125
  def test_polymorphic_reference_is_false
    %w(foo bar baz).each do |attribute_type|
      assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
    end
  end
126

127 128 129
  def test_blank_type_defaults_to_string_raises_exception
    assert_equal :string, create_generated_attribute(nil, 'title').type
    assert_equal :string, create_generated_attribute("", 'title').type
130
  end
J
José Valim 已提交
131 132 133 134

  def test_handles_index_names_for_references
    assert_equal "post", create_generated_attribute('string', 'post').index_name
    assert_equal "post_id", create_generated_attribute('references', 'post').index_name
135
    assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name
136
    assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name
J
José Valim 已提交
137
  end
138

139
  def test_handles_column_names_for_references
140 141 142 143
    assert_equal "post", create_generated_attribute('string', 'post').column_name
    assert_equal "post_id", create_generated_attribute('references', 'post').column_name
    assert_equal "post_id", create_generated_attribute('belongs_to', 'post').column_name
  end
144
end