graphql-engine/cli/commands/migrate_create_test.go
Kali Vara Purushotham Santhati ca567bf8cb cli: enhance e2e tests
- 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
2021-07-28 06:45:03 +00:00

168 lines
4.9 KiB
Go

package commands
import (
"fmt"
"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 _ = Describe("hasura migrate create (config v3)", func() {
var projectDirectory string
var teardown func()
BeforeEach(func() {
projectDirectory = testutil.RandDirName()
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectory},
})
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
teardown = func() {
os.RemoveAll(projectDirectory)
teardownHGE()
}
})
AfterEach(func() { teardown() })
It("can create migrations for default database", func() {
migrationName := "create_schema_testing"
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{
"migrate",
"create",
migrationName,
"--up-sql",
"create schema \"testing\";",
"--down-sql",
"drop schema \"testing\" cascade;",
"--database-name", "default",
},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"Migrations files created",
migrationName,
"version",
}
Eventually(session, timeout).Should(Exit(0))
for _, keyword := range wantKeywordList {
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
}
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", "default"))
Expect(err).To(BeNil())
for _, d := range dirs {
Expect(d.Name()).Should(ContainSubstring(migrationName))
}
})
It("can create migrations for database that is not connected to server", func() {
migrationName := "create_schema_testing"
sourceName := randomdata.SillyName()
session := testutil.Hasura(testutil.CmdOpts{
Args: []string{
"migrate",
"create",
migrationName,
"--up-sql",
"create schema \"testing\";",
"--down-sql",
"drop schema \"testing\" cascade;",
"--database-name", sourceName,
},
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
fmt.Sprintf("database %s is not connected to hasura", sourceName),
"Migrations files created",
migrationName,
"version",
}
Eventually(session, timeout).Should(Exit(0))
for _, keyword := range wantKeywordList {
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
}
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", sourceName))
Expect(err).To(BeNil())
for _, d := range dirs {
Expect(d.Name()).Should(ContainSubstring(migrationName))
}
})
})
var _ = Describe("hasura migrate create (config v2)", func() {
var projectDirectoryLatest, projectDirectoryV13 string
var session *Session
var teardown func()
BeforeEach(func() {
projectDirectoryLatest = testutil.RandDirName()
hgeEndPortLatest, teardownHGELatest := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
hgeEndpointLatest := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPortLatest)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectoryLatest, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectoryLatest, defaultConfigFilename), hgeEndpointLatest)
projectDirectoryV13 = testutil.RandDirName()
hgeEndPortV13, teardownHGEV13 := testutil.StartHasura(GinkgoT(), "hasura/graphql-engine:v1.3.3")
hgeEndpointV13 := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPortV13)
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectoryV13, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectoryV13, defaultConfigFilename), hgeEndpointV13)
teardown = func() {
session.Kill()
os.RemoveAll(projectDirectoryLatest)
os.RemoveAll(projectDirectoryV13)
teardownHGEV13()
teardownHGELatest()
}
})
AfterEach(func() { teardown() })
It("can create migration files", func() {
test := func(projectDirectory string) {
migrationName := "create_schema_testing"
wantKeywordList := []string{
"Migrations files created",
migrationName,
"version",
}
session = testutil.Hasura(testutil.CmdOpts{
Args: []string{
"migrate",
"create",
migrationName,
"--up-sql", "create schema \"testing\";",
"--down-sql",
"drop schema \"testing\" cascade;",
},
WorkingDirectory: projectDirectory,
})
Eventually(session, timeout).Should(Exit(0))
for _, keyword := range wantKeywordList {
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
}
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations"))
Expect(err).To(BeNil())
for _, d := range dirs {
Expect(d.Name()).Should(ContainSubstring(migrationName))
}
}
test(projectDirectoryLatest)
test(projectDirectoryV13)
})
})