Skip to content

Commit 83ade9a

Browse files
committed
Increase test coverage
1 parent 3f2d3c9 commit 83ade9a

File tree

6 files changed

+89
-19
lines changed

6 files changed

+89
-19
lines changed

common/reflect_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func typeToSchema(v reflect.Value, t reflect.Type, path []string) map[string]*sc
183183
elem := typeField.Type.Elem()
184184
sv := reflect.New(elem).Elem()
185185
if strings.Contains(tfTag, "suppress_diff") {
186-
// TODO: we may also suppress count diffs on all json:"..,omitempty" (101 occurences)
186+
// TODO: we may also suppress count diffs on all json:"..,omitempty" without tf:"force_new"
187187
// find . -type f -name '*.go' -not -path "vendor/*" | xargs grep ',omitempty' | grep '*'
188188
blockCount := strings.Join(append(path, fieldName, "#"), ".")
189189
scm[fieldName].DiffSuppressFunc = makeEmptyBlockSuppressFunc(blockCount)

common/reflect_resource_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ type Dummy struct {
194194
Unique []Address `json:"unique,omitempty" tf:"slice_set"`
195195
Things []string `json:"things,omitempty" tf:"slice_set"`
196196
Tags map[string]string `json:"tags,omitempty" tf:"max_items:5"`
197-
Home *Address `json:"home,omitempty" tf:"group:v"`
197+
Home *Address `json:"home,omitempty" tf:"group:v,suppress_diff"`
198198
House *Address `json:"house,omitempty" tf:"group:v"`
199199
}
200200

@@ -368,6 +368,10 @@ func TestStructToData(t *testing.T) {
368368
assert.Equal(t, false, d.Get("enabled"))
369369
assert.Equal(t, 2, d.Get("addresses.#"))
370370

371+
assert.NotNil(t, s["home"].DiffSuppressFunc)
372+
assert.True(t, s["home"].DiffSuppressFunc("home.#", "1", "0", d))
373+
assert.False(t, s["home"].DiffSuppressFunc("home.#", "1", "1", d))
374+
371375
{
372376
//lint:ignore SA1019 Empty optional string should not be set.
373377
_, ok := d.GetOkExists("addresses.0.optional_string")

common/resource_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ func TestUpdate(t *testing.T) {
5656
},
5757
}.ToResource()
5858

59+
client := &DatabricksClient{}
60+
ctx := context.Background()
5961
d := r.TestResourceData()
60-
datas, err := r.Importer.StateContext(
61-
context.Background(), d,
62-
&DatabricksClient{})
62+
datas, err := r.Importer.StateContext(ctx, d, client)
6363
require.NoError(t, err)
6464
assert.Len(t, datas, 1)
6565
assert.False(t, r.Schema["foo"].ForceNew)
6666
assert.Equal(t, "", d.Id())
67+
68+
diags := r.UpdateContext(ctx, d, client)
69+
assert.True(t, diags.HasError())
70+
assert.Equal(t, "nope", diags[0].Summary)
6771
}

compute/resource_job.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func ResourceJob() *schema.Resource {
266266
return fmt.Errorf("`always_running` must be specified only with `max_concurrent_runs = 1`")
267267
}
268268
for _, task := range js.Tasks {
269+
if task.NewCluster == nil {
270+
continue
271+
}
269272
err = validateClusterDefinition(*task.NewCluster)
270273
if err != nil {
271274
return fmt.Errorf("task %s invalid: %w", task.TaskKey, err)

compute/resource_job_test.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func TestResourceJobCreate_MultiTask(t *testing.T) {
132132
NewCluster: &Cluster{
133133
SparkVersion: "a",
134134
NodeTypeID: "b",
135+
NumWorkers: 1,
135136
AzureAttributes: &AzureAttributes{
136137
SpotBidMaxPrice: 0.99,
137138
},
@@ -152,7 +153,16 @@ func TestResourceJobCreate_MultiTask(t *testing.T) {
152153
Resource: "/api/2.1/jobs/get?job_id=789",
153154
Response: Job{
154155
// good enough for mock
155-
Settings: &JobSettings{},
156+
Settings: &JobSettings{
157+
Tasks: []JobTaskSettings{
158+
{
159+
TaskKey: "b",
160+
},
161+
{
162+
TaskKey: "a",
163+
},
164+
},
165+
},
156166
},
157167
},
158168
},
@@ -181,6 +191,7 @@ func TestResourceJobCreate_MultiTask(t *testing.T) {
181191
new_cluster {
182192
spark_version = "a"
183193
node_type_id = "b"
194+
num_workers = 1
184195
azure_attributes {
185196
spot_bid_max_price = 0.99
186197
}
@@ -997,8 +1008,29 @@ func TestResourceJobDelete_Error(t *testing.T) {
9971008
assert.Equal(t, "789", d.Id())
9981009
}
9991010

1011+
func TestJobsAPIList(t *testing.T) {
1012+
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
1013+
{
1014+
Method: "GET",
1015+
Resource: "/api/2.0/jobs/list",
1016+
Response: JobList{
1017+
Jobs: []Job{
1018+
{
1019+
JobID: 1,
1020+
},
1021+
},
1022+
},
1023+
},
1024+
}, func(ctx context.Context, client *common.DatabricksClient) {
1025+
a := NewJobsAPI(ctx, client)
1026+
l, err := a.List()
1027+
require.NoError(t, err)
1028+
assert.Len(t, l.Jobs, 1)
1029+
})
1030+
}
1031+
10001032
func TestJobsAPIRunsList(t *testing.T) {
1001-
c, s, err := qa.HttpFixtureClient(t, []qa.HTTPFixture{
1033+
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
10021034
{
10031035
Method: "GET",
10041036
Resource: "/api/2.0/jobs/runs/list?completed_only=true&job_id=234&limit=1",
@@ -1013,17 +1045,15 @@ func TestJobsAPIRunsList(t *testing.T) {
10131045
},
10141046
},
10151047
},
1048+
}, func(ctx context.Context, client *common.DatabricksClient) {
1049+
a := NewJobsAPI(ctx, client)
1050+
l, err := a.RunsList(JobRunsListRequest{
1051+
JobID: 234,
1052+
CompletedOnly: true,
1053+
Limit: 1,
1054+
Offset: 0,
1055+
})
1056+
require.NoError(t, err)
1057+
assert.Len(t, l.Runs, 1)
10161058
})
1017-
require.NoError(t, err)
1018-
defer s.Close()
1019-
1020-
a := NewJobsAPI(context.Background(), c)
1021-
l, err := a.RunsList(JobRunsListRequest{
1022-
JobID: 234,
1023-
CompletedOnly: true,
1024-
Limit: 1,
1025-
Offset: 0,
1026-
})
1027-
require.NoError(t, err)
1028-
assert.Len(t, l.Runs, 1)
10291059
}

qa/testing_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,32 @@ func TestDiagsToString(t *testing.T) {
281281
},
282282
}))
283283
}
284+
285+
func TestResourceCornerCases(t *testing.T) {
286+
type dummy struct{}
287+
x := map[string]string{
288+
"foo": "bar",
289+
}
290+
ResourceCornerCases(t, common.Resource{
291+
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
292+
var b dummy
293+
return c.Post(ctx, "/dummy", x, &b)
294+
},
295+
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
296+
var b dummy
297+
return c.Get(ctx, "/dummy", x, &b)
298+
},
299+
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
300+
return c.Put(ctx, "/dummy", x)
301+
},
302+
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
303+
return c.Delete(ctx, "/dummy", x)
304+
},
305+
Schema: map[string]*schema.Schema{
306+
"foo": {
307+
Type: schema.TypeInt,
308+
Required: true,
309+
},
310+
},
311+
}.ToResource(), "x")
312+
}

0 commit comments

Comments
 (0)