2021-04-01 13:38:55 +03:00
|
|
|
package seed
|
|
|
|
|
|
|
|
import (
|
2021-06-21 17:34:10 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2021-04-01 13:38:55 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura/pgdump"
|
2021-06-21 17:34:10 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura/v2query"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
2021-04-01 13:38:55 +03:00
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura/v1query"
|
2021-04-01 13:38:55 +03:00
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
|
2021-04-01 13:38:55 +03:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDriver_ApplySeedsToDatabase(t *testing.T) {
|
2021-06-03 16:26:28 +03:00
|
|
|
port13, teardown := testutil.StartHasura(t, "hasura/graphql-engine:v1.3.3")
|
2021-04-01 13:38:55 +03:00
|
|
|
defer teardown()
|
2021-06-03 16:26:28 +03:00
|
|
|
portLatest, teardown := testutil.StartHasura(t, testutil.HasuraDockerImage)
|
2021-04-01 13:38:55 +03:00
|
|
|
defer teardown()
|
2021-06-21 17:34:10 +03:00
|
|
|
portCitus, citusSource, teardown := testutil.StartHasuraWithCitusSource(t, testutil.HasuraDockerImage)
|
|
|
|
defer teardown()
|
2021-04-01 13:38:55 +03:00
|
|
|
type fields struct {
|
|
|
|
SendBulk sendBulk
|
|
|
|
PGDumpClient hasura.PGDump
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
fs afero.Fs
|
|
|
|
rootSeedsDirectory string
|
|
|
|
filenames []string
|
|
|
|
source cli.Source
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2022-10-26 09:01:02 +03:00
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
before func(t *testing.T) // functions which should be run before the test possibly to prepare test fixtures maybe
|
|
|
|
assertErr require.ErrorAssertionFunc
|
2021-04-01 13:38:55 +03:00
|
|
|
}{
|
|
|
|
{
|
2021-06-03 16:26:28 +03:00
|
|
|
"can apply seeds in hasura/graphql-engine:v1.3.3",
|
2021-04-01 13:38:55 +03:00
|
|
|
fields{
|
|
|
|
func() sendBulk {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, port13, nil)
|
2021-04-01 13:38:55 +03:00
|
|
|
return v1query.New(c, "v1/query").Bulk
|
|
|
|
}(),
|
|
|
|
func() hasura.PGDump {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, port13, nil)
|
2021-04-01 13:38:55 +03:00
|
|
|
return pgdump.New(c, "v1alpha1/pg_dump")
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
fs: afero.NewOsFs(),
|
|
|
|
rootSeedsDirectory: "testdata/seeds",
|
|
|
|
filenames: []string{},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
nil,
|
2022-10-26 09:01:02 +03:00
|
|
|
require.NoError,
|
2021-04-01 13:38:55 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"can apply seeds in latest",
|
|
|
|
fields{
|
|
|
|
func() sendBulk {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, portLatest, nil)
|
2021-06-21 17:34:10 +03:00
|
|
|
return v2query.New(c, "v2/query").Bulk
|
2021-04-01 13:38:55 +03:00
|
|
|
}(),
|
|
|
|
func() hasura.PGDump {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, portLatest, nil)
|
2021-04-01 13:38:55 +03:00
|
|
|
return pgdump.New(c, "v1alpha1/pg_dump")
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
fs: afero.NewOsFs(),
|
|
|
|
rootSeedsDirectory: "testdata/seeds",
|
|
|
|
filenames: []string{},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
nil,
|
2022-10-26 09:01:02 +03:00
|
|
|
require.NoError,
|
2021-04-01 13:38:55 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"can apply seeds from files",
|
|
|
|
fields{
|
|
|
|
func() sendBulk {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, portLatest, nil)
|
2021-06-21 17:34:10 +03:00
|
|
|
return v2query.New(c, "v2/query").Bulk
|
2021-04-01 13:38:55 +03:00
|
|
|
}(),
|
|
|
|
func() hasura.PGDump {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, portLatest, nil)
|
2021-04-01 13:38:55 +03:00
|
|
|
return pgdump.New(c, "v1alpha1/pg_dump")
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
fs: afero.NewOsFs(),
|
|
|
|
rootSeedsDirectory: "testdata/seeds",
|
|
|
|
filenames: []string{
|
|
|
|
"articles.sql",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
func(t *testing.T) {
|
2021-05-17 03:29:11 +03:00
|
|
|
c := testutil.NewHttpcClient(t, portLatest, nil)
|
2021-06-21 17:34:10 +03:00
|
|
|
v2QueryClient := v2query.New(c, "v2/query")
|
|
|
|
_, err := v2QueryClient.PGRunSQL(hasura.PGRunSQLInput{
|
2021-04-01 13:38:55 +03:00
|
|
|
SQL: "DROP TABLE articles",
|
|
|
|
Source: "default",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-10-26 09:01:02 +03:00
|
|
|
require.NoError,
|
2021-04-01 13:38:55 +03:00
|
|
|
},
|
2021-06-21 17:34:10 +03:00
|
|
|
{
|
|
|
|
"can apply seeds in citus",
|
|
|
|
fields{
|
|
|
|
func() sendBulk {
|
|
|
|
c := testutil.NewHttpcClient(t, portCitus, nil)
|
|
|
|
return v2query.New(c, "v2/query").Bulk
|
|
|
|
}(),
|
|
|
|
func() hasura.PGDump {
|
|
|
|
c := testutil.NewHttpcClient(t, portCitus, nil)
|
|
|
|
return pgdump.New(c, "v1alpha1/pg_dump")
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
fs: func() afero.Fs {
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
b, err := ioutil.ReadFile("testdata/seeds/articles.sql")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = afero.WriteFile(fs, filepath.Join("testdata/seeds/", citusSource, "articles.sql"), b, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
source: cli.Source{Name: citusSource, Kind: hasura.SourceKindCitus},
|
|
|
|
rootSeedsDirectory: "testdata/seeds",
|
|
|
|
filenames: []string{},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
nil,
|
2022-10-26 09:01:02 +03:00
|
|
|
require.NoError,
|
2021-06-21 17:34:10 +03:00
|
|
|
},
|
2021-04-01 13:38:55 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
d := &Driver{
|
|
|
|
SendBulk: tt.fields.SendBulk,
|
|
|
|
PGDumpClient: tt.fields.PGDumpClient,
|
|
|
|
}
|
|
|
|
if tt.before != nil {
|
|
|
|
tt.before(t)
|
|
|
|
}
|
2022-10-26 09:01:02 +03:00
|
|
|
err := d.ApplySeedsToDatabase(tt.args.fs, tt.args.rootSeedsDirectory, tt.args.filenames, tt.args.source)
|
|
|
|
tt.assertErr(t, err)
|
2021-04-01 13:38:55 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|