File: util_test.go

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (129 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download | duplicates (4)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package graphson

import (
	"bytes"
	"errors"
	"fmt"
	"testing"
	"unsafe"

	jsoniter "github.com/json-iterator/go"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

func TestTypeEncode(t *testing.T) {
	var got bytes.Buffer
	stream := config.BorrowStream(&got)
	defer config.ReturnStream(stream)

	typ, val := int32Type, 42
	ptr := unsafe.Pointer(&val)

	var m mocker
	m.On("Encode", ptr, stream).
		Run(func(args mock.Arguments) {
			stream := args.Get(1).(*jsoniter.Stream)
			stream.WriteInt(val)
		}).
		Once()
	defer m.AssertExpectations(t)

	typeEncoder{&m, typ}.Encode(ptr, stream)
	require.NoError(t, stream.Flush())
	want := fmt.Sprintf(`{"@type": "%s", "@value": %d}`, typ, val)
	assert.JSONEq(t, want, got.String())
}

func TestTypeDecode(t *testing.T) {
	typ, val := int64Type, 84
	ptr := unsafe.Pointer(&val)

	data := fmt.Sprintf(`{"@value": %d, "@type": "%s"}`, val, typ)
	iter := config.BorrowIterator([]byte(data))
	defer config.ReturnIterator(iter)

	m := &mocker{}
	m.On("CheckType", typ).
		Return(nil).
		Once()
	m.On("Decode", ptr, mock.Anything).
		Run(func(args mock.Arguments) {
			iter := args.Get(1).(*jsoniter.Iterator)
			assert.Equal(t, val, iter.ReadInt())
		}).
		Once()
	defer m.AssertExpectations(t)

	typeDecoder{m, m}.Decode(ptr, iter)
	assert.NoError(t, iter.Error)
}

func TestTypeDecodeBadType(t *testing.T) {
	typ, val := int64Type, 55
	ptr := unsafe.Pointer(&val)

	m := &mocker{}
	m.On("CheckType", typ).Return(errors.New("bad type")).Once()
	defer m.AssertExpectations(t)

	data := fmt.Sprintf(`{"@type": "%s", "@value": %d}`, typ, val)
	iter := config.BorrowIterator([]byte(data))
	defer config.ReturnIterator(iter)

	typeDecoder{m, m}.Decode(ptr, iter)
	require.Error(t, iter.Error)
	assert.Contains(t, iter.Error.Error(), "bad type")
}

func TestTypeDecodeDuplicateField(t *testing.T) {
	data := `{"@type": "gx:Byte", "@value": 33, "@type": "g:Int32"}`
	iter := config.BorrowIterator([]byte(data))
	defer config.ReturnIterator(iter)
	var ptr unsafe.Pointer

	m := &mocker{}
	m.On("CheckType", mock.MatchedBy(func(typ Type) bool { return typ == int32Type })).
		Return(nil).
		Once()
	m.On("Decode", ptr, mock.Anything).
		Run(func(args mock.Arguments) {
			args.Get(1).(*jsoniter.Iterator).Skip()
			require.NoError(t, iter.Error)
		}).
		Once()
	defer m.AssertExpectations(t)

	typeDecoder{m, m}.Decode(ptr, iter)
	assert.NoError(t, iter.Error)
}

func TestTypeDecodeMissingField(t *testing.T) {
	data := `{"@type": "g:Int32"}`
	iter := config.BorrowIterator([]byte(data))
	defer config.ReturnIterator(iter)

	m := &mocker{}
	defer m.AssertExpectations(t)

	typeDecoder{m, m}.Decode(nil, iter)
	require.Error(t, iter.Error)
	assert.Contains(t, iter.Error.Error(), "missing type or value")
}

func TestTypeDecodeSyntaxError(t *testing.T) {
	data := `{"@type": "gx:Int16", "@value", 65000}`
	iter := config.BorrowIterator([]byte(data))
	defer config.ReturnIterator(iter)

	m := &mocker{}
	defer m.AssertExpectations(t)

	typeDecoder{m, m}.Decode(nil, iter)
	assert.Error(t, iter.Error)
}