postgresql tests

This commit is contained in:
Murat Kabilov 2017-05-26 12:44:44 +02:00
parent 0bfb81eb4f
commit 1708f7b8e0
1 changed files with 123 additions and 74 deletions

View File

@ -1,8 +1,8 @@
package spec package spec
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@ -104,7 +104,31 @@ var wrongMaintenanceWindows = [][]byte{
[]byte(`"Mon:00:00"`), []byte(`"Mon:00:00"`),
} }
var cl = []byte(`{ var unmarshalCluster = []struct {
in []byte
out Postgresql
}{{
[]byte(`{
"kind": "Postgresql","apiVersion": "acid.zalan.do/v1",
"metadata": {"name": "acid-testcluster1"}, "spec": {"teamId": 100}}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
Name: "acid-testcluster1",
},
Status: ClusterStatusInvalid,
Error: &json.UnmarshalTypeError{
Value: "number",
Type: reflect.TypeOf(""),
Offset: 126,
Struct: "PostgresSpec",
Field: "teamId",
},
}},
{[]byte(`{
"kind": "Postgresql", "kind": "Postgresql",
"apiVersion": "acid.zalan.do/v1", "apiVersion": "acid.zalan.do/v1",
"metadata": { "metadata": {
@ -164,9 +188,8 @@ var cl = []byte(`{
"Sat:00:00-Sat:04:00" "Sat:00:00-Sat:04:00"
] ]
} }
}`) }`),
Postgresql{
var clExp = Postgresql{
TypeMeta: unversioned.TypeMeta{ TypeMeta: unversioned.TypeMeta{
Kind: "Postgresql", Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1", APIVersion: "acid.zalan.do/v1",
@ -222,10 +245,31 @@ var clExp = Postgresql{
}, },
ClusterName: "testcluster1", ClusterName: "testcluster1",
}, },
Status: "",
Error: nil, Error: nil,
}},
{
[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "teapot-testcluster1"}, "spec": {"teamId": "acid"}}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
Name: "teapot-testcluster1",
},
Spec: PostgresSpec{TeamID: "acid"},
Status: ClusterStatusInvalid,
Error: errors.New("name must match {TEAM}-{NAME} format"),
}},
} }
var invalidClusterSpec = []struct {
in []byte
err error
}{{[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1"`),
errors.New("unexpected end of JSON input"),
}}
func mustParseTime(s string) time.Time { func mustParseTime(s string) time.Time {
v, err := time.Parse("15:04", s) v, err := time.Parse("15:04", s)
if err != nil { if err != nil {
@ -323,25 +367,30 @@ func TestUnmarshalMaintWindowsErrs(t *testing.T) {
} }
} }
func TestPostgresqlUnmarshal(t *testing.T) { func TestPostgresUnmarshal(t *testing.T) {
for _, tt := range unmarshalCluster {
var cluster Postgresql var cluster Postgresql
err := cluster.UnmarshalJSON(cl) err := cluster.UnmarshalJSON(tt.in)
if err != nil { if err != nil {
t.Errorf("Unmarshal Error: %v", err) t.Errorf("Unmarshal Error: %v", err)
} }
if !reflect.DeepEqual(cluster, clExp) { if !reflect.DeepEqual(cluster, tt.out) {
t.Errorf("Expected Postgresql: %#v, got %#v", clExp, cluster) t.Errorf("Expected Postgresql: %#v, got %#v", tt.out, cluster)
}
} }
} }
func TestPostgresqlMarshal(t *testing.T) { func TestInvalidPostgresUnmarshal(t *testing.T) {
m, err := json.Marshal(clExp) for _, tt := range invalidClusterSpec {
if err != nil { var cluster Postgresql
t.Errorf("Marshal Error: %v", err) err := cluster.UnmarshalJSON(tt.in)
if err == nil {
t.Errorf("Error expected for %s", string(tt.in))
} }
if bytes.Compare(m, cl) != 0 { if err.Error() != tt.err.Error() {
t.Errorf("Expected postgresql marshal: %s, got %s", string(cl), string(m)) t.Errorf("Unmarshal error expected: %v, got: %v", tt.err, err)
}
} }
} }