Files
fid/form_test.go

155 lines
4.3 KiB
Go

package main
import (
"reflect"
"testing"
)
func testMeta() map[string]interface{} {
return map[string]interface{}{
"f_name": map[string]interface{}{"name": "Name", "type": "text", "mandatory": float64(1)},
"f_ip": map[string]interface{}{"name": "IP Number", "type": "text", "mandatory": float64(0)},
"l_dep": map[string]interface{}{"name": "Department", "type": "select", "mandatory": float64(1)},
"c_admin": map[string]interface{}{"name": "Self Admin", "type": "checkbox", "mandatory": float64(0)},
}
}
func testOpts() map[string][]option {
return map[string][]option{
"l_dep": {{"1", "AC"}, {"2", "CP"}},
}
}
// testOrder mimics the server's DB-column order (see api.go's Fields()),
// deliberately not alphabetical so tests catch a regression to sortedKeys.
func testOrder() []string {
return []string{"f_name", "l_dep", "f_ip", "c_admin"}
}
func TestBuildFormFieldsBlank(t *testing.T) {
ff := buildFormFields(testMeta(), testOrder(), testOpts(), nil)
if len(ff) != 4 {
t.Fatalf("got %d fields, want 4", len(ff))
}
want := []string{"f_name", "l_dep", "f_ip", "c_admin"}
var got []string
for _, f := range ff {
got = append(got, f.key)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("order = %v, want %v", got, want)
}
dep := ff[1]
if dep.kind != kindSelect || len(dep.options) != 3 || dep.options[0].name != "(none)" {
t.Fatalf("l_dep options = %+v", dep.options)
}
if dep.optIdx != 0 {
t.Fatalf("blank l_dep optIdx = %d, want 0 (none)", dep.optIdx)
}
}
func TestBuildFormFieldsPrefilled(t *testing.T) {
existing := map[string]interface{}{
"f_name": "twilightzone",
"f_ip": "141.14.142.157",
"l_dep": float64(2), // numeric id, as the real API returns for l_dep
"c_admin": float64(1),
}
ff := buildFormFields(testMeta(), testOrder(), testOpts(), existing)
byKey := map[string]*formField{}
for _, f := range ff {
byKey[f.key] = f
}
if got := string(byKey["f_name"].runes); got != "twilightzone" {
t.Errorf("f_name = %q", got)
}
if byKey["f_name"].cursor != len("twilightzone") {
t.Errorf("f_name cursor = %d, want at end", byKey["f_name"].cursor)
}
if !byKey["c_admin"].checked {
t.Errorf("c_admin should be checked")
}
dep := byKey["l_dep"]
if dep.options[dep.optIdx].id != "2" || dep.options[dep.optIdx].name != "CP" {
t.Errorf("l_dep selection = %+v", dep.options[dep.optIdx])
}
}
func TestGatherFieldsNew(t *testing.T) {
ff := buildFormFields(testMeta(), testOrder(), testOpts(), nil)
for _, f := range ff {
switch f.key {
case "f_name":
f.runes = []rune("newhost")
f.cursor = len(f.runes)
case "l_dep":
f.optIdx = 1 // "1" / AC
}
// f_ip and c_admin left at zero value
}
got := gatherFields(ff, nil)
want := map[string]interface{}{
"f_name": "newhost",
"l_dep": "1",
// f_ip empty -> omitted; c_admin false -> "0" is sent explicitly
"c_admin": "0",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("gatherFields(new) = %#v, want %#v", got, want)
}
}
func TestGatherFieldsEditOnlyChanged(t *testing.T) {
existing := map[string]interface{}{
"f_name": "twilightzone",
"f_ip": "141.14.142.157",
"l_dep": "2",
"c_admin": "1",
}
ff := buildFormFields(testMeta(), testOrder(), testOpts(), existing)
for _, f := range ff {
if f.key == "f_ip" {
f.runes = []rune("10.0.0.5")
f.cursor = len(f.runes)
}
}
got := gatherFields(ff, existing)
want := map[string]interface{}{"f_ip": "10.0.0.5"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("gatherFields(edit, one change) = %#v, want %#v", got, want)
}
}
func TestGatherFieldsEditNoChanges(t *testing.T) {
existing := map[string]interface{}{
"f_name": "twilightzone", "f_ip": "141.14.142.157", "l_dep": "2", "c_admin": "1",
}
ff := buildFormFields(testMeta(), testOrder(), testOpts(), existing)
got := gatherFields(ff, existing)
if len(got) != 0 {
t.Fatalf("gatherFields(edit, no changes) = %#v, want empty", got)
}
}
func TestDisplayValue(t *testing.T) {
ff := buildFormFields(testMeta(), testOrder(), testOpts(), nil)
for _, f := range ff {
switch f.key {
case "c_admin":
if got := displayValue(f); got != "[ ]" {
t.Errorf("unchecked checkbox = %q", got)
}
f.checked = true
if got := displayValue(f); got != "[x]" {
t.Errorf("checked checkbox = %q", got)
}
case "l_dep":
if got := displayValue(f); got != "< (none) >" {
t.Errorf("blank select = %q", got)
}
}
}
}