Fix unit test, it should return after any error is returned from Decode
function whether expected or not, rather than continue and try to compare invalid decoded results. Extend the unit test slightly to check the header. Remove go-test/deep dependency in favour of standard library reflect package.
This commit is contained in:
committed by
Jack Christensen
parent
9275da562f
commit
3d9a54f092
+2
-2
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/jackc/pgio"
|
"github.com/jackc/pgio"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FunctionCall struct{
|
type FunctionCall struct {
|
||||||
Function uint32
|
Function uint32
|
||||||
ArgFormatCodes []uint16
|
ArgFormatCodes []uint16
|
||||||
Arguments [][]byte
|
Arguments [][]byte
|
||||||
@@ -55,7 +55,7 @@ func (dst *FunctionCall) Decode(src []byte) error {
|
|||||||
arguments[i] = nil
|
arguments[i] = nil
|
||||||
} else {
|
} else {
|
||||||
// The value of the argument, in the format indicated by the associated format code. n is the above length.
|
// The value of the argument, in the format indicated by the associated format code. n is the above length.
|
||||||
argumentValue := src[rp:rp+argumentLength]
|
argumentValue := src[rp : rp+argumentLength]
|
||||||
rp += argumentLength
|
rp += argumentLength
|
||||||
arguments[i] = argumentValue
|
arguments[i] = argumentValue
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-8
@@ -1,7 +1,8 @@
|
|||||||
package pgproto3
|
package pgproto3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-test/deep"
|
"encoding/binary"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ func TestFunctionCall_EncodeDecode(t *testing.T) {
|
|||||||
fields fields
|
fields fields
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"foo", fields{uint32(123), []uint16{0, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(0)}, false},
|
{"valid", fields{uint32(123), []uint16{0, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(1)}, false},
|
||||||
{"invalid format code", fields{uint32(123), []uint16{2, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(0)}, true},
|
{"invalid format code", fields{uint32(123), []uint16{2, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(0)}, true},
|
||||||
{"invalid result format code", fields{uint32(123), []uint16{1, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(2)}, true},
|
{"invalid result format code", fields{uint32(123), []uint16{1, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(2)}, true},
|
||||||
}
|
}
|
||||||
@@ -30,14 +31,31 @@ func TestFunctionCall_EncodeDecode(t *testing.T) {
|
|||||||
ResultFormatCode: tt.fields.ResultFormatCode,
|
ResultFormatCode: tt.fields.ResultFormatCode,
|
||||||
}
|
}
|
||||||
encoded := src.Encode([]byte{})
|
encoded := src.Encode([]byte{})
|
||||||
decoded := &FunctionCall{}
|
dst := &FunctionCall{}
|
||||||
err := decoded.Decode(encoded[5:])
|
// Check the header
|
||||||
if (err != nil) != tt.wantErr {
|
msgTypeCode := encoded[0]
|
||||||
t.Errorf("FunctionCall.Decode() error = %v, wantErr %v", err, tt.wantErr)
|
if msgTypeCode != 'F' {
|
||||||
|
t.Errorf("msgTypeCode %v should be 'F'", msgTypeCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if diff := deep.Equal(src, decoded); diff != nil {
|
// Check length, does not include type code character
|
||||||
t.Error(diff)
|
l := binary.BigEndian.Uint32(encoded[1:5])
|
||||||
|
if int(l) != (len(encoded) - 1) {
|
||||||
|
t.Errorf("Incorrect message length, got = %v, wanted = %v", l, len(encoded))
|
||||||
|
}
|
||||||
|
// Check decoding works as expected
|
||||||
|
err := dst.Decode(encoded[5:])
|
||||||
|
if err != nil {
|
||||||
|
if !tt.wantErr {
|
||||||
|
t.Errorf("FunctionCall.Decode() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(src, dst) {
|
||||||
|
t.Error("difference after encode / decode cycle")
|
||||||
|
t.Errorf("src = %v", src)
|
||||||
|
t.Errorf("dst = %v", dst)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ module github.com/jackc/pgproto3/v2
|
|||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-test/deep v1.0.8
|
|
||||||
github.com/jackc/chunkreader/v2 v2.0.0
|
github.com/jackc/chunkreader/v2 v2.0.0
|
||||||
github.com/jackc/pgio v1.0.0
|
github.com/jackc/pgio v1.0.0
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
|
|
||||||
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
|
|
||||||
github.com/jackc/chunkreader/v2 v2.0.0 h1:DUwgMQuuPnS0rhMXenUtZpqZqrR/30NWY+qQvTpSvEs=
|
github.com/jackc/chunkreader/v2 v2.0.0 h1:DUwgMQuuPnS0rhMXenUtZpqZqrR/30NWY+qQvTpSvEs=
|
||||||
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
|
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
|
||||||
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
|
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
|
||||||
@@ -11,7 +9,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|||||||
Reference in New Issue
Block a user