You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.1 KiB
53 lines
1.1 KiB
2 years ago
|
package query
|
||
|
|
||
|
import (
|
||
|
"dim/types"
|
||
|
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestFieldListToSelect(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
names types.FieldList
|
||
|
mapping map[string]string
|
||
|
out string
|
||
|
}{
|
||
|
{types.NewFieldList("name"), map[string]string{"name": "name"}, "name as name"},
|
||
|
{
|
||
|
types.NewFieldList("foo", "bar", "baz"),
|
||
|
map[string]string{"foo": "c.foo", "bar": "b.bar"},
|
||
|
"b.bar as bar,attributes->'baz' as baz,c.foo as foo",
|
||
|
},
|
||
|
{
|
||
|
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 {
|
||
|
out := FieldListToSelect(test.names, test.mapping)
|
||
|
if out != test.out {
|
||
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestNameToAttrPath(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
in string
|
||
|
out string
|
||
|
}{
|
||
|
{"foo", `attributes->'foo'`},
|
||
|
{"foo.bar", `attributes->'foo'->'bar'`},
|
||
|
{"", `attributes`},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
out := nameToAttrPath(test.in)
|
||
|
if test.out != out {
|
||
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
||
|
}
|
||
|
}
|
||
|
}
|