graphql-engine/cli/internal/metadataobject/rest_endpoints/rest_endpoints_test.go
Kali Vara Purushotham Santhati 489835eb36 cli: metadata apply,export enhancements
- modified the `dry-run` flag of metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata apply command.
- added new flag `o` which takes "json" or "yaml" as parameters for metadata export command. It outputs the metadata from server to stdout in form of  json or yaml and won't change the project's metadata.
- added deprecation warnings for `--from-file` flag
- added info message for describing change of behavior of `--dry-run` flag
- v3 metadata objects like `rest_endpoints` was also added to list of metadata objects in config v2 (fix)
- the order in which metadata objects are appended to metadata objects list matter when using `--dry-run` flag, refactored this order to match server metadata.
- `metadata apply` command can now accept json/yaml metadata from io pipe
- config v3 `metadata apply` didn't show any information to user when applied metadata is inconsistent, this is addressed.
- removed `github.com/ghodss/yaml` dependency from repo
- version metadata object was added twice during intialization (fix)

Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 2316f519eb40645efd86ffee2a85d3c90543ec17
2021-05-14 19:09:59 +00:00

180 lines
3.6 KiB
Go

package restendpoints
import (
"io/ioutil"
"testing"
goccyaml "github.com/goccy/go-yaml"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
func TestRestEndpointsConfig_Build(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
type args struct {
metadata *yaml.MapSlice
}
tests := []struct {
name string
fields fields
args args
want *yaml.MapSlice
wantErr bool
}{
{
"can build rest endpoints",
fields{
MetadataDir: "testdata/metadata",
logger: logrus.New(),
},
args{
metadata: new(yaml.MapSlice),
},
func() *yaml.MapSlice {
var v = yaml.MapItem{
Key: "rest_endpoints",
Value: []yaml.MapSlice{},
}
b, err := ioutil.ReadFile("testdata/metadata/rest_endpoints.yaml")
assert.NoError(t, err)
var obj []yaml.MapSlice
assert.NoError(t, yaml.Unmarshal(b, &obj))
v.Value = obj
m := yaml.MapSlice{}
m = append(m, v)
return &m
}(),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := &RestEndpointsConfig{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
err := tc.Build(tt.args.metadata)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, tt.args.metadata)
}
})
}
}
func TestRestEndpointsConfig_Export(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
type args struct {
metadata yaml.MapSlice
}
tests := []struct {
name string
fields fields
args args
want map[string][]byte
wantErr bool
}{
{
name: "can export rest endpoints",
fields: fields{
MetadataDir: "testdata/metadata",
logger: logrus.New(),
},
args: args{
metadata: func() yaml.MapSlice {
metadata := []byte(`{
"version": 3,
"query_collections": [
{
"name": "allowed-queries",
"definition": {
"queries": [
{
"name": "getAlbum",
"query": "query getAlbums($id: Int) {\n albums(where: {id: {_eq: $id}}) {\n id\n title\n }\n}"
}
]
}
}
],
"allowlist": [
{
"collection": "allowed-queries"
}
],
"rest_endpoints": [
{
"definition": {
"query": {
"collection_name": "allowed-queries",
"query_name": "getAlbum"
}
},
"url": "get_album/:id",
"methods": [
"GET"
],
"name": "getAlbum",
"comment": null
}
]
}`)
yamlb, err := goccyaml.JSONToYAML(metadata)
assert.NoError(t, err)
var v yaml.MapSlice
assert.NoError(t, yaml.Unmarshal(yamlb, &v))
return v
}(),
},
want: func() map[string][]byte {
m := map[string][]byte{
"testdata/metadata/rest_endpoints.yaml": []byte(`- definition:
query:
collection_name: allowed-queries
query_name: getAlbum
url: get_album/:id
methods:
- GET
name: getAlbum
comment: null
`),
}
return m
}(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := &RestEndpointsConfig{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := tc.Export(tt.args.metadata)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
gotS := map[string]string{}
wantS := map[string]string{}
for k, v := range got {
gotS[k] = string(v)
}
for k, v := range tt.want {
wantS[k] = string(v)
}
assert.Equal(t, wantS, gotS)
}
})
}
}