graphql-engine/cli/migrate/database/driver_test.go
Vishnu Bharathi 0016d3cad5 cli: avoid exporting hasura-specific schemas during hasura init
fixes https://github.com/hasura/graphql-engine/issues/8352

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4178
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: eff7067c34932435207eff794f9435356b55b666
2022-04-25 13:06:52 +00:00

175 lines
3.3 KiB
Go

package database
import (
"io"
"testing"
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
"github.com/sirupsen/logrus"
)
type mockDriver struct {
url string
}
func (m *mockDriver) Open(url string, isCmd bool, logger *logrus.Logger, hasuraOpts *HasuraOpts) (Driver, error) {
return &mockDriver{
url: url,
}, nil
}
func (m *mockDriver) Close() error {
return nil
}
func (m *mockDriver) UnLock() error {
return nil
}
func (m *mockDriver) Run(migration io.Reader, fileType, fileName string) error {
return nil
}
func (m *mockDriver) Lock() error {
return nil
}
func (m *mockDriver) Scan() error {
return nil
}
func (m *mockDriver) SetVersion(version int64, dirty bool) error {
return nil
}
func (m *mockDriver) Version() (version int64, dirty bool, err error) {
return 0, false, nil
}
func (m *mockDriver) Drop() error {
return nil
}
func (m *mockDriver) InsertVersion(version int64) error {
return nil
}
func (m *mockDriver) RemoveVersion(version int64) error {
return nil
}
func (m *mockDriver) First() (migrationVersion *MigrationVersion, ok bool) {
return nil, false
}
func (m *mockDriver) Last() (*MigrationVersion, bool) {
return nil, false
}
func (m *mockDriver) Next(version uint64) (migrationVersion *MigrationVersion, ok bool) {
return nil, false
}
func (m *mockDriver) Prev(version uint64) (prevVersion *MigrationVersion, ok bool) {
return nil, false
}
func (m *mockDriver) Read(version uint64) (ok bool) {
return false
}
func (m *mockDriver) PushToList(migration io.Reader, fileType string, list *CustomList) error {
return nil
}
func (m *mockDriver) Query(data interface{}) error {
return nil
}
func (m *mockDriver) ResetQuery() {
}
func (m *mockDriver) Squash(list *CustomList, ret chan<- interface{}) {
}
func (m *mockDriver) EnableCheckMetadataConsistency(enabled bool) {
}
func (m *mockDriver) ExportSchemaDump(includeSchemas []string, excludeSchemas []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) {
return nil, nil
}
func (m *mockDriver) GetSetting(name string) (value string, err error) {
return "", nil
}
func (m *mockDriver) UpdateSetting(name string, value string) error {
return nil
}
func (m *mockDriver) ApplySeed(interface{}) error {
return nil
}
func (m *mockDriver) ExportDataDump(tableNames []string, sourceName string, sourceKind hasura.SourceKind) ([]byte, error) {
return nil, nil
}
func TestRegisterTwice(t *testing.T) {
Register("mock", &mockDriver{})
var err interface{}
func() {
defer func() {
err = recover()
}()
Register("mock", &mockDriver{})
}()
if err == nil {
t.Fatal("expected a panic when calling Register twice")
}
}
func TestOpen(t *testing.T) {
func() {
defer func() {
_ = recover()
}()
Register("mock", &mockDriver{})
}()
cases := []struct {
url string
err bool
}{
{
"mock://user:pass@host:1337/db",
false,
},
{
"unknown://bla",
true,
},
}
for _, c := range cases {
t.Run(c.url, func(t *testing.T) {
d, err := Open(c.url, false, nil, nil)
if err == nil {
if c.err {
t.Fatal("expected an error for an unknown driver")
} else {
if md, ok := d.(*mockDriver); !ok {
t.Fatalf("expected *mockDriver got %T", d)
} else if md.url != c.url {
t.Fatalf("expected %q got %q", c.url, md.url)
}
}
} else if !c.err {
t.Fatalf("did not expect %q", err)
}
})
}
}