Skip to content

Commit 013f31d

Browse files
Unify fake constructor input conventions to value-in (#452)
Gets all resource constructor / factory methods on the same pattern: value-in, pointer-out. This is a common approach in go: - Value-in stops the constructor from mutating the caller's struct in place - Pointer-out allows callers to mutate the constructed objects as a way to manipulate the server's state mid-test. (The pointers are also stored in the server's slices which represent database state). This is important for simulating test scenarios. Stacked on #450. Sneaking one additional PR into this pg resume/suspend stack to clarify usage of factories. GitOrigin-RevId: 664d60cc443a6bdb8d0add062c62184bb480a97f
1 parent 022e74a commit 013f31d

4 files changed

Lines changed: 10 additions & 16 deletions

File tree

cmd/kv_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var ACTIVE_WORKSPACE_ID = testids.WorkspaceID("active")
1717
// seedKV adds a KV instance owned by the active workspace, with a random
1818
// valid ID and the given name. Returns the seeded detail for assertions.
1919
func seedKV(server *renderapi.Server, name string) *client.KeyValueDetail {
20-
kv := renderapi.NewKV(&client.KeyValueDetail{
20+
kv := renderapi.NewKV(client.KeyValueDetail{
2121
Name: name,
2222
Owner: client.Owner{Id: ACTIVE_WORKSPACE_ID},
2323
})
@@ -27,7 +27,7 @@ func seedKV(server *renderapi.Server, name string) *client.KeyValueDetail {
2727

2828
// seedKVInEnv adds a KV instance scoped to a specific environment.
2929
func seedKVInEnv(server *renderapi.Server, name, envID string) *client.KeyValueDetail {
30-
kv := renderapi.NewKV(&client.KeyValueDetail{
30+
kv := renderapi.NewKV(client.KeyValueDetail{
3131
Name: name,
3232
Owner: client.Owner{Id: ACTIVE_WORKSPACE_ID},
3333
EnvironmentId: &envID,

cmd/pg_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ func executePGCommand(t *testing.T, server *renderapi.Server, args ...string) (C
4949

5050
// seedPG adds a new Postgres database to the seeded active workspace.
5151
func seedPG(server *renderapi.Server, name string) *client.PostgresDetail {
52-
return server.Postgres.Add(renderapi.NewPostgres(&client.PostgresDetail{
52+
return server.Postgres.Add(renderapi.NewPostgres(client.PostgresDetail{
5353
Name: name,
5454
Owner: client.Owner{Id: pgActiveWorkspaceID},
5555
}))
5656
}
5757

5858
// seedPGInEnv adds a new Postgres database to the specified environment inside the active workspace.
5959
func seedPGInEnv(server *renderapi.Server, name string, envID string) *client.PostgresDetail {
60-
return server.Postgres.Add(renderapi.NewPostgres(&client.PostgresDetail{
60+
return server.Postgres.Add(renderapi.NewPostgres(client.PostgresDetail{
6161
Name: name,
6262
Owner: client.Owner{Id: pgActiveWorkspaceID},
6363
EnvironmentId: pointers.From(envID),

internal/fakes/renderapi/server.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ func NewEnvironment(e client.Environment) *client.Environment {
158158
}
159159

160160
// NewKV returns a KeyValueDetail with sensible defaults for any zero-value fields.
161-
func NewKV(kv *client.KeyValueDetail) *client.KeyValueDetail {
162-
if kv == nil {
163-
kv = &client.KeyValueDetail{}
164-
}
161+
func NewKV(kv client.KeyValueDetail) *client.KeyValueDetail {
165162
if kv.Id == "" {
166163
kv.Id = testids.RandomKeyValueID()
167164
}
@@ -186,15 +183,12 @@ func NewKV(kv *client.KeyValueDetail) *client.KeyValueDetail {
186183
kv.UpdatedAt = now
187184
}
188185
}
189-
return kv
186+
return &kv
190187
}
191188

192189
// NewPostgres returns a PostgresDetail with sensible defaults for any
193190
// zero-value fields.
194-
func NewPostgres(pg *client.PostgresDetail) *client.PostgresDetail {
195-
if pg == nil {
196-
pg = &client.PostgresDetail{}
197-
}
191+
func NewPostgres(pg client.PostgresDetail) *client.PostgresDetail {
198192
if pg.Id == "" {
199193
pg.Id = testids.RandomPostgresID()
200194
}
@@ -235,7 +229,7 @@ func NewPostgres(pg *client.PostgresDetail) *client.PostgresDetail {
235229
if pg.UpdatedAt.IsZero() {
236230
pg.UpdatedAt = now
237231
}
238-
return pg
232+
return &pg
239233
}
240234

241235
func postgresListItem(pg *client.PostgresDetail) client.Postgres {

pkg/postgres/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newHarness(t *testing.T) testHarness {
5858

5959
// addPostgres creates a Postgres that is owned by the seeded Active Workspace directly (no environment)
6060
func (h testHarness) addPostgres(name string) *client.PostgresDetail {
61-
return h.server.Postgres.Add(renderapi.NewPostgres(&client.PostgresDetail{
61+
return h.server.Postgres.Add(renderapi.NewPostgres(client.PostgresDetail{
6262
Name: name,
6363
Owner: client.Owner{Id: h.workspaceID},
6464
}))
@@ -77,7 +77,7 @@ func (h testHarness) addPostgresInWorkspaceEnvironment(name string, workspaceID
7777
project := h.requireProject(env.ProjectId)
7878
require.Equal(h.t, workspaceID, project.Owner.Id, "test setup: environment %q belongs to project %q in workspace %q, not workspace %q", environmentID, project.Id, project.Owner.Id, workspaceID)
7979

80-
return h.server.Postgres.Add(renderapi.NewPostgres(&client.PostgresDetail{
80+
return h.server.Postgres.Add(renderapi.NewPostgres(client.PostgresDetail{
8181
Name: name,
8282
Owner: client.Owner{Id: workspaceID},
8383
EnvironmentId: pointers.From(environmentID),

0 commit comments

Comments
 (0)