forked from violetpay-org/go-saga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_mock.go
72 lines (57 loc) · 1.33 KB
/
session_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package saga
import "errors"
type mockSession struct {
id string
currentStep Step
pending bool
state State
mockField string
}
func (m *mockSession) ID() string {
return m.id
}
func (m *mockSession) CurrentStep() Step {
return m.currentStep
}
func (m *mockSession) UpdateCurrentStep(step Step) error {
m.currentStep = step
return nil
}
func (m *mockSession) IsPending() bool {
return m.pending
}
func (m *mockSession) SetPending(pending bool) {
m.pending = pending
}
func (m *mockSession) State() State {
return m.state
}
func (m *mockSession) SetState(state State) {
m.state = state
}
func newMockSessionRepository[S Session, Tx TxContext]() *mockSessionRepository[S, Tx] {
return &mockSessionRepository[S, Tx]{
sessions: make(map[string]*S),
}
}
type mockSessionRepository[S Session, Tx TxContext] struct {
sessions map[string]*S
}
func (m *mockSessionRepository[S, Tx]) Load(id string) (s S, err error) {
if sess, ok := m.sessions[id]; ok {
return *sess, nil
}
return s, errors.New("session not found")
}
func (m *mockSessionRepository[S, Tx]) Save(sess S) Executable[Tx] {
return func(ctx Tx) error {
m.sessions[sess.ID()] = &sess
return nil
}
}
func (m *mockSessionRepository[S, Tx]) Delete(sess S) Executable[Tx] {
return func(ctx Tx) error {
delete(m.sessions, sess.ID())
return nil
}
}