dry-transaction 0.13.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +308 -0
- data/LICENSE +20 -0
- data/README.md +15 -41
- data/dry-transaction.gemspec +35 -0
- data/lib/dry-transaction.rb +2 -0
- data/lib/dry/transaction.rb +2 -0
- data/lib/dry/transaction/builder.rb +6 -4
- data/lib/dry/transaction/callable.rb +5 -0
- data/lib/dry/transaction/dsl.rb +2 -0
- data/lib/dry/transaction/errors.rb +2 -0
- data/lib/dry/transaction/instance_methods.rb +6 -4
- data/lib/dry/transaction/operation.rb +5 -3
- data/lib/dry/transaction/operation_resolver.rb +2 -0
- data/lib/dry/transaction/result_matcher.rb +3 -1
- data/lib/dry/transaction/stack.rb +2 -0
- data/lib/dry/transaction/step.rb +6 -4
- data/lib/dry/transaction/step_adapter.rb +6 -4
- data/lib/dry/transaction/step_adapters.rb +9 -7
- data/lib/dry/transaction/step_adapters/around.rb +4 -2
- data/lib/dry/transaction/step_adapters/check.rb +2 -0
- data/lib/dry/transaction/step_adapters/map.rb +2 -0
- data/lib/dry/transaction/step_adapters/raw.rb +5 -3
- data/lib/dry/transaction/step_adapters/tee.rb +2 -0
- data/lib/dry/transaction/step_adapters/try.rb +3 -1
- data/lib/dry/transaction/step_failure.rb +2 -0
- data/lib/dry/transaction/version.rb +3 -1
- metadata +14 -110
- data/Gemfile +0 -15
- data/Gemfile.lock +0 -97
- data/LICENSE.md +0 -9
- data/Rakefile +0 -6
- data/spec/examples.txt +0 -88
- data/spec/integration/around_spec.rb +0 -120
- data/spec/integration/auto_injection_spec.rb +0 -32
- data/spec/integration/custom_step_adapters_spec.rb +0 -41
- data/spec/integration/operation_spec.rb +0 -30
- data/spec/integration/passing_step_arguments_spec.rb +0 -51
- data/spec/integration/publishing_step_events_spec.rb +0 -119
- data/spec/integration/transaction_spec.rb +0 -573
- data/spec/integration/transaction_without_steps_spec.rb +0 -101
- data/spec/spec_helper.rb +0 -116
- data/spec/support/container.rb +0 -10
- data/spec/support/database.rb +0 -12
- data/spec/support/db_transactions.rb +0 -45
- data/spec/support/result_mixin.rb +0 -3
- data/spec/support/test_module_constants.rb +0 -11
- data/spec/unit/step_adapters/around_spec.rb +0 -46
- data/spec/unit/step_adapters/check_spec.rb +0 -43
- data/spec/unit/step_adapters/map_spec.rb +0 -16
- data/spec/unit/step_adapters/raw_spec.rb +0 -36
- data/spec/unit/step_adapters/tee_spec.rb +0 -17
- data/spec/unit/step_adapters/try_spec.rb +0 -89
- data/spec/unit/step_spec.rb +0 -139
data/LICENSE.md
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright © 2015-2016 [Icelab](http://icelab.com.au/).
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
-
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
-
|
9
|
-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
data/spec/examples.txt
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
example_id | status | run_time |
|
2
|
-
----------------------------------------------------------- | ------ | --------------- |
|
3
|
-
./spec/integration/around_spec.rb[1:1] | passed | 0.00234 seconds |
|
4
|
-
./spec/integration/around_spec.rb[1:2] | passed | 0.00095 seconds |
|
5
|
-
./spec/integration/around_spec.rb[1:3] | passed | 0.00149 seconds |
|
6
|
-
./spec/integration/around_spec.rb[1:4] | passed | 0.00118 seconds |
|
7
|
-
./spec/integration/around_spec.rb[1:5] | passed | 0.00104 seconds |
|
8
|
-
./spec/integration/around_spec.rb[1:6:1] | passed | 0.00096 seconds |
|
9
|
-
./spec/integration/auto_injection_spec.rb[1:1] | failed | 0.00149 seconds |
|
10
|
-
./spec/integration/custom_step_adapters_spec.rb[1:1] | passed | 0.00372 seconds |
|
11
|
-
./spec/integration/operation_spec.rb[1:1] | passed | 0.00013 seconds |
|
12
|
-
./spec/integration/operation_spec.rb[1:2] | passed | 0.00014 seconds |
|
13
|
-
./spec/integration/passing_step_arguments_spec.rb[1:1:1] | passed | 0.00063 seconds |
|
14
|
-
./spec/integration/passing_step_arguments_spec.rb[1:2:1] | passed | 0.00072 seconds |
|
15
|
-
./spec/integration/passing_step_arguments_spec.rb[1:3:1] | passed | 0.00038 seconds |
|
16
|
-
./spec/integration/publishing_step_events_spec.rb[1:1:1] | passed | 0.00057 seconds |
|
17
|
-
./spec/integration/publishing_step_events_spec.rb[1:1:2] | passed | 0.0007 seconds |
|
18
|
-
./spec/integration/publishing_step_events_spec.rb[1:2:1] | passed | 0.00054 seconds |
|
19
|
-
./spec/integration/publishing_step_events_spec.rb[1:2:2] | passed | 0.00052 seconds |
|
20
|
-
./spec/integration/publishing_step_events_spec.rb[1:3:1] | passed | 0.00075 seconds |
|
21
|
-
./spec/integration/publishing_step_events_spec.rb[1:3:2] | passed | 0.00071 seconds |
|
22
|
-
./spec/integration/transaction_spec.rb[1:1:1] | passed | 0.0006 seconds |
|
23
|
-
./spec/integration/transaction_spec.rb[1:1:2] | passed | 0.0006 seconds |
|
24
|
-
./spec/integration/transaction_spec.rb[1:1:3] | passed | 0.00058 seconds |
|
25
|
-
./spec/integration/transaction_spec.rb[1:1:4] | passed | 0.00072 seconds |
|
26
|
-
./spec/integration/transaction_spec.rb[1:1:5] | passed | 0.00058 seconds |
|
27
|
-
./spec/integration/transaction_spec.rb[1:2:1] | passed | 0.00075 seconds |
|
28
|
-
./spec/integration/transaction_spec.rb[1:3:1] | passed | 0.00055 seconds |
|
29
|
-
./spec/integration/transaction_spec.rb[1:4:1] | passed | 0.00055 seconds |
|
30
|
-
./spec/integration/transaction_spec.rb[1:5:1] | passed | 0.00059 seconds |
|
31
|
-
./spec/integration/transaction_spec.rb[1:6:1] | passed | 0.00043 seconds |
|
32
|
-
./spec/integration/transaction_spec.rb[1:7:1] | passed | 0.00047 seconds |
|
33
|
-
./spec/integration/transaction_spec.rb[1:8:1] | passed | 0.00043 seconds |
|
34
|
-
./spec/integration/transaction_spec.rb[1:9:1] | passed | 0.00068 seconds |
|
35
|
-
./spec/integration/transaction_spec.rb[1:10:1] | passed | 0.00052 seconds |
|
36
|
-
./spec/integration/transaction_spec.rb[1:11:1] | passed | 0.00053 seconds |
|
37
|
-
./spec/integration/transaction_spec.rb[1:11:2] | passed | 0.00047 seconds |
|
38
|
-
./spec/integration/transaction_spec.rb[1:12:1] | passed | 0.00065 seconds |
|
39
|
-
./spec/integration/transaction_spec.rb[1:12:2] | passed | 0.00062 seconds |
|
40
|
-
./spec/integration/transaction_spec.rb[1:12:3] | passed | 0.00066 seconds |
|
41
|
-
./spec/integration/transaction_spec.rb[1:12:4] | passed | 0.00063 seconds |
|
42
|
-
./spec/integration/transaction_spec.rb[1:12:5] | passed | 0.0006 seconds |
|
43
|
-
./spec/integration/transaction_spec.rb[1:12:6] | passed | 0.00063 seconds |
|
44
|
-
./spec/integration/transaction_spec.rb[1:13:1] | passed | 0.00076 seconds |
|
45
|
-
./spec/integration/transaction_spec.rb[1:13:2] | passed | 0.0007 seconds |
|
46
|
-
./spec/integration/transaction_spec.rb[1:13:3] | passed | 0.00198 seconds |
|
47
|
-
./spec/integration/transaction_spec.rb[1:13:4] | passed | 0.00065 seconds |
|
48
|
-
./spec/integration/transaction_spec.rb[1:13:5] | passed | 0.00068 seconds |
|
49
|
-
./spec/integration/transaction_spec.rb[1:14:1] | passed | 0.00055 seconds |
|
50
|
-
./spec/integration/transaction_spec.rb[1:15:1] | passed | 0.00042 seconds |
|
51
|
-
./spec/integration/transaction_spec.rb[1:16:1:1:1] | passed | 0.0004 seconds |
|
52
|
-
./spec/integration/transaction_spec.rb[1:16:2:1:1] | passed | 0.00037 seconds |
|
53
|
-
./spec/integration/transaction_spec.rb[1:16:2:2:1] | passed | 0.00042 seconds |
|
54
|
-
./spec/integration/transaction_without_steps_spec.rb[1:1:1] | passed | 0.00066 seconds |
|
55
|
-
./spec/integration/transaction_without_steps_spec.rb[1:1:2] | passed | 0.0007 seconds |
|
56
|
-
./spec/integration/transaction_without_steps_spec.rb[1:1:3] | passed | 0.00072 seconds |
|
57
|
-
./spec/integration/transaction_without_steps_spec.rb[1:1:4] | passed | 0.00064 seconds |
|
58
|
-
./spec/integration/transaction_without_steps_spec.rb[1:2:1] | passed | 0.0008 seconds |
|
59
|
-
./spec/integration/transaction_without_steps_spec.rb[1:3:1] | passed | 0.00071 seconds |
|
60
|
-
./spec/unit/step_adapters/around_spec.rb[1:1:1:1] | passed | 0.0016 seconds |
|
61
|
-
./spec/unit/step_adapters/around_spec.rb[1:1:2:1] | passed | 0.00092 seconds |
|
62
|
-
./spec/unit/step_adapters/around_spec.rb[1:1:3:1] | passed | 0.00011 seconds |
|
63
|
-
./spec/unit/step_adapters/check_spec.rb[1:1:1] | passed | 0.00012 seconds |
|
64
|
-
./spec/unit/step_adapters/check_spec.rb[1:1:2:1] | passed | 0.00012 seconds |
|
65
|
-
./spec/unit/step_adapters/check_spec.rb[1:1:3:1] | passed | 0.00009 seconds |
|
66
|
-
./spec/unit/step_adapters/check_spec.rb[1:1:4:1] | passed | 0.00009 seconds |
|
67
|
-
./spec/unit/step_adapters/map_spec.rb[1:1:1] | passed | 0.00009 seconds |
|
68
|
-
./spec/unit/step_adapters/raw_spec.rb[1:1:1:1] | passed | 0.00014 seconds |
|
69
|
-
./spec/unit/step_adapters/raw_spec.rb[1:1:2:1] | passed | 0.00009 seconds |
|
70
|
-
./spec/unit/step_adapters/tee_spec.rb[1:1:1] | passed | 0.00012 seconds |
|
71
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:1:1] | passed | 0.00016 seconds |
|
72
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:1:1] | passed | 0.00013 seconds |
|
73
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:1:2] | passed | 0.00016 seconds |
|
74
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:1:3:1] | passed | 0.00017 seconds |
|
75
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:1:3:2] | passed | 0.00012 seconds |
|
76
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:2:1] | passed | 0.00012 seconds |
|
77
|
-
./spec/unit/step_adapters/try_spec.rb[1:1:2:2:2:1] | passed | 0.00012 seconds |
|
78
|
-
./spec/unit/step_spec.rb[1:1:1:1] | passed | 0.00225 seconds |
|
79
|
-
./spec/unit/step_spec.rb[1:1:1:2] | passed | 0.00071 seconds |
|
80
|
-
./spec/unit/step_spec.rb[1:1:2:1] | passed | 0.01719 seconds |
|
81
|
-
./spec/unit/step_spec.rb[1:1:3:1] | passed | 0.00026 seconds |
|
82
|
-
./spec/unit/step_spec.rb[1:1:3:2] | passed | 0.00306 seconds |
|
83
|
-
./spec/unit/step_spec.rb[1:1:3:3] | passed | 0.00057 seconds |
|
84
|
-
./spec/unit/step_spec.rb[1:2:1:1] | passed | 0.00014 seconds |
|
85
|
-
./spec/unit/step_spec.rb[1:2:2:1] | passed | 0.00014 seconds |
|
86
|
-
./spec/unit/step_spec.rb[1:2:3:1] | passed | 0.00013 seconds |
|
87
|
-
./spec/unit/step_spec.rb[1:3:1:1] | passed | 0.00014 seconds |
|
88
|
-
./spec/unit/step_spec.rb[1:3:2:1] | passed | 0.00013 seconds |
|
@@ -1,120 +0,0 @@
|
|
1
|
-
RSpec.describe "around steps" do
|
2
|
-
include_context "db transactions"
|
3
|
-
|
4
|
-
include Dry::Monads::Result::Mixin
|
5
|
-
|
6
|
-
before do
|
7
|
-
container.instance_exec do
|
8
|
-
register :validate, -> input {
|
9
|
-
if input[:account][:balance] >= 0
|
10
|
-
Success(input)
|
11
|
-
else
|
12
|
-
Failure(input)
|
13
|
-
end
|
14
|
-
}
|
15
|
-
|
16
|
-
register :persist_user do |user:, **other|
|
17
|
-
self[:database] << [:user, user]
|
18
|
-
Success(other)
|
19
|
-
end
|
20
|
-
|
21
|
-
register :persist_account do |account: |
|
22
|
-
self[:database] << [:account, account]
|
23
|
-
Success(true)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
let(:transaction) do
|
29
|
-
Class.new do
|
30
|
-
include Dry::Transaction(container: Test::Container)
|
31
|
-
|
32
|
-
around :transaction, with: :transaction
|
33
|
-
step :validate, with: :validate
|
34
|
-
step :persist_user, with: :persist_user
|
35
|
-
step :persist_account, with: :persist_account
|
36
|
-
step :finalize, with: :finalize
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
let(:input) { { user: { name: "Jane" }, account: { balance: 0 } } }
|
41
|
-
|
42
|
-
it "starts a transaction" do
|
43
|
-
called = false
|
44
|
-
|
45
|
-
finalize = -> x do
|
46
|
-
called = true
|
47
|
-
expect(database).to(be_in_transaction)
|
48
|
-
Success(x)
|
49
|
-
end
|
50
|
-
|
51
|
-
result = transaction.new(finalize: finalize).call(input)
|
52
|
-
expect(called).to be true
|
53
|
-
expect(result).to eql(Success(true))
|
54
|
-
end
|
55
|
-
|
56
|
-
it "commits transactions" do
|
57
|
-
transaction.new(finalize: -> x { Success(x) }).call(input)
|
58
|
-
|
59
|
-
expect(database).to be_committed
|
60
|
-
expect(database).not_to be_rolled_back
|
61
|
-
expect(database).not_to be_in_transaction
|
62
|
-
expect(database).to eql([[:user, name: "Jane"],
|
63
|
-
[:account, balance: 0]])
|
64
|
-
end
|
65
|
-
|
66
|
-
it "rolls back transactions on failure" do
|
67
|
-
transaction.new(finalize: -> x { Failure(x) }).call(input)
|
68
|
-
|
69
|
-
expect(database).to be_rolled_back
|
70
|
-
expect(database).not_to be_in_transaction
|
71
|
-
expect(database).not_to be_committed
|
72
|
-
expect(database).to be_empty
|
73
|
-
end
|
74
|
-
|
75
|
-
it "rolls back transaction on exception" do
|
76
|
-
uncaught = Class.new(StandardError)
|
77
|
-
|
78
|
-
expect {
|
79
|
-
transaction.new(finalize: -> x { raise uncaught }).call(input)
|
80
|
-
}.to raise_error(uncaught)
|
81
|
-
|
82
|
-
expect(database).to be_rolled_back
|
83
|
-
expect(database).not_to be_in_transaction
|
84
|
-
expect(database).not_to be_committed
|
85
|
-
expect(database).to be_empty
|
86
|
-
end
|
87
|
-
|
88
|
-
it "supports matching on nested failures" do
|
89
|
-
invalid_input = input.merge(account: {balance: -10})
|
90
|
-
|
91
|
-
failed_input = nil
|
92
|
-
|
93
|
-
transaction.new(finalize: -> x { Success(x) }).call(invalid_input) do |m|
|
94
|
-
m.success { |v| v }
|
95
|
-
m.failure(:validate) { |input| failed_input = input }
|
96
|
-
end
|
97
|
-
|
98
|
-
expect(failed_input).to eq invalid_input
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "subscribing to events" do
|
102
|
-
let(:subscriber) {
|
103
|
-
Class.new do
|
104
|
-
attr_reader :user_persisted
|
105
|
-
|
106
|
-
def on_step_succeeded(event)
|
107
|
-
@user_persisted = true if event[:step_name] == :persist_user
|
108
|
-
end
|
109
|
-
end.new
|
110
|
-
}
|
111
|
-
|
112
|
-
it "supports subscribing to events from nested steps" do
|
113
|
-
trans = transaction.new(finalize: -> x { Success(x) })
|
114
|
-
trans.subscribe(subscriber)
|
115
|
-
trans.call(input)
|
116
|
-
|
117
|
-
expect(subscriber.user_persisted).to be_truthy
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# require "dry-monads"
|
2
|
-
# require "dry-auto_inject"
|
3
|
-
|
4
|
-
# RSpec.describe "Using dry-auto_inject" do
|
5
|
-
# let(:transaction) {
|
6
|
-
# Class.new do
|
7
|
-
# include Dry::Transaction(container: Test::Container)
|
8
|
-
# include Test::Inject[:extract_email]
|
9
|
-
|
10
|
-
# step :symbolize
|
11
|
-
|
12
|
-
# def call(input)
|
13
|
-
# super(input).bind(extract_email)
|
14
|
-
# end
|
15
|
-
# end.new
|
16
|
-
# }
|
17
|
-
|
18
|
-
# before do
|
19
|
-
# module Test
|
20
|
-
# Container = {
|
21
|
-
# symbolize: -> input { Dry::Monads::Right(name: input["name"], email: input["email"]) },
|
22
|
-
# extract_email: -> input { Dry::Monads::Right(email: input[:email]) },
|
23
|
-
# }
|
24
|
-
|
25
|
-
# Inject = Dry::AutoInject(container: Container)
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
|
29
|
-
# it "support auto-injection of dependencies alongside step operations" do
|
30
|
-
# expect(transaction.("name" => "Jane", "email" => "jane@example.com").value).to eq(email: "jane@example.com")
|
31
|
-
# end
|
32
|
-
# end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
RSpec.describe "Custom step adapters" do
|
2
|
-
let(:transaction) {
|
3
|
-
Class.new do
|
4
|
-
include Dry::Transaction(container: Test::Container, step_adapters: Test::CustomStepAdapters)
|
5
|
-
|
6
|
-
check :jane?, with: :jane?
|
7
|
-
map :process, with: :process
|
8
|
-
tee :persist, with: :persist
|
9
|
-
enqueue :deliver, with: :deliver
|
10
|
-
end.new
|
11
|
-
}
|
12
|
-
|
13
|
-
before do
|
14
|
-
Test::DB = []
|
15
|
-
Test::QUEUE = []
|
16
|
-
|
17
|
-
module Test
|
18
|
-
Container = {
|
19
|
-
jane?: -> input { input["name"] == "Jane" },
|
20
|
-
process: -> input { {name: input["name"], email: input["email"]} },
|
21
|
-
persist: -> input { Test::DB << input and true },
|
22
|
-
deliver: -> input { "Delivered email to #{input[:email]}" },
|
23
|
-
}
|
24
|
-
|
25
|
-
class CustomStepAdapters < Dry::Transaction::StepAdapters
|
26
|
-
extend Dry::Monads::Result::Mixin
|
27
|
-
|
28
|
-
register :enqueue, -> operation, _options, args {
|
29
|
-
Test::QUEUE << operation.(*args)
|
30
|
-
Success(args[0])
|
31
|
-
}
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it "supports custom step adapters" do
|
37
|
-
input = {"name" => "Jane", "email" => "jane@doe.com"}
|
38
|
-
transaction.call(input)
|
39
|
-
expect(Test::QUEUE).to include("Delivered email to jane@doe.com")
|
40
|
-
end
|
41
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require "dry/transaction/operation"
|
2
|
-
|
3
|
-
RSpec.describe Dry::Transaction::Operation do
|
4
|
-
subject(:operation) {
|
5
|
-
Class.new do
|
6
|
-
include Dry::Transaction::Operation
|
7
|
-
|
8
|
-
def call(input)
|
9
|
-
Success(input)
|
10
|
-
end
|
11
|
-
end.new
|
12
|
-
}
|
13
|
-
|
14
|
-
it "mixes in the Result monad constructors" do
|
15
|
-
expect(operation.("hello")).to be_success
|
16
|
-
end
|
17
|
-
|
18
|
-
it "supports pattern matching when called with a block" do
|
19
|
-
result = operation.("hello") do |m|
|
20
|
-
m.success do |v|
|
21
|
-
"Success: #{v}"
|
22
|
-
end
|
23
|
-
m.failure do |v|
|
24
|
-
"Failure: #{v}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
expect(result).to eq "Success: hello"
|
29
|
-
end
|
30
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
RSpec.describe "Passing additional arguments to step operations" do
|
2
|
-
let(:call_transaction) { transaction.with_step_args(step_options).call(input) }
|
3
|
-
|
4
|
-
let(:transaction) {
|
5
|
-
Class.new do
|
6
|
-
include Dry::Transaction(container: Test::Container)
|
7
|
-
|
8
|
-
map :process, with: :process
|
9
|
-
try :validate, with: :validate, catch: Test::NotValidError
|
10
|
-
tee :persist, with: :persist
|
11
|
-
end.new
|
12
|
-
}
|
13
|
-
|
14
|
-
let(:input) { {"name" => "Jane", "email" => "jane@doe.com"} }
|
15
|
-
|
16
|
-
before do
|
17
|
-
Test::NotValidError = Class.new(StandardError)
|
18
|
-
Test::DB = []
|
19
|
-
module Test
|
20
|
-
Container = {
|
21
|
-
process: -> input { {name: input["name"], email: input["email"]} },
|
22
|
-
validate: -> input, allowed { !input[:email].include?(allowed) ? raise(Test::NotValidError, "email not allowed") : input },
|
23
|
-
persist: -> input { Test::DB << input and true }
|
24
|
-
}
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "required arguments provided" do
|
29
|
-
let(:step_options) { {validate: ["doe.com"]} }
|
30
|
-
|
31
|
-
it "passes the arguments and calls the operations successfully" do
|
32
|
-
expect(call_transaction).to be_a Dry::Monads::Result::Success
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "required arguments not provided" do
|
37
|
-
let(:step_options) { {} }
|
38
|
-
|
39
|
-
it "raises an ArgumentError" do
|
40
|
-
expect { call_transaction }.to raise_error(ArgumentError)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "spurious arguments provided" do
|
45
|
-
let(:step_options) { {validate: ["doe.com"], bogus: ["not matching any step"]} }
|
46
|
-
|
47
|
-
it "raises an ArgumentError" do
|
48
|
-
expect { call_transaction }.to raise_error(ArgumentError)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,119 +0,0 @@
|
|
1
|
-
RSpec.describe "publishing step events" do
|
2
|
-
let(:container) {
|
3
|
-
Class.new do
|
4
|
-
extend Dry::Container::Mixin
|
5
|
-
|
6
|
-
register :process, -> input { {name: input["name"]} }
|
7
|
-
register :verify, -> input { input[:name].to_s != "" ? Dry::Monads.Success(input) : Dry::Monads.Failure("no name") }
|
8
|
-
register :persist, -> input { Test::DB << input and true }
|
9
|
-
end
|
10
|
-
}
|
11
|
-
|
12
|
-
let(:transaction) {
|
13
|
-
Class.new do
|
14
|
-
include Dry::Transaction(container: Test::Container)
|
15
|
-
|
16
|
-
map :process, with: :process
|
17
|
-
step :verify, with: :verify
|
18
|
-
tee :persist, with: :persist
|
19
|
-
end.new
|
20
|
-
}
|
21
|
-
|
22
|
-
let(:subscriber) do
|
23
|
-
Class.new do
|
24
|
-
attr_reader :started, :success, :failed
|
25
|
-
|
26
|
-
def initialize
|
27
|
-
@started = []
|
28
|
-
@success = []
|
29
|
-
@failed = []
|
30
|
-
end
|
31
|
-
|
32
|
-
def on_step(event)
|
33
|
-
started << event[:step_name]
|
34
|
-
end
|
35
|
-
def on_step_succeeded(event)
|
36
|
-
success << {step_name: event[:step_name], args: event[:args]}
|
37
|
-
end
|
38
|
-
def on_step_failed(event)
|
39
|
-
failed << {step_name: event[:step_name], args: event[:args], value: event[:value]}
|
40
|
-
end
|
41
|
-
end.new
|
42
|
-
end
|
43
|
-
|
44
|
-
before do
|
45
|
-
Test::DB = []
|
46
|
-
Test::Container = container
|
47
|
-
end
|
48
|
-
|
49
|
-
context "subscribing to all step events" do
|
50
|
-
before do
|
51
|
-
transaction.subscribe(subscriber)
|
52
|
-
end
|
53
|
-
|
54
|
-
specify "subscriber receives success events" do
|
55
|
-
transaction.call("name" => "Jane")
|
56
|
-
|
57
|
-
expected_result = [
|
58
|
-
{ step_name: :process, args: [ {"name" => "Jane"} ] },
|
59
|
-
{ step_name: :verify, args: [ { name: "Jane" } ] },
|
60
|
-
{ step_name: :persist, args: [ { name: "Jane" } ] }
|
61
|
-
]
|
62
|
-
|
63
|
-
expect(subscriber.success).to eq expected_result
|
64
|
-
end
|
65
|
-
|
66
|
-
specify "subsriber receives success events for passing steps, a failure event for the failing step, and no subsequent events" do
|
67
|
-
transaction.call("name" => "")
|
68
|
-
|
69
|
-
expect(subscriber.success).to eq [ { step_name: :process, args:[ { "name" => "" } ] } ]
|
70
|
-
expect(subscriber.failed).to eq [ { step_name: :verify, args: [ { name: ""} ], value: "no name" } ]
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context "subscribing to particular step events" do
|
75
|
-
before do
|
76
|
-
transaction.subscribe(verify: subscriber)
|
77
|
-
end
|
78
|
-
|
79
|
-
specify "subscriber receives success event for the specified step" do
|
80
|
-
transaction.call("name" => "Jane")
|
81
|
-
|
82
|
-
expect(subscriber.success).to eq [ { step_name: :verify, args: [ { name: "Jane" } ] } ]
|
83
|
-
end
|
84
|
-
|
85
|
-
specify "subscriber receives failure event for the specified step" do
|
86
|
-
transaction.call("name" => "")
|
87
|
-
|
88
|
-
expect(subscriber.failed).to eq [ { step_name: :verify, args: [ { name: ""} ], value: "no name" } ]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "subscribing to step events when passing step arguments" do
|
93
|
-
before do
|
94
|
-
transaction.subscribe(verify: subscriber)
|
95
|
-
end
|
96
|
-
|
97
|
-
let(:container) {
|
98
|
-
Class.new do
|
99
|
-
extend Dry::Container::Mixin
|
100
|
-
|
101
|
-
register :process, -> input { {name: input["name"]} }
|
102
|
-
register :verify, -> input, name { input[:name].to_s == name ? Dry::Monads.Success(input) : Dry::Monads.Failure("wrong name") }
|
103
|
-
register :persist, -> input { Test::DB << input and true }
|
104
|
-
end
|
105
|
-
}
|
106
|
-
|
107
|
-
specify "subscriber receives success event for the specified step" do
|
108
|
-
transaction.with_step_args(verify: ["Jane"]).call("name" => "Jane")
|
109
|
-
|
110
|
-
expect(subscriber.success).to eq [ { step_name: :verify, args: [ { name: "Jane" }, "Jane"] } ]
|
111
|
-
end
|
112
|
-
|
113
|
-
specify "subscriber receives failure event for the specified step" do
|
114
|
-
transaction.with_step_args(verify: ["Jade"]).call("name" => "")
|
115
|
-
|
116
|
-
expect(subscriber.failed).to eq [ { step_name: :verify, args: [ { name: "" }, "Jade"], value: "wrong name"} ]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|