can_creates_and_updates_examples.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. RSpec.shared_examples 'ApplicationModel::CanCreatesAndUpdates' do
  2. describe '.create_if_not_exists' do
  3. let!(:record) { create(described_class.name.underscore) }
  4. context 'when given a valid #id' do
  5. let(:id) { record.id }
  6. it 'returns that record' do
  7. expect(described_class.create_if_not_exists(id: id)).to eq(record)
  8. end
  9. it 'does not create a new record' do
  10. allow(described_class).to receive(:create)
  11. described_class.create_if_not_exists(id: id)
  12. expect(described_class).not_to have_received(:create).with(id: id)
  13. end
  14. end
  15. context 'when given an invalid #id' do
  16. let(:id) { described_class.pluck(:id).max + 1 }
  17. it 'attempts to create a new record' do
  18. allow(described_class).to receive(:create)
  19. described_class.create_if_not_exists(id: id)
  20. expect(described_class).to have_received(:create).with(id: id)
  21. end
  22. end
  23. shared_examples 'for #name attribute' do
  24. context 'when given a valid #name' do
  25. let(:name) { record.name }
  26. it 'returns that record' do
  27. expect(described_class.create_if_not_exists(name: name)).to eq(record)
  28. end
  29. it 'does not create a new record' do
  30. allow(described_class).to receive(:create)
  31. described_class.create_if_not_exists(name: name)
  32. expect(described_class).not_to have_received(:create).with(name: name)
  33. end
  34. end
  35. context 'when given an invalid #name' do
  36. let(:name) { "#{described_class.pluck(:name).max}foo" }
  37. it 'attempts to create a new record' do
  38. allow(described_class).to receive(:create)
  39. described_class.create_if_not_exists(name: name)
  40. expect(described_class).to have_received(:create).with(name: name)
  41. end
  42. end
  43. end
  44. shared_examples 'for #login attribute' do
  45. context 'when given a valid #login' do
  46. let(:login) { record.login }
  47. it 'returns that record' do
  48. expect(described_class.create_if_not_exists(login: login)).to eq(record)
  49. end
  50. it 'does not create a new record' do
  51. allow(described_class).to receive(:create)
  52. described_class.create_if_not_exists(login: login)
  53. expect(described_class).not_to have_received(:create).with(login: login)
  54. end
  55. end
  56. context 'when given an invalid #login' do
  57. let(:login) { "#{described_class.pluck(:login).max}foo" }
  58. it 'attempts to create a new record' do
  59. allow(described_class).to receive(:create)
  60. described_class.create_if_not_exists(login: login)
  61. expect(described_class).to have_received(:create).with(login: login)
  62. end
  63. end
  64. end
  65. shared_examples 'for #email attribute' do
  66. context 'when given a valid #email' do
  67. let(:email) { record.email }
  68. it 'returns that record' do
  69. expect(described_class.create_if_not_exists(email: email)).to eq(record)
  70. end
  71. it 'does not create a new record' do
  72. allow(described_class).to receive(:create)
  73. described_class.create_if_not_exists(email: email)
  74. expect(described_class).not_to have_received(:create).with(email: email)
  75. end
  76. end
  77. context 'when given an invalid #email' do
  78. let(:email) { "#{described_class.pluck(:email).max}foo" }
  79. it 'attempts to create a new record' do
  80. allow(described_class).to receive(:create)
  81. described_class.create_if_not_exists(email: email)
  82. expect(described_class).to have_received(:create).with(email: email)
  83. end
  84. end
  85. end
  86. shared_examples 'for #source and #locale attributes' do
  87. context 'when given a valid #source and #locale' do
  88. let(:source) { record.source }
  89. let(:locale) { record.locale }
  90. it 'returns that record' do
  91. expect(described_class.create_if_not_exists(source: source, locale: locale)).to eq(record)
  92. end
  93. it 'does not create a new record' do
  94. allow(described_class).to receive(:create)
  95. described_class.create_if_not_exists(source: source, locale: locale)
  96. expect(described_class).not_to have_received(:create).with(source: source, locale: locale)
  97. end
  98. end
  99. context 'when given an invalid #source or #locale' do
  100. let(:source) { "#{described_class.pluck(:source).max}foo" }
  101. let(:locale) { record.locale }
  102. it 'attempts to 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).to have_received(:create).with(source: source, locale: locale)
  106. end
  107. end
  108. end
  109. include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
  110. include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
  111. include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
  112. include_examples 'for #source and #locale attributes' if (described_class.attribute_names & %w[source locale]).many?
  113. end
  114. describe '.create_or_update' do
  115. let!(:record) { create(described_class.name.underscore) }
  116. let(:yesterday) { 1.day.ago }
  117. context 'when given a valid #id' do
  118. let(:id) { record.id }
  119. it 'updates other attributes on (and returns) that record' do
  120. expect { described_class.create_or_update(id: id, updated_at: yesterday) }
  121. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  122. end
  123. end
  124. context 'when given an invalid #id' do
  125. let(:id) { described_class.pluck(:id).max + 1 }
  126. it 'attempts to create a new record' do
  127. allow(described_class).to receive(:create)
  128. described_class.create_or_update(id: id)
  129. expect(described_class).to have_received(:create).with(id: id)
  130. end
  131. end
  132. shared_examples 'for #name attribute' do
  133. context 'when given a valid #name' do
  134. let(:name) { record.name }
  135. it 'updates other attributes on (and returns) that record' do
  136. expect { described_class.create_or_update(name: name, updated_at: yesterday) }
  137. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  138. end
  139. end
  140. context 'when given an invalid #name' do
  141. let(:name) { "#{described_class.pluck(:name).max}foo" }
  142. it 'attempts to create a new record' do
  143. allow(described_class).to receive(:create)
  144. described_class.create_or_update(name: name)
  145. expect(described_class).to have_received(:create).with(name: name)
  146. end
  147. end
  148. end
  149. shared_examples 'for #login attribute' do
  150. context 'when given a valid #login' do
  151. let(:login) { record.login }
  152. it 'updates other attributes on (and returns) that record' do
  153. expect { described_class.create_or_update(login: login, updated_at: yesterday) }
  154. .to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
  155. end
  156. end
  157. context 'when given an invalid #login' do
  158. let(:login) { "#{described_class.pluck(:login).max}foo" }
  159. it 'attempts to create a new record' do
  160. allow(described_class).to receive(:create)
  161. described_class.create_or_update(login: login)
  162. expect(described_class).to have_received(:create).with(login: login)
  163. end
  164. end
  165. end
  166. shared_examples 'for #email attribute' do
  167. context 'when given a valid #email' do
  168. let(:email) { record.email }
  169. it 'updates other attributes on (and returns) that record' do
  170. expect { described_class.create_or_update(email: email, 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 #email' do
  175. let(:email) { "#{described_class.pluck(:email).max}foo" }
  176. it 'attempts to create a new record' do
  177. allow(described_class).to receive(:create)
  178. described_class.create_or_update(email: email)
  179. expect(described_class).to have_received(:create).with(email: email)
  180. end
  181. end
  182. end
  183. shared_examples 'for #locale attribute' do
  184. context 'when given a valid #locale' do
  185. let(:locale) { record.locale }
  186. it 'updates other attributes on (and returns) that record' do
  187. expect { described_class.create_or_update(locale: locale, 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 #locale' do
  192. let(:locale) { record.locale }
  193. it 'attempts to create a new record' do
  194. allow(described_class).to receive(:create)
  195. described_class.create_or_update(locale: locale)
  196. expect(described_class).to have_received(:create).with(locale: locale)
  197. end
  198. end
  199. end
  200. include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
  201. include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
  202. include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
  203. include_examples 'for #locale attribute' if described_class.attribute_names.include?('locale')
  204. end
  205. end