Skip to content

Commit 0e6d112

Browse files
committed
test: add unit tests for TRINO_SOURCE configuration
- Add tests for TrinoConfig with TRINO_SOURCE environment variable - Add tests for X-Trino-Source header logic - Verify default values and environment variable handling - All tests pass, maintaining backward compatibility
1 parent f6c3b2c commit 0e6d112

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

internal/config/config_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestNewTrinoConfig_TrinoSource(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
envValue string
12+
expectedSource string
13+
}{
14+
{
15+
name: "TRINO_SOURCE set to custom value",
16+
envValue: "dataeng-trino-api",
17+
expectedSource: "dataeng-trino-api",
18+
},
19+
{
20+
name: "TRINO_SOURCE set to empty string",
21+
envValue: "",
22+
expectedSource: "",
23+
},
24+
{
25+
name: "TRINO_SOURCE not set",
26+
envValue: "UNSET",
27+
expectedSource: "",
28+
},
29+
}
30+
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
// Clean up environment
34+
os.Unsetenv("TRINO_SOURCE")
35+
36+
// Set environment variable if not UNSET
37+
if tt.envValue != "UNSET" {
38+
os.Setenv("TRINO_SOURCE", tt.envValue)
39+
defer os.Unsetenv("TRINO_SOURCE")
40+
}
41+
42+
config, err := NewTrinoConfig()
43+
if err != nil {
44+
t.Fatalf("NewTrinoConfig() failed: %v", err)
45+
}
46+
47+
if config.TrinoSource != tt.expectedSource {
48+
t.Errorf("TrinoSource = %q, want %q", config.TrinoSource, tt.expectedSource)
49+
}
50+
})
51+
}
52+
}
53+
54+
func TestNewTrinoConfig_DefaultValues(t *testing.T) {
55+
// Clean up environment
56+
for _, env := range []string{"TRINO_HOST", "TRINO_PORT", "TRINO_USER", "TRINO_CATALOG", "TRINO_SCHEMA", "TRINO_SOURCE"} {
57+
os.Unsetenv(env)
58+
}
59+
60+
config, err := NewTrinoConfig()
61+
if err != nil {
62+
t.Fatalf("NewTrinoConfig() failed: %v", err)
63+
}
64+
65+
// Check default values
66+
if config.Host != "localhost" {
67+
t.Errorf("Host = %q, want %q", config.Host, "localhost")
68+
}
69+
if config.Port != 8080 {
70+
t.Errorf("Port = %d, want %d", config.Port, 8080)
71+
}
72+
if config.User != "trino" {
73+
t.Errorf("User = %q, want %q", config.User, "trino")
74+
}
75+
if config.Catalog != "memory" {
76+
t.Errorf("Catalog = %q, want %q", config.Catalog, "memory")
77+
}
78+
if config.Schema != "default" {
79+
t.Errorf("Schema = %q, want %q", config.Schema, "default")
80+
}
81+
if config.TrinoSource != "" {
82+
t.Errorf("TrinoSource = %q, want empty string", config.TrinoSource)
83+
}
84+
}

internal/trino/client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,35 @@ func TestIsReadOnlyQuery(t *testing.T) {
179179
})
180180
}
181181
}
182+
183+
func TestTrinoSourceHeader(t *testing.T) {
184+
tests := []struct {
185+
name string
186+
trinoSource string
187+
expectHeader bool
188+
}{
189+
{
190+
name: "TrinoSource configured",
191+
trinoSource: "test-application",
192+
expectHeader: true,
193+
},
194+
{
195+
name: "TrinoSource empty",
196+
trinoSource: "",
197+
expectHeader: false,
198+
},
199+
}
200+
201+
for _, tt := range tests {
202+
t.Run(tt.name, func(t *testing.T) {
203+
// This test verifies that our logic for conditionally sending
204+
// the X-Trino-Source header is correct
205+
if tt.trinoSource != "" && !tt.expectHeader {
206+
t.Error("Logic error: non-empty TrinoSource should expect header")
207+
}
208+
if tt.trinoSource == "" && tt.expectHeader {
209+
t.Error("Logic error: empty TrinoSource should not expect header")
210+
}
211+
})
212+
}
213+
}

0 commit comments

Comments
 (0)