Skip to content

Commit fd4950f

Browse files
committed
fixed tests
1 parent 661bab9 commit fd4950f

File tree

16 files changed

+268
-336
lines changed

16 files changed

+268
-336
lines changed

dsn_test.go

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88

99
"github.com/ydb-platform/ydb-go-sdk/v3/config"
1010
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
11-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn"
11+
querySql "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/conn"
12+
tableSql "github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn"
1213
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
1314
)
1415

@@ -25,6 +26,12 @@ func TestParse(t *testing.T) {
2526

2627
return c
2728
}
29+
newTableConn := func(opts ...tableSql.Option) *tableSql.Conn {
30+
return tableSql.New(context.Background(), nil, nil, opts...)
31+
}
32+
newQueryConn := func(opts ...querySql.Option) *querySql.Conn {
33+
return querySql.New(context.Background(), nil, nil, opts...)
34+
}
2835
compareConfigs := func(t *testing.T, lhs, rhs *config.Config) {
2936
require.Equal(t, lhs.Secure(), rhs.Secure())
3037
require.Equal(t, lhs.Endpoint(), rhs.Endpoint())
@@ -36,19 +43,6 @@ func TestParse(t *testing.T) {
3643
connectorOpts []xsql.Option
3744
err error
3845
}{
39-
{
40-
dsn: "grpc://localhost:2135/local?go_fake_tx=scripting,scheme",
41-
opts: []config.Option{
42-
config.WithSecure(false),
43-
config.WithEndpoint("localhost:2135"),
44-
config.WithDatabase("/local"),
45-
},
46-
connectorOpts: []xsql.Option{
47-
xsql.WithFakeTx(conn.ScriptingQueryMode),
48-
xsql.WithFakeTx(conn.SchemeQueryMode),
49-
},
50-
err: nil,
51-
},
5246
{
5347
dsn: "grpc://localhost:2135/local",
5448
opts: []config.Option{
@@ -77,7 +71,7 @@ func TestParse(t *testing.T) {
7771
config.WithDatabase("/local"),
7872
},
7973
connectorOpts: []xsql.Option{
80-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
74+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
8175
},
8276
err: nil,
8377
},
@@ -89,7 +83,7 @@ func TestParse(t *testing.T) {
8983
config.WithDatabase("/local"),
9084
},
9185
connectorOpts: []xsql.Option{
92-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
86+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
9387
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
9488
},
9589
err: nil,
@@ -102,7 +96,7 @@ func TestParse(t *testing.T) {
10296
config.WithDatabase("/local"),
10397
},
10498
connectorOpts: []xsql.Option{
105-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
99+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
106100
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
107101
xsql.WithQueryBind(bind.NumericArgs{}),
108102
},
@@ -116,7 +110,7 @@ func TestParse(t *testing.T) {
116110
config.WithDatabase("/local"),
117111
},
118112
connectorOpts: []xsql.Option{
119-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
113+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
120114
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
121115
xsql.WithQueryBind(bind.PositionalArgs{}),
122116
},
@@ -130,7 +124,7 @@ func TestParse(t *testing.T) {
130124
config.WithDatabase("/local"),
131125
},
132126
connectorOpts: []xsql.Option{
133-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
127+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
134128
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
135129
xsql.WithQueryBind(bind.AutoDeclare{}),
136130
},
@@ -144,7 +138,7 @@ func TestParse(t *testing.T) {
144138
config.WithDatabase("/local"),
145139
},
146140
connectorOpts: []xsql.Option{
147-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
141+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
148142
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
149143
},
150144
err: nil,
@@ -157,13 +151,26 @@ func TestParse(t *testing.T) {
157151
config.WithDatabase("/local"),
158152
},
159153
connectorOpts: []xsql.Option{
160-
xsql.WithDefaultQueryMode(conn.ScriptingQueryMode),
154+
xsql.WithDefaultQueryMode(tableSql.ScriptingQueryMode),
161155
xsql.WithQueryBind(bind.TablePathPrefix("path/to/tables")),
162156
xsql.WithQueryBind(bind.PositionalArgs{}),
163157
xsql.WithQueryBind(bind.AutoDeclare{}),
164158
},
165159
err: nil,
166160
},
161+
{
162+
dsn: "grpc://localhost:2135/local?go_fake_tx=scripting,scheme",
163+
opts: []config.Option{
164+
config.WithSecure(false),
165+
config.WithEndpoint("localhost:2135"),
166+
config.WithDatabase("/local"),
167+
},
168+
connectorOpts: []xsql.Option{
169+
xsql.WithFakeTx(tableSql.ScriptingQueryMode),
170+
xsql.WithFakeTx(tableSql.SchemeQueryMode),
171+
},
172+
err: nil,
173+
},
167174
} {
168175
t.Run("", func(t *testing.T) {
169176
opts, err := parseConnectionString(tt.dsn)
@@ -173,7 +180,20 @@ func TestParse(t *testing.T) {
173180
require.NoError(t, err)
174181
d, err := newConnectionFromOptions(context.Background(), opts...)
175182
require.NoError(t, err)
176-
require.Equal(t, newConnector(tt.connectorOpts...), newConnector(d.databaseSQLOptions...))
183+
exp := newConnector(tt.connectorOpts...)
184+
act := newConnector(d.databaseSQLOptions...)
185+
t.Run("tableOptions", func(t *testing.T) {
186+
require.Equal(t, newTableConn(exp.TableOpts...), newTableConn(act.TableOpts...))
187+
})
188+
t.Run("queryOptions", func(t *testing.T) {
189+
require.Equal(t, newQueryConn(exp.QueryOpts...), newQueryConn(act.QueryOpts...))
190+
})
191+
exp.TableOpts = nil
192+
exp.QueryOpts = nil
193+
act.TableOpts = nil
194+
act.QueryOpts = nil
195+
require.Equal(t, exp.Bindings(), act.Bindings())
196+
require.Equal(t, exp, act)
177197
compareConfigs(t, config.New(tt.opts...), d.config)
178198
}
179199
})

internal/query/conn/conn.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,70 +21,70 @@ import (
2121
type (
2222
Parent interface {
2323
Query() *query.Client
24+
Trace() *trace.DatabaseSQL
25+
TraceRetry() *trace.Retry
26+
RetryBudget() budget.Budget
27+
Bindings() bind.Bindings
28+
Clock() clockwork.Clock
2429
}
2530
currentTx interface {
2631
Rollback() error
2732
}
28-
conn struct {
29-
ctx context.Context //nolint:containedctx
30-
parent Parent
31-
trace *trace.DatabaseSQL
32-
traceRetry *trace.Retry
33-
retryBudget budget.Budget
34-
bindings []bind.Bind
35-
session *query.Session
36-
clock clockwork.Clock
37-
onClose []func()
38-
closed atomic.Bool
33+
Conn struct {
34+
ctx context.Context //nolint:containedctx
35+
parent Parent
36+
session *query.Session
37+
onClose []func()
38+
closed atomic.Bool
3939
currentTx
4040
}
4141
)
4242

43-
func (c *conn) ID() string {
43+
func (c *Conn) ID() string {
4444
return c.session.ID()
4545
}
4646

47-
func (c *conn) IsValid() bool {
47+
func (c *Conn) IsValid() bool {
4848
//TODO implement me
4949
panic("implement me")
5050
}
5151

52-
func (c *conn) CheckNamedValue(value *driver.NamedValue) error {
52+
func (c *Conn) CheckNamedValue(value *driver.NamedValue) error {
5353
//TODO implement me
5454
panic("implement me")
5555
}
5656

57-
func (c *conn) Ping(ctx context.Context) error {
57+
func (c *Conn) Ping(ctx context.Context) error {
5858
//TODO implement me
5959
panic("implement me")
6060
}
6161

62-
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
62+
func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
6363
//TODO implement me
6464
panic("implement me")
6565
}
6666

67-
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
67+
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
6868
//TODO implement me
6969
panic("implement me")
7070
}
7171

72-
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
72+
func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
7373
//TODO implement me
7474
panic("implement me")
7575
}
7676

77-
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
77+
func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
7878
//TODO implement me
7979
panic("implement me")
8080
}
8181

82-
func (c *conn) Prepare(query string) (driver.Stmt, error) {
82+
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
8383
//TODO implement me
8484
panic("implement me")
8585
}
8686

87-
func (c *conn) Close() (finalErr error) {
87+
func (c *Conn) Close() (finalErr error) {
8888
if !c.closed.CompareAndSwap(false, true) {
8989
return badconn.Map(xerrors.WithStackTrace(errConnClosedEarly))
9090
}
@@ -98,8 +98,8 @@ func (c *conn) Close() (finalErr error) {
9898
var (
9999
ctx = c.ctx
100100
onDone = trace.DatabaseSQLOnConnClose(
101-
c.trace, &ctx,
102-
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query/conn.(*conn).Close"),
101+
c.parent.Trace(), &ctx,
102+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query/conn.(*Conn).Close"),
103103
)
104104
)
105105
defer func() {
@@ -116,28 +116,21 @@ func (c *conn) Close() (finalErr error) {
116116
return nil
117117
}
118118

119-
func (c *conn) Begin() (driver.Tx, error) {
119+
func (c *Conn) Begin() (driver.Tx, error) {
120120
//TODO implement me
121121
panic("implement me")
122122
}
123123

124-
func (c *conn) LastUsage() time.Time {
124+
func (c *Conn) LastUsage() time.Time {
125125
//TODO implement me
126126
panic("implement me")
127127
}
128128

129-
func New(ctx context.Context, parent Parent, opts ...Option) (*conn, error) {
130-
s, err := query.CreateSession(ctx, parent.Query())
131-
if err != nil {
132-
return nil, xerrors.WithStackTrace(err)
133-
}
134-
135-
cc := &conn{
129+
func New(ctx context.Context, parent Parent, s *query.Session, opts ...Option) *Conn {
130+
cc := &Conn{
136131
ctx: ctx,
137132
parent: parent,
138133
session: s,
139-
clock: clockwork.NewRealClock(),
140-
trace: &trace.DatabaseSQL{},
141134
}
142135

143136
for _, opt := range opts {
@@ -146,5 +139,5 @@ func New(ctx context.Context, parent Parent, opts ...Option) (*conn, error) {
146139
}
147140
}
148141

149-
return cc, nil
142+
return cc
150143
}

internal/query/conn/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package conn
22

33
import "errors"
44

5-
var errConnClosedEarly = errors.New("conn closed early")
5+
var errConnClosedEarly = errors.New("Conn closed early")

internal/query/conn/options.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,9 @@
11
package conn
22

3-
import (
4-
"github.com/jonboulle/clockwork"
5-
6-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
7-
"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
8-
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
9-
)
10-
11-
type Option func(c *conn)
12-
13-
func WithTrace(
14-
t *trace.DatabaseSQL,
15-
opts ...trace.DatabaseSQLComposeOption,
16-
) Option {
17-
return func(c *conn) {
18-
c.trace = c.trace.Compose(t, opts...)
19-
}
20-
}
21-
22-
func WithTraceRetry(
23-
t *trace.Retry,
24-
opts ...trace.RetryComposeOption,
25-
) Option {
26-
return func(c *conn) {
27-
c.traceRetry = c.traceRetry.Compose(t, opts...)
28-
}
29-
}
30-
31-
func WithRetryBudget(budget budget.Budget) Option {
32-
return func(c *conn) {
33-
c.retryBudget = budget
34-
}
35-
}
36-
37-
func WithQueryBindings(binds ...bind.Bind) Option {
38-
return func(c *conn) {
39-
c.bindings = append(c.bindings, binds...)
40-
}
41-
}
42-
43-
func WithClock(clock clockwork.Clock) Option {
44-
return func(c *conn) {
45-
c.clock = clock
46-
}
47-
}
3+
type Option func(c *Conn)
484

495
func WithOnClose(onClose func()) Option {
50-
return func(c *conn) {
6+
return func(c *Conn) {
517
c.onClose = append(c.onClose, onClose)
528
}
539
}

0 commit comments

Comments
 (0)