attribute_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'rails_helper'
  2. RSpec.describe ObjectManager::Attribute, type: :model do
  3. describe 'callbacks' do
  4. context 'for setting default values on local data options' do
  5. let(:subject) { described_class.new }
  6. context ':null' do
  7. it 'sets nil values to true' do
  8. expect { subject.validate }
  9. .to change { subject.data_option[:null] }.to(true)
  10. end
  11. it 'does not overwrite false values' do
  12. subject.data_option[:null] = false
  13. expect { subject.validate }
  14. .not_to change { subject.data_option[:null] }
  15. end
  16. end
  17. context ':maxlength' do
  18. context 'for data_type: select / tree_select / checkbox' do
  19. let(:subject) { described_class.new(data_type: 'select') }
  20. it 'sets nil values to 255' do
  21. expect { subject.validate }
  22. .to change { subject.data_option[:maxlength] }.to(255)
  23. end
  24. end
  25. end
  26. context ':nulloption' do
  27. context 'for data_type: select / tree_select / checkbox' do
  28. let(:subject) { described_class.new(data_type: 'select') }
  29. it 'sets nil values to true' do
  30. expect { subject.validate }
  31. .to change { subject.data_option[:nulloption] }.to(true)
  32. end
  33. it 'does not overwrite false values' do
  34. subject.data_option[:nulloption] = false
  35. expect { subject.validate }
  36. .not_to change { subject.data_option[:nulloption] }
  37. end
  38. end
  39. end
  40. end
  41. end
  42. describe 'check name' do
  43. it 'rejects ActiveRecord reserved word "attribute"' do
  44. expect do
  45. ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: 'attribute'
  46. end.to raise_error 'attribute is a reserved word, please choose a different one'
  47. end
  48. it 'rejects Zammad reserved word "table"' do
  49. expect do
  50. ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: 'table'
  51. end.to raise_error 'table is a reserved word, please choose a different one'
  52. end
  53. it 'rejects duplicate attribute name of conflicting types' do
  54. attribute = attributes_for :object_manager_attribute_text
  55. ObjectManager::Attribute.add attribute
  56. attribute[:data_type] = 'boolean'
  57. expect do
  58. ObjectManager::Attribute.add attribute
  59. end.to raise_error ActiveRecord::RecordInvalid
  60. end
  61. it 'accepts duplicate attribute name on the same types (editing an existing attribute)' do
  62. attribute = attributes_for :object_manager_attribute_text
  63. ObjectManager::Attribute.add attribute
  64. expect do
  65. ObjectManager::Attribute.add attribute
  66. end.to_not raise_error
  67. end
  68. it 'accepts duplicate attribute name on compatible types (editing the type of an existing attribute)' do
  69. attribute = attributes_for :object_manager_attribute_text
  70. ObjectManager::Attribute.add attribute
  71. attribute[:data_type] = 'select'
  72. attribute[:data_option_new] = { default: '', options: { 'a' => 'a' } }
  73. expect do
  74. ObjectManager::Attribute.add attribute
  75. end.to_not raise_error
  76. end
  77. it 'accepts valid attribute names' do
  78. expect do
  79. ObjectManager::Attribute.add attributes_for :object_manager_attribute_text
  80. end.to_not raise_error
  81. end
  82. end
  83. end