File: marshaler_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 (101 lines) | stat: -rw-r--r-- 2,622 bytes parent folder | download | duplicates (2)
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
// 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 (
	"errors"
	"fmt"
	"reflect"
	"testing"

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

func TestMarshalerEncode(t *testing.T) {
	want := []byte(`{"@type": "g:Int32", "@value": 42}`)
	m := &mocker{}
	call := m.On("MarshalGraphson").Return(want, nil)
	defer m.AssertExpectations(t)

	tests := []interface{}{m, &m, func() *Marshaler { marshaler := Marshaler(m); return &marshaler }(), Marshaler(nil)}
	call.Times(len(tests) - 1)

	for _, tc := range tests {
		tc := tc
		t.Run(fmt.Sprintf("%T", tc), func(t *testing.T) {
			got, err := Marshal(tc)
			assert.NoError(t, err)
			if !reflect2.IsNil(tc) {
				assert.Equal(t, want, got)
			} else {
				assert.Equal(t, []byte("null"), got)
			}
		})
	}
}

func TestMarshalerError(t *testing.T) {
	errStr := "marshaler error"
	m := &mocker{}
	m.On("MarshalGraphson").Return(nil, errors.New(errStr)).Once()
	defer m.AssertExpectations(t)

	_, err := Marshal(m)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), errStr)
}

func TestBadMarshaler(t *testing.T) {
	m := &mocker{}
	m.On("MarshalGraphson").Return([]byte(`{"@type": "g:Int32", "@value":`), nil).Once()
	defer m.AssertExpectations(t)
	_, err := Marshal(m)
	assert.Error(t, err)
}

func TestUnmarshalerDecode(t *testing.T) {
	data := `{"@type": "g:UUID", "@value": "cb682578-9d92-4499-9ebc-5c6aa73c5397"}`
	var value string

	m := &mocker{}
	m.On("UnmarshalGraphson", mock.Anything).
		Run(func(args mock.Arguments) {
			data := args.Get(0).([]byte)
			value = jsoniter.Get(data, "@value").ToString()
		}).
		Return(nil).
		Once()
	defer m.AssertExpectations(t)

	err := UnmarshalFromString(data, m)
	require.NoError(t, err)
	assert.Equal(t, "cb682578-9d92-4499-9ebc-5c6aa73c5397", value)
}

func TestUnmarshalerError(t *testing.T) {
	errStr := "unmarshaler error"
	m := &mocker{}
	m.On("UnmarshalGraphson", mock.Anything).Return(errors.New(errStr)).Once()
	defer m.AssertExpectations(t)

	err := Unmarshal([]byte(`{}`), m)
	require.Error(t, err)
	assert.Contains(t, err.Error(),
		fmt.Sprintf("graphson: error calling UnmarshalGraphson for type %s: %s",
			reflect.TypeOf(m), errStr,
		),
	)
}

func TestUnmarshalBadInput(t *testing.T) {
	m := &mocker{}
	defer m.AssertExpectations(t)
	err := UnmarshalFromString(`{"@type"}`, m)
	assert.Error(t, err)
}