2010-05-16 05:20:11 -04:00
|
|
|
require 'generators/generators_test_helper'
|
|
|
|
require 'rails/generators/generated_attribute'
|
|
|
|
|
|
|
|
class GeneratedAttributeTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
|
2011-05-16 10:43:05 -04:00
|
|
|
def test_field_type_returns_number_field
|
|
|
|
assert_field_type :integer, :number_field
|
|
|
|
end
|
|
|
|
|
2010-05-16 05:20:11 -04:00
|
|
|
def test_field_type_returns_text_field
|
2011-05-16 10:43:05 -04:00
|
|
|
%w(float decimal string).each do |attribute_type|
|
2010-05-17 16:43:06 -04:00
|
|
|
assert_field_type attribute_type, :text_field
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_returns_datetime_select
|
2010-05-17 16:43:06 -04:00
|
|
|
%w(datetime timestamp).each do |attribute_type|
|
|
|
|
assert_field_type attribute_type, :datetime_select
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_returns_time_select
|
2010-05-17 16:43:06 -04:00
|
|
|
assert_field_type :time, :time_select
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_returns_date_select
|
2010-05-17 16:43:06 -04:00
|
|
|
assert_field_type :date, :date_select
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_returns_text_area
|
2010-05-17 16:43:06 -04:00
|
|
|
assert_field_type :text, :text_area
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_returns_check_box
|
2010-05-17 16:43:06 -04:00
|
|
|
assert_field_type :boolean, :check_box
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_field_type_with_unknown_type_returns_text_field
|
2010-05-17 16:43:06 -04:00
|
|
|
%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
|
|
|
|
|
2011-03-05 16:25:20 -05:00
|
|
|
def test_default_value_for_type
|
2011-12-24 04:38:19 -05:00
|
|
|
att = Rails::Generators::GeneratedAttribute.parse("type:string")
|
2011-03-05 16:25:20 -05:00
|
|
|
assert_equal("", att.default)
|
|
|
|
end
|
|
|
|
|
2010-05-17 16:43:06 -04:00
|
|
|
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|
|
2012-06-25 14:01:31 -04:00
|
|
|
assert create_generated_attribute(attribute_type).reference?
|
2010-05-17 16:43:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reference_is_false
|
|
|
|
%w(foo bar baz).each do |attribute_type|
|
2012-06-25 14:01:31 -04:00
|
|
|
assert !create_generated_attribute(attribute_type).reference?
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|
|
|
|
end
|
2010-09-02 05:41:58 -04:00
|
|
|
|
2012-06-25 14:01:31 -04:00
|
|
|
def test_polymorphic_reference_is_true
|
|
|
|
%w(references belongs_to).each do |attribute_type|
|
|
|
|
assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
|
|
|
|
end
|
|
|
|
end
|
2012-12-09 15:01:03 -05:00
|
|
|
|
2012-06-25 14:01:31 -04:00
|
|
|
def test_polymorphic_reference_is_false
|
|
|
|
%w(foo bar baz).each do |attribute_type|
|
|
|
|
assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
|
|
|
|
end
|
|
|
|
end
|
2012-12-09 15:01:03 -05:00
|
|
|
|
2011-06-17 14:10:53 -04:00
|
|
|
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
|
2010-09-02 05:41:58 -04:00
|
|
|
end
|
2011-12-24 04:38:19 -05:00
|
|
|
|
|
|
|
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
|
2012-12-09 15:01:03 -05:00
|
|
|
assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name
|
2012-06-25 14:01:31 -04:00
|
|
|
assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name
|
2011-12-24 04:38:19 -05:00
|
|
|
end
|
2012-12-09 15:01:03 -05:00
|
|
|
|
2012-12-12 23:59:07 -05:00
|
|
|
def test_handles_column_names_for_references
|
2012-12-09 15:01:03 -05:00
|
|
|
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
|
2014-10-16 09:47:30 -04:00
|
|
|
|
|
|
|
def test_parse_required_attribute_with_index
|
|
|
|
att = Rails::Generators::GeneratedAttribute.parse("supplier:references{required}:index")
|
|
|
|
assert_equal "supplier", att.name
|
|
|
|
assert_equal :references, att.type
|
|
|
|
assert att.has_index?
|
|
|
|
assert att.required?
|
|
|
|
end
|
2010-05-16 05:20:11 -04:00
|
|
|
end
|