Skip to content

Commit

Permalink
tried to make the test less flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
philkra committed Jan 13, 2024
1 parent aff8c86 commit e324ad2
Showing 1 changed file with 45 additions and 75 deletions.
120 changes: 45 additions & 75 deletions internal/integration-tests/table_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package integrationtests
import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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: &regionID,
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)
}
Expand Down Expand Up @@ -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)
}
})
})
}

0 comments on commit e324ad2

Please sign in to comment.