open-nomad/nomad/memdb/index_test.go
2015-06-07 19:38:01 -05:00

95 lines
1.6 KiB
Go

package memdb
import "testing"
type TestObject struct {
ID string
Foo string
Bar int
Baz string
Empty string
}
func testObj() *TestObject {
obj := &TestObject{
ID: "my-cool-obj",
Foo: "Testing",
Bar: 42,
Baz: "yep",
}
return obj
}
func TestStringFieldIndex_FromObject(t *testing.T) {
obj := testObj()
indexer := StringFieldIndex{"Foo", false}
ok, val, err := indexer.FromObject(obj)
if err != nil {
t.Fatalf("err: %v", err)
}
if string(val) != "Testing" {
t.Fatalf("bad: %s", val)
}
if !ok {
t.Fatalf("should be ok")
}
lower := StringFieldIndex{"Foo", true}
ok, val, err = lower.FromObject(obj)
if err != nil {
t.Fatalf("err: %v", err)
}
if string(val) != "testing" {
t.Fatalf("bad: %s", val)
}
if !ok {
t.Fatalf("should be ok")
}
badField := StringFieldIndex{"NA", true}
ok, val, err = badField.FromObject(obj)
if err == nil {
t.Fatalf("should get error")
}
emptyField := StringFieldIndex{"Empty", true}
ok, val, err = emptyField.FromObject(obj)
if err != nil {
t.Fatalf("err: %v", err)
}
if ok {
t.Fatalf("should not ok")
}
}
func TestStringFieldIndex_FromArgs(t *testing.T) {
indexer := StringFieldIndex{"Foo", false}
_, err := indexer.FromArgs()
if err == nil {
t.Fatalf("should get err")
}
_, err = indexer.FromArgs(42)
if err == nil {
t.Fatalf("should get err")
}
val, err := indexer.FromArgs("foo")
if err != nil {
t.Fatalf("err: %v", err)
}
if string(val) != "foo" {
t.Fatalf("foo")
}
lower := StringFieldIndex{"Foo", true}
val, err = lower.FromArgs("Foo")
if err != nil {
t.Fatalf("err: %v", err)
}
if string(val) != "foo" {
t.Fatalf("foo")
}
}