Skip to content

Commit

Permalink
added xtest.ToJSON()
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Mar 25, 2024
1 parent 173e976 commit 72aca06
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 27 deletions.
11 changes: 2 additions & 9 deletions internal/params/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package params

import (
"encoding/json"
"testing"
"time"

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

func paramsToJSON(params map[string]*Ydb.TypedValue) string {
b, _ := json.MarshalIndent(params, "", "\t") //nolint:errchkjson

return string(b)
}

func TestBuilder(t *testing.T) {
type expected struct {
Type *Ydb.Type
Expand Down Expand Up @@ -427,14 +420,14 @@ func TestBuilder(t *testing.T) {
params := result.Build().ToYDB(a)

require.Equal(t,
paramsToJSON(
xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: tc.expected.Type,
Value: tc.expected.Value,
},
}),
paramsToJSON(params),
xtest.ToJSON(params),
)
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/params/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestDict(t *testing.T) {
require.True(t, ok)

params := d.EndDict().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand All @@ -444,7 +444,7 @@ func TestDict(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
})
}
}
Expand All @@ -467,7 +467,7 @@ func TestDict_AddPairs(t *testing.T) {

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

require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -515,5 +515,5 @@ func TestDict_AddPairs(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
}
8 changes: 4 additions & 4 deletions internal/params/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func TestList(t *testing.T) {
require.True(t, ok)

params := result.EndList().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand All @@ -435,7 +435,7 @@ func TestList(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
})
}
}
Expand All @@ -446,7 +446,7 @@ func TestList_AddItems(t *testing.T) {
params := Builder{}.Param("$x").BeginList().
AddItems(value.Uint64Value(123), value.Uint64Value(321)).
EndList().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand All @@ -471,5 +471,5 @@ func TestList_AddItems(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
}
4 changes: 2 additions & 2 deletions internal/params/optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestOptional(t *testing.T) {
require.True(t, ok)

params := result.EndOptional().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand All @@ -430,7 +430,7 @@ func TestOptional(t *testing.T) {
},
Value: tc.expected.Value,
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
})
}
}
4 changes: 2 additions & 2 deletions internal/params/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func TestPg(t *testing.T) {

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

require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: tc.expected.Type,
Value: tc.expected.Value,
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
})
}
}
8 changes: 4 additions & 4 deletions internal/params/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func TestSet(t *testing.T) {
require.True(t, ok)

params := result.EndSet().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand All @@ -443,7 +443,7 @@ func TestSet(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
})
}
}
Expand All @@ -454,7 +454,7 @@ func TestSet_AddItems(t *testing.T) {
params := Builder{}.Param("$x").BeginSet().
AddItems(value.Uint64Value(123), value.Uint64Value(321)).
EndSet().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
require.Equal(t, xtest.ToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -496,5 +496,5 @@ func TestSet_AddItems(t *testing.T) {
},
},
},
}), paramsToJSON(params))
}), xtest.ToJSON(params))
}
2 changes: 1 addition & 1 deletion internal/params/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ func TestStruct(t *testing.T) {
a := allocator.New()
defer a.Free()
params := tt.builder.Build().ToYDB(a)
require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
require.Equal(t, xtest.ToJSON(tt.params), xtest.ToJSON(params))
})
}
}
2 changes: 1 addition & 1 deletion internal/params/tuple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ func TestTuple(t *testing.T) {
a := allocator.New()
defer a.Free()
params := tt.builder.Build().ToYDB(a)
require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
require.Equal(t, xtest.ToJSON(tt.params), xtest.ToJSON(params))
})
}
}
9 changes: 9 additions & 0 deletions internal/xtest/to_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package xtest

import "encoding/json"

func ToJSON(v interface{}) string {
b, _ := json.MarshalIndent(v, "", "\t") //nolint:errchkjson

return string(b)
}
58 changes: 58 additions & 0 deletions internal/xtest/to_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package xtest

import (
"testing"

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

func TestToJSON(t *testing.T) {
for _, tt := range []struct {
name string
v interface{}
s string
}{
{
name: CurrentFileLine(),
v: int64(123),
s: "123",
},
{
name: CurrentFileLine(),
v: struct {
A string
B int64
C bool
}{
A: "123",
B: 123,
C: true,
},
s: "{\n\t\"A\": \"123\",\n\t\"B\": 123,\n\t\"C\": true\n}",
},
{
name: CurrentFileLine(),
v: map[string]struct {
A string
B int64
C bool
}{
"abc": {
A: "123",
B: 123,
C: true,
},
"def": {
A: "456",
B: 456,
C: false,
},
},
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
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.s, ToJSON(tt.v))
})
}
}

0 comments on commit 72aca06

Please sign in to comment.