|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/h2non/gock" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/supabase/cli/internal/testing/apitest" |
| 12 | + "github.com/supabase/cli/internal/testing/fstest" |
| 13 | + "github.com/supabase/cli/internal/utils" |
| 14 | + "github.com/supabase/cli/internal/utils/flags" |
| 15 | + "github.com/supabase/cli/pkg/api" |
| 16 | + "github.com/supabase/cli/pkg/cast" |
| 17 | +) |
| 18 | + |
| 19 | +func TestUpdatePostgresConfig(t *testing.T) { |
| 20 | + flags.ProjectRef = apitest.RandomProjectRef() |
| 21 | + |
| 22 | + t.Run("updates postgres config", func(t *testing.T) { |
| 23 | + t.Cleanup(fstest.MockStdout(t, ` |
| 24 | + |
| 25 | + Parameter | Value |
| 26 | + -----------------|------- |
| 27 | + max_connections | 100 |
| 28 | +
|
| 29 | +`)) |
| 30 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 31 | + // Setup mock api |
| 32 | + gock.New(utils.DefaultApiHost). |
| 33 | + Put("v1/projects/" + flags.ProjectRef + "/config/database/postgres"). |
| 34 | + Reply(http.StatusOK). |
| 35 | + JSON(api.PostgresConfigResponse{ |
| 36 | + MaxConnections: cast.Ptr(100), |
| 37 | + }) |
| 38 | + // Run test |
| 39 | + err := Run(context.Background(), flags.ProjectRef, []string{ |
| 40 | + "max_connections=100", |
| 41 | + "track_commit_timestamp=true", |
| 42 | + "statement_timeout=600", |
| 43 | + "wal_keep_size=1GB", |
| 44 | + }, true, true, nil) |
| 45 | + assert.NoError(t, err) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("throws error on missing key", func(t *testing.T) { |
| 49 | + err := Run(context.Background(), flags.ProjectRef, []string{"value"}, true, true, nil) |
| 50 | + assert.ErrorContains(t, err, "expected config value in key:value format") |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("throws error on missing project", func(t *testing.T) { |
| 54 | + errNetwork := errors.New("network error") |
| 55 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 56 | + // Setup mock api |
| 57 | + gock.New(utils.DefaultApiHost). |
| 58 | + Get("v1/projects/" + flags.ProjectRef + "/config/database/postgres"). |
| 59 | + ReplyError(errNetwork) |
| 60 | + // Run test |
| 61 | + err := Run(context.Background(), flags.ProjectRef, []string{}, false, false, nil) |
| 62 | + assert.ErrorIs(t, err, errNetwork) |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("throws error on network error", func(t *testing.T) { |
| 66 | + errNetwork := errors.New("network error") |
| 67 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 68 | + // Setup mock api |
| 69 | + gock.New(utils.DefaultApiHost). |
| 70 | + Get("v1/projects/" + flags.ProjectRef + "/config/database/postgres"). |
| 71 | + Reply(http.StatusOK). |
| 72 | + JSON(api.PostgresConfigResponse{ |
| 73 | + MaxConnections: cast.Ptr(100), |
| 74 | + }) |
| 75 | + gock.New(utils.DefaultApiHost). |
| 76 | + Put("/v1/projects/" + flags.ProjectRef + "/config/database/postgres"). |
| 77 | + ReplyError(errNetwork) |
| 78 | + // Run test |
| 79 | + err := Run(context.Background(), flags.ProjectRef, []string{}, false, false, nil) |
| 80 | + assert.ErrorIs(t, err, errNetwork) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("throws error on service unavailable", func(t *testing.T) { |
| 84 | + utils.OutputFormat.Value = utils.OutputEnv |
| 85 | + t.Cleanup(func() { utils.OutputFormat.Value = utils.OutputPretty }) |
| 86 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 87 | + // Setup mock api |
| 88 | + gock.New(utils.DefaultApiHost). |
| 89 | + Put("/v1/projects/" + flags.ProjectRef + "/config/database/postgres"). |
| 90 | + Reply(http.StatusServiceUnavailable) |
| 91 | + // Run test |
| 92 | + err := Run(context.Background(), flags.ProjectRef, []string{}, true, true, nil) |
| 93 | + assert.ErrorContains(t, err, "unexpected update config overrides status 503:") |
| 94 | + }) |
| 95 | +} |
0 commit comments