-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
164 lines (129 loc) · 4.1 KB
/
context_test.go
File metadata and controls
164 lines (129 loc) · 4.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package dbx_test
import (
"context"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ziflex/dbx"
)
func TestContextHelpers(t *testing.T) {
t.Run("Is should return true for dbx context", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
db := dbx.New(mockDB)
ctx := db.Context(context.Background())
assert.True(t, dbx.Is(ctx))
})
t.Run("Is should return false for regular context", func(t *testing.T) {
ctx := context.Background()
assert.False(t, dbx.Is(ctx))
})
t.Run("As should return dbx context when present", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
db := dbx.New(mockDB)
originalCtx := db.Context(context.Background())
extractedCtx, ok := dbx.As(originalCtx)
assert.True(t, ok)
assert.Equal(t, originalCtx, extractedCtx)
})
t.Run("As should return false for regular context", func(t *testing.T) {
ctx := context.Background()
extractedCtx, ok := dbx.As(ctx)
assert.False(t, ok)
assert.Nil(t, extractedCtx)
})
t.Run("WithContext and FromContext should work together", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
db := dbx.New(mockDB)
dbCtx := db.Context(context.Background())
// Embed dbx context into regular context
regularCtx := context.Background()
embeddedCtx := dbx.WithContext(regularCtx, dbCtx)
// Extract it back
extractedCtx := dbx.FromContext(embeddedCtx)
assert.Equal(t, dbCtx, extractedCtx)
})
t.Run("FromContext should return nil for regular context", func(t *testing.T) {
ctx := context.Background()
extracted := dbx.FromContext(ctx)
assert.Nil(t, extracted)
})
t.Run("FromContext should work with direct dbx context", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
db := dbx.New(mockDB)
dbCtx := db.Context(context.Background())
extracted := dbx.FromContext(dbCtx)
assert.Equal(t, dbCtx, extracted)
})
}
func TestDefaultContext(t *testing.T) {
t.Run("context methods should delegate to parent", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
// Create a context with deadline
deadline := time.Now().Add(5 * time.Second)
parentCtx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
db := dbx.New(mockDB)
dbCtx := dbx.NewContext(parentCtx, db)
// Test Deadline
ctxDeadline, ok := dbCtx.Deadline()
assert.True(t, ok)
assert.Equal(t, deadline, ctxDeadline)
// Test Done channel
select {
case <-dbCtx.Done():
t.Error("Done channel should not be closed yet")
default:
// Expected
}
// Test Err (should be nil since not cancelled/timed out)
assert.NoError(t, dbCtx.Err())
// Test Value
key := "test-key"
value := "test-value"
parentWithValue := context.WithValue(parentCtx, key, value)
dbCtxWithValue := dbx.NewContext(parentWithValue, db)
assert.Equal(t, value, dbCtxWithValue.Value(key))
assert.Nil(t, dbCtxWithValue.Value("nonexistent-key"))
})
t.Run("cancelled context should propagate cancellation", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
parentCtx, cancel := context.WithCancel(context.Background())
db := dbx.New(mockDB)
dbCtx := dbx.NewContext(parentCtx, db)
// Cancel parent context
cancel()
// Check that cancellation is propagated
assert.Error(t, dbCtx.Err())
assert.Equal(t, context.Canceled, dbCtx.Err())
// Check Done channel is closed
select {
case <-dbCtx.Done():
// Expected
case <-time.After(100 * time.Millisecond):
t.Error("Done channel should be closed after parent cancellation")
}
})
t.Run("Executor should return the provided executor", func(t *testing.T) {
mockDB, _, err := sqlmock.New()
require.NoError(t, err)
defer mockDB.Close()
db := dbx.New(mockDB)
ctx := dbx.NewContext(context.Background(), db)
executor := ctx.Executor()
assert.Equal(t, db, executor)
})
}