130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
package query
|
|
|
|
import (
|
|
"dim/types"
|
|
|
|
"testing"
|
|
)
|
|
|
|
func TestFieldListToSelect(t *testing.T) {
|
|
tests := []struct {
|
|
table string
|
|
names types.FieldList
|
|
mapping map[string]string
|
|
out string
|
|
}{
|
|
{
|
|
"foo",
|
|
types.NewFieldList("name"),
|
|
map[string]string{"name": "name"},
|
|
`name as "name"`,
|
|
},
|
|
{
|
|
"foo",
|
|
types.NewFieldList("foo", "bar", "baz"),
|
|
map[string]string{"foo": "c.foo", "bar": "b.bar"},
|
|
`b.bar as "bar",foo.attributes->>'baz' as "baz",c.foo as "foo"`,
|
|
},
|
|
{
|
|
"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.table, test.names, test.mapping)
|
|
if out != test.out {
|
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestJSONSelect(t *testing.T) {
|
|
tests := []struct {
|
|
table string
|
|
fields map[string]string
|
|
out string
|
|
}{
|
|
{
|
|
"zoo",
|
|
map[string]string{"foo": "foo", "bar": "bar", "zoo": "f.bar"},
|
|
"jsonb_build_object('foo',foo,'bar',bar,'zoo',f.bar) || zoo.attributes",
|
|
},
|
|
{
|
|
"f",
|
|
map[string]string{"foo": "l.attributes->>'foo'", "zoo": "a.zoo"},
|
|
"jsonb_build_object('foo',l.attributes->>'foo','zoo',a.zoo) || f.attributes",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
out := FieldsToJSON(test.table, test.fields)
|
|
if out != test.out {
|
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNameToAttrPath(t *testing.T) {
|
|
tests := []struct {
|
|
table string
|
|
in string
|
|
out string
|
|
}{
|
|
{"zoo", "foo", `zoo.attributes->>'foo'`},
|
|
{"zoo", "foo.bar", `zoo.attributes->'foo'->>'bar'`},
|
|
{"zoo", "", `zoo.attributes`},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
out := nameToAttrPath(test.table, test.in)
|
|
if test.out != out {
|
|
t.Errorf("expected `%s`, got `%s`", test.out, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFieldMapToUpdate(t *testing.T) {
|
|
tests := []struct {
|
|
vals types.FieldMap
|
|
mapping map[string]string
|
|
set string // expected set clause
|
|
args []interface{} // expected arguments
|
|
}{
|
|
{ // check for normal field mapping
|
|
types.NewFieldMap(map[string]interface{}{"key": "value"}),
|
|
map[string]string{"key": "field"},
|
|
"field = $1",
|
|
[]interface{}{"value"},
|
|
},
|
|
{ // generate attributes field
|
|
types.NewFieldMap(map[string]interface{}{"key2": "value"}),
|
|
map[string]string{"key": "field"},
|
|
"attributes = jsonb_strip_nulls(attributes || $1::jsonb)",
|
|
[]interface{}{`{"key2":"value"}`},
|
|
},
|
|
{ // mixed mapped and unmapped field
|
|
types.NewFieldMap(map[string]interface{}{"key2": "value", "key": "value"}),
|
|
map[string]string{"key": "field"},
|
|
"field = $1,attributes = jsonb_strip_nulls(attributes || $2::jsonb)",
|
|
[]interface{}{"value", `{"key2":"value"}`},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
set, args, err := FieldMapToUpdate(test.vals, test.mapping)
|
|
if err != nil {
|
|
t.Errorf("could not map to update: %s", err)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|