diff --git a/internal/integration-tests/table_client_test.go b/internal/integration-tests/table_client_test.go index 7a638b9..dbcb84c 100644 --- a/internal/integration-tests/table_client_test.go +++ b/internal/integration-tests/table_client_test.go @@ -5,7 +5,6 @@ package integrationtests import ( "context" "fmt" - "os" "testing" "github.com/stretchr/testify/assert" @@ -15,90 +14,51 @@ import ( ) func Test_tableClient(t *testing.T) { - apiKey, found := os.LookupEnv("XATA_API_KEY") - if !found { - t.Skipf("%s not found in env vars", "XATA_API_KEY") + cfg, err := setupDatabase() + if err != nil { + t.Fatalf("unable to setup database: %v", err) } - ctx := context.Background() - t.Run("should create/delete, get schema and columns of a table and add/delete column", func(t *testing.T) { - httpCli := retryablehttp.NewClient().StandardClient() - - workspaceCli, err := xata.NewWorkspacesClient( - xata.WithAPIKey(apiKey), - xata.WithHTTPClient(httpCli), - ) - if err != nil { - t.Fatal(err) - } - - testID := testIdentifier() - - ws, err := workspaceCli.Create( - context.Background(), - &xata.WorkspaceMeta{Name: "ws_name_" + testID}, - ) - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - err := workspaceCli.Delete(ctx, ws.Id) - if err != nil { - t.Fatal(err) - } - }) - - databaseCli, err := xata.NewDatabasesClient( - xata.WithAPIKey(apiKey), - xata.WithHTTPClient(httpCli), - ) + t.Cleanup(func() { + err = cleanup(cfg) if err != nil { - t.Fatal(err) + t.Fatalf("unable to cleanup test setup: %v", err) } + }) - listRegionsResponse, err := databaseCli.GetRegionsWithWorkspaceID(ctx, ws.Id) - if err != nil { - t.Fatal(err) - } + databaseCli, err := xata.NewDatabasesClient( + xata.WithAPIKey(cfg.apiKey), + xata.WithBaseURL(fmt.Sprintf( + "https://%s.%s.xata.sh", + cfg.wsID, + cfg.region, + )), + xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()), + ) + if err != nil { + t.Fatal(err) + } - regionID := listRegionsResponse.Regions[0].Id + tableCli, err := xata.NewTableClient( + xata.WithAPIKey(cfg.apiKey), + xata.WithBaseURL(fmt.Sprintf( + "https://%s.%s.xata.sh", + cfg.wsID, + cfg.region, + )), + xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()), + ) + if err != nil { + t.Fatal(err) + } + ctx := context.Background() + t.Run("should create/delete, get schema and columns of a table and add/delete column", func(t *testing.T) { + testID := testIdentifier() db, err := databaseCli.Create(ctx, xata.CreateDatabaseRequest{ DatabaseName: "db_name_" + testID, - WorkspaceID: xata.String(ws.Id), - Region: ®ionID, - UI: &xata.UI{Color: xata.String("RED")}, - BranchMetaData: &xata.BranchMetadata{ - Repository: xata.String("github.com/my/repository"), - Branch: xata.String("feature-branch"), - Stage: xata.String("testing"), - Labels: &[]string{"development"}, - }, - }) - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - _, err = databaseCli.Delete(ctx, xata.DeleteDatabaseRequest{ - WorkspaceID: xata.String(ws.Id), - DatabaseName: db.DatabaseName, - }) - if err != nil { - t.Fatal(err) - } + WorkspaceID: xata.String(cfg.wsID), }) - - tableCli, err := xata.NewTableClient( - xata.WithAPIKey(apiKey), - xata.WithBaseURL(fmt.Sprintf( - "https://%s.%s.xata.sh", - ws.Id, - regionID, - )), - xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()), - ) if err != nil { t.Fatal(err) } @@ -152,5 +112,15 @@ func Test_tableClient(t *testing.T) { if err != nil { t.Fatal(err) } + + t.Cleanup(func() { + _, err = databaseCli.Delete(ctx, xata.DeleteDatabaseRequest{ + WorkspaceID: xata.String(cfg.wsID), + DatabaseName: db.DatabaseName, + }) + if err != nil { + t.Fatal(err) + } + }) }) }