Guest User

Untitled

a guest
Jun 6th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. LOG = false #true
  2. load 'dynamic_attributes.rb'
  3. require 'test/unit'
  4. require 'rubygems'
  5. require 'active_record'
  6.  
  7. ActiveRecord::Base.establish_connection(
  8. :adapter => "mysql",
  9. :host => "localhost",
  10. :username => "dynattr",
  11. :password => "dynattr",
  12. :database => "dynattr"
  13. )
  14.  
  15. if LOG
  16. require 'logger'
  17. ActiveRecord::Base.logger = Logger.new(STDOUT)
  18. end
  19.  
  20. class DynamicAttributesTest < Test::Unit::TestCase
  21.  
  22. class Foo < ActiveRecord::Base
  23. include DynamicAttributes
  24. end
  25.  
  26. class Bar < ActiveRecord::Base
  27. include DynamicAttributes
  28. end
  29.  
  30. def reset_test_tables
  31. ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `foos`"
  32. ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS `bars`"
  33.  
  34. ActiveRecord::Base.connection.execute %Q(
  35. CREATE TABLE `foos`
  36. (`id` INT(8) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT)
  37. COLLATE `utf8_unicode_ci`
  38. ENGINE `InnoDB`
  39. )
  40. end
  41.  
  42. def setup
  43. reset_test_tables
  44. Foo.reset_column_information
  45. Foo.migrated_attributes = []
  46. @f = Foo.new
  47. end
  48.  
  49. def test_initialize_dynamic_attributes_hash
  50. assert_equal({}, @f.dynamic_columns_hash)
  51. end
  52.  
  53. def test_dynamic_attributes_are_undefinded
  54. assert_equal(:_undefined_dynamic_column, @f.dynamic_columns_hash[:random_attribute])
  55. end
  56.  
  57. def test_method_missing_assigns_dynamic_attributes
  58. assert_equal 'a string', (@f.bar = 'a string')
  59. end
  60.  
  61. def test_method_missing_returns_dynamic_attribute_value
  62. @f.bar = 'a string'
  63. assert_equal 'a string', @f.bar
  64. end
  65.  
  66. def test_method_missing_works_as_expected_on_undefined_methods
  67. assert_raise(NoMethodError) { @f.bar }
  68. end
  69.  
  70. def test_has_dynamic_attributes
  71. assert not(@f.has_dynamic_attributes?)
  72. @f.bar = 'a string'
  73. assert @f.has_dynamic_attributes?
  74. end
  75.  
  76. def test_attribute_mysql_type
  77. @f.bar = 'a string'
  78. assert_equal 'VARCHAR(255) CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`', @f.send(:mysql_type, :bar)
  79. @f.baz = 23
  80. assert_equal 'FLOAT', @f.send(:mysql_type, :baz)
  81. @f.foo = 5.9
  82. assert_equal 'FLOAT', @f.send(:mysql_type ,:foo)
  83. end
  84.  
  85. def test_migration_sql
  86. @f.foo = 'Mike'
  87. @f.bar = 23
  88. @f.baz = 2.9
  89. 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)
  90. end
  91.  
  92. def test_migrate_new_columns
  93. @f.foo = 'Mike'
  94. @f.bar = 23
  95. @f.baz = 2.9
  96. @f.send(:migrate_new_columns!)
  97. Foo.reset_column_information
  98. @g = Foo.new(:foo => 'Mok')
  99. assert_equal 'Mok', @g.foo
  100. end
  101.  
  102. def test_ar_creates_attribute_methods_after_migrate
  103. @f.foo = 'Mike'
  104. @f.bar = 23
  105. @f.baz = 2.9
  106. @f.send(:migrate_new_columns!)
  107. Foo.reset_column_information;
  108.  
  109. assert Foo.new.has_attribute?(:bar)
  110. end
  111.  
  112. def test_clear_dynamic_attributes
  113. @f.foo = 'Mike'
  114. @f.bar = 23
  115. @f.baz = 2.9
  116.  
  117. @f.clear_dynamic_attributes
  118.  
  119. assert not(@f.has_dynamic_attributes?)
  120. assert not(@f.has_dynamic_attribute?(:bar))
  121. assert_equal({}, @f.instance_variable_get(:@dynamic_columns_hash))
  122. assert_raise(NoMethodError) { @f.foo }
  123. end
  124.  
  125. def test_migrate_columns_and_move_dynamic_to_ar_attributes
  126. @f.foo = 'Mike'
  127. @f.bar = 23
  128. @f.baz = 2.9
  129.  
  130. @f.send(:migrate_columns_and_move_dynamic_to_ar_attributes!)
  131.  
  132. assert not(@f.has_dynamic_attributes?)
  133. assert_equal 'Mike', @f.foo
  134. assert_equal 23, @f.bar
  135. end
  136.  
  137. def test_clear_dynamic_attribute
  138. @f.foo = 'Mike'
  139. assert @f.has_dynamic_attribute?(:foo)
  140. @f.clear_dynamic_attribute(:foo)
  141. assert not(@f.has_dynamic_attribute?(:foo))
  142. assert_raise(NoMethodError) { @f.foo }
  143. end
  144.  
  145. def test_save
  146. @f.foo = 'Mike'
  147. @f.bar = 23
  148. @f.baz = 2.9
  149. @f.save
  150. assert @f.has_attribute?(:foo)
  151. @g = Foo.find :last
  152. assert_equal 'Mike', @g.foo
  153. end
  154.  
  155. def test_dynamic_attributes_through_initializer
  156. @ia = Foo.new(:dyninit => 'test')
  157. assert_equal 'test', @ia.dyninit
  158. @ia.save
  159. assert_equal 'test', Foo.last.dyninit
  160. end
  161.  
  162. def test_columns_for_migration
  163. @f.foo = 'Mike'
  164. @g = Foo.new(:foo => 'Mok', :bar => 25)
  165.  
  166. assert @f.send(:columns_for_migration).include?(:foo)
  167. assert @g.send(:columns_for_migration).include?(:foo) && @g.send(:columns_for_migration).include?(:bar)
  168.  
  169. @f.save
  170. assert not(@g.send(:columns_for_migration).include?(:foo))
  171.  
  172. end
  173.  
  174. def test_dont_duplicate_columns_but_save_anyway
  175. @g = Foo.new
  176. @f.foo = 'erstes'
  177. @g.foo = 'zweites'
  178.  
  179. @f.save
  180. assert_nothing_thrown { @g.save }
  181. assert_equal 'zweites', Foo.last.foo
  182. end
  183.  
  184. def test_existing_records_can_get_new_dynamic_attributes
  185. @f.foo = 'Mike'
  186. @f.save
  187. @g = Foo.last
  188. assert_nothing_thrown { @g.bar = 25 }
  189. assert @g.save
  190. end
  191.  
  192. def test_creates_new_tables_automatically
  193. bar = Bar.new
  194. assert_nothing_thrown { bar.save }
  195. assert_equal bar, Bar.last
  196.  
  197. bar.foo = 'Mike'
  198. assert bar.save
  199. assert_equal 'Mike', Bar.last.foo
  200. end
  201.  
  202. end
Add Comment
Please, Sign In to add comment