commons_yellowme 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +234 -0
  6. data/bin/test +5 -0
  7. data/commons.gemspec +43 -0
  8. data/lib/commons/version.rb +1 -1
  9. data/spec/commons/authentication/authenticate_by_jwt_spec.rb +37 -0
  10. data/spec/commons/authentication/json_web_token_spec.rb +41 -0
  11. data/spec/commons/concerns/attributes/sex_spec.rb +24 -0
  12. data/spec/commons/concerns/extensions/deleted_spec.rb +42 -0
  13. data/spec/commons/concerns/guard/capitalizable_spec.rb +13 -0
  14. data/spec/commons/concerns/validations/undestroyable_spec.rb +9 -0
  15. data/spec/commons/errors/bad_request_spec.rb +62 -0
  16. data/spec/commons/errors/conflict_spec.rb +62 -0
  17. data/spec/commons/errors/forbidden_spec.rb +62 -0
  18. data/spec/commons/errors/internal_server_error_spec.rb +62 -0
  19. data/spec/commons/errors/invalid_resource_spec.rb +62 -0
  20. data/spec/commons/errors/maintenance_mode_spec.rb +49 -0
  21. data/spec/commons/errors/missing_parameter_spec.rb +49 -0
  22. data/spec/commons/errors/not_unique_spec.rb +62 -0
  23. data/spec/commons/errors/payment_required_spec.rb +62 -0
  24. data/spec/commons/errors/precondition_failed_spec.rb +62 -0
  25. data/spec/commons/errors/resource_not_found_spec.rb +49 -0
  26. data/spec/commons/errors/route_not_found_spec.rb +49 -0
  27. data/spec/commons/errors/unauthorized_spec.rb +62 -0
  28. data/spec/commons/errors/unprocessable_entity_spec.rb +62 -0
  29. data/spec/commons/formatter/e164_phone_spec.rb +155 -0
  30. data/spec/commons/formatter/regex_constants_spec.rb +102 -0
  31. data/spec/commons/formatter/string_utils_spec.rb +19 -0
  32. data/spec/commons/repositories/base_repository_spec.rb +504 -0
  33. data/spec/commons/repositories/catalogs/base_catalog_spec.rb +55 -0
  34. data/spec/commons/serializers/bad_request_spec.rb +46 -0
  35. data/spec/commons/serializers/conflict_spec.rb +46 -0
  36. data/spec/commons/serializers/forbidden_spec.rb +46 -0
  37. data/spec/commons/serializers/internal_server_error_spec.rb +46 -0
  38. data/spec/commons/serializers/invalid_resource_spec.rb +46 -0
  39. data/spec/commons/serializers/maintenance_mode_spec.rb +46 -0
  40. data/spec/commons/serializers/missing_parameter_spec.rb +44 -0
  41. data/spec/commons/serializers/not_unique_spec.rb +46 -0
  42. data/spec/commons/serializers/payment_required_spec.rb +46 -0
  43. data/spec/commons/serializers/precondition_failed_spec.rb +46 -0
  44. data/spec/commons/serializers/route_not_found_spec.rb +46 -0
  45. data/spec/commons/serializers/unauthorized_spec.rb +46 -0
  46. data/spec/commons/serializers/unprocessable_entity_spec.rb +46 -0
  47. data/spec/commons/shared-examples/user_spec.rb +18 -0
  48. data/spec/dummy/.ruby-version +1 -0
  49. data/spec/dummy/Rakefile +6 -0
  50. data/spec/dummy/app/assets/config/manifest.js +2 -0
  51. data/spec/dummy/app/assets/images/.keep +0 -0
  52. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  53. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  54. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  55. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  56. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  57. data/spec/dummy/app/controllers/miscellaneous_controller.rb +13 -0
  58. data/spec/dummy/app/errors/default_handling.rb +35 -0
  59. data/spec/dummy/app/errors/error_notifier.rb +12 -0
  60. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  61. data/spec/dummy/app/javascript/packs/application.js +15 -0
  62. data/spec/dummy/app/jobs/application_job.rb +7 -0
  63. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  64. data/spec/dummy/app/models/application_record.rb +3 -0
  65. data/spec/dummy/app/models/catalogs/application_parameter.rb +6 -0
  66. data/spec/dummy/app/models/concerns/.keep +0 -0
  67. data/spec/dummy/app/models/employee.rb +3 -0
  68. data/spec/dummy/app/models/user.rb +16 -0
  69. data/spec/dummy/app/repositories/catalogs/application_parameter_repository.rb +4 -0
  70. data/spec/dummy/app/repositories/employee_repository.rb +2 -0
  71. data/spec/dummy/app/repositories/user_repository.rb +2 -0
  72. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  73. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  74. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  75. data/spec/dummy/bin/rails +4 -0
  76. data/spec/dummy/bin/rake +4 -0
  77. data/spec/dummy/bin/setup +33 -0
  78. data/spec/dummy/config.ru +5 -0
  79. data/spec/dummy/config/application.rb +29 -0
  80. data/spec/dummy/config/boot.rb +5 -0
  81. data/spec/dummy/config/cable.yml +10 -0
  82. data/spec/dummy/config/database.yml +25 -0
  83. data/spec/dummy/config/environment.rb +5 -0
  84. data/spec/dummy/config/environments/development.rb +62 -0
  85. data/spec/dummy/config/environments/production.rb +112 -0
  86. data/spec/dummy/config/environments/test.rb +48 -0
  87. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  88. data/spec/dummy/config/initializers/assets.rb +12 -0
  89. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  90. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  91. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  92. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  93. data/spec/dummy/config/initializers/inflections.rb +16 -0
  94. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  95. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  96. data/spec/dummy/config/locales/en.yml +33 -0
  97. data/spec/dummy/config/puma.rb +38 -0
  98. data/spec/dummy/config/routes.rb +9 -0
  99. data/spec/dummy/config/spring.rb +6 -0
  100. data/spec/dummy/config/storage.yml +34 -0
  101. data/spec/dummy/db/migrate/20191212233443_create_user.rb +13 -0
  102. data/spec/dummy/db/migrate/20191213072543_create_application_parameters.rb +8 -0
  103. data/spec/dummy/db/migrate/20200101204534_create_employee.rb +8 -0
  104. data/spec/dummy/db/schema.rb +35 -0
  105. data/spec/dummy/lib/assets/.keep +0 -0
  106. data/spec/dummy/log/.keep +0 -0
  107. data/spec/dummy/public/404.html +67 -0
  108. data/spec/dummy/public/422.html +67 -0
  109. data/spec/dummy/public/500.html +66 -0
  110. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  111. data/spec/dummy/public/apple-touch-icon.png +0 -0
  112. data/spec/dummy/public/favicon.ico +0 -0
  113. data/spec/factories/catalogs/application_parameters.rb +6 -0
  114. data/spec/factories/employees.rb +6 -0
  115. data/spec/factories/users.rb +7 -0
  116. data/spec/rails_helper.rb +68 -0
  117. data/spec/spec_helper.rb +104 -0
  118. data/spec/support/.DS_Store +0 -0
  119. data/spec/support/shared-examples/capitalizable.rb +16 -0
  120. data/spec/support/shared-examples/deletable.rb +39 -0
  121. data/spec/support/shared-examples/dimorphic.rb +28 -0
  122. data/spec/support/shared-examples/stripable.rb +17 -0
  123. data/spec/support/shared-examples/undestroyable.rb +8 -0
  124. metadata +240 -3
