-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.go
More file actions
88 lines (75 loc) · 2.33 KB
/
mock.go
File metadata and controls
88 lines (75 loc) · 2.33 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package uow
import (
"context"
"sync"
)
// State struct simulates application state and provides methods for setting,
// getting, committing, and rolling back the state. It uses a mutex to ensure
// thread safety.
type State struct {
value string
mu sync.Mutex
}
// SetValue sets the value of the state. It uses a mutex to ensure thread safety.
func (s *State) SetValue(str string) {
s.mu.Lock()
defer s.mu.Unlock()
s.value = str
}
// Value gets the value of the state. It uses a mutex to ensure thread safety.
func (s *State) Value() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.value
}
// Commit appends " committed!" to the state value. It uses a mutex to ensure
// thread safety. This simulates a successful commit operation.
func (s *State) Commit() {
s.mu.Lock()
defer s.mu.Unlock()
s.value += " committed!"
}
// Rollback appends " rolled back!" to the state value. It uses a mutex to ensure
// thread safety. This simulates a rollback operation.
func (s *State) Rollback() {
s.mu.Lock()
defer s.mu.Unlock()
s.value += " rolled back!"
}
// MockTx implements the Runner interface for testing purposes. It simulates a
// transaction without actually interacting with a database.
var _ Runner = &MockTx{}
// MockTx struct holds a State object to simulate application state changes within
// a transaction.
type MockTx struct {
state *State
}
// NewMockTx creates a new MockTx instance with a new State object. This function
// is used to initialize a mock transaction for testing.
func NewMockTx() *MockTx {
return &MockTx{
state: &State{},
}
}
// Ctx returns the context without any modification. This is a placeholder
// function for the mock transaction.
func (t *MockTx) Ctx(ctx context.Context) (context.Context, error) {
return ctx, nil
}
// Get returns the internal State object. This allows access to the simulated
// transaction state.
func (t *MockTx) Get(ctx context.Context) any {
return t.state
}
// Rollback calls the Rollback method on the internal State object. This simulates
// a rollback operation in the mock transaction.
func (t *MockTx) Rollback(ctx context.Context) error {
t.state.Rollback()
return nil
}
// Commit calls the Commit method on the internal State object. This simulates a
// commit operation in the mock transaction.
func (t *MockTx) Commit(ctx context.Context) error {
t.state.Commit()
return nil
}