Fix empty string int marshalling in go 1.14

This commit is contained in:
Paul Tyng
2020-03-26 16:13:19 -04:00
parent d076e78005
commit f74d29bd54
5 changed files with 102 additions and 38 deletions

53
unifi/network_test.go Normal file
View File

@@ -0,0 +1,53 @@
package unifi_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/paultyng/go-unifi/unifi"
)
func TestNetworkUnmarshalJSON(t *testing.T) {
for n, c := range map[string]struct {
expected unifi.Network
json string
}{
"int vlan": {
expected: unifi.Network{VLAN: 1},
json: `{ "vlan": 1 }`,
},
"string vlan": {
expected: unifi.Network{VLAN: 1},
json: `{ "vlan": "1" }`,
},
"empty string vlan": {
expected: unifi.Network{VLAN: 0},
json: `{ "vlan": "" }`,
},
"int dhcpd_leasetime": {
expected: unifi.Network{DHCPDLeaseTime: 1},
json: `{ "dhcpd_leasetime": 1 }`,
},
"string dhcpd_leasetime": {
expected: unifi.Network{DHCPDLeaseTime: 1},
json: `{ "dhcpd_leasetime": "1" }`,
},
"empty string dhcpd_leasetime": {
expected: unifi.Network{DHCPDLeaseTime: 0},
json: `{ "dhcpd_leasetime": "" }`,
},
} {
t.Run(n, func(t *testing.T) {
var actual unifi.Network
err := json.Unmarshal(([]byte)(c.json), &actual)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(c.expected, actual) {
t.Fatalf("not equal:\nexpected: %#v\nactual: %#v", c.expected, actual)
}
})
}
}