2021-04-13 11:22:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
2021-04-13 11:22:48 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var testSeedCreate = func(projectDirectory string, globalFlags []string) {
|
|
|
|
upSql := `CREATE TABLE "public"."table1" ("id" serial NOT NULL, PRIMARY KEY ("id") );
|
|
|
|
INSERT INTO public.table1 (id) VALUES (1);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (2);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (3);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (4);`
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
downSql := `DROP TABLE "public"."table1";`
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"migrate", "create", "table1", "--up-sql", upSql, "--down-sql", downSql}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"migrate", "apply"}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"seed", "create", "table_seed", "--from-table", "table1"}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring("created seed file successfully"))
|
2021-06-03 16:26:28 +03:00
|
|
|
}
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var _ = Describe("hasura seed create", func() {
|
|
|
|
Context("config v3", func() {
|
|
|
|
var teardown func()
|
|
|
|
var projectDirectory string
|
|
|
|
var sourceName string
|
|
|
|
BeforeEach(func() {
|
|
|
|
projectDirectory = testutil.RandDirName()
|
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasuraWithMetadataDatabase(GinkgoT(), testutil.HasuraDockerImage)
|
|
|
|
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
|
|
|
sourceName = randomdata.SillyName()
|
2022-09-27 14:15:09 +03:00
|
|
|
_, teardownPG := testutil.AddDatabaseToHasura(GinkgoT(), hgeEndpoint, sourceName, "postgres")
|
2021-04-13 11:22:48 +03:00
|
|
|
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-06-03 16:26:28 +03:00
|
|
|
Args: []string{"init", projectDirectory},
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
|
|
|
|
|
|
|
teardown = func() {
|
|
|
|
teardownPG()
|
|
|
|
teardownHGE()
|
|
|
|
os.RemoveAll(projectDirectory)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
It("can create seeds in config v3", func() {
|
|
|
|
testSeedCreate(projectDirectory, []string{"--database-name", sourceName})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("config v2", func() {
|
|
|
|
var teardown func()
|
|
|
|
var projectDirectoryConfigV2 string
|
|
|
|
BeforeEach(func() {
|
|
|
|
projectDirectoryConfigV2 = testutil.RandDirName()
|
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
|
|
|
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
2021-04-13 11:22:48 +03:00
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-06-03 16:26:28 +03:00
|
|
|
Args: []string{"init", projectDirectoryConfigV2, "--version", "2"},
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
editEndpointInConfig(filepath.Join(projectDirectoryConfigV2, defaultConfigFilename), hgeEndpoint)
|
|
|
|
|
|
|
|
teardown = func() {
|
|
|
|
teardownHGE()
|
|
|
|
os.RemoveAll(projectDirectoryConfigV2)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
It("can create seeds in config v2", func() {
|
|
|
|
testSeedCreate(projectDirectoryConfigV2, nil)
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|