diff --git a/internal/integration-tests/databases_client_test.go b/internal/integration-tests/databases_client_test.go index 019b592..aabab6a 100644 --- a/internal/integration-tests/databases_client_test.go +++ b/internal/integration-tests/databases_client_test.go @@ -7,16 +7,15 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" - "github.com/hashicorp/go-retryablehttp" + "github.com/stretchr/testify/assert" "github.com/xataio/xata-go/xata" ) func Test_databasesClient(t *testing.T) { - apiKey, found := os.LookupEnv("XATA_API_KEY") + apiKey, found := os.LookupEnv(xata.EnvXataAPIKey) if !found { - t.Skipf("%s not found in env vars", "XATA_API_KEY") + t.Skipf("%s not found in env vars", xata.EnvXataAPIKey) } ctx := context.Background() diff --git a/internal/integration-tests/setup_test.go b/internal/integration-tests/setup_test.go index 0fb6750..40111db 100644 --- a/internal/integration-tests/setup_test.go +++ b/internal/integration-tests/setup_test.go @@ -9,8 +9,6 @@ import ( "net/http" "os" - //"strings" - "github.com/hashicorp/go-retryablehttp" "github.com/xataio/xata-go/xata" ) @@ -28,15 +26,15 @@ type config struct { func setupDatabase() (*config, error) { ctx := context.Background() - apiKey, found := os.LookupEnv("XATA_API_KEY") + apiKey, found := os.LookupEnv(xata.EnvXataAPIKey) if !found { - return nil, fmt.Errorf("%s not found in env vars", "XATA_API_KEY") + return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataAPIKey) } // require workspace ID to come from the env var // instead of creating new workspace on each client - wsID, found := os.LookupEnv("XATA_WORKSPACE_ID") + wsID, found := os.LookupEnv(xata.EnvXataWorkspaceID) if !found { - return nil, fmt.Errorf("%s not found in env vars", "XATA_WORKSPACE_ID") + return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataWorkspaceID) } testID := testIdentifier() diff --git a/internal/integration-tests/users_client_test.go b/internal/integration-tests/users_client_test.go index f9b3949..12fe2ca 100644 --- a/internal/integration-tests/users_client_test.go +++ b/internal/integration-tests/users_client_test.go @@ -14,9 +14,9 @@ import ( ) func Test_usersClient(t *testing.T) { - apiKey, found := os.LookupEnv("XATA_API_KEY") + apiKey, found := os.LookupEnv(xata.EnvXataAPIKey) if !found { - t.Skipf("%s not found in env vars", "XATA_API_KEY") + t.Skipf("%s not found in env vars", xata.EnvXataAPIKey) } t.Run("should get the current user", func(t *testing.T) { diff --git a/internal/integration-tests/workspaces_client_test.go b/internal/integration-tests/workspaces_client_test.go index 42dbfda..3ee8d65 100644 --- a/internal/integration-tests/workspaces_client_test.go +++ b/internal/integration-tests/workspaces_client_test.go @@ -13,9 +13,9 @@ import ( ) func Test_workspacesClient(t *testing.T) { - apiKey, found := os.LookupEnv("XATA_API_KEY") + apiKey, found := os.LookupEnv(xata.EnvXataAPIKey) if !found { - t.Skipf("%s not found in env vars", "XATA_API_KEY") + t.Skipf("%s not found in env vars", xata.EnvXataAPIKey) } t.Run("should create, get, list, update and delete workspace", func(t *testing.T) { diff --git a/xata/utils.go b/xata/utils.go index 696a401..3fecee3 100644 --- a/xata/utils.go +++ b/xata/utils.go @@ -14,30 +14,34 @@ import ( "github.com/joho/godotenv" ) +// Environment variables +const ( + EnvXataAPIKey = "XATA_API_KEY" + EnvXataWorkspaceID = "XATA_WORKSPACE_ID" + EnvXataBranch = "XATA_BRANCH" + EnvXataRegion = "XATA_REGION" +) + const ( personalAPIKeyLocation = "~/.config/xata/key" defaultControlPlaneDomain = "api.xata.io" - xataAPIKeyEnvVar = "XATA_API_KEY" - xataWsIDEnvVar = "XATA_WORKSPACE_ID" dbURLFormat = "https://{workspace_id}.{region}.xata.sh/db/{db_name}:{branch_name}" defaultBranchName = "main" configFileName = ".xatarc" - branchNameEnvVar = "XATA_BRANCH" defaultDataPlaneDomain = "xata.sh" defaultRegion = "us-east-1" - regionEnvVar = "XATA_REGION" ) -var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", xataAPIKeyEnvVar, personalAPIKeyLocation) +var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", EnvXataAPIKey, personalAPIKeyLocation) // assignAPIkey add the API key to the ClientOptions by going through the following options in order: -// - In env vars by the xataAPIKeyEnvVar dbName. -// - In .env file with the xataAPIKeyEnvVar dbName. +// - In env vars by the EnvXataAPIKey dbName. +// - In .env file with the EnvXataAPIKey dbName. // - In .xatarc config file (TODO: not ready!) // // See: https://xata.io/docs/python-sdk/overview#authorization func getAPIKey() (string, error) { - if key, found := os.LookupEnv(xataAPIKeyEnvVar); found { + if key, found := os.LookupEnv(EnvXataAPIKey); found { return key, nil } @@ -50,7 +54,7 @@ func getAPIKey() (string, error) { } } - if key, found := myEnv[xataAPIKeyEnvVar]; found { + if key, found := myEnv[EnvXataAPIKey]; found { return key, nil } @@ -187,13 +191,13 @@ func getEnvVar(name string, defaultValue string) string { // getBranchName retrieves the branch name. // If not found, falls back to defaultBranchName func getBranchName() string { - return getEnvVar(branchNameEnvVar, defaultBranchName) + return getEnvVar(EnvXataBranch, defaultBranchName) } // Get the region if the corresponding env var `XATA_REGION` is set // otherwise return the default region: us-east-1 func getRegion() string { - return getEnvVar(regionEnvVar, defaultRegion) + return getEnvVar(EnvXataRegion, defaultRegion) } // loadDatabaseConfig will return config with defaults if the error is not nil. @@ -205,15 +209,14 @@ func loadDatabaseConfig() (databaseConfig, error) { } // Setup with env var - // XATA_WORKSPACE_ID to set the workspace Id - wsID := getEnvVar(xataWsIDEnvVar, "") + // XATA_WORKSPACE_ID to set the workspace ID + wsID := getEnvVar(EnvXataWorkspaceID, "") if wsID != "" { - region := getRegion() - branch := getBranchName() db := databaseConfig{ - workspaceID: wsID, - region: region, - branchName: branch, + workspaceID: wsID, + region: getRegion(), + branchName: getBranchName(), + domainWorkspace: defaultDataPlaneDomain, } return db, nil } diff --git a/xata/utils_test.go b/xata/utils_test.go index a240d7d..b053fd4 100644 --- a/xata/utils_test.go +++ b/xata/utils_test.go @@ -12,11 +12,11 @@ import ( func TestClientOptions_getAPIKey(t *testing.T) { apiKeyFromEnv := "test-API-key-from-env" - err := os.Setenv(xataAPIKeyEnvVar, apiKeyFromEnv) + err := os.Setenv(EnvXataAPIKey, apiKeyFromEnv) if err != nil { t.Fatal(err) } - t.Cleanup(func() { os.Unsetenv(xataAPIKeyEnvVar) }) + t.Cleanup(func() { os.Unsetenv(EnvXataAPIKey) }) t.Run("should assign the API key from the env vars", func(t *testing.T) { apiKey, err := getAPIKey() @@ -34,7 +34,7 @@ func Test_getBranchName(t *testing.T) { }) setBranchName := "feature-042" - err := os.Setenv(branchNameEnvVar, setBranchName) + err := os.Setenv(EnvXataBranch, setBranchName) if err != nil { t.Fatal(err) } @@ -45,7 +45,7 @@ func Test_getBranchName(t *testing.T) { assert.Equal(t, gotBranchName, setBranchName) }) - t.Cleanup(func() { os.Unsetenv(branchNameEnvVar) }) + t.Cleanup(func() { os.Unsetenv(EnvXataBranch) }) } func Test_getRegion(t *testing.T) { @@ -56,7 +56,7 @@ func Test_getRegion(t *testing.T) { }) setRegion := "eu-west-3" - err := os.Setenv(regionEnvVar, setRegion) + err := os.Setenv(EnvXataRegion, setRegion) if err != nil { t.Fatal(err) } @@ -67,7 +67,7 @@ func Test_getRegion(t *testing.T) { assert.Equal(t, gotRegion, setRegion) }) - t.Cleanup(func() { os.Unsetenv(regionEnvVar) }) + t.Cleanup(func() { os.Unsetenv(EnvXataRegion) }) } func Test_parseDatabaseURL(t *testing.T) { @@ -176,7 +176,7 @@ func Test_loadConfig(t *testing.T) { func Test_loadDatabaseConfig_with_envvars(t *testing.T) { setWsId := "workspace-0lac00" - err := os.Setenv(xataWsIDEnvVar, setWsId) + err := os.Setenv(EnvXataWorkspaceID, setWsId) if err != nil { t.Fatal(err) } @@ -184,53 +184,36 @@ func Test_loadDatabaseConfig_with_envvars(t *testing.T) { // test workspace id from env var t.Run("load config from WORKSPACE_ID env var", func(t *testing.T) { dbCfg, err := loadDatabaseConfig() - if err != nil { - t.Fatalf("Error loading config: %v", err) - } - - if dbCfg.workspaceID != setWsId { - t.Fatalf("Expected Workspace ID: %s, got: %s", setWsId, dbCfg.workspaceID) - } - if dbCfg.branchName != defaultBranchName { - t.Fatalf("Expected branch name: %s, got: %s", defaultBranchName, dbCfg.branchName) - } - if dbCfg.region != defaultRegion { - t.Fatalf("Expected region: %s, got: %s", defaultRegion, dbCfg.region) - } + assert.NoError(t, err) + assert.Equal(t, setWsId, dbCfg.workspaceID) + assert.Equal(t, defaultBranchName, dbCfg.branchName) + assert.Equal(t, defaultRegion, dbCfg.region) + assert.Equal(t, defaultDataPlaneDomain, dbCfg.domainWorkspace) }) setBranch := "branch123" - err2 := os.Setenv(branchNameEnvVar, setBranch) + err2 := os.Setenv(EnvXataBranch, setBranch) if err2 != nil { t.Fatal(err2) } setRegion := "ap-southeast-16" - err3 := os.Setenv(regionEnvVar, setRegion) + err3 := os.Setenv(EnvXataRegion, setRegion) if err3 != nil { t.Fatal(err3) } // with branch and region env vars - t.Run("load config from XATA_WORKSPACE_ID, regionEnvVar and XATA_BRANCH env vars", func(t *testing.T) { + t.Run("load config from XATA_WORKSPACE_ID, XATA_REGION and XATA_BRANCH env vars", func(t *testing.T) { dbCfg, err := loadDatabaseConfig() - if err != nil { - t.Fatalf("Error loading config: %v", err) - } - - if dbCfg.workspaceID != setWsId { - t.Fatalf("Expected Workspace ID: %s, got: %s", setWsId, dbCfg.workspaceID) - } - if dbCfg.branchName != setBranch { - t.Fatalf("Expected branch name: %s, got: %s", setBranch, dbCfg.branchName) - } - if dbCfg.region != setRegion { - t.Fatalf("Expected region: %s, got: %s", setRegion, dbCfg.region) - } + assert.NoError(t, err) + assert.Equal(t, setWsId, dbCfg.workspaceID) + assert.Equal(t, setBranch, dbCfg.branchName) + assert.Equal(t, setRegion, dbCfg.region) }) t.Cleanup(func() { - os.Unsetenv(xataWsIDEnvVar) - os.Unsetenv(branchNameEnvVar) - os.Unsetenv(regionEnvVar) + assert.NoError(t, os.Unsetenv(EnvXataWorkspaceID)) + assert.NoError(t, os.Unsetenv(EnvXataBranch)) + assert.NoError(t, os.Unsetenv(EnvXataRegion)) }) }