Skip to content
quarry Artifact [40d7a4649b]
Login

Artifact [40d7a4649b]

Artifact 40d7a4649b362b748d4b3246a3514cf8e704f29efef3e7ed2b64dc4444cc7f9f:


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)
	}
}