Skip to content

Commit 3551f5b

Browse files
authored
refactor: consolidate batch fields tests into table-driven test (#42)
## Summary - Collapse two near-identical test functions (`TestBatch_PerOpFieldsOverrideGlobal` and `TestBatch_GlobalFieldsAppliedWhenNoPerOpFields`) into a single table-driven `TestBatch_FieldsPrecedence` - Hoist `opMap` construction outside the test loop (built once instead of twice) - Net reduction of 37 lines with no behavior change ## Test plan - [x] `go test ./cmd/ -run TestBatch_FieldsPrecedence -v` passes both subtests
1 parent fd73620 commit 3551f5b

1 file changed

Lines changed: 50 additions & 87 deletions

File tree

cmd/bughunt2_test.go

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -336,79 +336,22 @@ func TestFetchOAuth2Token_EmptyAccessToken(t *testing.T) {
336336
// Bug #73: batch global --fields overwrites per-op fields args
337337
// ============================================================
338338

339-
func TestBatch_PerOpFieldsOverrideGlobal(t *testing.T) {
340-
// When a batch op specifies "fields" in its args, that value should
341-
// take precedence over the global --fields flag, not be overwritten.
342-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
343-
fieldsParam := r.URL.Query().Get("fields")
344-
w.Header().Set("Content-Type", "application/json")
345-
fmt.Fprintf(w, `{"fields_received":"%s"}`, fieldsParam)
346-
}))
347-
defer ts.Close()
348-
349-
var stdout, stderr bytes.Buffer
350-
c := newTestClient(ts.URL, &stdout, &stderr)
351-
c.Fields = "key" // Simulates global --fields key
352-
353-
ctx := client.NewContext(t.Context(), c)
354-
cmd := &cobra.Command{Use: "batch"}
355-
cmd.Flags().String("input", "", "")
356-
cmd.SetContext(ctx)
357-
358-
// Build a batch op that specifies its own "fields" arg.
359-
bop := BatchOp{
360-
Command: "issue get",
361-
Args: map[string]string{"issueIdOrKey": "TEST-1", "fields": "summary,status"},
362-
}
363-
364-
allOps := generated.AllSchemaOps()
365-
allOps = append(allOps, HandWrittenSchemaOps()...)
366-
opMap := make(map[string]generated.SchemaOp, len(allOps))
367-
for _, op := range allOps {
368-
opMap[op.Resource+" "+op.Verb] = op
369-
}
370-
371-
result := executeBatchOp(cmd, c, 0, bop, opMap)
372-
373-
if result.ExitCode != jrerrors.ExitOK {
374-
t.Fatalf("expected exit 0, got %d; error=%s", result.ExitCode, string(result.Error))
375-
}
376-
377-
// Parse the data to check which fields value was sent.
378-
var data map[string]string
379-
if err := json.Unmarshal(result.Data, &data); err != nil {
380-
t.Fatalf("failed to parse result data: %v (raw: %s)", err, string(result.Data))
381-
}
382-
383-
// The per-op "summary,status" should win over global "key".
384-
if data["fields_received"] != "summary,status" {
385-
t.Errorf("expected per-op fields 'summary,status', got '%s' — global --fields overwrote per-op fields", data["fields_received"])
386-
}
387-
}
388-
389-
func TestBatch_GlobalFieldsAppliedWhenNoPerOpFields(t *testing.T) {
390-
// When a batch op does NOT specify "fields" in its args, the global --fields
391-
// should still be applied.
392-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
393-
fieldsParam := r.URL.Query().Get("fields")
394-
w.Header().Set("Content-Type", "application/json")
395-
fmt.Fprintf(w, `{"fields_received":"%s"}`, fieldsParam)
396-
}))
397-
defer ts.Close()
398-
399-
var stdout, stderr bytes.Buffer
400-
c := newTestClient(ts.URL, &stdout, &stderr)
401-
c.Fields = "key" // Simulates global --fields key
402-
403-
ctx := client.NewContext(t.Context(), c)
404-
cmd := &cobra.Command{Use: "batch"}
405-
cmd.Flags().String("input", "", "")
406-
cmd.SetContext(ctx)
407-
408-
// Batch op WITHOUT "fields" in args.
409-
bop := BatchOp{
410-
Command: "issue get",
411-
Args: map[string]string{"issueIdOrKey": "TEST-1"},
339+
func TestBatch_FieldsPrecedence(t *testing.T) {
340+
tests := []struct {
341+
name string
342+
args map[string]string
343+
wantFields string
344+
}{
345+
{
346+
name: "per-op fields override global",
347+
args: map[string]string{"issueIdOrKey": "TEST-1", "fields": "summary,status"},
348+
wantFields: "summary,status",
349+
},
350+
{
351+
name: "global fields applied when no per-op fields",
352+
args: map[string]string{"issueIdOrKey": "TEST-1"},
353+
wantFields: "key",
354+
},
412355
}
413356

414357
allOps := generated.AllSchemaOps()
@@ -418,20 +361,40 @@ func TestBatch_GlobalFieldsAppliedWhenNoPerOpFields(t *testing.T) {
418361
opMap[op.Resource+" "+op.Verb] = op
419362
}
420363

421-
result := executeBatchOp(cmd, c, 0, bop, opMap)
422-
423-
if result.ExitCode != jrerrors.ExitOK {
424-
t.Fatalf("expected exit 0, got %d; error=%s", result.ExitCode, string(result.Error))
425-
}
426-
427-
var data map[string]string
428-
if err := json.Unmarshal(result.Data, &data); err != nil {
429-
t.Fatalf("failed to parse result data: %v (raw: %s)", err, string(result.Data))
430-
}
431-
432-
// Global "key" should be applied since no per-op fields.
433-
if data["fields_received"] != "key" {
434-
t.Errorf("expected global fields 'key', got '%s'", data["fields_received"])
364+
for _, tt := range tests {
365+
t.Run(tt.name, func(t *testing.T) {
366+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
367+
fieldsParam := r.URL.Query().Get("fields")
368+
w.Header().Set("Content-Type", "application/json")
369+
fmt.Fprintf(w, `{"fields_received":"%s"}`, fieldsParam)
370+
}))
371+
defer ts.Close()
372+
373+
var stdout, stderr bytes.Buffer
374+
c := newTestClient(ts.URL, &stdout, &stderr)
375+
c.Fields = "key" // Simulates global --fields key
376+
377+
ctx := client.NewContext(t.Context(), c)
378+
cmd := &cobra.Command{Use: "batch"}
379+
cmd.Flags().String("input", "", "")
380+
cmd.SetContext(ctx)
381+
382+
bop := BatchOp{Command: "issue get", Args: tt.args}
383+
result := executeBatchOp(cmd, c, 0, bop, opMap)
384+
385+
if result.ExitCode != jrerrors.ExitOK {
386+
t.Fatalf("expected exit 0, got %d; error=%s", result.ExitCode, string(result.Error))
387+
}
388+
389+
var data map[string]string
390+
if err := json.Unmarshal(result.Data, &data); err != nil {
391+
t.Fatalf("failed to parse result data: %v (raw: %s)", err, string(result.Data))
392+
}
393+
394+
if data["fields_received"] != tt.wantFields {
395+
t.Errorf("expected fields %q, got %q", tt.wantFields, data["fields_received"])
396+
}
397+
})
435398
}
436399
}
437400

0 commit comments

Comments
 (0)