@@ -0,0 +1,102 @@
1
+ RSpec.describe Commons::Formatter::RegexConstants do
2
+ describe 'PROPER_NOUN' do
3
+ context 'detects valid' do
4
+ names = [
5
+ 'London',
6
+ "Jérémie O'Co-nor",
7
+ 'Jorge Arturo',
8
+ 'Sr. Dr. Profesor Patricio',
9
+ Faker::Name.name,
10
+ Faker::Name.first_name,
11
+ Faker::Name.last_name,
12
+ Faker::Movies::StarWars.character,
13
+ ]
14
+
15
+ names.each do |name|
16
+ it "for name #{name}" do
17
+ expect(name =~ Commons::Formatter::RegexConstants::PROPER_NOUN).to be >= 0
18
+ end
19
+ end
20
+ end
21
+
22
+ context 'detects invalid' do
23
+ names = [
24
+ ' London',
25
+ 'London ',
26
+ '123456789',
27
+ 'Me?',
28
+ '.',
29
+ '-',
30
+ "'",
31
+ Faker::Internet.email,
32
+ ]
33
+
34
+ names.each do |name|
35
+ it "for name #{name}" do
36
+ expect(name =~ Commons::Formatter::RegexConstants::PROPER_NOUN).to be_falsey
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'CRUP' do
43
+ context 'works when valid' do
44
+ curp_list = [
45
+ 'BEML920313HMCLNS09',
46
+ 'PUXB571021HNELXR00',
47
+ 'SUPC820131HNELRS02',
48
+ 'PIMJ940217HDFNRR08',
49
+ ]
50
+ curp_list.each do |curp|
51
+ it "for CURP #{curp}" do
52
+ expect(curp =~ Commons::Formatter::RegexConstants::CURP).to be >= 0
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'fail when invalid' do
58
+ curp_list = [
59
+ 'xxxx',
60
+ '11111',
61
+ 'XAXX000000HXXYYY00'
62
+ ]
63
+ curp_list.each do |curp|
64
+ it "for CURP #{curp}" do
65
+ expect(curp =~ Commons::Formatter::RegexConstants::CURP).to be_falsey
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ describe 'RFC' do
72
+ context 'works when valid' do
73
+ rfc_list = [
74
+ 'CUPU800825569',
75
+ 'HEGJ880317LS4',
76
+ 'PUMJ810101TB9',
77
+ 'PIMJ940217CP0',
78
+ 'YME141002J52',
79
+ 'XAXX010101000',
80
+ 'XEXX010101000'
81
+ ]
82
+ rfc_list.each do |rfc|
83
+ it "for RFC #{rfc}" do
84
+ expect(rfc =~ Commons::Formatter::RegexConstants::RFC).to be >= 0
85
+ end
86
+ end
87
+ end
88
+
89
+ context 'fail when not valid' do
90
+ rfc_list = [
91
+ 'xxxx',
92
+ '11111',
93
+ 'XAXX000000HXXYYY00'
94
+ ]
95
+ rfc_list.each do |rfc|
96
+ it "for RFC #{rfc}" do
97
+ expect(rfc =~ Commons::Formatter::RegexConstants::RFC).to be_falsey
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,19 @@
1
+ RSpec.describe Commons::Formatter::StringUtils do
2
+ describe 'capitalize works correctly' do
3
+ names = [
4
+ ['name', 'Name'],
5
+ ['paternal surname', 'Paternal Surname'],
6
+ ['maternal surname', 'Maternal Surname'],
7
+ ['NAME', 'Name'],
8
+ ['PATERNAL SURNAME', 'Paternal Surname'],
9
+ ['MATERNAL SURNAME', 'Maternal Surname'],
10
+ ['reyna del rocio', 'Reyna del Rocio']
11
+ ]
12
+
13
+ names.each do |name|
14
+ it "for name #{name[0]}" do
15
+ expect(Commons::Formatter::StringUtils.capitalize(name[0])).to eq name[1]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,504 @@
1
+ RSpec.describe 'Commons::Repositories::BaseRepository' do
2
+ let(:user) { create(:user) }
3
+ let(:valid_params) do
4
+ {
5
+ name: Faker::Name.first_name,
6
+ last_name: Faker::Name.last_name
7
+ }
8
+ end
9
+
10
+ describe 'all' do
11
+ context 'when data exists' do
12
+ let(:users_amount) { 10 }
13
+ let(:deleted_users_amount) { 5 }
14
+ before do
15
+ users_amount.times {
16
+ UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
17
+ }
18
+ deleted_users_amount.times {
19
+ user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
20
+ UserRepository.instance.soft_delete!(user.id)
21
+ }
22
+ end
23
+ subject { UserRepository.instance.all }
24
+
25
+ it do
26
+ expect(subject.count).to eq users_amount + deleted_users_amount
27
+ expect(subject.first).to be_an_instance_of User
28
+ end
29
+ end
30
+
31
+ context 'when no data' do
32
+ it { expect(UserRepository.instance.all).to be_empty }
33
+ end
34
+ end
35
+
36
+ describe 'kept' do
37
+ context 'when data exists' do
38
+ let(:users_amount) { 10 }
39
+ let(:deleted_users_amount) { 5 }
40
+ before do
41
+ users_amount.times {
42
+ UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
43
+ }
44
+ deleted_users_amount.times {
45
+ user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
46
+ UserRepository.instance.soft_delete!(user.id)
47
+ }
48
+ end
49
+ subject { UserRepository.instance.kept }
50
+
51
+ it do
52
+ expect(subject.count).to eq users_amount
53
+ end
54
+ end
55
+
56
+ context 'when no data' do
57
+ let(:deleted_users_amount) { 5 }
58
+ before do
59
+ deleted_users_amount.times {
60
+ user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
61
+ UserRepository.instance.soft_delete!(user.id)
62
+ }
63
+ end
64
+
65
+ it { expect(UserRepository.instance.kept).to be_empty }
66
+ end
67
+ end
68
+
69
+ describe 'deleted' do
70
+ context 'when data exists' do
71
+ let(:users_amount) { 10 }
72
+ let(:deleted_users_amount) { 5 }
73
+ before do
74
+ users_amount.times {
75
+ UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
76
+ }
77
+ deleted_users_amount.times {
78
+ user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
79
+ UserRepository.instance.soft_delete!(user.id)
80
+ }
81
+ end
82
+ subject { UserRepository.instance.deleted }
83
+
84
+ it do
85
+ expect(subject.count).to eq deleted_users_amount
86
+ end
87
+ end
88
+
89
+ context 'when no data' do
90
+ let(:users_amount) { 10 }
91
+ before do
92
+ users_amount.times {
93
+ UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
94
+ }
95
+ end
96
+
97
+ it { expect(UserRepository.instance.deleted).to be_empty }
98
+ end
99
+ end
100
+
101
+ describe 'find' do
102
+ context 'by a valid id' do
103
+ subject { UserRepository.instance.find(user.id) }
104
+
105
+ it { is_expected.to be_an_instance_of User }
106
+ end
107
+
108
+ context 'by non-existent id' do
109
+ it do
110
+ expect do
111
+ UserRepository.instance.find('my totally non-existent id')
112
+ end.to raise_error(ActiveRecord::RecordNotFound)
113
+ end
114
+ end
115
+ end
116
+
117
+ describe 'find_kept' do
118
+ context 'by a valid id' do
119
+ subject { UserRepository.instance.find_kept(user.id) }
120
+
121
+ it { is_expected.to be_an_instance_of User }
122
+ end
123
+
124
+ context 'by non-existent id' do
125
+ it do
126
+ expect do
127
+ UserRepository.instance.find_kept('my totally non-existent id')
128
+ end.to raise_error(ActiveRecord::RecordNotFound)
129
+ end
130
+ end
131
+
132
+ context 'by previously deleted user' do
133
+ before do
134
+ UserRepository.instance.soft_delete!(user.id)
135
+ end
136
+ subject { UserRepository.instance.find_kept(user.id) }
137
+
138
+ it do
139
+ expect{ subject }.to raise_error(ActiveRecord::RecordNotFound)
140
+ end
141
+ end
142
+ end
143
+
144
+ describe 'find_by' do
145
+ context 'by a valid id' do
146
+ subject { UserRepository.instance.find_by(id: user.id) }
147
+
148
+ it { is_expected.to be_an_instance_of User }
149
+ end
150
+
151
+ context 'by non-existent id' do
152
+ it { expect(UserRepository.instance.find_by(id: 'my totally non-existent id')).to be_falsey }
153
+ end
154
+ end
155
+
156
+ describe 'find_kept_by' do
157
+ context 'by a valid id' do
158
+ subject { UserRepository.instance.find_kept_by(id: user.id) }
159
+
160
+ it { is_expected.to be_an_instance_of User }
161
+ end
162
+
163
+ context 'by non-existent id' do
164
+ it { expect(UserRepository.instance.find_kept_by(id: 'my totally non-existent id')).to be_falsey }
165
+ end
166
+
167
+ context 'by previously deleted user' do
168
+ before do
169
+ UserRepository.instance.soft_delete!(user.id)
170
+ end
171
+ it { expect(UserRepository.instance.find_kept_by(id: 'my totally non-existent id')).to be_falsey }
172
+ end
173
+ end
174
+
175
+ describe 'find_by!' do
176
+ context 'by a valid id!' do
177
+ subject { UserRepository.instance.find_by!(id: user.id) }
178
+
179
+ it { is_expected.to be_an_instance_of User }
180
+ end
181
+
182
+ context 'by non-existent id!' do
183
+ it do
184
+ expect do
185
+ UserRepository.instance.find_by!(
186
+ id: 'my totally non-existent id'
187
+ )
188
+ end.to raise_error(ActiveRecord::RecordNotFound)
189
+ end
190
+ end
191
+ end
192
+
193
+ describe 'find_kept_by!' do
194
+ context 'by a valid id!' do
195
+ subject { UserRepository.instance.find_kept_by!(id: user.id) }
196
+
197
+ it { is_expected.to be_an_instance_of User }
198
+ end
199
+
200
+ context 'by non-existent id!' do
201
+ it do
202
+ expect do
203
+ UserRepository.instance.find_kept_by!(
204
+ id: 'my totally non-existent id'
205
+ )
206
+ end.to raise_error(ActiveRecord::RecordNotFound)
207
+ end
208
+ end
209
+
210
+ context 'by previously deleted user' do
211
+ before do
212
+ UserRepository.instance.soft_delete!(user.id)
213
+ end
214
+ it do
215
+ expect do
216
+ UserRepository.instance.find_kept_by!(
217
+ id: 'my totally non-existent id'
218
+ )
219
+ end.to raise_error(ActiveRecord::RecordNotFound)
220
+ end
221
+ end
222
+ end
223
+
224
+ describe 'find_or_create_by!' do
225
+ describe 'works ok!' do
226
+ context 'when exists' do
227
+ let(:previous_user) { UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys) }
228
+ before do
229
+ previous_user
230
+ end
231
+ subject { UserRepository.instance.find_or_create_by!(**valid_params.to_h.symbolize_keys) }
232
+
233
+ it do
234
+ expect(subject).to be_an_instance_of User
235
+ expect(subject.id).to eq previous_user.id
236
+ expect(subject.name).to eq valid_params[:name]
237
+ expect(subject.last_name).to eq valid_params[:last_name]
238
+ end
239
+ end
240
+
241
+ context 'when creates' do
242
+ let(:previous_valid_params) do
243
+ {
244
+ name: 'Johnny',
245
+ last_name: Faker::Name.last_name
246
+ }
247
+ end
248
+ let(:previous_user) { UserRepository.instance.create_from_params!(**previous_valid_params.to_h.symbolize_keys) }
249
+ before do
250
+ previous_user
251
+ end
252
+ subject { UserRepository.instance.find_or_create_by!(**valid_params.to_h.symbolize_keys) }
253
+
254
+ it do
255
+ expect(subject).to be_an_instance_of User
256
+ expect(subject.id).not_to eq previous_user.id
257
+ expect(subject.name).to eq valid_params[:name]
258
+ expect(subject.last_name).to eq valid_params[:last_name]
259
+ end
260
+ end
261
+ end
262
+
263
+ context 'fails ok!' do
264
+ let(:invalid_params) do
265
+ {
266
+ last_name: Faker::Name.last_name,
267
+ age: 18
268
+ }
269
+ end
270
+
271
+ subject { UserRepository.instance.find_or_create_by!(**invalid_params.to_h.symbolize_keys) }
272
+
273
+ it { expect { subject }.to raise_error(ActiveRecord::RecordInvalid) }
274
+ end
275
+ end
276
+
277
+ describe 'where_first_or_create!' do
278
+ describe 'works ok!' do
279
+ context 'when exists' do
280
+ let(:previous_user) { UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys) }
281
+ let(:new_params) do
282
+ {
283
+ name: 'Jhonny',
284
+ last_name: 'Be Good'
285
+ }
286
+ end
287
+ before do
288
+ previous_user
289
+ end
290
+ subject { UserRepository.instance.where_first_or_create!({ name: valid_params[:name]}, **new_params.to_h.symbolize_keys) }
291
+
292
+ it do
293
+ expect(subject).to be_an_instance_of User
294
+ expect(subject.id).to eq previous_user.id
295
+ expect(subject.name).to eq valid_params[:name]
296
+ expect(subject.last_name).to eq valid_params[:last_name]
297
+ end
298
+ end
299
+
300
+ context 'when creates' do
301
+ let(:previous_user) { UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys) }
302
+ let(:new_params) do
303
+ {
304
+ name: 'Jhonny',
305
+ last_name: 'Be Good'
306
+ }
307
+ end
308
+ before do
309
+ previous_user
310
+ end
311
+ subject { UserRepository.instance.where_first_or_create!({ name: new_params[:name]}, **new_params.to_h.symbolize_keys) }
312
+
313
+ it do
314
+ expect(subject).to be_an_instance_of User
315
+ expect(subject.id).not_to eq previous_user.id
316
+ expect(subject.name).to eq new_params[:name]
317
+ expect(subject.last_name).to eq new_params[:last_name]
318
+ end
319
+ end
320
+ end
321
+
322
+ context 'fails ok!' do
323
+ let(:invalid_params) do
324
+ {
325
+ last_name: Faker::Name.last_name,
326
+ age: 18
327
+ }
328
+ end
329
+
330
+ subject { UserRepository.instance.where_first_or_create!({last_name: valid_params[:last_name]}, **invalid_params.to_h.symbolize_keys) }
331
+
332
+ it { expect { subject }.to raise_error(ActiveRecord::RecordInvalid) }
333
+ end
334
+ end
335
+
336
+ describe 'create!' do
337
+ describe 'works when' do
338
+ context 'using a valid user' do
339
+ let(:user) { build(:user) }
340
+
341
+ subject { UserRepository.instance.create!(user) }
342
+
343
+ it { expect(subject).to be true }
344
+ end
345
+ end
346
+
347
+ describe 'fails when' do
348
+ context 'is not a user' do
349
+ let(:employee) { build(:employee) }
350
+
351
+ it do
352
+ expect do
353
+ UserRepository.instance.create!(employee)
354
+ end.to raise_error(ArgumentError)
355
+ end
356
+ end
357
+
358
+ context 'is not a valid user' do
359
+ let(:user) { build(:user, name: nil) }
360
+
361
+ it do
362
+ expect do
363
+ UserRepository.instance.create!(user)
364
+ end.to raise_error(ActiveRecord::RecordInvalid)
365
+ end
366
+ end
367
+ end
368
+ end
369
+
370
+ describe 'create_from_params!' do
371
+ context 'works ok!' do
372
+ subject { UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys) }
373
+
374
+ it do
375
+ expect(subject).to be_an_instance_of User
376
+ expect(subject.name).to eq valid_params[:name]
377
+ expect(subject.last_name).to eq valid_params[:last_name]
378
+ end
379
+ end
380
+
381
+ context 'fails ok!' do
382
+ let(:invalid_params) {}
383
+
384
+ subject { UserRepository.instance.create_from_params!(**invalid_params.to_h.symbolize_keys) }
385
+
386
+ it { expect { subject }.to raise_error(ActiveRecord::RecordInvalid) }
387
+ end
388
+ end
389
+
390
+ describe 'update!' do
391
+ describe 'works when' do
392
+ context 'using a valid user' do
393
+ let(:user) { create(:user) }
394
+
395
+ subject { UserRepository.instance.update!(user) }
396
+
397
+ it { expect(subject).to be true }
398
+ end
399
+ end
400
+
401
+ describe 'fails when' do
402
+ context 'is not a user' do
403
+ let(:employee) { create(:employee) }
404
+
405
+ it do
406
+ expect do
407
+ UserRepository.instance.update!(employee)
408
+ end.to raise_error(ArgumentError)
409
+ end
410
+ end
411
+
412
+ context 'is not a valid user' do
413
+ let(:user) { create(:user) }
414
+ let(:invalid_user) do
415
+ user.name = nil
416
+ user
417
+ end
418
+
419
+ it do
420
+ expect do
421
+ UserRepository.instance.update!(invalid_user)
422
+ end.to raise_error(ActiveRecord::RecordInvalid)
423
+ end
424
+ end
425
+ end
426
+ end
427
+
428
+ describe 'update_from_params!' do
429
+ context 'works ok!' do
430
+ subject { UserRepository.instance.update_from_params!(id: user.id, **valid_params.to_h.symbolize_keys) }
431
+
432
+ it do
433
+ expect(subject).to be_an_instance_of User
434
+ expect(subject.name).to eq valid_params[:name]
435
+ expect(subject.last_name).to eq valid_params[:last_name]
436
+ end
437
+ end
438
+
439
+ describe 'fails ok!' do
440
+ context 'if invalid params' do
441
+ let(:invalid_params) do
442
+ {
443
+ name: nil
444
+ }
445
+ end
446
+
447
+ subject { UserRepository.instance.update_from_params!(id: user.id, **invalid_params.to_h.symbolize_keys) }
448
+
449
+ it { expect { subject }.to raise_error(ActiveRecord::RecordInvalid) }
450
+ end
451
+
452
+ context 'by a unknown param' do
453
+ it do
454
+ expect do
455
+ UserRepository.instance.update_from_params!(
456
+ id: user.id,
457
+ data: 'dummy'
458
+ )
459
+ end.to raise_error(ActiveRecord::UnknownAttributeError)
460
+ end
461
+ end
462
+
463
+ context 'if not found' do
464
+ subject { UserRepository.instance.update_from_params!(id: Faker::Number.number(digits: 2), **valid_params.to_h.symbolize_keys) }
465
+
466
+ it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
467
+ end
468
+ end
469
+ end
470
+
471
+ describe 'soft_delete' do
472
+ describe 'works when' do
473
+ context 'using a valid user' do
474
+ let(:user) { create(:user) }
475
+
476
+ subject { UserRepository.instance.soft_delete!(user.id) }
477
+
478
+ it { expect(subject.deleted_at).not_to be nil }
479
+ end
480
+ end
481
+
482
+ describe 'fails when' do
483
+ context 'is not deletable' do
484
+ let(:employee) { create(:employee) }
485
+
486
+ it do
487
+ expect do
488
+ EmployeeRepository.instance.soft_delete!(employee.id)
489
+ end.to raise_error(ActiveModel::MissingAttributeError)
490
+ end
491
+ end
492
+
493
+ context 'is not a valid user' do
494
+ let(:user) { create(:user, deleted_at: Time.current) }
495
+
496
+ it do
497
+ expect do
498
+ UserRepository.instance.soft_delete!(user)
499
+ end.to raise_error(ActiveRecord::RecordNotFound)
500
+ end
501
+ end
502
+ end
503
+ end
504
+ end