-
Notifications
You must be signed in to change notification settings - Fork 0
/
ent_test.go
68 lines (54 loc) · 1.62 KB
/
ent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package xo
import (
"database/sql"
"testing"
"github.com/nekomeowww/xo/protobufs/testpb"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
)
func TestProtoValueScanner(t *testing.T) {
t.Run("NULL", func(t *testing.T) {
scanner := ProtoValueScanner[testpb.TestMessage]{}
require.NotNil(t, scanner)
val, err := scanner.Value(nil)
require.NoError(t, err)
assert.Equal(t, sql.NullString{}, val)
value, err := val.(sql.NullString).Value()
require.NoError(t, err)
require.Nil(t, value)
pb, err := scanner.FromValue(&sql.NullString{String: "", Valid: false})
require.NoError(t, err)
require.Nil(t, pb)
pb, err = scanner.FromValue(lo.ToPtr(val.(sql.NullString)))
require.NoError(t, err)
require.Nil(t, pb)
})
t.Run("NonNULL", func(t *testing.T) {
original := &testpb.TestMessage{
Property_1: "Hello, World!",
Property_2: "John Doe",
OneofField: &testpb.TestMessage_PossibleOne{
PossibleOne: &testpb.PossibleOne{
Property_1: "Hello, World!",
Property_2: "John Doe",
},
},
}
scanner := ProtoValueScanner[testpb.TestMessage]{}
require.NotNil(t, scanner)
val, err := scanner.Value(original)
require.NoError(t, err)
str, ok := val.(*sql.NullString)
require.True(t, ok)
require.NotEmpty(t, str)
bytes, err := protojson.Marshal(original)
require.NoError(t, err)
assert.Equal(t, string(bytes), str.String)
pb, err := scanner.FromValue(&sql.NullString{String: str.String, Valid: true})
require.NoError(t, err)
require.NotNil(t, pb)
assert.Equal(t, original, pb)
})
}