File: context_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (128 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (4)
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
package correlation

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestExtractFromContext(t *testing.T) {
	require := require.New(t)

	tests := []struct {
		name string
		ctx  context.Context
		want string
	}{
		{"missing", context.Background(), ""},
		{"set", context.WithValue(context.Background(), keyCorrelationID, "CORRELATION_ID"), "CORRELATION_ID"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			require.Equal(tt.want, ExtractFromContext(tt.ctx))
		})
	}
}

func TestExtractFromContextOrGenerate(t *testing.T) {
	require := require.New(t)

	tests := []struct {
		name string
		ctx  context.Context
		want string
	}{
		{"missing", context.Background(), ""},
		{"set", context.WithValue(context.Background(), keyCorrelationID, "CORRELATION_ID"), "CORRELATION_ID"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if tt.want == "" { // want a random one
				require.NotEmpty(ExtractFromContextOrGenerate(tt.ctx))
			} else {
				require.Equal(tt.want, ExtractFromContextOrGenerate(tt.ctx))
			}
		})
	}
}

func TestContextWithCorrelation(t *testing.T) {
	require := require.New(t)

	tests := []struct {
		name          string
		ctx           context.Context
		correlationID string
		wantValue     string
	}{
		{
			name:          "value",
			ctx:           context.Background(),
			correlationID: "CORRELATION_ID",
			wantValue:     "CORRELATION_ID",
		},
		{
			name:          "empty",
			ctx:           context.Background(),
			correlationID: "",
			wantValue:     "",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := ContextWithCorrelation(tt.ctx, tt.correlationID)
			gotValue := got.Value(keyCorrelationID)
			require.Equal(tt.wantValue, gotValue)
		})
	}
}

func TestExtractClientNameFromContext(t *testing.T) {
	require := require.New(t)

	tests := []struct {
		name string
		ctx  context.Context
		want string
	}{
		{"missing", context.Background(), ""},
		{"set", context.WithValue(context.Background(), keyClientName, "CLIENT_NAME"), "CLIENT_NAME"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			require.Equal(tt.want, ExtractClientNameFromContext(tt.ctx))
		})
	}
}

func TestContextWithClientName(t *testing.T) {
	require := require.New(t)

	tests := []struct {
		name       string
		ctx        context.Context
		clientName string
		wantValue  string
	}{
		{
			name:       "value",
			ctx:        context.Background(),
			clientName: "CLIENT_NAME",
			wantValue:  "CLIENT_NAME",
		},
		{
			name:       "empty",
			ctx:        context.Background(),
			clientName: "",
			wantValue:  "",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := ContextWithClientName(tt.ctx, tt.clientName)
			gotValue := got.Value(keyClientName)
			require.Equal(tt.wantValue, gotValue)
		})
	}
}