forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_test.go
65 lines (52 loc) · 1.19 KB
/
translate_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
package proto
import (
"testing"
"time"
"github.com/gogo/protobuf/types"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/require"
)
type pbTSWrapper struct {
Timestamp *types.Timestamp
}
type timeTSWrapper struct {
Timestamp time.Time
}
func TestHookPBTimestampToTime(t *testing.T) {
in := pbTSWrapper{
Timestamp: &types.Timestamp{
Seconds: 1000,
Nanos: 42,
},
}
expected := timeTSWrapper{
Timestamp: time.Unix(1000, 42),
}
var actual timeTSWrapper
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: HookPBTimestampToTime,
Result: &actual,
})
require.NoError(t, err)
require.NoError(t, decoder.Decode(in))
require.Equal(t, expected, actual)
}
func TestHookTimeToPBTimestamp(t *testing.T) {
in := timeTSWrapper{
Timestamp: time.Unix(999999, 123456),
}
expected := pbTSWrapper{
Timestamp: &types.Timestamp{
Seconds: 999999,
Nanos: 123456,
},
}
var actual pbTSWrapper
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: HookTimeToPBTimestamp,
Result: &actual,
})
require.NoError(t, err)
require.NoError(t, decoder.Decode(in))
require.Equal(t, expected, actual)
}