|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func expectPanic(t *testing.T, message string) func() { |
| 10 | + return func() { |
| 11 | + r := recover() |
| 12 | + if r == nil { |
| 13 | + t.Errorf("expected panic: %s", message) |
| 14 | + } |
| 15 | + if str, ok := r.(string); ok && str != message { |
| 16 | + t.Errorf("expected panic: %s, got: %v", message, r) |
| 17 | + } |
| 18 | + if err, ok := r.(error); ok && err.Error() != message { |
| 19 | + t.Errorf("expected panic: %s, got: %v", message, r) |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestSetForceSendFields_RequestMustBePointer(t *testing.T) { |
| 25 | + defer expectPanic(t, "request argument to setForceSendFields must be a pointer")() |
| 26 | + |
| 27 | + SetForceSendFields(1, nil, nil) |
| 28 | +} |
| 29 | + |
| 30 | +type noForceSendFields struct { |
| 31 | + A string `json:"a"` |
| 32 | +} |
| 33 | + |
| 34 | +func TestSetForceSendFields_RequestMustHaveForceSendFields(t *testing.T) { |
| 35 | + defer expectPanic(t, "request argument to setForceSendFields must have ForceSendFields field")() |
| 36 | + |
| 37 | + SetForceSendFields(&noForceSendFields{}, nil, nil) |
| 38 | +} |
| 39 | + |
| 40 | +type incorrectForceSendFields struct { |
| 41 | + A string `json:"a"` |
| 42 | + ForceSendFields int `json:"force_send_fields"` |
| 43 | +} |
| 44 | + |
| 45 | +func TestSetForceSendFields_RequestMustHaveForceSendFieldsWithCorrectType(t *testing.T) { |
| 46 | + defer expectPanic(t, "request argument to setForceSendFields must have ForceSendFields field of type []string (got int)")() |
| 47 | + |
| 48 | + SetForceSendFields(&incorrectForceSendFields{}, nil, nil) |
| 49 | +} |
| 50 | + |
| 51 | +type withForceSendFields struct { |
| 52 | + A string `json:"a"` |
| 53 | + B string `json:"-"` |
| 54 | + ForceSendFields []string `json:"force_send_fields"` |
| 55 | +} |
| 56 | + |
| 57 | +func TestSetForceSendFields_NoFields(t *testing.T) { |
| 58 | + req := &withForceSendFields{} |
| 59 | + SetForceSendFields(req, nil, nil) |
| 60 | + assert.Len(t, req.ForceSendFields, 0) |
| 61 | +} |
| 62 | + |
| 63 | +func TestSetForceSendFields_ForceAWhenPresent(t *testing.T) { |
| 64 | + req := &withForceSendFields{} |
| 65 | + SetForceSendFields(req, data{"a": ""}, []string{"a"}) |
| 66 | + assert.Len(t, req.ForceSendFields, 1) |
| 67 | + assert.Equal(t, "A", req.ForceSendFields[0]) |
| 68 | +} |
| 69 | + |
| 70 | +func TestSetForceSendFields_DoNotForceAWhenAbsent(t *testing.T) { |
| 71 | + req := &withForceSendFields{} |
| 72 | + SetForceSendFields(req, data{}, []string{"a"}) |
| 73 | + assert.Len(t, req.ForceSendFields, 0) |
| 74 | +} |
| 75 | + |
| 76 | +func TestSetForceSendFields_DoNotDuplicate(t *testing.T) { |
| 77 | + req := &withForceSendFields{ForceSendFields: []string{"A"}} |
| 78 | + SetForceSendFields(req, data{"a": ""}, []string{"a"}) |
| 79 | + assert.Len(t, req.ForceSendFields, 1) |
| 80 | + assert.Equal(t, "A", req.ForceSendFields[0]) |
| 81 | +} |
| 82 | + |
| 83 | +func TestSetForceSendFields_CannotForceNonJsonFields(t *testing.T) { |
| 84 | + defer expectPanic(t, "unexpected field b not found in request structure, expected one of: a")() |
| 85 | + req := &withForceSendFields{} |
| 86 | + SetForceSendFields(req, data{"b": ""}, []string{"b"}) |
| 87 | +} |
| 88 | + |
| 89 | +func TestSetForceSendFields_CannotForceUnknownFields(t *testing.T) { |
| 90 | + defer expectPanic(t, "unexpected field c not found in request structure, expected one of: a")() |
| 91 | + req := &withForceSendFields{} |
| 92 | + SetForceSendFields(req, data{"b": ""}, []string{"c"}) |
| 93 | +} |
0 commit comments