Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(test): implement test for creating a database creation issue (#507)

Danny Xue58c68d0218a
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 66232f5b84..2e722a7dbb 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -2,6 +2,7 @@ package tests
import (
"context"
+ "encoding/json"
"fmt"
"testing"
@@ -22,7 +23,16 @@ func TestSchemaUpdate(t *testing.T) {
t.Fatal(err)
}
- // provision an instance.
+ // Create a project.
+ project, err := ctl.createProject(api.ProjectCreate{
+ Name: "Test Project",
+ Key: "TestSchemaUpdate",
+ })
+ if err != nil {
+ t.Fatal(fmt.Errorf("failed to create project, error: %w", err))
+ }
+
+ // Provision an instance.
instanceDir := t.TempDir()
instance1Name := "testInstance1"
instance1Dir, err := ctl.provisionSQLiteInstance(instanceDir, instance1Name)
@@ -53,7 +63,7 @@ func TestSchemaUpdate(t *testing.T) {
Host: instance1Dir,
})
if err != nil {
- t.Fatal(fmt.Errorf("failed to add instance, error %w", err))
+ t.Fatal(fmt.Errorf("failed to add instance, error: %w", err))
}
// Expecting no database.
@@ -61,9 +71,33 @@ func TestSchemaUpdate(t *testing.T) {
InstanceID: &instance1.ID,
})
if err != nil {
- t.Fatal(fmt.Errorf("failed to get databases, error %w", err))
+ t.Fatal(fmt.Errorf("failed to get databases, error: %w", err))
}
if len(databases) != 0 {
t.Fatal(fmt.Errorf("invalid number of databases %v, expecting no database", len(databases)))
}
+
+ // Create an issue that creates a database.
+ databaseName := "testSchemaUpdate"
+ m := &api.CreateDatabaseContext{
+ InstanceID: instance1.ID,
+ DatabaseName: databaseName,
+ Labels: "",
+ }
+ createContext, err := json.Marshal(m)
+ if err != nil {
+ t.Fatal(fmt.Errorf("failed to construct issue create context payload, error: %w", err))
+ }
+ _, err = ctl.createIssue(api.IssueCreate{
+ ProjectID: project.ID,
+ Name: fmt.Sprintf("create database %q", databaseName),
+ Type: api.IssueDatabaseCreate,
+ Description: fmt.Sprintf("This creates a database %q.", databaseName),
+ // Assign to self.
+ AssigneeID: project.Creator.ID,
+ CreateContext: string(createContext),
+ })
+ if err != nil {
+ t.Fatal(fmt.Errorf("failed to create issue, error: %w", err))
+ }
}
diff --git a/tests/tests.go b/tests/tests.go
index eba4cb6176..02521278cd 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -34,7 +34,7 @@ func (ctl *controller) StartMain(ctx context.Context, dataDir string) error {
// start main server.
logger, err := cmd.GetLogger()
if err != nil {
- return fmt.Errorf("failed to get logger, error %w", err)
+ return fmt.Errorf("failed to get logger, error: %w", err)
}
defer logger.Sync()
profile := cmd.GetTestProfile(dataDir)
@@ -43,12 +43,12 @@ func (ctl *controller) StartMain(ctx context.Context, dataDir string) error {
errChan := make(chan error, 1)
go func() {
if err := ctl.main.Run(ctx); err != nil {
- errChan <- fmt.Errorf("failed to run main server, error %w", err)
+ errChan <- fmt.Errorf("failed to run main server, error: %w", err)
}
}()
if err := waitForServerStart(ctl.main, errChan); err != nil {
- return fmt.Errorf("failed to wait for server to start, error %w", err)
+ return fmt.Errorf("failed to wait for server to start, error: %w", err)
}
// initialize controller clients.
@@ -122,7 +122,7 @@ func (ctl *controller) Login() error {
func (ctl *controller) provisionSQLiteInstance(rootDir, name string) (string, error) {
p := path.Join(rootDir, name)
if err := os.MkdirAll(p, os.ModePerm); err != nil {
- return "", fmt.Errorf("failed to make directory %q, error %w", p, err)
+ return "", fmt.Errorf("failed to make directory %q, error: %w", p, err)
}
return p, nil
@@ -133,7 +133,7 @@ func (ctl *controller) get(shortURL string, params map[string]string) (io.ReadCl
gURL := fmt.Sprintf("%s%s", rootURL, shortURL)
req, err := http.NewRequest("GET", gURL, nil)
if err != nil {
- return nil, fmt.Errorf("fail to create a new GET request(%q), error %w", gURL, err)
+ return nil, fmt.Errorf("fail to create a new GET request(%q), error: %w", gURL, err)
}
req.Header.Set("Cookie", ctl.cookie)
q := url.Values{}
@@ -143,7 +143,14 @@ func (ctl *controller) get(shortURL string, params map[string]string) (io.ReadCl
req.URL.RawQuery = q.Encode()
resp, err := ctl.client.Do(req)
if err != nil {
- return nil, fmt.Errorf("fail to send a GET request(%q), error %w", gURL, err)
+ return nil, fmt.Errorf("fail to send a GET request(%q), error: %w", gURL, err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read http response body, error: %w", err)
+ }
+ return nil, fmt.Errorf("http response error code %v body %q", resp.StatusCode, string(body))
}
return resp.Body, nil
}
@@ -153,12 +160,19 @@ func (ctl *controller) post(shortURL string, body io.Reader) (io.ReadCloser, err
url := fmt.Sprintf("%s%s", rootURL, shortURL)
req, err := http.NewRequest("POST", url, body)
if err != nil {
- return nil, fmt.Errorf("fail to create a new POST request(%q), error %w", url, err)
+ return nil, fmt.Errorf("fail to create a new POST request(%q), error: %w", url, err)
}
req.Header.Set("Cookie", ctl.cookie)
resp, err := ctl.client.Do(req)
if err != nil {
- return nil, fmt.Errorf("fail to send a POST request(%q), error %w", url, err)
+ return nil, fmt.Errorf("fail to send a POST request(%q), error: %w", url, err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read http response body, error: %w", err)
+ }
+ return nil, fmt.Errorf("http response error code %v body %q", resp.StatusCode, string(body))
}
return resp.Body, nil
}
@@ -173,7 +187,7 @@ func (ctl *controller) getProjects() ([]*api.Project, error) {
var projects []*api.Project
ps, err := jsonapi.UnmarshalManyPayload(body, reflect.TypeOf(new(api.Project)))
if err != nil {
- return nil, fmt.Errorf("fail to unmarshal get project response, error %w", err)
+ return nil, fmt.Errorf("fail to unmarshal get project response, error: %w", err)
}
for _, p := range ps {
project, ok := p.(*api.Project)
@@ -185,6 +199,25 @@ func (ctl *controller) getProjects() ([]*api.Project, error) {
return projects, nil
}
+// createProject creates an project.
+func (ctl *controller) createProject(projectCreate api.ProjectCreate) (*api.Project, error) {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, &projectCreate); err != nil {
+ return nil, fmt.Errorf("failed to marshal project create, error: %w", err)
+ }
+
+ body, err := ctl.post("/project", buf)
+ if err != nil {
+ return nil, err
+ }
+
+ project := new(api.Project)
+ if err = jsonapi.UnmarshalPayload(body, project); err != nil {
+ return nil, fmt.Errorf("fail to unmarshal post project response, error: %w", err)
+ }
+ return project, nil
+}
+
// getProjects gets the environments.
func (ctl *controller) getEnvironments() ([]*api.Environment, error) {
body, err := ctl.get("/environment", nil)
@@ -237,7 +270,7 @@ func (ctl *controller) getDatabases(databaseFind api.DatabaseFind) ([]*api.Datab
func (ctl *controller) addInstance(instanceCreate api.InstanceCreate) (*api.Instance, error) {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, &instanceCreate); err != nil {
- return nil, fmt.Errorf("failed to marshal instance create, error %w", err)
+ return nil, fmt.Errorf("failed to marshal instance create, error: %w", err)
}
body, err := ctl.post("/instance", buf)
@@ -247,7 +280,26 @@ func (ctl *controller) addInstance(instanceCreate api.InstanceCreate) (*api.Inst
instance := new(api.Instance)
if err = jsonapi.UnmarshalPayload(body, instance); err != nil {
- return nil, fmt.Errorf("fail to unmarshal post instance response, error %w", err)
+ return nil, fmt.Errorf("fail to unmarshal post instance response, error: %w", err)
}
return instance, nil
}
+
+// createIssue creates an issue.
+func (ctl *controller) createIssue(issueCreate api.IssueCreate) (*api.Issue, error) {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, &issueCreate); err != nil {
+ return nil, fmt.Errorf("failed to marshal issue create, error: %w", err)
+ }
+
+ body, err := ctl.post("/issue", buf)
+ if err != nil {
+ return nil, err
+ }
+
+ issue := new(api.Issue)
+ if err = jsonapi.UnmarshalPayload(body, issue); err != nil {
+ return nil, fmt.Errorf("fail to unmarshal post issue response, error: %w", err)
+ }
+ return issue, nil
+}