Skip to content

Commit fda5a9d

Browse files
Copilotasmyasnikov
andauthored
Add unit tests for internal/xsql package to increase test coverage (#1895)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: asmyasnikov <[email protected]> Co-authored-by: Aleksey Myasnikov <[email protected]>
1 parent f53995e commit fda5a9d

File tree

11 files changed

+1468
-0
lines changed

11 files changed

+1468
-0
lines changed

internal/xsql/conn_helpers_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ package xsql
33
import (
44
"context"
55
"database/sql/driver"
6+
"testing"
67
"time"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
12+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
713
)
814

915
var (
@@ -63,3 +69,170 @@ var (
6369
GetIndexColumns(ctx context.Context, tableName string, indexName string) (columns []string, err error)
6470
} = (*Conn)(nil)
6571
)
72+
73+
func TestConn_Engine(t *testing.T) {
74+
tests := []struct {
75+
name string
76+
engine Engine
77+
expected Engine
78+
}{
79+
{
80+
name: "QUERY",
81+
engine: QUERY,
82+
expected: QUERY,
83+
},
84+
{
85+
name: "TABLE",
86+
engine: TABLE,
87+
expected: TABLE,
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
conn := &Conn{
94+
processor: tt.engine,
95+
}
96+
require.Equal(t, tt.expected, conn.Engine())
97+
})
98+
}
99+
}
100+
101+
func TestConn_LastUsage(t *testing.T) {
102+
conn := &Conn{
103+
lastUsage: xsync.NewLastUsage(),
104+
}
105+
106+
// Initially should be zero time
107+
lastUsage := conn.LastUsage()
108+
require.False(t, lastUsage.IsZero())
109+
110+
// Start and stop usage
111+
done := conn.lastUsage.Start()
112+
time.Sleep(10 * time.Millisecond)
113+
done()
114+
115+
// Should have updated time
116+
newLastUsage := conn.LastUsage()
117+
require.True(t, newLastUsage.After(lastUsage) || newLastUsage.Equal(lastUsage))
118+
}
119+
120+
func TestConn_GetDatabaseName(t *testing.T) {
121+
// We can't easily mock ydbDriver, so this test is covered
122+
// by integration tests instead
123+
t.Skip("requires full ydbDriver mock")
124+
}
125+
126+
func TestConn_normalizePath(t *testing.T) {
127+
tests := []struct {
128+
name string
129+
pathPrefix string
130+
tableName string
131+
expected string
132+
}{
133+
{
134+
name: "WithPrefix",
135+
pathPrefix: "/local",
136+
tableName: "test_table",
137+
expected: "/local/test_table",
138+
},
139+
{
140+
name: "AlreadyPrefixed",
141+
pathPrefix: "/local",
142+
tableName: "/local/test_table",
143+
expected: "/local/test_table",
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
conn := &Conn{
150+
connector: &Connector{
151+
pathNormalizer: bind.TablePathPrefix(tt.pathPrefix),
152+
},
153+
}
154+
155+
result := conn.normalizePath(tt.tableName)
156+
require.Equal(t, tt.expected, result)
157+
})
158+
}
159+
}
160+
161+
func TestConn_Version(t *testing.T) {
162+
conn := &Conn{}
163+
version, err := conn.Version(context.Background())
164+
require.NoError(t, err)
165+
require.Equal(t, "default", version)
166+
}
167+
168+
func TestIsSysDir(t *testing.T) {
169+
tests := []struct {
170+
name string
171+
databaseName string
172+
dirAbsPath string
173+
expected bool
174+
}{
175+
{
176+
name: "SysDirExact",
177+
databaseName: "/local",
178+
dirAbsPath: "/local/.sys",
179+
expected: true,
180+
},
181+
{
182+
name: "SysDirSubpath",
183+
databaseName: "/local",
184+
dirAbsPath: "/local/.sys/something",
185+
expected: true,
186+
},
187+
{
188+
name: "SysHealthDirExact",
189+
databaseName: "/local",
190+
dirAbsPath: "/local/.sys_health",
191+
expected: true,
192+
},
193+
{
194+
name: "SysHealthDirSubpath",
195+
databaseName: "/local",
196+
dirAbsPath: "/local/.sys_health/metrics",
197+
expected: true,
198+
},
199+
{
200+
name: "NormalDir",
201+
databaseName: "/local",
202+
dirAbsPath: "/local/tables",
203+
expected: false,
204+
},
205+
{
206+
name: "DifferentDatabase",
207+
databaseName: "/local",
208+
dirAbsPath: "/other/.sys",
209+
expected: false,
210+
},
211+
{
212+
name: "EmptyPath",
213+
databaseName: "/local",
214+
dirAbsPath: "",
215+
expected: false,
216+
},
217+
}
218+
219+
for _, tt := range tests {
220+
t.Run(tt.name, func(t *testing.T) {
221+
result := isSysDir(tt.databaseName, tt.dirAbsPath)
222+
require.Equal(t, tt.expected, result)
223+
})
224+
}
225+
}
226+
227+
func TestConn_toYdb(t *testing.T) {
228+
conn := &Conn{
229+
connector: &Connector{
230+
bindings: newMockBindings(),
231+
},
232+
}
233+
234+
yql, p, err := conn.toYdb("SELECT 1", driver.NamedValue{Name: "p1", Value: 42})
235+
require.NoError(t, err)
236+
require.NotEmpty(t, yql)
237+
require.NotNil(t, p)
238+
}

0 commit comments

Comments
 (0)