set_defaults_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. DEFAULT_VALUES = {
  4. textarea: 'rspec',
  5. text: 'rspec',
  6. boolean: true,
  7. date: 1,
  8. datetime: 12,
  9. integer: 123,
  10. select: 'key_1'
  11. }.freeze
  12. RSpec.describe ObjectManager::Attribute::SetDefaults, time_zone: 'Europe/London', type: :model do
  13. describe 'setting default', db_strategy: :reset_all do
  14. before :all do # rubocop:disable RSpec/BeforeAfterAll
  15. DEFAULT_VALUES.each do |key, value|
  16. create("object_manager_attribute_#{key}", name: "rspec_#{key}", default: value)
  17. create("object_manager_attribute_#{key}", name: "rspec_#{key}_no_default", default: nil)
  18. end
  19. create(:object_manager_attribute_text, name: 'rspec_empty', default: '')
  20. ObjectManager::Attribute.migration_execute
  21. end
  22. after :all do # rubocop:disable RSpec/BeforeAfterAll
  23. ObjectManager::Attribute.where('name LIKE ?', 'rspec_%').destroy_all
  24. end
  25. context 'with text type' do # on text
  26. it 'default value is set' do
  27. ticket = create(:ticket)
  28. expect(ticket.rspec_text).to eq 'rspec'
  29. end
  30. it 'empty string as default value gets saved' do
  31. ticket = create(:ticket)
  32. expect(ticket.rspec_empty).to eq ''
  33. end
  34. it 'given value overrides default value' do
  35. ticket = create(:ticket, rspec_text: 'another')
  36. expect(ticket.rspec_text).to eq 'another'
  37. end
  38. # actual create works slightly differently than FactoryGirl!
  39. it 'given value overrides default value when using native #create' do
  40. ticket_attrs = attributes_for(:ticket, rspec_text: 'another', group: Group.first)
  41. ticket_attrs[:group] = Group.first
  42. ticket_attrs[:customer] = User.first
  43. ticket_created = Ticket.create! ticket_attrs
  44. expect(ticket_created.rspec_text).to eq 'another'
  45. end
  46. it 'given nil overrides default value' do
  47. ticket = create(:ticket, rspec_text: nil)
  48. expect(ticket.rspec_text).to be_nil
  49. end
  50. it 'updating attribute to nil does not instantiate default' do
  51. ticket = create(:ticket)
  52. ticket.update! rspec_text: nil
  53. expect(ticket.rspec_text).to be_nil
  54. end
  55. end
  56. context 'when using other types' do
  57. subject(:example) { create(:ticket) }
  58. it 'boolean is set' do
  59. expect(example.rspec_boolean).to be true
  60. end
  61. it 'date is set' do
  62. freeze_time
  63. expect(example.rspec_date).to eq 1.day.from_now.to_date
  64. end
  65. it 'datetime is set' do
  66. travel_to Time.current.change(usec: 0, sec: 0)
  67. expect(example.rspec_datetime).to eq 12.hours.from_now
  68. end
  69. it 'integer is set' do
  70. expect(example.rspec_integer).to eq 123
  71. end
  72. it 'select value is set' do
  73. expect(example.rspec_select).to eq 'key_1'
  74. end
  75. context 'when system uses different time zone' do
  76. before do
  77. Setting.set('timezone_default', 'Europe/Vilnius')
  78. travel_to Time.current.change(hour: 23, usec: 0, sec: 0)
  79. end
  80. it 'date is set' do
  81. expect(example.rspec_date).to eq 2.days.from_now.to_date
  82. end
  83. it 'datetime is set' do
  84. expect(example.rspec_datetime).to eq 12.hours.from_now
  85. end
  86. end
  87. end
  88. context 'when overriding default to empty value' do
  89. subject(:example) do
  90. params = DEFAULT_VALUES.keys.each_with_object({}) { |elem, memo| memo["rspec_#{elem}"] = nil }
  91. create(:ticket, params)
  92. end
  93. DEFAULT_VALUES.each_key do |elem|
  94. it "#{elem} is empty" do
  95. expect(example.send(:"rspec_#{elem}")).to be_nil
  96. end
  97. end
  98. end
  99. context 'when default is not set' do
  100. subject(:example) { create(:ticket) }
  101. DEFAULT_VALUES.each_key do |elem|
  102. it "#{elem} is empty" do
  103. expect(example.send(:"rspec_#{elem}_no_default")).to be_nil
  104. end
  105. end
  106. end
  107. end
  108. end