Skip to content

Commit 72aca06

Browse files
committed
added xtest.ToJSON()
1 parent 173e976 commit 72aca06

File tree

10 files changed

+87
-27
lines changed

10 files changed

+87
-27
lines changed

internal/params/builder_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package params
22

33
import (
4-
"encoding/json"
54
"testing"
65
"time"
76

@@ -12,12 +11,6 @@ import (
1211
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
1312
)
1413

15-
func paramsToJSON(params map[string]*Ydb.TypedValue) string {
16-
b, _ := json.MarshalIndent(params, "", "\t") //nolint:errchkjson
17-
18-
return string(b)
19-
}
20-
2114
func TestBuilder(t *testing.T) {
2215
type expected struct {
2316
Type *Ydb.Type
@@ -427,14 +420,14 @@ func TestBuilder(t *testing.T) {
427420
params := result.Build().ToYDB(a)
428421

429422
require.Equal(t,
430-
paramsToJSON(
423+
xtest.ToJSON(
431424
map[string]*Ydb.TypedValue{
432425
"$x": {
433426
Type: tc.expected.Type,
434427
Value: tc.expected.Value,
435428
},
436429
}),
437-
paramsToJSON(params),
430+
xtest.ToJSON(params),
438431
)
439432
})
440433
}

internal/params/dict_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func TestDict(t *testing.T) {
424424
require.True(t, ok)
425425

426426
params := d.EndDict().Build().ToYDB(a)
427-
require.Equal(t, paramsToJSON(
427+
require.Equal(t, xtest.ToJSON(
428428
map[string]*Ydb.TypedValue{
429429
"$x": {
430430
Type: &Ydb.Type{
@@ -444,7 +444,7 @@ func TestDict(t *testing.T) {
444444
},
445445
},
446446
},
447-
}), paramsToJSON(params))
447+
}), xtest.ToJSON(params))
448448
})
449449
}
450450
}
@@ -467,7 +467,7 @@ func TestDict_AddPairs(t *testing.T) {
467467

468468
params := Builder{}.Param("$x").BeginDict().AddPairs(pairs...).EndDict().Build().ToYDB(a)
469469

470-
require.Equal(t, paramsToJSON(
470+
require.Equal(t, xtest.ToJSON(
471471
map[string]*Ydb.TypedValue{
472472
"$x": {
473473
Type: &Ydb.Type{
@@ -515,5 +515,5 @@ func TestDict_AddPairs(t *testing.T) {
515515
},
516516
},
517517
},
518-
}), paramsToJSON(params))
518+
}), xtest.ToJSON(params))
519519
}

internal/params/list_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func TestList(t *testing.T) {
419419
require.True(t, ok)
420420

421421
params := result.EndList().Build().ToYDB(a)
422-
require.Equal(t, paramsToJSON(
422+
require.Equal(t, xtest.ToJSON(
423423
map[string]*Ydb.TypedValue{
424424
"$x": {
425425
Type: &Ydb.Type{
@@ -435,7 +435,7 @@ func TestList(t *testing.T) {
435435
},
436436
},
437437
},
438-
}), paramsToJSON(params))
438+
}), xtest.ToJSON(params))
439439
})
440440
}
441441
}
@@ -446,7 +446,7 @@ func TestList_AddItems(t *testing.T) {
446446
params := Builder{}.Param("$x").BeginList().
447447
AddItems(value.Uint64Value(123), value.Uint64Value(321)).
448448
EndList().Build().ToYDB(a)
449-
require.Equal(t, paramsToJSON(
449+
require.Equal(t, xtest.ToJSON(
450450
map[string]*Ydb.TypedValue{
451451
"$x": {
452452
Type: &Ydb.Type{
@@ -471,5 +471,5 @@ func TestList_AddItems(t *testing.T) {
471471
},
472472
},
473473
},
474-
}), paramsToJSON(params))
474+
}), xtest.ToJSON(params))
475475
}

internal/params/optional_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func TestOptional(t *testing.T) {
418418
require.True(t, ok)
419419

420420
params := result.EndOptional().Build().ToYDB(a)
421-
require.Equal(t, paramsToJSON(
421+
require.Equal(t, xtest.ToJSON(
422422
map[string]*Ydb.TypedValue{
423423
"$x": {
424424
Type: &Ydb.Type{
@@ -430,7 +430,7 @@ func TestOptional(t *testing.T) {
430430
},
431431
Value: tc.expected.Value,
432432
},
433-
}), paramsToJSON(params))
433+
}), xtest.ToJSON(params))
434434
})
435435
}
436436
}

