mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
ca567bf8cb
- actions use-codegen - Assert if starter kit files are created - metadata apply - Assert server metadata after apply - assert that --dry-run flag will not modify metadata on server - metadata clear - Assert with server metadata that operation was successful. - metadata inconsistency drop - Assert with server metadata that operation was successful. - metadata inconsistency status - Assert for all possible states - metadata reload - Assert effect of operation with server - migrate and seed subcommands - Add tests for MSSQL sources - Structure tests in such a way that it’s easy to add tests for new sources https://github.com/hasura/graphql-engine-mono/pull/1693 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: fcdbb806d105978a07487a40f7fa6e16b3ab8816
139 lines
5.2 KiB
Go
139 lines
5.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
. "github.com/onsi/gomega/gexec"
|
|
)
|
|
|
|
var test = func(projectDirectory string, globalFlags []string, databaseKind string) {
|
|
var upSql, downSql string
|
|
if databaseKind == "mssql" {
|
|
upSql = `CREATE SCHEMA test` + "\n" + `CREATE TABLE "test"."table1" ("id" int NOT NULL, PRIMARY KEY ("id") );`
|
|
downSql = `DROP TABLE "test"."table1";` + "\n" + `DROP SCHEMA test`
|
|
} else {
|
|
upSql = `CREATE TABLE "public"."table1" ("id" serial NOT NULL, PRIMARY KEY ("id") );`
|
|
downSql = `DROP TABLE "public"."table1";`
|
|
}
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
Args: append([]string{"migrate", "create", "table1", "--up-sql", upSql, "--down-sql", downSql}, globalFlags...),
|
|
WorkingDirectory: projectDirectory,
|
|
})
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
Args: append([]string{"migrate", "apply"}, globalFlags...),
|
|
WorkingDirectory: projectDirectory,
|
|
})
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
Args: append([]string{"seed", "apply", "--file", "table_seed.sql"}, globalFlags...),
|
|
WorkingDirectory: projectDirectory,
|
|
})
|
|
Eventually(session, timeout).Should(Exit(0))
|
|
Expect(session.Err.Contents()).Should(ContainSubstring("Seeds planted"))
|
|
}
|
|
|
|
var _ = Describe("seed apply (config v2)", func() {
|
|
var projectDirectoryConfigV2 string
|
|
var teardown func()
|
|
var hgeEndpoint string
|
|
BeforeEach(func() {
|
|
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
|
hgeEndpoint = fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
|
teardown = func() {
|
|
teardownHGE()
|
|
}
|
|
})
|
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
It("can apply seeds in config v2", func() {
|
|
projectDirectoryConfigV2 = testutil.RandDirName()
|
|
defer os.RemoveAll(projectDirectoryConfigV2)
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
Args: []string{"init", projectDirectoryConfigV2, "--version", "2"},
|
|
})
|
|
editEndpointInConfig(filepath.Join(projectDirectoryConfigV2, defaultConfigFilename), hgeEndpoint)
|
|
|
|
err := os.MkdirAll(filepath.Join(filepath.Join(projectDirectoryConfigV2, "seeds")), 0755)
|
|
file, err := os.Create(filepath.Join(projectDirectoryConfigV2, "seeds", "table_seed.sql"))
|
|
Expect(err).To(BeNil())
|
|
|
|
data := `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);`
|
|
|
|
_, err = file.WriteString(data)
|
|
Expect(err).To(BeNil())
|
|
test(projectDirectoryConfigV2, nil, "postgres")
|
|
})
|
|
})
|
|
|
|
var _ = Describe("seed apply (config v3)", func() {
|
|
// automatic state migration should not affect new config v3 projects
|
|
var projectDirectory string
|
|
var teardown func()
|
|
var pgSource = randomdata.SillyName()
|
|
var citusSource = randomdata.SillyName()
|
|
var mssqlSource = randomdata.SillyName()
|
|
BeforeEach(func() {
|
|
projectDirectory = testutil.RandDirName()
|
|
hgeEndPort, teardownHGE := testutil.StartHasuraWithMetadataDatabase(GinkgoT(), testutil.HasuraDockerImage)
|
|
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
|
_, teardownPG := AddDatabaseToHasura(hgeEndpoint, pgSource, "postgres")
|
|
_, teardownCitus := AddDatabaseToHasura(hgeEndpoint, citusSource, "citus")
|
|
_, teardownMSSQL := AddDatabaseToHasura(hgeEndpoint, mssqlSource, "mssql")
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
Args: []string{"init", projectDirectory},
|
|
})
|
|
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
|
teardown = func() {
|
|
os.RemoveAll(projectDirectory)
|
|
teardownHGE()
|
|
teardownPG()
|
|
teardownCitus()
|
|
teardownMSSQL()
|
|
}
|
|
})
|
|
AfterEach(func() { teardown() })
|
|
|
|
It("can apply seeds in config v3", func() {
|
|
err := os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", pgSource)), 0755)
|
|
Expect(err).To(BeNil())
|
|
data := []byte(`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);`)
|
|
|
|
mssqlData := []byte(`INSERT INTO "test"."table1" (id) VALUES (1);
|
|
INSERT INTO "test"."table1" (id) VALUES (2);
|
|
INSERT INTO "test"."table1" (id) VALUES (3);
|
|
INSERT INTO "test"."table1" (id) VALUES (4);`)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", pgSource, "table_seed.sql"), data, 0655)
|
|
Expect(err).To(BeNil())
|
|
test(projectDirectory, []string{"--database-name", pgSource}, "postgres")
|
|
|
|
err = os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", citusSource)), 0755)
|
|
Expect(err).To(BeNil())
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", citusSource, "table_seed.sql"), data, 0655)
|
|
Expect(err).To(BeNil())
|
|
test(projectDirectory, []string{"--database-name", citusSource}, "citus")
|
|
|
|
err = os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", mssqlSource)), 0755)
|
|
Expect(err).To(BeNil())
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", mssqlSource, "table_seed.sql"), mssqlData, 0655)
|
|
Expect(err).To(BeNil())
|
|
test(projectDirectory, []string{"--database-name", mssqlSource}, "mssql")
|
|
})
|
|
|
|
})
|