can_creates_and_updates_examples.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'ApplicationModel::CanCreatesAndUpdates' do |unique_name: true|
  3. describe '.create_if_not_exists' do
  4. let!(:record) { create(described_class.name.underscore) }
  5. context 'when given a valid #id' do
  6. let(:id) { record.id }
  7. it 'returns that record' do
  8. expect(described_class.create_if_not_exists(id: id)).to eq(record)
  9. end
  10. it 'does not create a new record' do
  11. allow(described_class).to receive(:create!)
  12. described_class.create_if_not_exists(id: id)
  13. expect(described_class).not_to have_received(:create!).with(id: id)
  14. end
  15. end
  16. context 'when given an invalid #id' do
  17. let(:id) { described_class.pluck(:id).max + 1 }
  18. it 'attempts to create a new record' do
  19. allow(described_class).to receive(:create!)
  20. described_class.create_if_not_exists(id: id)
  21. expect(described_class).to have_received(:create!).with(id: id)
  22. end
  23. end
  24. shared_examples 'for #name attribute' do
  25. context 'when given a valid #name' do
  26. let(:name) { record.name }
  27. it 'returns that record' do
  28. expect(described_class.create_if_not_exists(name: name)).to eq(record)
  29. end
  30. it 'does not create a new record for matching identifier' do
  31. allow(described_class).to receive(:create!)
  32. described_class.create_if_not_exists(name: name)
  33. expect(described_class).not_to have_received(:create!).with(name: name)
  34. end
  35. if unique_name
  36. it 'raises uniqueness error if identifier is different in case only' do
  37. swapcase_attibutes = attributes_for(described_class.name.underscore)
  38. .merge(name: record.name.swapcase)
  39. expect { described_class.create_if_not_exists(**swapcase_attibutes) }
  40. .to raise_error(ActiveRecord::RecordInvalid, %r{has already been taken})
  41. end
  42. end
  43. end
  44. context 'when given an invalid #name' do
  45. let(:name) { "#{described_class.pluck(:name).max}foo" }
  46. it 'attempts to create a new record' do
  47. allow(described_class).to receive(:create!)
  48. described_class.create_if_not_exists(name: name)
  49. expect(described_class).to have_received(:create!).with(name: name)
  50. end
  51. end
  52. end
  53. shared_examples 'for #login attribute' do
  54. context 'when given a valid #login' do
  55. let(:login) { record.login }
  56. it 'returns that record' do
  57. expect(described_class.create_if_not_exists(login: login)).to eq(record)
  58. end
  59. it 'does not create a new record' do
  60. allow(described_class).to receive(:create!)
  61. described_class.create_if_not_exists(login: login)
  62. expect(described_class).not_to have_received(:create!).with(login: login)
  63. end
  64. end
  65. context 'when given an invalid #login' do
  66. let(:login) { "#{described_class.pluck(:login).max}foo" }
  67. it 'attempts to create a new record' do
  68. allow(described_class).to receive(:create!)
  69. described_class.create_if_not_exists(login: login)
  70. expect(described_class).to have_received(:create!).with(login: login)
  71. end
  72. end
  73. end
  74. shared_examples 'for #email attribute' do
  75. context 'when given a valid #email' do
  76. let(:email) { record.email }
  77. it 'returns that record' do
  78. expect(described_class.create_if_not_exists(email: email)).to eq(record)
  79. end
  80. it 'does not create a new record' do
  81. allow(described_class).to receive(:create!)
  82. described_class.create_if_not_exists(email: email)
  83. expect(described_class).not_to have_received(:create!).with(email: email)
  84. end
  85. end
  86. context 'when given an invalid #email' do
  87. let(:email) { "#{described_class.pluck(:email).max}foo" }
  88. it 'attempts to create a new record' do
  89. allow(described_class).to receive(:create!)
  90. described_class.create_if_not_exists(email: email)
  91. expect(described_class).to have_received(:create!).with(email: email)
  92. end
  93. end
  94. end
  95. shared_examples 'for #source and #locale attributes' do
  96. context 'when given a valid #source and #locale' do
  97. let(:source) { record.source }
  98. let(:locale) { record.locale }
  99. it 'returns that record' do
  100. expect(described_class.create_if_not_exists(source: source, locale: locale)).to eq(record)
  101. end
  102. it 'does not create a new record' do
  103. allow(described_class).to receive(:create!)
  104. described_class.create_if_not_exists(source: source, locale: locale)
  105. expect(described_class).not_to have_received(:create!).with(source: source, locale: locale)
  106. end
  107. end
  108. context 'when given an invalid #source or #locale' do
  109. let(:source) { "#{described_class.pluck(:source).max}foo" }
  110. let(:locale) { record.locale }
  111. it 'attempts to create a new record' do
  112. allow(described_class).to receive(:create!)
  113. described_class.create_if_not_exists(source: source, locale: locale)
  114. expect(described_class).to have_received(:create!).with(source: source, locale: locale)
  115. end
  116. end
  117. end
  118. include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
  119. include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
  120. include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
  121. include_examples 'for #source and #locale attributes' if (described_class.attribute_names & %w[source locale]).many?
  122. end
  123. describe '.create_or_update' do
  124. let!(:record) { create(described_class.name.underscore) }
  125. let(:yesterday) { 1.day.ago }
  126. context 'when given a valid #id' do
  127. let(:id) { record.id }
  128. it 'updates other attributes on (and returns) that record' do
  129. expect { described_class.create_or_update(id: id, updated_at: yesterday) }
  130. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  131. end
  132. end
  133. context 'when given an invalid #id' do
  134. let(:id) { described_class.pluck(:id).max + 1 }
  135. it 'attempts to create a new record' do
  136. allow(described_class).to receive(:create!)
  137. described_class.create_or_update(id: id)
  138. expect(described_class).to have_received(:create!).with(id: id)
  139. end
  140. end
  141. shared_examples 'for #name attribute' do
  142. context 'when given a valid #name' do
  143. let(:name) { record.name }
  144. it 'updates other attributes on (and returns) that record' do
  145. expect { described_class.create_or_update(name: name, updated_at: yesterday) }
  146. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  147. end
  148. if unique_name
  149. it 'raises uniqueness error if identifier is different in case only' do
  150. swapcase_attibutes = attributes_for(described_class.name.underscore)
  151. .merge(name: record.name.swapcase)
  152. expect { described_class.create_or_update(**swapcase_attibutes) }
  153. .to raise_error(ActiveRecord::RecordInvalid, %r{has already been taken})
  154. end
  155. end
  156. end
  157. context 'when given an invalid #name' do
  158. let(:name) { "#{described_class.pluck(:name).max}foo" }
  159. it 'attempts to create a new record' do
  160. allow(described_class).to receive(:create!)
  161. described_class.create_or_update(name: name)
  162. expect(described_class).to have_received(:create!).with(name: name)
  163. end
  164. end
  165. end
  166. shared_examples 'for #login attribute' do
  167. context 'when given a valid #login' do
  168. let(:login) { record.login }
  169. it 'updates other attributes on (and returns) that record' do
  170. expect { described_class.create_or_update(login: login, updated_at: yesterday) }
  171. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  172. end
  173. end
  174. context 'when given an invalid #login' do
  175. let(:login) { "#{described_class.pluck(:login).max}foo" }
  176. it 'attempts to create a new record' do
  177. allow(described_class).to receive(:create!)
  178. described_class.create_or_update(login: login)
  179. expect(described_class).to have_received(:create!).with(login: login)
  180. end
  181. end
  182. end
  183. shared_examples 'for #email attribute' do
  184. context 'when given a valid #email' do
  185. let(:email) { record.email }
  186. it 'updates other attributes on (and returns) that record' do
  187. expect { described_class.create_or_update(email: email, updated_at: yesterday) }
  188. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  189. end
  190. end
  191. context 'when given an invalid #email' do
  192. let(:email) { "#{described_class.pluck(:email).max}foo" }
  193. it 'attempts to create a new record' do
  194. allow(described_class).to receive(:create!)
  195. described_class.create_or_update(email: email)
  196. expect(described_class).to have_received(:create!).with(email: email)
  197. end
  198. end
  199. end
  200. shared_examples 'for #locale attribute' do
  201. context 'when given a valid #locale' do
  202. let(:locale) { record.locale }
  203. it 'updates other attributes on (and returns) that record' do
  204. expect { described_class.create_or_update(locale: locale, updated_at: yesterday) }
  205. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  206. end
  207. end
  208. context 'when given an invalid #locale' do
  209. let(:locale) { record.locale }
  210. it 'attempts to create a new record' do
  211. allow(described_class).to receive(:create!)
  212. described_class.create_or_update(locale: locale)
  213. expect(described_class).to have_received(:create!).with(locale: locale)
  214. end
  215. end
  216. end
  217. include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
  218. include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
  219. include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
  220. include_examples 'for #locale attribute' if described_class.attribute_names.include?('locale')
  221. end
  222. describe '.create_or_update_with_ref' do
  223. before do
  224. allow(described_class)
  225. .to receive(:association_name_to_id_convert).and_return(converted_params)
  226. allow(described_class)
  227. .to receive(:create_or_update)
  228. end
  229. let(:given_params) { { given: 'attr' } }
  230. let(:converted_params) { { converted: 'attr' } }
  231. it 'calls create_or_update with given data', aggregate_failures: true do
  232. described_class.create_or_update_with_ref(given_params)
  233. expect(described_class).to have_received(:association_name_to_id_convert).with(given_params)
  234. expect(described_class).to have_received(:create_or_update).with(converted_params)
  235. end
  236. end
  237. end