fix(test): a table case holds the org, not a context

containedctx rejects a struct field of type context.Context, and both new
table tests stored one. Each case now carries what the caller put on the
context -- a builder in the org test, the org id in the client test -- and
builds the context inside the subtest.
This commit is contained in:
sam committed 2026-09-30 07:24:09 +10:00
1 parent 96408c8e6a
commit da731338f1
2 files changed
+37 -25

No files matched your search

+15 -8
View File
@@ -53,26 +53,33 @@ type eofWatchStream struct{ grpc.ClientStream }
func (eofWatchStream) Recv() (*pb.WatchResponse, error) { return nil, io.EOF }
func TestClientStampsCallerOrgOnEveryRequestShape(t *testing.T) {
// The table holds the org, not a context: a stored context.Context field is
// what containedctx rejects, and each case only needs to say what the caller
// put on its context.
tests := []struct {
name string
ctx context.Context
want string
name string
orgID string
want string
}{
{name: "no org on the context is the default org", ctx: context.Background(), want: string(org.DefaultOrgID)},
{name: "org on the context travels on the wire", ctx: org.WithOrg(context.Background(), "acme"), want: "acme"},
{name: "no org on the context is the default org", want: string(org.DefaultOrgID)},
{name: "org on the context travels on the wire", orgID: "acme", want: "acme"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
if tt.orgID != "" {
ctx = org.WithOrg(ctx, org.OrgID(tt.orgID))
}
rpc := &orgRecordingClient{}
client := NewRPCClient(rpc)
if _, _, err := client.Query(tt.ctx, ChannelSettingsKind, func([]byte) error { return nil }); err != nil {
if _, _, err := client.Query(ctx, ChannelSettingsKind, func([]byte) error { return nil }); err != nil {
t.Fatalf("Query: %v", err)
}
if _, err := client.ReplaceSchedules(tt.ctx, nil, 0, "actor", "source", "request"); err != nil {
if _, err := client.ReplaceSchedules(ctx, nil, 0, "actor", "source", "request"); err != nil {
t.Fatalf("ReplaceSchedules: %v", err)
}
if _, err := client.WatchResource(tt.ctx, WorkflowExecutionSettingsKind, 0); err != nil {
if _, err := client.WatchResource(ctx, WorkflowExecutionSettingsKind, 0); err != nil {
t.Fatalf("WatchResource: %v", err)
}
+22 -17
View File
@@ -10,38 +10,43 @@ import (
type foreignContextKey struct{}
func TestOrgFromContext(t *testing.T) {
// build derives each case's context from a fresh background one. The table
// holds no context.Context field on purpose: a stored context is what
// containedctx rejects, and a builder keeps every case readable without one.
tests := []struct {
name string
ctx context.Context
want OrgID
name string
build func(context.Context) context.Context
want OrgID
}{
{name: "unset is the default org", ctx: context.Background(), want: DefaultOrgID},
{name: "set org", ctx: WithOrg(context.Background(), "acme"), want: "acme"},
{name: "unset is the default org", build: func(ctx context.Context) context.Context { return ctx }, want: DefaultOrgID},
{name: "set org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "acme") }, want: "acme"},
{
name: "inner org overrides an outer one",
ctx: WithOrg(WithOrg(context.Background(), "outer"), "inner"),
want: "inner",
name: "inner org overrides an outer one",
build: func(ctx context.Context) context.Context { return WithOrg(WithOrg(ctx, "outer"), "inner") },
want: "inner",
},
{
name: "nil stored value is the default org",
ctx: context.WithValue(context.Background(), orgContextKey{}, nil),
want: DefaultOrgID,
name: "nil stored value is the default org",
build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, nil) },
want: DefaultOrgID,
},
{
name: "a value of a foreign type is the default org",
ctx: context.WithValue(context.Background(), orgContextKey{}, "acme"),
want: DefaultOrgID,
name: "a value of a foreign type is the default org",
build: func(ctx context.Context) context.Context { return context.WithValue(ctx, orgContextKey{}, "acme") },
want: DefaultOrgID,
},
{
name: "a value under a foreign key is the default org",
ctx: context.WithValue(context.Background(), foreignContextKey{}, OrgID("acme")),
build: func(ctx context.Context) context.Context {
return context.WithValue(ctx, foreignContextKey{}, OrgID("acme"))
},
want: DefaultOrgID,
},
{name: "empty org is the default org", ctx: WithOrg(context.Background(), ""), want: DefaultOrgID},
{name: "empty org is the default org", build: func(ctx context.Context) context.Context { return WithOrg(ctx, "") }, want: DefaultOrgID},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := OrgFromContext(tt.ctx); got != tt.want {
if got := OrgFromContext(tt.build(context.Background())); got != tt.want {
t.Fatalf("OrgFromContext() = %q, want %q", got, tt.want)
}
})