-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvars_test.go
More file actions
57 lines (45 loc) · 1.34 KB
/
Copy pathvars_test.go
File metadata and controls
57 lines (45 loc) · 1.34 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
package entpgx_test
import (
"context"
"testing"
"github.com/incroy/entpgx"
"github.com/stretchr/testify/require"
)
func TestWithVar(t *testing.T) {
ctx := context.Background()
// Initial context should not contain "foo"
val, ok := entpgx.VarFromContext(ctx, "foo")
require.False(t, ok)
require.Empty(t, val)
// Add variable "foo" = "bar"
ctx1 := entpgx.WithVar(ctx, "foo", "bar")
val, ok = entpgx.VarFromContext(ctx1, "foo")
require.True(t, ok)
require.Equal(t, "bar", val)
// Original context remains unchanged
val, ok = entpgx.VarFromContext(ctx, "foo")
require.False(t, ok)
require.Empty(t, val)
// Add second variable "baz" = "qux" on top of ctx1
ctx2 := entpgx.WithVar(ctx1, "baz", "qux")
val, ok = entpgx.VarFromContext(ctx2, "foo")
require.True(t, ok)
require.Equal(t, "bar", val)
val, ok = entpgx.VarFromContext(ctx2, "baz")
require.True(t, ok)
require.Equal(t, "qux", val)
}
func TestWithIntVar(t *testing.T) {
ctx := context.Background()
ctx = entpgx.WithIntVar(ctx, "app.user_id", 42)
val, ok := entpgx.VarFromContext(ctx, "app.user_id")
require.True(t, ok)
require.Equal(t, "42", val)
}
func TestVarFromContext_NotFound(t *testing.T) {
ctx := context.Background()
ctx = entpgx.WithVar(ctx, "key1", "val1")
val, ok := entpgx.VarFromContext(ctx, "non_existent_key")
require.False(t, ok)
require.Empty(t, val)
}