-
Notifications
You must be signed in to change notification settings - Fork 6
/
hook_upload_test.go
77 lines (65 loc) · 1.69 KB
/
hook_upload_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
package itchio
import (
"encoding/json"
"testing"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)
func Test_UploadHook(t *testing.T) {
ref := Upload{
ID: 123,
Demo: true,
Platforms: Platforms{
OSX: ArchitecturesAll,
Windows: ArchitecturesAll,
},
}
marshalledTraits := []byte(`{
"id": 123,
"traits": ["demo", "p_osx", "p_windows"]
}`)
marshalledSane := []byte(`{
"id": 123,
"demo": true,
"platforms": {"osx": "all", "windows": "all"}
}`)
{
intermediateTraits := make(map[string]interface{})
err := json.Unmarshal(marshalledTraits, &intermediateTraits)
assert.NoError(t, err)
var decodedTraits Upload
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
DecodeHook: UploadHookFunc,
WeaklyTypedInput: true,
Result: &decodedTraits,
})
assert.NoError(t, err)
err = dec.Decode(intermediateTraits)
assert.NoError(t, err)
assert.EqualValues(t, ref, decodedTraits)
}
{
intermediateSane := make(map[string]interface{})
err := json.Unmarshal(marshalledSane, &intermediateSane)
assert.NoError(t, err)
var decodedSane Upload
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
DecodeHook: UploadHookFunc,
WeaklyTypedInput: true,
Result: &decodedSane,
})
assert.NoError(t, err)
err = dec.Decode(intermediateSane)
assert.NoError(t, err)
assert.EqualValues(t, ref, decodedSane)
}
// -------------
bs, err := json.Marshal(ref)
assert.NoError(t, err)
var unmarshalled Upload
err = json.Unmarshal(bs, &unmarshalled)
assert.NoError(t, err)
assert.EqualValues(t, ref, unmarshalled)
}