check_for_object_attributes_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. RSpec.describe CheckForObjectAttributes, type: :db_migration do
  3. it 'performs no action for new systems', system_init_done: false do
  4. migrate do |instance|
  5. expect(instance).not_to receive(:attributes)
  6. end
  7. end
  8. context 'with a valid #data_option hash' do
  9. it 'does not change converted text attribute' do
  10. attribute = create(:object_manager_attribute_text)
  11. expect { migrate }
  12. .not_to change { attribute.reload.data_option }
  13. end
  14. it 'does not change select attribute' do
  15. attribute = create(:object_manager_attribute_select)
  16. expect { migrate }
  17. .not_to change { attribute.reload.data_option }
  18. end
  19. it 'does not change tree_select attribute' do
  20. attribute = create(:object_manager_attribute_tree_select)
  21. expect { migrate }
  22. .not_to change { attribute.reload.data_option }
  23. end
  24. end
  25. context 'for #data_option key:' do
  26. context ':options' do
  27. it 'converts String to Hash' do
  28. wrong = {
  29. default: '',
  30. options: '',
  31. relation: '',
  32. type: 'text',
  33. maxlength: 255,
  34. null: true
  35. }
  36. attribute = create(:object_manager_attribute_text, data_option: wrong)
  37. migrate
  38. attribute.reload
  39. expect(attribute[:data_option][:options]).to be_a(Hash)
  40. expect(attribute[:data_option][:options]).to be_blank
  41. end
  42. end
  43. context ':relation' do
  44. it 'ensures an empty String' do
  45. wrong = {
  46. default: '',
  47. options: {},
  48. type: 'text',
  49. maxlength: 255,
  50. null: true
  51. }
  52. attribute = create(:object_manager_attribute_text, data_option: wrong)
  53. migrate
  54. attribute.reload
  55. expect(attribute[:data_option][:relation]).to be_a(String)
  56. end
  57. it 'converts Hash to String' do
  58. wrong = {
  59. default: '',
  60. options: {},
  61. relation: {},
  62. type: 'text',
  63. maxlength: 255,
  64. null: true
  65. }
  66. attribute = create(:object_manager_attribute_text, data_option: wrong)
  67. migrate
  68. attribute.reload
  69. expect(attribute[:data_option][:relation]).to be_a(String)
  70. expect(attribute[:data_option][:relation]).to be_blank
  71. end
  72. end
  73. # see https://github.com/zammad/zammad/issues/2159
  74. context ':null' do
  75. it 'does not fail on missing values' do
  76. wrong = {
  77. default: '',
  78. options: '', # <- this is not the attribute under test,
  79. relation: '', # but it must be invalid
  80. type: 'text', # to trigger a #save in the migration.
  81. maxlength: 255,
  82. }
  83. create(:object_manager_attribute_text)
  84. .update_columns(data_option: wrong)
  85. expect { migrate }.not_to raise_error
  86. end
  87. end
  88. end
  89. # regression test for issue #2318 - Upgrade to Zammad 2.7 was not possible (migration 20180220171219 CheckForObjectAttributes failed)
  90. context 'for interger attributes' do
  91. it 'missing :min and :max' do
  92. attribute = create(:object_manager_attribute_integer)
  93. attribute.update_columns(data_option: {})
  94. expect { migrate }.not_to raise_error
  95. attribute.reload
  96. expect(attribute[:data_option][:min]).to be_a(Integer)
  97. expect(attribute[:data_option][:max]).to be_a(Integer)
  98. end
  99. end
  100. end