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
|
// +build go1.7
package csm
import (
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
)
func TestTruncateString(t *testing.T) {
cases := map[string]struct {
Val string
Len int
Expect string
}{
"no change": {
Val: "123456789", Len: 10,
Expect: "123456789",
},
"max len": {
Val: "1234567890", Len: 10,
Expect: "1234567890",
},
"too long": {
Val: "12345678901", Len: 10,
Expect: "1234567890",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
v := c.Val
actual := truncateString(&v, c.Len)
if e, a := c.Val, v; e != a {
t.Errorf("expect input value not to change, %v, %v", e, a)
}
if e, a := c.Expect, *actual; e != a {
t.Errorf("expect %v, got %v", e, a)
}
})
}
}
func TestMetric_SetException(t *testing.T) {
cases := map[string]struct {
Exc metricException
Expect metric
Final bool
}{
"aws exc": {
Exc: awsException{
requestException{exception: "abc", message: "123"},
},
Expect: metric{
AWSException: aws.String("abc"),
AWSExceptionMessage: aws.String("123"),
},
},
"sdk exc": {
Exc: sdkException{
requestException{exception: "abc", message: "123"},
},
Expect: metric{
SDKException: aws.String("abc"),
SDKExceptionMessage: aws.String("123"),
},
},
"final aws exc": {
Exc: awsException{
requestException{exception: "abc", message: "123"},
},
Expect: metric{
FinalAWSException: aws.String("abc"),
FinalAWSExceptionMessage: aws.String("123"),
},
Final: true,
},
"final sdk exc": {
Exc: sdkException{
requestException{exception: "abc", message: "123"},
},
Expect: metric{
FinalSDKException: aws.String("abc"),
FinalSDKExceptionMessage: aws.String("123"),
},
Final: true,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var m metric
if c.Final {
m.SetFinalException(c.Exc)
} else {
m.SetException(c.Exc)
}
if e, a := c.Expect, m; !reflect.DeepEqual(e, a) {
t.Errorf("expect:\n%#v\nactual:\n%#v\n", e, a)
}
})
}
}
|