Skip to content

Commit de3dbda

Browse files
refactor: simplifies unit test
1 parent 77bc751 commit de3dbda

File tree

1 file changed

+36
-111
lines changed

1 file changed

+36
-111
lines changed

bundle/config/validate/for_each_task_test.go

Lines changed: 36 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -47,88 +47,55 @@ func createBundleWithForEachTask(parentTask jobs.Task) *bundle.Bundle {
4747
return b
4848
}
4949

50-
func TestForEachTask_InvalidRetryFields(t *testing.T) {
51-
tests := []struct {
52-
name string
53-
task jobs.Task
54-
expectedSeverity diag.Severity
55-
expectedSummary string
56-
expectedDetail string
57-
}{
58-
{
59-
name: "max_retries on parent",
60-
task: jobs.Task{
61-
TaskKey: "parent_task",
62-
MaxRetries: 3,
63-
},
64-
expectedSeverity: diag.Error,
65-
expectedSummary: "Invalid max_retries configuration for for_each_task",
66-
expectedDetail: "max_retries must be defined on the nested task",
67-
},
68-
{
69-
name: "min_retry_interval_millis on parent",
70-
task: jobs.Task{
71-
TaskKey: "parent_task",
72-
MinRetryIntervalMillis: 1000,
73-
},
74-
expectedSeverity: diag.Warning,
75-
expectedSummary: "Invalid min_retry_interval_millis configuration for for_each_task",
76-
expectedDetail: "min_retry_interval_millis must be defined on the nested task",
77-
},
78-
{
79-
name: "retry_on_timeout on parent",
80-
task: jobs.Task{
81-
TaskKey: "parent_task",
82-
RetryOnTimeout: true,
83-
},
84-
expectedSeverity: diag.Warning,
85-
expectedSummary: "Invalid retry_on_timeout configuration for for_each_task",
86-
expectedDetail: "retry_on_timeout must be defined on the nested task",
87-
},
88-
}
89-
90-
for _, tt := range tests {
91-
t.Run(tt.name, func(t *testing.T) {
92-
ctx := context.Background()
93-
b := createBundleWithForEachTask(tt.task)
50+
func TestForEachTask_MaxRetriesError(t *testing.T) {
51+
ctx := context.Background()
52+
b := createBundleWithForEachTask(jobs.Task{
53+
TaskKey: "parent_task",
54+
MaxRetries: 3,
55+
})
9456

95-
diags := ForEachTask().Apply(ctx, b)
57+
diags := ForEachTask().Apply(ctx, b)
9658

97-
require.Len(t, diags, 1)
98-
assert.Equal(t, tt.expectedSeverity, diags[0].Severity)
99-
assert.Equal(t, tt.expectedSummary, diags[0].Summary)
100-
assert.Contains(t, diags[0].Detail, tt.expectedDetail)
101-
})
102-
}
59+
require.Len(t, diags, 1)
60+
assert.Equal(t, diag.Error, diags[0].Severity)
61+
assert.Equal(t, "Invalid max_retries configuration for for_each_task", diags[0].Summary)
62+
assert.Contains(t, diags[0].Detail, `Task "parent_task" has max_retries defined at the parent level`)
63+
assert.Contains(t, diags[0].Detail, "for_each_task.task.max_retries")
10364
}
10465

105-
func TestForEachTask_MultipleRetryFieldsOnParent(t *testing.T) {
66+
func TestForEachTask_MinRetryIntervalWarning(t *testing.T) {
10667
ctx := context.Background()
10768
b := createBundleWithForEachTask(jobs.Task{
10869
TaskKey: "parent_task",
109-
MaxRetries: 3,
11070
MinRetryIntervalMillis: 1000,
111-
RetryOnTimeout: true,
11271
})
11372

11473
diags := ForEachTask().Apply(ctx, b)
115-
require.Len(t, diags, 3)
116-
117-
errorCount := 0
118-
warningCount := 0
119-
for _, d := range diags {
120-
switch d.Severity {
121-
case diag.Error:
122-
errorCount++
123-
case diag.Warning:
124-
warningCount++
125-
}
126-
}
127-
assert.Equal(t, 1, errorCount)
128-
assert.Equal(t, 2, warningCount)
74+
75+
require.Len(t, diags, 1)
76+
assert.Equal(t, diag.Warning, diags[0].Severity)
77+
assert.Equal(t, "Invalid min_retry_interval_millis configuration for for_each_task", diags[0].Summary)
78+
assert.Contains(t, diags[0].Detail, `Task "parent_task" has min_retry_interval_millis defined at the parent level`)
79+
assert.Contains(t, diags[0].Detail, "for_each_task.task.min_retry_interval_millis")
80+
}
81+
82+
func TestForEachTask_RetryOnTimeoutWarning(t *testing.T) {
83+
ctx := context.Background()
84+
b := createBundleWithForEachTask(jobs.Task{
85+
TaskKey: "parent_task",
86+
RetryOnTimeout: true,
87+
})
88+
89+
diags := ForEachTask().Apply(ctx, b)
90+
91+
require.Len(t, diags, 1)
92+
assert.Equal(t, diag.Warning, diags[0].Severity)
93+
assert.Equal(t, "Invalid retry_on_timeout configuration for for_each_task", diags[0].Summary)
94+
assert.Contains(t, diags[0].Detail, `Task "parent_task" has retry_on_timeout defined at the parent level`)
95+
assert.Contains(t, diags[0].Detail, "for_each_task.task.retry_on_timeout")
12996
}
13097

131-
func TestForEachTask_ValidConfigurationOnChild(t *testing.T) {
98+
func TestForEachTask_ValidConfiguration(t *testing.T) {
13299
ctx := context.Background()
133100
b := createBundleWithForEachTask(jobs.Task{
134101
TaskKey: "parent_task",
@@ -147,45 +114,3 @@ func TestForEachTask_ValidConfigurationOnChild(t *testing.T) {
147114
diags := ForEachTask().Apply(ctx, b)
148115
assert.Empty(t, diags)
149116
}
150-
151-
func TestForEachTask_NoForEachTask(t *testing.T) {
152-
ctx := context.Background()
153-
b := &bundle.Bundle{
154-
Config: config.Root{
155-
Resources: config.Resources{
156-
Jobs: map[string]*resources.Job{
157-
"job1": {
158-
JobSettings: jobs.JobSettings{
159-
Name: "My Job",
160-
Tasks: []jobs.Task{
161-
{
162-
TaskKey: "simple_task",
163-
MaxRetries: 3,
164-
NotebookTask: &jobs.NotebookTask{
165-
NotebookPath: "test.py",
166-
},
167-
},
168-
},
169-
},
170-
},
171-
},
172-
},
173-
},
174-
}
175-
176-
bundletest.SetLocation(b, "resources.jobs.job1.tasks[0]", []dyn.Location{{File: "job.yml", Line: 1, Column: 1}})
177-
178-
diags := ForEachTask().Apply(ctx, b)
179-
assert.Empty(t, diags)
180-
}
181-
182-
func TestForEachTask_RetryOnTimeoutFalse(t *testing.T) {
183-
ctx := context.Background()
184-
b := createBundleWithForEachTask(jobs.Task{
185-
TaskKey: "parent_task",
186-
RetryOnTimeout: false,
187-
})
188-
189-
diags := ForEachTask().Apply(ctx, b)
190-
assert.Empty(t, diags)
191-
}

0 commit comments

Comments
 (0)