graphql-engine/cli/internal/metadataobject/querycollections/query_collections_test.go
Aravind K P 2ff0f25e08 cli: refactor metadataobject package
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3943
GitOrigin-RevId: 4cc8f67dd8b6ba0e78f1c35d415f89bd1b901705
2022-03-10 08:13:50 +00:00

194 lines
4.6 KiB
Go

package querycollections
import (
"io/ioutil"
"testing"
goyaml "github.com/goccy/go-yaml"
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestQueryCollectionConfig_Build(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
tests := []struct {
id string
name string
fields fields
wantGolden string
wantErr bool
}{
{
"t1",
"can build metadata json",
fields{
MetadataDir: "testdata/build_test/t1/metadata",
logger: logrus.New(),
},
"testdata/build_test/t1/want.golden.json",
false,
},
{
"t2",
"can build metadata json from empty file",
fields{
MetadataDir: "testdata/build_test/t2/metadata",
logger: logrus.New(),
},
"testdata/build_test/t2/want.golden.json",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &QueryCollectionConfig{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := q.Build()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
gotbs, err := yaml.Marshal(got)
assert.NoError(t, err)
jsonbs, err := goyaml.YAMLToJSON(gotbs)
assert.NoError(t, err)
// uncomment following lines to update golden file
//assert.NoError(t, ioutil.WriteFile(tt.wantGolden, jsonbs, os.ModePerm))
wantbs, err := ioutil.ReadFile(tt.wantGolden)
assert.NoError(t, err)
assert.Equal(t, string(wantbs), string(jsonbs))
}
})
}
}
func TestQueryCollectionConfig_Export(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
type args struct {
metadata map[string]yaml.Node
}
tests := []struct {
id string
name string
fields fields
args args
want map[string][]byte
wantErr bool
}{
{
"t1",
"can export metadata",
fields{
MetadataDir: "metadata",
logger: logrus.New(),
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t1/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
var v map[string]yaml.Node
assert.NoError(t, yaml.Unmarshal(yamlbs, &v))
return v
}(),
},
map[string][]byte{
"metadata/query_collections.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t1/want.query_collections.yaml")
assert.NoError(t, err)
return bs
}(),
},
false,
},
{
"t2",
"can export metadata when query collections is not present in metadata",
fields{
MetadataDir: "metadata",
logger: logrus.New(),
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t2/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
var v map[string]yaml.Node
assert.NoError(t, yaml.Unmarshal(yamlbs, &v))
return v
}(),
},
map[string][]byte{
"metadata/query_collections.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t2/want.query_collections.yaml")
assert.NoError(t, err)
return bs
}(),
},
false,
},
{
"t3",
"can export metadata correctly with multiline strings",
fields{
MetadataDir: "metadata",
logger: logrus.New(),
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t3/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
var v map[string]yaml.Node
assert.NoError(t, yaml.Unmarshal(yamlbs, &v))
return v
}(),
},
map[string][]byte{
"metadata/query_collections.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t3/want.query_collections.yaml")
assert.NoError(t, err)
return bs
}(),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &QueryCollectionConfig{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := q.Export(tt.args.metadata)
if tt.wantErr {
assert.Error(t, err)
} else {
for k, v := range got {
assert.Contains(t, tt.want, k)
// uncomment to update golden files
//assert.NoError(t, ioutil.WriteFile(fmt.Sprintf("testdata/export_test/%v/want.%v", tt.id, filepath.Base(k)), v, os.ModePerm))
assert.Equalf(t, string(tt.want[k]), string(v), "%v", k)
}
}
})
}
}