graphql-engine/cli/internal/metadataobject/api_limits/api_limits_test.go
Aravind K P ca6605a036 cli: port metadataobject/apilimits to use internal/errors
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6397
Co-authored-by: Mohd Bilal <24944223+m-Bilal@users.noreply.github.com>
GitOrigin-RevId: ff266638f767da75da584f366d1a464ba6b70f0c
2022-10-27 08:28:23 +00:00

132 lines
2.9 KiB
Go

package apilimits
import (
"io/ioutil"
"testing"
goyaml "github.com/goccy/go-yaml"
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
"gopkg.in/yaml.v3"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMetadataObject_Build(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
tests := []struct {
id string
name string
fields fields
wantGolden string
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"t1",
"can build metadata json",
fields{
MetadataDir: "testdata/build_test/t1/metadata",
logger: logrus.New(),
},
"testdata/build_test/t1/want.golden.json",
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &MetadataObject{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := o.Build()
tt.assertErr(t, err)
if !tt.wantErr {
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 TestMetadataObject_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
assertErr require.ErrorAssertionFunc
}{
{
"t1",
"can export metadata",
fields{
MetadataDir: "testdata/export_test",
logger: logrus.New(),
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/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{
"testdata/export_test/api_limits.yaml": []byte(`disabled: false
rate_limit:
per_role: {}
global:
unique_params: IP
max_reqs_per_min: 1
`)},
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &MetadataObject{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := o.Export(tt.args.metadata)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
for k, v := range got {
assert.Contains(t, tt.want, k)
assert.Equalf(t, string(tt.want[k]), string(v), "%v", k)
}
}
})
}
}