internal/params/pg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ func TestPg(t *testing.T) {
8888

8989
params := result.Build().ToYDB(a)
9090

91-
require.Equal(t, paramsToJSON(
91+
require.Equal(t, xtest.ToJSON(
9292
map[string]*Ydb.TypedValue{
9393
"$x": {
9494
Type: tc.expected.Type,
9595
Value: tc.expected.Value,
9696
},
97-
}), paramsToJSON(params))
97+
}), xtest.ToJSON(params))
9898
})
9999
}
100100
}

internal/params/set_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func TestSet(t *testing.T) {
419419
require.True(t, ok)
420420

421421
params := result.EndSet().Build().ToYDB(a)
422-
require.Equal(t, paramsToJSON(
422+
require.Equal(t, xtest.ToJSON(
423423
map[string]*Ydb.TypedValue{
424424
"$x": {
425425
Type: &Ydb.Type{
@@ -443,7 +443,7 @@ func TestSet(t *testing.T) {
443443
},
444444
},
445445
},
446-
}), paramsToJSON(params))
446+
}), xtest.ToJSON(params))
447447
})
448448
}
449449
}
@@ -454,7 +454,7 @@ func TestSet_AddItems(t *testing.T) {
454454
params := Builder{}.Param("$x").BeginSet().
455455
AddItems(value.Uint64Value(123), value.Uint64Value(321)).
456456
EndSet().Build().ToYDB(a)
457-
require.Equal(t, paramsToJSON(
457+
require.Equal(t, xtest.ToJSON(
458458
map[string]*Ydb.TypedValue{
459459
"$x": {
460460
Type: &Ydb.Type{
@@ -496,5 +496,5 @@ func TestSet_AddItems(t *testing.T) {
496496
},
497497
},
498498
},
499-
}), paramsToJSON(params))
499+
}), xtest.ToJSON(params))
500500
}

internal/params/struct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ func TestStruct(t *testing.T) {
916916
a := allocator.New()
917917
defer a.Free()
918918
params := tt.builder.Build().ToYDB(a)
919-
require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
919+
require.Equal(t, xtest.ToJSON(tt.params), xtest.ToJSON(params))
920920
})
921921
}
922922
}

internal/params/tuple_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ func TestTuple(t *testing.T) {
729729
a := allocator.New()
730730
defer a.Free()
731731
params := tt.builder.Build().ToYDB(a)
732-
require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
732+
require.Equal(t, xtest.ToJSON(tt.params), xtest.ToJSON(params))
733733
})
734734
}
735735
}

internal/xtest/to_json.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package xtest
2+
3+
import "encoding/json"
4+
5+
func ToJSON(v interface{}) string {
6+
b, _ := json.MarshalIndent(v, "", "\t") //nolint:errchkjson
7+
8+
return string(b)
9+
}

internal/xtest/to_json_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package xtest
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestToJSON(t *testing.T) {
10+
for _, tt := range []struct {
11+
name string
12+
v interface{}
13+
s string
14+
}{
15+
{
16+
name: CurrentFileLine(),
17+
v: int64(123),
18+
s: "123",
19+
},
20+
{
21+
name: CurrentFileLine(),
22+
v: struct {
23+
A string
24+
B int64
25+
C bool
26+
}{
27+
A: "123",
28+
B: 123,
29+
C: true,
30+
},
31+
s: "{\n\t\"A\": \"123\",\n\t\"B\": 123,\n\t\"C\": true\n}",
32+
},
33+
{
34+
name: CurrentFileLine(),
35+
v: map[string]struct {
36+
A string
37+
B int64
38+
C bool
39+
}{
40+
"abc": {
41+
A: "123",
42+
B: 123,
43+
C: true,
44+
},
45+
"def": {
46+
A: "456",
47+
B: 456,
48+
C: false,
49+
},
50+
},
51+
s: "{\n\t\"abc\": {\n\t\t\"A\": \"123\",\n\t\t\"B\": 123,\n\t\t\"C\": true\n\t},\n\t\"def\": {\n\t\t\"A\": \"456\",\n\t\t\"B\": 456,\n\t\t\"C\": false\n\t}\n}", //nolint:lll
52+
},
53+
} {
54+
t.Run(tt.name, func(t *testing.T) {
55+
require.Equal(t, tt.s, ToJSON(tt.v))
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)