1
0
Fork 0
secondly/field_test.go

86 lines
2.1 KiB
Go
Raw Normal View History

2015-08-29 12:41:20 +00:00
package secondly
2015-08-29 12:25:47 +00:00
import (
"testing"
)
func TestExtractFields(t *testing.T) {
c := testConf{
2015-08-29 12:41:20 +00:00
AppName: "Secondly",
2015-08-29 12:25:47 +00:00
Version: 1.1,
Database: testDatabaseConf{
Adapter: "mysql",
Host: "localhost",
Port: 3306,
},
}
2015-08-29 14:32:39 +00:00
fields := indexFields(extractFields(c, ""))
2015-08-29 12:29:56 +00:00
testField := func(fname, kind string, val interface{}) {
if f, ok := fields[fname]; ok {
2015-08-29 12:25:47 +00:00
if f.Kind != kind {
2015-08-29 12:29:56 +00:00
t.Errorf("%s expected to be of kind %q, got %q", fname, kind, f.Kind)
2015-08-29 12:25:47 +00:00
}
2015-08-29 14:35:51 +00:00
if f.Value != val {
t.Errorf("%s expected to have value %q, got %q", fname, val, f.Value)
2015-08-29 12:25:47 +00:00
}
} else {
2015-08-29 12:29:56 +00:00
t.Errorf("Missing %s field", fname)
2015-08-29 12:25:47 +00:00
}
}
testField("AppName", "string", c.AppName)
testField("Version", "float32", c.Version)
testField("Database.Adapter", "string", c.Database.Adapter)
testField("Database.Host", "string", c.Database.Host)
testField("Database.Port", "int", c.Database.Port)
}
2015-08-29 12:25:59 +00:00
func TestDiff(t *testing.T) {
c1 := testConf{
2015-08-29 12:41:20 +00:00
AppName: "Secondly",
2015-08-29 12:25:59 +00:00
Version: 1.3,
Database: testDatabaseConf{
Adapter: "mysql",
Host: "localhost",
Port: 3306,
},
}
c2 := testConf{
2015-08-29 12:41:20 +00:00
AppName: "Secondly",
2015-08-29 12:25:59 +00:00
Version: 2,
Database: testDatabaseConf{
Adapter: "postgresql",
Host: "localhost",
Port: 5432,
Username: "root",
},
}
d := diff(c1, c2)
2015-08-29 12:29:56 +00:00
testField := func(fname string, oldVal, newVal interface{}) {
if f, ok := d[fname]; ok {
2015-08-29 12:25:59 +00:00
if f[0] != oldVal {
t.Errorf("%s field old value was %q, not %q", oldVal, f[0])
}
if f[1] != newVal {
t.Errorf("%s field new value was %q, not %q", newVal, f[1])
}
} else {
2015-08-29 12:29:56 +00:00
t.Errorf("Expected %s field to have different values", fname)
2015-08-29 12:25:59 +00:00
}
}
unchangedFields := []string{"AppName", "Database.Host", "Database.Password"}
for _, f := range unchangedFields {
if _, ok := d[f]; ok {
t.Errorf("Expected %q field to be unchanged", f)
}
}
testField("Version", c1.Version, c2.Version)
testField("Database.Adapter", c1.Database.Adapter, c2.Database.Adapter)
testField("Database.Port", c1.Database.Port, c2.Database.Port)
testField("Database.Username", c1.Database.Username, c2.Database.Username)
}