Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LOG = false #true
- load 'dynamic_attributes.rb'
- require 'test/unit'
- require 'rubygems'
- require 'active_record'
- ActiveRecord::Base.establish_connection(
- :adapter => "mysql",
- :host => "localhost",
- :username => "dynattr",
- :password => "dynattr",
- :database => "dynattr"
- )
- if LOG
- require 'logger'
- ActiveRecord::Base.logger = Logger.new(STDOUT)
- end
- class DynamicAttributesTest < Test::Unit::TestCase
- class Foo < ActiveRecord::Base
- include DynamicAttributes
- end
- class Bar < ActiveRecord::Base
- include DynamicAttributes
- end
- def reset_test_tables
- ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `foos`"
- ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `bars`"
- ActiveRecord::Base.connection.execute %Q(
- CREATE TABLE `foos`
- (`id` INT(8) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT)
- COLLATE `utf8_unicode_ci`
- ENGINE `InnoDB`
- )
- end
- def setup
- reset_test_tables
- Foo.reset_column_information
- Foo.migrated_attributes = []
- @f = Foo.new
- end
- def test_initialize_dynamic_attributes_hash
- assert_equal({}, @f.dynamic_columns_hash)
- end
- def test_dynamic_attributes_are_undefinded
- assert_equal(:_undefined_dynamic_column, @f.dynamic_columns_hash[:random_attribute])
- end
- def test_method_missing_assigns_dynamic_attributes
- assert_equal 'a string', (@f.bar = 'a string')
- end
- def test_method_missing_returns_dynamic_attribute_value
- @f.bar = 'a string'
- assert_equal 'a string', @f.bar
- end
- def test_method_missing_works_as_expected_on_undefined_methods
- assert_raise(NoMethodError) { @f.bar }
- end
- def test_has_dynamic_attributes
- assert not(@f.has_dynamic_attributes?)
- @f.bar = 'a string'
- assert @f.has_dynamic_attributes?
- end
- def test_attribute_mysql_type
- @f.bar = 'a string'
- assert_equal 'VARCHAR(255) CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`', @f.send(:mysql_type, :bar)
- @f.baz = 23
- assert_equal 'FLOAT', @f.send(:mysql_type, :baz)
- @f.foo = 5.9
- assert_equal 'FLOAT', @f.send(:mysql_type ,:foo)
- end
- def test_migration_sql
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- assert_equal "ALTER TABLE `foos` ADD COLUMN `foo` VARCHAR(255) CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`, ADD COLUMN `bar` FLOAT, ADD COLUMN `baz` FLOAT", @f.send(:migration_sql)
- end
- def test_migrate_new_columns
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- @f.send(:migrate_new_columns!)
- Foo.reset_column_information
- @g = Foo.new(:foo => 'Mok')
- assert_equal 'Mok', @g.foo
- end
- def test_ar_creates_attribute_methods_after_migrate
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- @f.send(:migrate_new_columns!)
- Foo.reset_column_information;
- assert Foo.new.has_attribute?(:bar)
- end
- def test_clear_dynamic_attributes
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- @f.clear_dynamic_attributes
- assert not(@f.has_dynamic_attributes?)
- assert not(@f.has_dynamic_attribute?(:bar))
- assert_equal({}, @f.instance_variable_get(:@dynamic_columns_hash))
- assert_raise(NoMethodError) { @f.foo }
- end
- def test_migrate_columns_and_move_dynamic_to_ar_attributes
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- @f.send(:migrate_columns_and_move_dynamic_to_ar_attributes!)
- assert not(@f.has_dynamic_attributes?)
- assert_equal 'Mike', @f.foo
- assert_equal 23, @f.bar
- end
- def test_clear_dynamic_attribute
- @f.foo = 'Mike'
- assert @f.has_dynamic_attribute?(:foo)
- @f.clear_dynamic_attribute(:foo)
- assert not(@f.has_dynamic_attribute?(:foo))
- assert_raise(NoMethodError) { @f.foo }
- end
- def test_save
- @f.foo = 'Mike'
- @f.bar = 23
- @f.baz = 2.9
- @f.save
- assert @f.has_attribute?(:foo)
- @g = Foo.find :last
- assert_equal 'Mike', @g.foo
- end
- def test_dynamic_attributes_through_initializer
- @ia = Foo.new(:dyninit => 'test')
- assert_equal 'test', @ia.dyninit
- @ia.save
- assert_equal 'test', Foo.last.dyninit
- end
- def test_columns_for_migration
- @f.foo = 'Mike'
- @g = Foo.new(:foo => 'Mok', :bar => 25)
- assert @f.send(:columns_for_migration).include?(:foo)
- assert @g.send(:columns_for_migration).include?(:foo) && @g.send(:columns_for_migration).include?(:bar)
- @f.save
- assert not(@g.send(:columns_for_migration).include?(:foo))
- end
- def test_dont_duplicate_columns_but_save_anyway
- @g = Foo.new
- @f.foo = 'erstes'
- @g.foo = 'zweites'
- @f.save
- assert_nothing_thrown { @g.save }
- assert_equal 'zweites', Foo.last.foo
- end
- def test_existing_records_can_get_new_dynamic_attributes
- @f.foo = 'Mike'
- @f.save
- @g = Foo.last
- assert_nothing_thrown { @g.bar = 25 }
- assert @g.save
- end
- def test_creates_new_tables_automatically
- bar = Bar.new
- assert_nothing_thrown { bar.save }
- assert_equal bar, Bar.last
- bar.foo = 'Mike'
- assert bar.save
- assert_equal 'Mike', Bar.last.foo
- end
- end
Add Comment
Please, Sign In to add comment