2021-04-28 21:05:31 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dim/types"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFieldListToSelect(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-05-03 21:14:08 +02:00
|
|
|
table string
|
2021-04-28 21:05:31 +02:00
|
|
|
names types.FieldList
|
|
|
|
mapping map[string]string
|
|
|
|
out string
|
|
|
|
}{
|
2021-05-03 21:14:08 +02:00
|
|
|
{"foo", types.NewFieldList("name"), map[string]string{"name": "name"}, "name as name"},
|
2021-04-28 21:05:31 +02:00
|
|
|
{
|
2021-05-03 21:14:08 +02:00
|
|
|
"foo",
|
2021-04-28 21:05:31 +02:00
|
|
|
types.NewFieldList("foo", "bar", "baz"),
|
|
|
|
map[string]string{"foo": "c.foo", "bar": "b.bar"},
|
2021-05-03 21:14:08 +02:00
|
|
|
"b.bar as bar,foo.attributes->'baz' as baz,c.foo as foo",
|
2021-04-28 21:05:31 +02:00
|
|
|
},
|
|
|
|
{
|
2021-05-03 21:14:08 +02:00
|
|
|
"foo",
|
2021-04-28 21:05:31 +02:00
|
|
|
types.NewFieldList("subnets", "vlan", "name"),
|
|
|
|
map[string]string{"subnets": "s.subnets", "vlan": "vlan.vlan_id", "name": "p.name"},
|
|
|
|
"p.name as name,s.subnets as subnets,vlan.vlan_id as vlan",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-05-03 21:14:08 +02:00
|
|
|
out := FieldListToSelect(test.table, test.names, test.mapping)
|
2021-04-28 21:05:31 +02:00
|
|
|
if out != test.out {
|
|
|
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameToAttrPath(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-05-03 21:14:08 +02:00
|
|
|
table string
|
|
|
|
in string
|
|
|
|
out string
|
2021-04-28 21:05:31 +02:00
|
|
|
}{
|
2021-05-03 21:14:08 +02:00
|
|
|
{"zoo", "foo", `zoo.attributes->'foo'`},
|
|
|
|
{"zoo", "foo.bar", `zoo.attributes->'foo'->'bar'`},
|
|
|
|
{"zoo", "", `zoo.attributes`},
|
2021-04-28 21:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2021-05-03 21:14:08 +02:00
|
|
|
out := nameToAttrPath(test.table, test.in)
|
2021-04-28 21:05:31 +02:00
|
|
|
if test.out != out {
|
|
|
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 10:17:05 +02:00
|
|
|
|
|
|
|
func TestFieldMapToUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
table string
|
|
|
|
vals types.FieldMap
|
|
|
|
mapping map[string]string
|
|
|
|
set string // expected set clause
|
|
|
|
args []interface{} // expected arguments
|
|
|
|
}{
|
|
|
|
{ // check for normal field mapping
|
|
|
|
"zoo",
|
|
|
|
types.NewFieldMap(map[string]interface{}{"key": "value"}),
|
|
|
|
map[string]string{"key": "field"},
|
|
|
|
"zoo.field = $1",
|
|
|
|
[]interface{}{"value"},
|
|
|
|
},
|
|
|
|
{ // generate attributes field
|
|
|
|
"zoo",
|
|
|
|
types.NewFieldMap(map[string]interface{}{"key2": "value"}),
|
|
|
|
map[string]string{"key": "field"},
|
|
|
|
"zoo.attributes->'key2' = $1",
|
|
|
|
[]interface{}{"value"},
|
|
|
|
},
|
|
|
|
{ // mixed mapped and unmapped field
|
|
|
|
"zoo",
|
|
|
|
types.NewFieldMap(map[string]interface{}{"key2": "value", "key": "value"}),
|
|
|
|
map[string]string{"key": "field"},
|
|
|
|
"zoo.attributes->'key2' = $1,zoo.field = $2",
|
|
|
|
[]interface{}{"value", "value"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
set, args := FieldMapToUpdate(test.table, test.vals, test.mapping)
|
|
|
|
if set != test.set {
|
|
|
|
t.Errorf("expected set clause `%s`, got `%s`", test.set, set)
|
|
|
|
}
|
|
|
|
for i, arg := range args {
|
|
|
|
if arg != test.args[i] {
|
|
|
|
t.Errorf("expected argument at pos %d to be %#v, but was %#v", i, test.args[i], arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|