graphql-engine/cli/internal/statestore/statestore_test.go
Aravind K P 22ec874069 cli: port statestore/settings to use internal/errors
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6492
Co-authored-by: Mohd Bilal <24944223+m-Bilal@users.noreply.github.com>
GitOrigin-RevId: 409b220908d773cc6b45bc7ebe11a6ea950e9d35
2022-10-28 07:51:58 +00:00

124 lines
2.4 KiB
Go

package statestore
import (
"io/ioutil"
"testing"
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
"github.com/hasura/graphql-engine/cli/v2/internal/hasura/catalogstate"
"github.com/hasura/graphql-engine/cli/v2/internal/httpc"
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClientCatalogState_GetCLIState(t *testing.T) {
port, teardown := testutil.StartHasura(t, testutil.HasuraDockerImage)
defer teardown()
type fields struct {
Client *httpc.Client
path string
}
tests := []struct {
name string
fields fields
want CLIState
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"can get catalog state",
fields{
Client: testutil.NewHttpcClient(t, port, nil),
path: "v1/metadata",
},
CLIState{
Migrations: MigrationsState{
"test": {
"123": true,
},
},
},
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewCLICatalogState(catalogstate.New(tt.fields.Client, tt.fields.path))
_, err := c.Set(CLIState{
Migrations: MigrationsState{
"test": {
"123": true,
},
},
})
assert.NoError(t, err)
got, err := c.Get()
tt.assertErr(t, err)
if tt.wantErr {
return
}
assert.Equal(t, tt.want, *got)
})
}
}
func TestCLICatalogState_Set(t *testing.T) {
port, teardown := testutil.StartHasura(t, testutil.HasuraDockerImage)
defer teardown()
type fields struct {
client hasura.CatalogStateOperations
}
type args struct {
state CLIState
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"can set CLI state",
fields{
client: catalogstate.New(testutil.NewHttpcClient(t, port, nil), "v1/metadata"),
},
args{
state: CLIState{
Migrations: MigrationsState{
"test": map[string]bool{
"123": false,
},
},
Settings: nil,
},
},
`{
"message": "success"
}`,
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CLICatalogState{
client: tt.fields.client,
}
got, err := c.Set(tt.args.state)
tt.assertErr(t, err)
if tt.wantErr {
return
}
b, err := ioutil.ReadAll(got)
assert.NoError(t, err)
assert.Equal(t, tt.want, string(b))
})
}
}