package ops
import (
"net/url"
"testing"
)
type schemaIn struct {
Repo string `json:"repo"`
Fields map[string]string `json:"fields" validate:"max=2"`
}
// A tracker's schema is the repository's, so the set of keys is not
// knowable at compile time and cannot be struct fields. The dotted key
// is the one exception to the flat-struct rule.
func TestAMapFieldTakesDottedKeys(t *testing.T) {
var in schemaIn
if err := DecodeValues(url.Values{
"repo": {"quarry"},
"fields.assignee": {"dan"},
"fields.closer": {"lou"},
}, &in); err != nil {
t.Fatal(err)
}
if in.Fields["assignee"] != "dan" || in.Fields["closer"] != "lou" {
t.Errorf("FAIL: fields = %v", in.Fields)
}
if in.Repo != "quarry" {
t.Errorf("FAIL: the flat fields beside it broke: repo = %q", in.Repo)
}
}
// No dotted keys means no map at all, not an empty one: a request that
// named nothing must be distinguishable from one that cleared
// everything.
func TestNoDottedKeysLeavesTheMapNil(t *testing.T) {
var in schemaIn
if err := DecodeValues(url.Values{"repo": {"quarry"}}, &in); err != nil {
t.Fatal(err)
}
if in.Fields != nil {
t.Errorf("FAIL: fields = %v, want nil", in.Fields)
}
}
// A bare prefix asked for nothing. Storing it under "" would make the
// op invent a message for a key that does not exist.
func TestABarePrefixIsSkipped(t *testing.T) {
var in schemaIn
if err := DecodeValues(url.Values{"fields.": {"x"}}, &in); err != nil {
t.Fatal(err)
}
if len(in.Fields) != 0 {
t.Errorf("FAIL: fields = %v", in.Fields)
}
}
// max on a map counts its keys, which is what a limit on one means.
func TestMaxOnAMapCountsKeys(t *testing.T) {
in := schemaIn{Fields: map[string]string{"a": "1", "b": "2", "c": "3"}}
if err := Validate(in); err == nil {
t.Error("FAIL: three keys passed a max of two")
}
in.Fields = map[string]string{"a": "1", "b": "2"}
if err := Validate(in); err != nil {
t.Errorf("FAIL: two keys refused by a max of two: %v", err)
}
}