|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mark3labs/mcp-go/mcp" |
| 9 | + "github.com/render-oss/render-mcp-server/pkg/client" |
| 10 | + pgclient "github.com/render-oss/render-mcp-server/pkg/client/postgres" |
| 11 | + "github.com/render-oss/render-mcp-server/pkg/fakes" |
| 12 | + "github.com/render-oss/render-mcp-server/pkg/pointers" |
| 13 | + "github.com/render-oss/render-mcp-server/pkg/session" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestCreatePostgresToolSchemaDefault(t *testing.T) { |
| 19 | + fakeClient := &fakes.FakePostgresRepoClient{} |
| 20 | + repo := NewRepo(fakeClient) |
| 21 | + tool := createPostgres(repo) |
| 22 | + |
| 23 | + planProp := tool.Tool.InputSchema.Properties["plan"].(map[string]any) |
| 24 | + assert.Equal(t, "free", planProp["default"]) |
| 25 | +} |
| 26 | + |
| 27 | +func TestCreatePostgresTool(t *testing.T) { |
| 28 | + ownerId := "own-123456" |
| 29 | + dbName := "test-database" |
| 30 | + |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + plan *string |
| 34 | + expectedPlan pgclient.PostgresPlans |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "Create postgres with no plan defaults to free", |
| 38 | + plan: nil, |
| 39 | + expectedPlan: pgclient.Free, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Create postgres with free plan", |
| 43 | + plan: pointers.From("free"), |
| 44 | + expectedPlan: pgclient.Free, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Create postgres with basic_256mb plan", |
| 48 | + plan: pointers.From("basic_256mb"), |
| 49 | + expectedPlan: pgclient.Basic256mb, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + fakeClient := &fakes.FakePostgresRepoClient{} |
| 56 | + repo := NewRepo(fakeClient) |
| 57 | + |
| 58 | + fakeClient.CreatePostgresWithResponseReturns(&client.CreatePostgresResponse{ |
| 59 | + JSON201: &client.PostgresDetail{ |
| 60 | + Id: "pg-123", |
| 61 | + Name: dbName, |
| 62 | + }, |
| 63 | + HTTPResponse: &http.Response{ |
| 64 | + StatusCode: 201, |
| 65 | + }, |
| 66 | + }, nil) |
| 67 | + |
| 68 | + ctx := createTestContext(ownerId) |
| 69 | + |
| 70 | + args := map[string]any{ |
| 71 | + "name": dbName, |
| 72 | + } |
| 73 | + if tt.plan != nil { |
| 74 | + args["plan"] = *tt.plan |
| 75 | + } |
| 76 | + request := mcp.CallToolRequest{} |
| 77 | + request.Params.Arguments = args |
| 78 | + |
| 79 | + tool := createPostgres(repo) |
| 80 | + result, err := tool.Handler(ctx, request) |
| 81 | + |
| 82 | + require.NoError(t, err) |
| 83 | + require.NotNil(t, result) |
| 84 | + require.False(t, result.IsError, "expected no error but got: %v", result.Content) |
| 85 | + |
| 86 | + assert.Equal(t, 1, fakeClient.CreatePostgresWithResponseCallCount()) |
| 87 | + _, requestBody, _ := fakeClient.CreatePostgresWithResponseArgsForCall(0) |
| 88 | + assert.Equal(t, dbName, requestBody.Name) |
| 89 | + assert.Equal(t, ownerId, requestBody.OwnerId) |
| 90 | + assert.Equal(t, tt.expectedPlan, requestBody.Plan) |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func createTestContext(workspaceID string) context.Context { |
| 96 | + ctx := session.ContextWithStdioSession(context.Background()) |
| 97 | + sess := session.FromContext(ctx) |
| 98 | + sess.SetWorkspace(ctx, workspaceID) |
| 99 | + return ctx |
| 100 | +} |
0 commit comments