auction_fun_core 0.8.7 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +2 -0
  3. data/CHANGELOG.md +29 -0
  4. data/Procfile +1 -0
  5. data/README.md +34 -32
  6. data/Rakefile +1 -1
  7. data/auction_fun_core.gemspec +1 -1
  8. data/i18n/en-US/contracts/contracts.en-US.yml +3 -0
  9. data/i18n/en-US/mail/application.en-US.yml +7 -0
  10. data/i18n/en-US/mail/auction_context/pre_auction/auction_start_reminder.en-US.yml +11 -0
  11. data/i18n/pt-BR/contracts/contracts.pt-BR.yml +3 -0
  12. data/i18n/pt-BR/mail/application.pt-BR.yml +7 -0
  13. data/i18n/pt-BR/mail/auction_context/pre_auction/auction_start_reminder.pt-BR.yml +11 -0
  14. data/lib/auction_fun_core/business/configuration.rb +31 -0
  15. data/lib/auction_fun_core/business/token_generator.rb +19 -1
  16. data/lib/auction_fun_core/contracts/application_contract.rb +9 -1
  17. data/lib/auction_fun_core/contracts/auction_context/create_contract.rb +35 -20
  18. data/lib/auction_fun_core/contracts/auction_context/post_auction/participant_contract.rb +23 -1
  19. data/lib/auction_fun_core/contracts/auction_context/post_auction/winner_contract.rb +22 -1
  20. data/lib/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract.rb +48 -0
  21. data/lib/auction_fun_core/contracts/auction_context/processor/finish/closed_contract.rb +19 -7
  22. data/lib/auction_fun_core/contracts/auction_context/processor/finish/penny_contract.rb +19 -7
  23. data/lib/auction_fun_core/contracts/auction_context/processor/finish/standard_contract.rb +19 -7
  24. data/lib/auction_fun_core/contracts/auction_context/processor/pause_contract.rb +16 -4
  25. data/lib/auction_fun_core/contracts/auction_context/processor/start_contract.rb +17 -5
  26. data/lib/auction_fun_core/contracts/auction_context/processor/unpause_contract.rb +16 -4
  27. data/lib/auction_fun_core/contracts/bid_context/create_bid_closed_contract.rb +20 -11
  28. data/lib/auction_fun_core/contracts/bid_context/create_bid_penny_contract.rb +18 -9
  29. data/lib/auction_fun_core/contracts/bid_context/create_bid_standard_contract.rb +19 -10
  30. data/lib/auction_fun_core/contracts/staff_context/authentication_contract.rb +18 -4
  31. data/lib/auction_fun_core/contracts/staff_context/registration_contract.rb +20 -8
  32. data/lib/auction_fun_core/contracts/user_context/authentication_contract.rb +18 -4
  33. data/lib/auction_fun_core/contracts/user_context/email_confirmation_contract.rb +17 -2
  34. data/lib/auction_fun_core/contracts/user_context/phone_confirmation_contract.rb +17 -2
  35. data/lib/auction_fun_core/contracts/user_context/registration_contract.rb +26 -8
  36. data/lib/auction_fun_core/entities/auction.rb +48 -4
  37. data/lib/auction_fun_core/entities/bid.rb +3 -2
  38. data/lib/auction_fun_core/entities/staff.rb +15 -2
  39. data/lib/auction_fun_core/entities/user.rb +31 -2
  40. data/lib/auction_fun_core/events/app.rb +8 -2
  41. data/lib/auction_fun_core/events/listener.rb +19 -16
  42. data/lib/auction_fun_core/operations/auction_context/create_operation.rb +25 -9
  43. data/lib/auction_fun_core/operations/auction_context/post_auction/participant_operation.rb +36 -3
  44. data/lib/auction_fun_core/operations/auction_context/post_auction/winner_operation.rb +36 -2
  45. data/lib/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation.rb +96 -0
  46. data/lib/auction_fun_core/operations/auction_context/processor/finish/closed_operation.rb +82 -10
  47. data/lib/auction_fun_core/operations/auction_context/processor/finish/penny_operation.rb +81 -10
  48. data/lib/auction_fun_core/operations/auction_context/processor/finish/standard_operation.rb +81 -12
  49. data/lib/auction_fun_core/operations/auction_context/processor/pause_operation.rb +36 -1
  50. data/lib/auction_fun_core/operations/auction_context/processor/unpause_operation.rb +36 -1
  51. data/lib/auction_fun_core/relations/auctions.rb +178 -97
  52. data/lib/auction_fun_core/relations/bids.rb +18 -0
  53. data/lib/auction_fun_core/repos/auction_context/auction_repository.rb +40 -11
  54. data/lib/auction_fun_core/repos/bid_context/bid_repository.rb +27 -5
  55. data/lib/auction_fun_core/repos/staff_context/staff_repository.rb +63 -21
  56. data/lib/auction_fun_core/repos/user_context/user_repository.rb +69 -25
  57. data/lib/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer.rb +7 -1
  58. data/lib/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer.rb +7 -1
  59. data/lib/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer.rb +35 -0
  60. data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/participant.html.erb +1 -1
  61. data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/winner.html.erb +1 -1
  62. data/lib/auction_fun_core/services/mail/templates/auction_context/pre_auction/auction_start_reminder.html.erb +192 -0
  63. data/lib/auction_fun_core/services/mail/user_context/registration_mailer.rb +6 -0
  64. data/lib/auction_fun_core/version.rb +1 -1
  65. data/lib/auction_fun_core/workers/application_job.rb +10 -0
  66. data/lib/auction_fun_core/workers/operations/auction_context/post_auction/participant_operation_job.rb +7 -2
  67. data/lib/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job.rb +6 -2
  68. data/lib/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job.rb +44 -0
  69. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job.rb +6 -3
  70. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job.rb +6 -3
  71. data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job.rb +6 -2
  72. data/lib/auction_fun_core/workers/operations/auction_context/processor/start_operation_job.rb +6 -3
  73. data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job.rb +12 -7
  74. data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job.rb +11 -5
  75. data/lib/auction_fun_core/workers/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_job.rb +48 -0
  76. data/lib/auction_fun_core/workers/services/mail/user_context/registration_mailer_job.rb +8 -6
  77. data/spec/auction_fun_core/contracts/auction_context/create_contract_spec.rb +158 -0
  78. data/spec/auction_fun_core/contracts/auction_context/post_auction/participant_contract_spec.rb +77 -0
  79. data/spec/auction_fun_core/contracts/auction_context/post_auction/winner_contract_spec.rb +76 -0
  80. data/spec/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract_spec.rb +48 -0
  81. data/spec/auction_fun_core/contracts/auction_context/processor/finish/closed_contract_spec.rb +59 -0
  82. data/spec/auction_fun_core/contracts/auction_context/processor/finish/penny_contract_spec.rb +59 -0
  83. data/spec/auction_fun_core/contracts/auction_context/processor/finish/standard_contract_spec.rb +59 -0
  84. data/spec/auction_fun_core/contracts/auction_context/processor/pause_contract_spec.rb +39 -0
  85. data/spec/auction_fun_core/contracts/auction_context/processor/start_contract_spec.rb +59 -0
  86. data/spec/auction_fun_core/contracts/auction_context/processor/unpause_contract_spec.rb +39 -0
  87. data/spec/auction_fun_core/contracts/bid_context/create_bid_closed_contract_spec.rb +130 -0
  88. data/spec/auction_fun_core/contracts/bid_context/create_bid_penny_contract_spec.rb +118 -0
  89. data/spec/auction_fun_core/contracts/bid_context/create_bid_standard_contract_spec.rb +154 -0
  90. data/spec/auction_fun_core/contracts/staff_context/authentication_contract_spec.rb +89 -0
  91. data/spec/auction_fun_core/contracts/staff_context/registration_contract_spec.rb +24 -0
  92. data/spec/auction_fun_core/contracts/user_context/authentication_contract_spec.rb +89 -0
  93. data/spec/auction_fun_core/contracts/user_context/email_confirmation_contract_spec.rb +54 -0
  94. data/spec/auction_fun_core/contracts/user_context/phone_confirmation_contract_spec.rb +54 -0
  95. data/spec/auction_fun_core/contracts/user_context/registration_contract_spec.rb +27 -0
  96. data/spec/auction_fun_core/entities/auction_spec.rb +75 -0
  97. data/spec/auction_fun_core/entities/bid_spec.rb +7 -0
  98. data/spec/auction_fun_core/entities/staff_spec.rb +38 -0
  99. data/spec/auction_fun_core/entities/user_spec.rb +106 -0
  100. data/spec/auction_fun_core/operations/auction_context/create_operation_spec.rb +126 -0
  101. data/spec/auction_fun_core/operations/auction_context/post_auction/participant_operation_spec.rb +88 -0
  102. data/spec/auction_fun_core/operations/auction_context/post_auction/winner_operation_spec.rb +76 -0
  103. data/spec/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation_spec.rb +98 -0
  104. data/spec/auction_fun_core/operations/auction_context/processor/finish/closed_operation_spec.rb +107 -0
  105. data/spec/auction_fun_core/operations/auction_context/processor/finish/penny_operation_spec.rb +107 -0
  106. data/spec/auction_fun_core/operations/auction_context/processor/finish/standard_operation_spec.rb +106 -0
  107. data/spec/auction_fun_core/operations/auction_context/processor/pause_operation_spec.rb +83 -0
  108. data/spec/auction_fun_core/operations/auction_context/processor/start_operation_spec.rb +147 -0
  109. data/spec/auction_fun_core/operations/auction_context/processor/unpause_operation_spec.rb +80 -0
  110. data/spec/auction_fun_core/operations/bid_context/create_bid_closed_operation_spec.rb +95 -0
  111. data/spec/auction_fun_core/operations/bid_context/create_bid_penny_operation_spec.rb +133 -0
  112. data/spec/auction_fun_core/operations/bid_context/create_bid_standard_operation_spec.rb +95 -0
  113. data/spec/auction_fun_core/operations/staff_context/authentication_operation_spec.rb +74 -0
  114. data/spec/auction_fun_core/operations/staff_context/registration_operation_spec.rb +93 -0
  115. data/spec/auction_fun_core/operations/user_context/authentication_operation_spec.rb +74 -0
  116. data/spec/auction_fun_core/operations/user_context/email_confirmation_operation_spec.rb +76 -0
  117. data/spec/auction_fun_core/operations/user_context/phone_confirmation_operation_spec.rb +76 -0
  118. data/spec/auction_fun_core/operations/user_context/registration_operation_spec.rb +100 -0
  119. data/spec/auction_fun_core/relations/auctions_spec.rb +471 -0
  120. data/spec/auction_fun_core/repos/auction_context/auction_repository_spec.rb +64 -0
  121. data/spec/auction_fun_core/repos/bid_context/bid_repository_spec.rb +64 -0
  122. data/spec/auction_fun_core/repos/staff_context/staff_repository_spec.rb +118 -0
  123. data/spec/auction_fun_core/repos/user_context/user_repository_spec.rb +117 -0
  124. data/spec/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer_spec.rb +48 -0
  125. data/spec/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer_spec.rb +48 -0
  126. data/spec/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_spec.rb +40 -0
  127. data/spec/auction_fun_core/services/mail/user_context/registration_mailer_spec.rb +33 -0
  128. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/participation_operation_job_spec.rb +54 -0
  129. data/spec/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job_spec.rb +48 -0
  130. data/spec/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job_spec.rb +53 -0
  131. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job_spec.rb +47 -0
  132. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job_spec.rb +47 -0
  133. data/spec/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job_spec.rb +47 -0
  134. data/spec/auction_fun_core/workers/operations/auction_context/processor/start_operation_job_spec.rb +47 -0
  135. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job_spec.rb +62 -0
  136. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/pre_auction/auction_start_reminder_mailer_job_spec.rb +59 -0
  137. data/spec/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job_spec.rb +62 -0
  138. data/spec/auction_fun_core/workers/services/mail/user_context/registration_mailer_job_spec.rb +53 -0
  139. data/spec/auction_fun_core_spec.rb +7 -0
  140. data/spec/spec_helper.rb +61 -0
  141. data/spec/support/background_job.rb +7 -0
  142. data/spec/support/factories/auctions.rb +143 -0
  143. data/spec/support/factories/bids.rb +6 -0
  144. data/spec/support/factories/staffs.rb +18 -0
  145. data/spec/support/factories/users.rb +43 -0
  146. data/spec/support/faker.rb +6 -0
  147. data/spec/support/mail.rb +13 -0
  148. data/spec/support/shared_examples/validate_email_contract.rb +25 -0
  149. data/spec/support/shared_examples/validate_name_contract.rb +19 -0
  150. data/spec/support/shared_examples/validate_password_confirmation_contract.rb +16 -0
  151. data/spec/support/shared_examples/validate_password_contract.rb +18 -0
  152. data/spec/support/shared_examples/validate_phone_contract.rb +25 -0
  153. data/spec/support/shared_examples/validate_stopwatch_contract.rb +32 -0
  154. data/system/providers/background_job.rb +6 -0
  155. metadata +87 -2
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::PreAuction::AuctionStartReminderOperation, type: :operation do
6
+ describe ".call(attributes, &block)" do
7
+ let(:operation) { described_class }
8
+
9
+ context "when block is given" do
10
+ context "when operation happens with success" do
11
+ let(:auction) { Factory[:auction, :default_scheduled_standard, started_at: 3.hours.from_now] }
12
+ let(:participant) { Factory[:user] }
13
+ let(:attributes) { {auction_id: auction.id} }
14
+
15
+ before do
16
+ Factory[:bid, user_id: participant.id, auction_id: auction.id, value_cents: auction.minimal_bid_cents]
17
+ end
18
+
19
+ it "expect result success matching block" do
20
+ matched_success = nil
21
+ matched_failure = nil
22
+
23
+ operation.call(attributes) do |o|
24
+ o.success { |v| matched_success = v }
25
+ o.failure { |f| matched_failure = f }
26
+ end
27
+
28
+ expect(matched_success).to include([participant.id])
29
+ expect(matched_failure).to be_nil
30
+ end
31
+ end
32
+
33
+ context "when operation happens with failure" do
34
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
35
+
36
+ it "expect result matching block" do
37
+ matched_success = nil
38
+ matched_failure = nil
39
+
40
+ operation.call(attributes) do |o|
41
+ o.success { |v| matched_success = v }
42
+ o.failure { |f| matched_failure = f }
43
+ end
44
+
45
+ expect(matched_success).to be_nil
46
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#call(attributes)" do
53
+ subject(:operation) { described_class.new.call(attributes) }
54
+
55
+ context "when contract is invalid" do
56
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
57
+
58
+ it "expect return failure with error messages" do
59
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
60
+ end
61
+ end
62
+
63
+ context "when contract is valid" do
64
+ let(:auction) { Factory[:auction, :default_scheduled_standard, started_at: 3.hours.from_now] }
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ context "when the auction has no participants" do
68
+ it "expect not to schedule a reminder email to the participant" do
69
+ allow(AuctionFunCore::Workers::Services::Mail::AuctionContext::PreAuction::AuctionStartReminderMailerJob)
70
+ .to receive(:perform_async)
71
+
72
+ operation
73
+
74
+ expect(AuctionFunCore::Workers::Services::Mail::AuctionContext::PreAuction::AuctionStartReminderMailerJob)
75
+ .not_to have_received(:perform_async)
76
+ end
77
+ end
78
+
79
+ context "when the auction has participants" do
80
+ let(:participant) { Factory[:user] }
81
+
82
+ before do
83
+ Factory[:bid, user_id: participant.id, auction_id: auction.id, value_cents: auction.minimal_bid_cents]
84
+ end
85
+
86
+ it "expects to schedule a reminder email to the participant" do
87
+ allow(AuctionFunCore::Workers::Services::Mail::AuctionContext::PreAuction::AuctionStartReminderMailerJob)
88
+ .to receive(:perform_async).with(auction.id, participant.id)
89
+
90
+ operation
91
+
92
+ expect(AuctionFunCore::Workers::Services::Mail::AuctionContext::PreAuction::AuctionStartReminderMailerJob)
93
+ .to have_received(:perform_async).with(auction.id, participant.id).once
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::Finish::ClosedOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_running_closed, finished_at: Time.current] }
8
+
9
+ describe ".call(auction_id, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:attributes) { {auction_id: auction.id} }
15
+ it "expect result success matching block" do
16
+ matched_success = nil
17
+ matched_failure = nil
18
+
19
+ operation.call(attributes) do |o|
20
+ o.success { |v| matched_success = v }
21
+ o.failure { |f| matched_failure = f }
22
+ end
23
+
24
+ expect(matched_success.id).to eq(auction.id)
25
+ expect(matched_success.status).to eq("finished")
26
+ expect(matched_failure).to be_nil
27
+ end
28
+ end
29
+
30
+ context "when operation happens with failure" do
31
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
32
+
33
+ it "expect result matching block" do
34
+ matched_success = nil
35
+ matched_failure = nil
36
+
37
+ operation.call(attributes) do |o|
38
+ o.success { |v| matched_success = v }
39
+ o.failure { |f| matched_failure = f }
40
+ end
41
+
42
+ expect(matched_success).to be_nil
43
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#call" do
50
+ subject(:operation) { described_class.new.call(attributes) }
51
+
52
+ context "when contract is invalid" do
53
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
54
+
55
+ it "expect not change auction status" do
56
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
57
+ end
58
+
59
+ it "expect return failure with error messages" do
60
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
61
+ end
62
+ end
63
+
64
+ context "when contract is valid" do
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect update status auction record on database" do
68
+ expect { operation }
69
+ .to change { auction_repository.by_id(auction.id).status }
70
+ .from("running")
71
+ .to("finished")
72
+ end
73
+
74
+ it "expect publish the auction finish event" do
75
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
76
+
77
+ operation
78
+
79
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
80
+ end
81
+
82
+ context "when the auction has a winning bid with other participations" do
83
+ let(:participant) { Factory[:user] }
84
+ let(:winner) { Factory[:user] }
85
+
86
+ before do
87
+ Factory[:bid, auction: auction, user: winner, value_cents: (auction.minimal_bid_cents * 2)]
88
+ Factory[:bid, auction: auction, user: participant, value_cents: auction.minimal_bid_cents]
89
+ end
90
+
91
+ it "expect call winner operation and participant operation jobs" do
92
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
93
+ .to receive(:perform_async).with(auction.id, winner.id)
94
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
95
+ .to receive(:perform_async).with(auction.id, participant.id)
96
+
97
+ operation
98
+
99
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
100
+ .to have_received(:perform_async).with(auction.id, winner.id).once
101
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
102
+ .to have_received(:perform_async).with(auction.id, participant.id).once
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::Finish::PennyOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_running_penny, finished_at: Time.current] }
8
+
9
+ describe ".call(auction_id, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:attributes) { {auction_id: auction.id} }
15
+ it "expect result success matching block" do
16
+ matched_success = nil
17
+ matched_failure = nil
18
+
19
+ operation.call(attributes) do |o|
20
+ o.success { |v| matched_success = v }
21
+ o.failure { |f| matched_failure = f }
22
+ end
23
+
24
+ expect(matched_success.id).to eq(auction.id)
25
+ expect(matched_success.status).to eq("finished")
26
+ expect(matched_failure).to be_nil
27
+ end
28
+ end
29
+
30
+ context "when operation happens with failure" do
31
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
32
+
33
+ it "expect result matching block" do
34
+ matched_success = nil
35
+ matched_failure = nil
36
+
37
+ operation.call(attributes) do |o|
38
+ o.success { |v| matched_success = v }
39
+ o.failure { |f| matched_failure = f }
40
+ end
41
+
42
+ expect(matched_success).to be_nil
43
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#call" do
50
+ subject(:operation) { described_class.new.call(attributes) }
51
+
52
+ context "when contract is invalid" do
53
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
54
+
55
+ it "expect not change auction status" do
56
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
57
+ end
58
+
59
+ it "expect return failure with error messages" do
60
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
61
+ end
62
+ end
63
+
64
+ context "when contract is valid" do
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect update status auction record on database" do
68
+ expect { operation }
69
+ .to change { auction_repository.by_id(auction.id).status }
70
+ .from("running")
71
+ .to("finished")
72
+ end
73
+
74
+ it "expect publish the auction finish event" do
75
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
76
+
77
+ operation
78
+
79
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
80
+ end
81
+
82
+ context "when the auction has a winning bid with other participations" do
83
+ let(:participant) { Factory[:user] }
84
+ let(:winner) { Factory[:user] }
85
+
86
+ before do
87
+ Factory[:bid, auction: auction, user: participant, value_cents: auction.minimal_bid_cents]
88
+ Factory[:bid, auction: auction, user: winner, value_cents: auction.minimal_bid_cents]
89
+ end
90
+
91
+ it "expect call winner operation and participant operation jobs" do
92
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
93
+ .to receive(:perform_async).with(auction.id, winner.id)
94
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
95
+ .to receive(:perform_async).with(auction.id, participant.id)
96
+
97
+ operation
98
+
99
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
100
+ .to have_received(:perform_async).with(auction.id, winner.id).once
101
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
102
+ .to have_received(:perform_async).with(auction.id, participant.id).once
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::Finish::StandardOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_running_standard, finished_at: Time.current] }
8
+
9
+ describe ".call(auction_id, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:attributes) { {auction_id: auction.id} }
15
+ it "expect result success matching block" do
16
+ matched_success = nil
17
+ matched_failure = nil
18
+
19
+ operation.call(attributes) do |o|
20
+ o.success { |v| matched_success = v }
21
+ o.failure { |f| matched_failure = f }
22
+ end
23
+
24
+ expect(matched_success.id).to eq(auction.id)
25
+ expect(matched_success.status).to eq("finished")
26
+ expect(matched_failure).to be_nil
27
+ end
28
+ end
29
+
30
+ context "when operation happens with failure" do
31
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
32
+
33
+ it "expect result matching block" do
34
+ matched_success = nil
35
+ matched_failure = nil
36
+
37
+ operation.call(attributes) do |o|
38
+ o.success { |v| matched_success = v }
39
+ o.failure { |f| matched_failure = f }
40
+ end
41
+
42
+ expect(matched_success).to be_nil
43
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#call" do
50
+ subject(:operation) { described_class.new.call(attributes) }
51
+
52
+ context "when contract is invalid" do
53
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
54
+
55
+ it "expect not change auction status" do
56
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
57
+ end
58
+
59
+ it "expect return failure with error messages" do
60
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
61
+ end
62
+ end
63
+
64
+ context "when contract is valid" do
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect update status auction record on database" do
68
+ expect { operation }
69
+ .to change { auction_repository.by_id(auction.id).status }
70
+ .from("running")
71
+ .to("finished")
72
+ end
73
+
74
+ it "expect publish the auction finish event" do
75
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
76
+
77
+ operation
78
+
79
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
80
+ end
81
+
82
+ context "when the auction has a winning bid with other participations" do
83
+ let(:winner) { Factory[:user] }
84
+
85
+ before do
86
+ Factory[:bid, auction: auction, value_cents: auction.minimal_bid_cents]
87
+ Factory[:bid, auction: auction, user: winner, value_cents: (auction.minimal_bid_cents * 2)]
88
+ end
89
+
90
+ it "expect call winner operation and participant operation jobs" do
91
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
92
+ .to receive(:perform_async)
93
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
94
+ .to receive(:perform_async)
95
+
96
+ operation
97
+
98
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::WinnerOperationJob)
99
+ .to have_received(:perform_async).once
100
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::PostAuction::ParticipantOperationJob)
101
+ .to have_received(:perform_async).once
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::PauseOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_running_standard] }
8
+
9
+ describe ".call(attributes, &block)" do
10
+ let(:operation) { described_class }
11
+
12
+ context "when block is given" do
13
+ context "when operation happens with success" do
14
+ let(:attributes) { {auction_id: auction.id} }
15
+
16
+ it "expect result success matching block" do
17
+ matched_success = nil
18
+ matched_failure = nil
19
+
20
+ operation.call(attributes) do |o|
21
+ o.success { |v| matched_success = v }
22
+ o.failure { |f| matched_failure = f }
23
+ end
24
+
25
+ expect(matched_success).to eq(auction.id)
26
+ expect(matched_failure).to be_nil
27
+ end
28
+ end
29
+
30
+ context "when operation happens with failure" do
31
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
32
+
33
+ it "expect result matching block" do
34
+ matched_success = nil
35
+ matched_failure = nil
36
+
37
+ operation.call(attributes) do |o|
38
+ o.success { |v| matched_success = v }
39
+ o.failure { |f| matched_failure = f }
40
+ end
41
+
42
+ expect(matched_success).to be_nil
43
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#call(attributes)" do
50
+ subject(:operation) { described_class.new.call(attributes) }
51
+
52
+ context "when contract is invalid" do
53
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
54
+
55
+ it "expect not change auction status" do
56
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
57
+ end
58
+
59
+ it "expect return failure with error messages" do
60
+ expect(operation.failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
61
+ end
62
+ end
63
+
64
+ context "when contract is valid" do
65
+ let(:attributes) { {auction_id: auction.id} }
66
+
67
+ it "expect update status auction record on database" do
68
+ expect { operation }
69
+ .to change { auction_repository.by_id(auction.id).status }
70
+ .from("running")
71
+ .to("paused")
72
+ end
73
+
74
+ it "expect publish the auction pause event" do
75
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
76
+
77
+ operation
78
+
79
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe AuctionFunCore::Operations::AuctionContext::Processor::StartOperation, type: :operation do
6
+ let(:auction_repository) { AuctionFunCore::Repos::AuctionContext::AuctionRepository.new }
7
+ let(:auction) { Factory[:auction, :default_scheduled_standard, started_at: Time.current] }
8
+ let(:auction_id) { auction.id }
9
+ let(:kind) { auction.kind }
10
+ let(:stopwatch) { 0 }
11
+
12
+ describe ".call(attributes, &block)" do
13
+ let(:operation) { described_class }
14
+
15
+ context "when block is given" do
16
+ context "when operation happens with success" do
17
+ let(:attributes) do
18
+ {
19
+ auction_id: auction.id,
20
+ kind: auction.kind,
21
+ stopwatch: auction.stopwatch
22
+ }
23
+ end
24
+
25
+ it "expect result success matching block" do
26
+ matched_success = nil
27
+ matched_failure = nil
28
+
29
+ operation.call(attributes) do |o|
30
+ o.success { |v| matched_success = v }
31
+ o.failure { |f| matched_failure = f }
32
+ end
33
+
34
+ expect(matched_success.id).to eq(auction.id)
35
+ expect(matched_success.status).to eq("running")
36
+ expect(matched_failure).to be_nil
37
+ end
38
+ end
39
+
40
+ context "when operation happens with failure" do
41
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
42
+
43
+ it "expect result matching block" do
44
+ matched_success = nil
45
+ matched_failure = nil
46
+
47
+ operation.call(attributes) do |o|
48
+ o.success { |v| matched_success = v }
49
+ o.failure { |f| matched_failure = f }
50
+ end
51
+
52
+ expect(matched_success).to be_nil
53
+ expect(matched_failure[:auction_id]).to include(I18n.t("contracts.errors.key?"))
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "#call" do
60
+ subject(:operation) { described_class.new.call(attributes) }
61
+
62
+ context "when contract is invalid" do
63
+ let(:attributes) { Dry::Core::Constants::EMPTY_HASH }
64
+
65
+ it "expect not change auction status" do
66
+ expect { operation }.not_to change { auction_repository.by_id(auction.id).status }
67
+ end
68
+ end
69
+
70
+ context "when contract is valid" do
71
+ let(:attributes) do
72
+ {
73
+ auction_id: auction.id,
74
+ kind: auction.kind,
75
+ stopwatch: auction.stopwatch
76
+ }
77
+ end
78
+
79
+ it "expect update status auction record on database and publish the auction start event" do
80
+ allow(AuctionFunCore::Application[:event]).to receive(:publish)
81
+
82
+ expect { operation }.to change { auction_repository.by_id(auction.id).status }.from("scheduled").to("running")
83
+
84
+ expect(AuctionFunCore::Application[:event]).to have_received(:publish).once
85
+ end
86
+
87
+ context "when auction kind is equal to 'standard'" do
88
+ it "expect create a new job to finish the standard auction" do
89
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::StandardOperationJob).to receive(:perform_at)
90
+
91
+ operation
92
+
93
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::StandardOperationJob)
94
+ .to have_received(:perform_at)
95
+ .once
96
+ end
97
+ end
98
+
99
+ context "when auction kind is equal to 'closed'" do
100
+ let(:auction) { Factory[:auction, :default_scheduled_closed, started_at: Time.current] }
101
+
102
+ it "expect create a new job to finish the closed auction" do
103
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::ClosedOperationJob).to receive(:perform_at)
104
+
105
+ operation
106
+
107
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::ClosedOperationJob)
108
+ .to have_received(:perform_at)
109
+ .once
110
+ end
111
+ end
112
+
113
+ context "when auction kind is equal to 'penny'" do
114
+ let(:auction) { Factory[:auction, :default_scheduled_closed, started_at: Time.current] }
115
+ let(:stopwatch) { 45 }
116
+ let(:old_finished_at) { auction.finished_at.strftime("%Y-%m-%d %H:%M:%S") }
117
+ let(:new_finished_at) { stopwatch.seconds.from_now.strftime("%Y-%m-%d %H:%M:%S") }
118
+ let(:attributes) do
119
+ {
120
+ auction_id: auction.id,
121
+ kind: "penny",
122
+ stopwatch: stopwatch
123
+ }
124
+ end
125
+
126
+ before { auction_repository.update(auction.id, kind: "penny") }
127
+
128
+ it "expect update finished_at to stopwatch seconds from now" do
129
+ expect { operation }
130
+ .to change { auction_repository.by_id(auction.id).finished_at.strftime("%Y-%m-%d %H:%M:%S") }
131
+ .from(old_finished_at)
132
+ .to(new_finished_at)
133
+ end
134
+
135
+ it "expect call the finish penny auction job" do
136
+ allow(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
137
+ .to receive(:perform_at)
138
+
139
+ operation
140
+
141
+ expect(AuctionFunCore::Workers::Operations::AuctionContext::Processor::Finish::PennyOperationJob)
142
+ .to have_received(:perform_at)
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end