PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/padrino-gen/test/test_migration_generator.rb

https://github.com/ganesan/padrino-framework
Ruby | 189 lines | 186 code | 3 blank | 0 comment | 4 complexity | de242cd11c4cc9b1174ce1b36f163714 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/helper')
  2. class TestMigrationGenerator < Test::Unit::TestCase
  3. def setup
  4. `rm -rf /tmp/sample_project`
  5. end
  6. context 'the migration generator' do
  7. should "fail outside app root" do
  8. output = silence_logger { generate(:migration, 'add_email_to_users', '-r=/tmp') }
  9. assert_match(/not at the root/, output)
  10. assert_no_file_exists('/tmp/db/migration')
  11. end
  12. should "fail if we don't use an adapter" do
  13. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon') }
  14. assert_raise(SystemExit) { silence_logger { generate(:migration, 'AddEmailToUsers', '-r=/tmp/sample_project') } }
  15. end
  16. should "generate migration inside app root" do
  17. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  18. response_success = silence_logger { generate(:migration, 'AddEmailToUsers', '-r=/tmp/sample_project') }
  19. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_users.rb"
  20. assert_match_in_file(/class AddEmailToUser/m, migration_file_path)
  21. end
  22. should "generate migration inside app root with lowercase migration argument" do
  23. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  24. response_success = silence_logger { generate(:migration, 'add_email_to_users', '-r=/tmp/sample_project') }
  25. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_users.rb"
  26. assert_match_in_file(/class AddEmailToUsers/m, migration_file_path)
  27. end
  28. should "generate migration inside app root with singular table" do
  29. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  30. silence_logger { generate(:migration, 'add_email_to_user', "email:string", '-r=/tmp/sample_project') }
  31. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_user.rb"
  32. assert_match_in_file(/class AddEmailToUser/m, migration_file_path)
  33. assert_match_in_file(/t.string :email/, migration_file_path)
  34. assert_match_in_file(/t.remove :email/, migration_file_path)
  35. end
  36. should "properly calculate version number" do
  37. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  38. silence_logger { generate(:migration, 'add_email_to_person', "email:string", '-r=/tmp/sample_project') }
  39. silence_logger { generate(:migration, 'add_name_to_person', "email:string", '-r=/tmp/sample_project') }
  40. silence_logger { generate(:migration, 'add_age_to_user', "email:string", '-r=/tmp/sample_project') }
  41. assert_match_in_file(/class AddEmailToPerson/m, "/tmp/sample_project/db/migrate/001_add_email_to_person.rb")
  42. assert_match_in_file(/class AddNameToPerson/m, "/tmp/sample_project/db/migrate/002_add_name_to_person.rb")
  43. assert_match_in_file(/class AddAgeToUser/m, "/tmp/sample_project/db/migrate/003_add_age_to_user.rb")
  44. end
  45. end
  46. context 'the migration generator for activerecord' do
  47. should "generate migration for generic needs" do
  48. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  49. response_success = silence_logger { generate(:migration, 'ModifyUserFields', '-r=/tmp/sample_project') }
  50. migration_file_path = "/tmp/sample_project/db/migrate/001_modify_user_fields.rb"
  51. assert_match_in_file(/class ModifyUserFields/m, migration_file_path)
  52. assert_match_in_file(/def self\.up\s+end/m, migration_file_path)
  53. assert_match_in_file(/def self\.down\s+end/m, migration_file_path)
  54. end
  55. should "generate migration for adding columns" do
  56. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  57. migration_params = ['AddEmailToUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  58. response_success = silence_logger { generate(:migration, *migration_params) }
  59. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_users.rb"
  60. assert_match_in_file(/class AddEmailToUsers/m, migration_file_path)
  61. assert_match_in_file(/change_table :users.*?t\.string :email/m, migration_file_path)
  62. assert_match_in_file(/t\.integer :age/m, migration_file_path)
  63. assert_match_in_file(/change_table :users.*?t\.remove :email/m, migration_file_path)
  64. assert_match_in_file(/t\.remove :age/m, migration_file_path)
  65. end
  66. should "generate migration for removing columns" do
  67. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=activerecord') }
  68. migration_params = ['RemoveEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  69. response_success = silence_logger { generate(:migration, *migration_params) }
  70. migration_file_path = "/tmp/sample_project/db/migrate/001_remove_email_from_users.rb"
  71. assert_match_in_file(/class RemoveEmailFromUsers/m, migration_file_path)
  72. assert_match_in_file(/change_table :users.*?t\.remove :email/m, migration_file_path)
  73. assert_match_in_file(/t\.remove :age/m, migration_file_path)
  74. assert_match_in_file(/change_table :users.*?t\.string :email/m, migration_file_path)
  75. assert_match_in_file(/t\.integer :age/m, migration_file_path)
  76. end
  77. end
  78. context 'the migration generator for datamapper' do
  79. should "generate migration for generic needs" do
  80. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=datamapper') }
  81. response_success = silence_logger { generate(:migration, 'ModifyUserFields', '-r=/tmp/sample_project') }
  82. migration_file_path = "/tmp/sample_project/db/migrate/001_modify_user_fields.rb"
  83. assert_match_in_file(/migration\s1.*?:modify_user_fields/m, migration_file_path)
  84. assert_match_in_file(/up\sdo\s+end/m, migration_file_path)
  85. assert_match_in_file(/down\sdo\s+end/m, migration_file_path)
  86. end
  87. should "generate migration for adding columns" do
  88. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=datamapper') }
  89. migration_params = ['AddEmailToUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  90. response_success = silence_logger { generate(:migration, *migration_params) }
  91. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_users.rb"
  92. assert_match_in_file(/migration\s1.*?:add_email_to_users/m, migration_file_path)
  93. assert_match_in_file(/modify_table :users.*?add_column :email, DataMapper::Property::String/m, migration_file_path)
  94. assert_match_in_file(/add_column :age, DataMapper::Property::Integer/m, migration_file_path)
  95. assert_match_in_file(/modify_table :users.*?drop_column :email/m, migration_file_path)
  96. assert_match_in_file(/drop_column :age/m, migration_file_path)
  97. end
  98. should "generate migration for removing columns" do
  99. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=datamapper') }
  100. migration_params = ['RemoveEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  101. response_success = silence_logger { generate(:migration, *migration_params) }
  102. migration_file_path = "/tmp/sample_project/db/migrate/001_remove_email_from_users.rb"
  103. assert_match_in_file(/migration\s1.*?:remove_email_from_users/m, migration_file_path)
  104. assert_match_in_file(/modify_table :users.*?drop_column :email/m, migration_file_path)
  105. assert_match_in_file(/drop_column :age/m, migration_file_path)
  106. assert_match_in_file(/modify_table :users.*?add_column :email, DataMapper::Property::String/m, migration_file_path)
  107. assert_match_in_file(/add_column :age, DataMapper::Property::Integer/m, migration_file_path)
  108. end
  109. should "properly version migration files" do
  110. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=datamapper') }
  111. response_success = silence_logger { generate(:migration, 'ModifyUserFields', '-r=/tmp/sample_project') }
  112. response_success = silence_logger { generate(:migration, 'ModifyUserFields2', '-r=/tmp/sample_project') }
  113. response_success = silence_logger { generate(:migration, 'ModifyUserFields3', '-r=/tmp/sample_project') }
  114. assert_match_in_file(/migration\s1.*?:modify_user_fields/m, "/tmp/sample_project/db/migrate/001_modify_user_fields.rb")
  115. assert_match_in_file(/migration\s2.*?:modify_user_fields2/m, "/tmp/sample_project/db/migrate/002_modify_user_fields2.rb")
  116. assert_match_in_file(/migration\s3.*?:modify_user_fields3/m, "/tmp/sample_project/db/migrate/003_modify_user_fields3.rb")
  117. end
  118. end
  119. context 'the migration generator for sequel' do
  120. should "generate migration for generic needs" do
  121. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  122. response_success = silence_logger { generate(:migration, 'ModifyUserFields', '-r=/tmp/sample_project') }
  123. migration_file_path = "/tmp/sample_project/db/migrate/001_modify_user_fields.rb"
  124. assert_match_in_file(/class ModifyUserFields/m, migration_file_path)
  125. assert_match_in_file(/def\sup\s+end/m, migration_file_path)
  126. assert_match_in_file(/def\sdown\s+end/m, migration_file_path)
  127. end
  128. should "generate migration for adding columns" do
  129. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  130. migration_params = ['AddEmailToUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  131. response_success = silence_logger { generate(:migration, *migration_params) }
  132. migration_file_path = "/tmp/sample_project/db/migrate/001_add_email_to_users.rb"
  133. assert_match_in_file(/class AddEmailToUsers/m, migration_file_path)
  134. assert_match_in_file(/alter_table :users.*?add_column :email, String/m, migration_file_path)
  135. assert_match_in_file(/add_column :age, Integer/m, migration_file_path)
  136. assert_match_in_file(/alter_table :users.*?drop_column :email/m, migration_file_path)
  137. assert_match_in_file(/drop_column :age/m, migration_file_path)
  138. end
  139. should "generate migration for removing columns" do
  140. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  141. migration_params = ['RemoveEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  142. response_success = silence_logger { generate(:migration, *migration_params) }
  143. migration_file_path = "/tmp/sample_project/db/migrate/001_remove_email_from_users.rb"
  144. assert_match_in_file(/class RemoveEmailFromUsers/m, migration_file_path)
  145. assert_match_in_file(/alter_table :users.*?drop_column :email/m, migration_file_path)
  146. assert_match_in_file(/drop_column :age/m, migration_file_path)
  147. assert_match_in_file(/alter_table :users.*?add_column :email, String/m, migration_file_path)
  148. assert_match_in_file(/add_column :age, Integer/m, migration_file_path)
  149. end
  150. end
  151. context "the migration destroy option" do
  152. should "destroy the migration files" do
  153. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  154. migration_params = ['RemoveEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  155. silence_logger { generate(:migration, *migration_params) }
  156. silence_logger { generate(:migration, 'RemoveEmailFromUsers', '-r=/tmp/sample_project','-d') }
  157. assert_no_file_exists("/tmp/sample_project/db/migrate/001_remove_email_from_users.rb")
  158. end
  159. should "destroy the migration file regardless of number" do
  160. silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--script=none', '-t=bacon', '-d=sequel') }
  161. migration_params = ['RemoveEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  162. migration_param2 = ['AddEmailFromUsers', "email:string", "age:integer", '-r=/tmp/sample_project']
  163. silence_logger { generate(:migration, *migration_param2) }
  164. silence_logger { generate(:migration, *migration_params) }
  165. silence_logger { generate(:migration, 'RemoveEmailFromUsers', '-r=/tmp/sample_project','-d') }
  166. assert_no_file_exists("/tmp/sample_project/db/migrate/002_remove_email_from_users.rb")
  167. end
  168. end
  169. end