forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce_send_fields_test.go
93 lines (75 loc) · 2.79 KB
/
force_send_fields_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
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
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func expectPanic(t *testing.T, message string) func() {
return func() {
r := recover()
if r == nil {
t.Errorf("expected panic: %s", message)
}
if str, ok := r.(string); ok && str != message {
t.Errorf("expected panic: %s, got: %v", message, r)
}
if err, ok := r.(error); ok && err.Error() != message {
t.Errorf("expected panic: %s, got: %v", message, r)
}
}
}
func TestSetForceSendFields_RequestMustBePointer(t *testing.T) {
defer expectPanic(t, "request argument to setForceSendFields must be a pointer")()
SetForceSendFields(1, nil, nil)
}
type noForceSendFields struct {
A string `json:"a"`
}
func TestSetForceSendFields_RequestMustHaveForceSendFields(t *testing.T) {
defer expectPanic(t, "request argument to setForceSendFields must have ForceSendFields field")()
SetForceSendFields(&noForceSendFields{}, nil, nil)
}
type incorrectForceSendFields struct {
A string `json:"a"`
ForceSendFields int `json:"force_send_fields"`
}
func TestSetForceSendFields_RequestMustHaveForceSendFieldsWithCorrectType(t *testing.T) {
defer expectPanic(t, "request argument to setForceSendFields must have ForceSendFields field of type []string (got int)")()
SetForceSendFields(&incorrectForceSendFields{}, nil, nil)
}
type withForceSendFields struct {
A string `json:"a"`
B string `json:"-"`
ForceSendFields []string `json:"force_send_fields"`
}
func TestSetForceSendFields_NoFields(t *testing.T) {
req := &withForceSendFields{}
SetForceSendFields(req, nil, nil)
assert.Len(t, req.ForceSendFields, 0)
}
func TestSetForceSendFields_ForceAWhenPresent(t *testing.T) {
req := &withForceSendFields{}
SetForceSendFields(req, data{"a": ""}, []string{"a"})
assert.Len(t, req.ForceSendFields, 1)
assert.Equal(t, "A", req.ForceSendFields[0])
}
func TestSetForceSendFields_DoNotForceAWhenAbsent(t *testing.T) {
req := &withForceSendFields{}
SetForceSendFields(req, data{}, []string{"a"})
assert.Len(t, req.ForceSendFields, 0)
}
func TestSetForceSendFields_DoNotDuplicate(t *testing.T) {
req := &withForceSendFields{ForceSendFields: []string{"A"}}
SetForceSendFields(req, data{"a": ""}, []string{"a"})
assert.Len(t, req.ForceSendFields, 1)
assert.Equal(t, "A", req.ForceSendFields[0])
}
func TestSetForceSendFields_CannotForceNonJsonFields(t *testing.T) {
defer expectPanic(t, "unexpected field b not found in request structure, expected one of: a")()
req := &withForceSendFields{}
SetForceSendFields(req, data{"b": ""}, []string{"b"})
}
func TestSetForceSendFields_CannotForceUnknownFields(t *testing.T) {
defer expectPanic(t, "unexpected field c not found in request structure, expected one of: a")()
req := &withForceSendFields{}
SetForceSendFields(req, data{"b": ""}, []string{"c"})
}