Skip to content

Commit 9993f3d

Browse files
authored
[Internal] Move some tests from HTTPFixture to Go SDK mocks, part 2 (#4648)
NO_CHANGELOG=true ## Changes <!-- Summary of your changes that are easy to understand --> ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [x] using Go SDK - [ ] using TF Plugin Framework
1 parent 996dd25 commit 9993f3d

9 files changed

+342
-298
lines changed

catalog/data_current_metastore_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ package catalog
33
import (
44
"testing"
55

6+
"github.com/databricks/databricks-sdk-go/apierr"
7+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
68
"github.com/databricks/databricks-sdk-go/service/catalog"
79
"github.com/databricks/terraform-provider-databricks/qa"
10+
"github.com/stretchr/testify/mock"
811
)
912

1013
func TestCurrentMetastoreDataVerify(t *testing.T) {
1114
qa.ResourceFixture{
12-
Fixtures: []qa.HTTPFixture{
13-
{
14-
Method: "GET",
15-
Resource: "/api/2.1/unity-catalog/metastore_summary",
16-
Response: catalog.GetMetastoreSummaryResponse{
15+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
16+
w.GetMockMetastoresAPI().EXPECT().
17+
Summary(mock.Anything).
18+
Return(&catalog.GetMetastoreSummaryResponse{
1719
Name: "xyz",
1820
MetastoreId: "abc",
1921
Owner: "pqr",
2022
Cloud: "aws",
21-
},
22-
},
23+
}, nil)
2324
},
2425
Resource: DataSourceCurrentMetastore(),
2526
Read: true,
@@ -35,10 +36,17 @@ func TestCurrentMetastoreDataVerify(t *testing.T) {
3536

3637
func TestCurrentMetastoreDataError(t *testing.T) {
3738
qa.ResourceFixture{
38-
Fixtures: qa.HTTPFailures,
39+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
40+
w.GetMockMetastoresAPI().EXPECT().
41+
Summary(mock.Anything).
42+
Return(nil, &apierr.APIError{
43+
ErrorCode: "BAD_REQUEST",
44+
Message: "Bad request: unable to get metastore summary",
45+
})
46+
},
3947
Resource: DataSourceCurrentMetastore(),
4048
Read: true,
4149
NonWritable: true,
4250
ID: "_",
43-
}.ExpectError(t, "i'm a teapot")
51+
}.ExpectError(t, "Bad request: unable to get metastore summary")
4452
}

catalog/data_metastores_test.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,34 @@ package catalog
33
import (
44
"testing"
55

6+
"github.com/databricks/databricks-sdk-go/apierr"
7+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
68
"github.com/databricks/databricks-sdk-go/service/catalog"
79
"github.com/databricks/terraform-provider-databricks/qa"
10+
"github.com/stretchr/testify/mock"
811
)
912

1013
func TestMetastoresDataContainsName(t *testing.T) {
1114
qa.ResourceFixture{
12-
Fixtures: []qa.HTTPFixture{
13-
{
14-
Method: "GET",
15-
Resource: "/api/2.0/accounts/testaccount/metastores",
16-
Response: catalog.ListMetastoresResponse{
17-
Metastores: []catalog.MetastoreInfo{
18-
{
19-
Name: "a",
20-
StorageRoot: "abc",
21-
DefaultDataAccessConfigId: "sth",
22-
23-
MetastoreId: "abc",
24-
},
25-
{
26-
Name: "b",
27-
StorageRoot: "dcw",
28-
DefaultDataAccessConfigId: "sth",
29-
30-
MetastoreId: "ded",
31-
},
15+
MockAccountClientFunc: func(a *mocks.MockAccountClient) {
16+
a.GetMockAccountMetastoresAPI().EXPECT().
17+
ListAll(mock.Anything).
18+
Return([]catalog.MetastoreInfo{
19+
{
20+
Name: "a",
21+
StorageRoot: "abc",
22+
DefaultDataAccessConfigId: "sth",
23+
24+
MetastoreId: "abc",
3225
},
33-
},
34-
},
26+
{
27+
Name: "b",
28+
StorageRoot: "dcw",
29+
DefaultDataAccessConfigId: "sth",
30+
31+
MetastoreId: "ded",
32+
},
33+
}, nil)
3534
},
3635
Resource: DataSourceMetastores(),
3736
Read: true,
@@ -45,11 +44,18 @@ func TestMetastoresDataContainsName(t *testing.T) {
4544

4645
func TestMetastoresData_Error(t *testing.T) {
4746
qa.ResourceFixture{
48-
Fixtures: qa.HTTPFailures,
47+
MockAccountClientFunc: func(a *mocks.MockAccountClient) {
48+
a.GetMockAccountMetastoresAPI().EXPECT().
49+
ListAll(mock.Anything).
50+
Return(nil, &apierr.APIError{
51+
ErrorCode: "BAD_REQUEST",
52+
Message: "Bad request: unable to list metastores",
53+
})
54+
},
4955
Resource: DataSourceMetastores(),
5056
Read: true,
5157
NonWritable: true,
5258
ID: "_",
5359
AccountID: "_",
54-
}.ExpectError(t, "i'm a teapot")
60+
}.ExpectError(t, "Bad request: unable to list metastores")
5561
}

catalog/data_tables_test.go

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@ package catalog
33
import (
44
"testing"
55

6+
"github.com/databricks/databricks-sdk-go/apierr"
7+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
68
"github.com/databricks/databricks-sdk-go/service/catalog"
79
"github.com/databricks/terraform-provider-databricks/qa"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/mock"
1013
"github.com/stretchr/testify/require"
1114
)
1215

1316
func TestTablesData(t *testing.T) {
1417
qa.ResourceFixture{
15-
Fixtures: []qa.HTTPFixture{
16-
{
17-
Method: "GET",
18-
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
19-
Response: catalog.ListTablesResponse{
20-
Tables: []catalog.TableInfo{
21-
{
22-
FullName: "a.b.c",
23-
Name: "c",
24-
},
25-
{
26-
FullName: "a.b.d",
27-
Name: "d",
28-
},
18+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
19+
w.GetMockTablesAPI().EXPECT().
20+
ListAll(mock.Anything, catalog.ListTablesRequest{
21+
CatalogName: "a",
22+
SchemaName: "b",
23+
}).
24+
Return([]catalog.TableInfo{
25+
{
26+
FullName: "a.b.c",
27+
Name: "c",
28+
TableType: "TABLE",
2929
},
30-
},
31-
},
30+
{
31+
FullName: "a.b.d",
32+
Name: "d",
33+
TableType: "TABLE",
34+
},
35+
}, nil)
3236
},
3337
Resource: DataSourceTables(),
3438
HCL: `
@@ -37,30 +41,33 @@ func TestTablesData(t *testing.T) {
3741
Read: true,
3842
NonWritable: true,
3943
ID: "_",
40-
}.ApplyNoError(t)
44+
}.ApplyAndExpectData(t, map[string]any{
45+
"ids": []string{"a.b.c", "a.b.d"},
46+
})
4147
}
4248

4349
// https://github.com/databricks/terraform-provider-databricks/issues/1264
4450
func TestTablesDataIssue1264(t *testing.T) {
4551
r := DataSourceTables()
4652
d, err := qa.ResourceFixture{
47-
Fixtures: []qa.HTTPFixture{
48-
{
49-
Method: "GET",
50-
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
51-
Response: catalog.ListTablesResponse{
52-
Tables: []catalog.TableInfo{
53-
{
54-
Name: "a",
55-
FullName: "a.b.a",
56-
},
57-
{
58-
Name: "b",
59-
FullName: "a.b.b",
60-
},
53+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
54+
w.GetMockTablesAPI().EXPECT().
55+
ListAll(mock.Anything, catalog.ListTablesRequest{
56+
CatalogName: "a",
57+
SchemaName: "b",
58+
}).
59+
Return([]catalog.TableInfo{
60+
{
61+
Name: "a",
62+
FullName: "a.b.a",
63+
TableType: "TABLE",
64+
},
65+
{
66+
Name: "b",
67+
FullName: "a.b.b",
68+
TableType: "TABLE",
6169
},
62-
},
63-
},
70+
}, nil)
6471
},
6572
Resource: r,
6673
HCL: `
@@ -76,23 +83,24 @@ func TestTablesDataIssue1264(t *testing.T) {
7683
assert.True(t, s.Contains("a.b.a"))
7784

7885
d, err = qa.ResourceFixture{
79-
Fixtures: []qa.HTTPFixture{
80-
{
81-
Method: "GET",
82-
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
83-
Response: catalog.ListTablesResponse{
84-
Tables: []catalog.TableInfo{
85-
{
86-
Name: "c",
87-
FullName: "a.b.c",
88-
},
89-
{
90-
Name: "d",
91-
FullName: "a.b.d",
92-
},
86+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
87+
w.GetMockTablesAPI().EXPECT().
88+
ListAll(mock.Anything, catalog.ListTablesRequest{
89+
CatalogName: "a",
90+
SchemaName: "b",
91+
}).
92+
Return([]catalog.TableInfo{
93+
{
94+
Name: "c",
95+
FullName: "a.b.c",
96+
TableType: "TABLE",
9397
},
94-
},
95-
},
98+
{
99+
Name: "d",
100+
FullName: "a.b.d",
101+
TableType: "TABLE",
102+
},
103+
}, nil)
96104
},
97105
Resource: r,
98106
HCL: `
@@ -110,10 +118,20 @@ func TestTablesDataIssue1264(t *testing.T) {
110118

111119
func TestTablesData_Error(t *testing.T) {
112120
qa.ResourceFixture{
113-
Fixtures: qa.HTTPFailures,
121+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
122+
w.GetMockTablesAPI().EXPECT().
123+
ListAll(mock.Anything, catalog.ListTablesRequest{
124+
CatalogName: "",
125+
SchemaName: "",
126+
}).
127+
Return(nil, &apierr.APIError{
128+
ErrorCode: "BAD_REQUEST",
129+
Message: "Bad request: unable to list tables",
130+
})
131+
},
114132
Resource: DataSourceTables(),
115133
Read: true,
116134
NonWritable: true,
117135
ID: "_",
118-
}.ExpectError(t, "i'm a teapot")
136+
}.ExpectError(t, "Bad request: unable to list tables")
119137
}

catalog/data_views_test.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@ package catalog
33
import (
44
"testing"
55

6+
"github.com/databricks/databricks-sdk-go/apierr"
7+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
68
"github.com/databricks/databricks-sdk-go/service/catalog"
79
"github.com/databricks/terraform-provider-databricks/qa"
10+
"github.com/stretchr/testify/mock"
811
)
912

1013
func TestViewsData(t *testing.T) {
1114
qa.ResourceFixture{
12-
Fixtures: []qa.HTTPFixture{
13-
{
14-
Method: "GET",
15-
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
16-
Response: catalog.ListTablesResponse{
17-
Tables: []catalog.TableInfo{
18-
{
19-
CatalogName: "a",
20-
SchemaName: "b",
21-
Name: "c",
22-
FullName: "a.b.c",
23-
TableType: "MANAGED",
24-
},
25-
{
26-
CatalogName: "a",
27-
SchemaName: "b",
28-
Name: "d",
29-
FullName: "a.b.d",
30-
TableType: "VIEW",
31-
},
15+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
16+
w.GetMockTablesAPI().EXPECT().
17+
ListAll(mock.Anything, catalog.ListTablesRequest{
18+
CatalogName: "a",
19+
SchemaName: "b",
20+
}).
21+
Return([]catalog.TableInfo{
22+
{
23+
CatalogName: "a",
24+
SchemaName: "b",
25+
Name: "c",
26+
FullName: "a.b.c",
27+
TableType: "MANAGED",
3228
},
33-
},
34-
},
29+
{
30+
CatalogName: "a",
31+
SchemaName: "b",
32+
Name: "d",
33+
FullName: "a.b.d",
34+
TableType: "VIEW",
35+
},
36+
}, nil)
3537
},
3638
Resource: DataSourceViews(),
3739
HCL: `
@@ -47,10 +49,20 @@ func TestViewsData(t *testing.T) {
4749

4850
func TestViewsData_Error(t *testing.T) {
4951
qa.ResourceFixture{
50-
Fixtures: qa.HTTPFailures,
52+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
53+
w.GetMockTablesAPI().EXPECT().
54+
ListAll(mock.Anything, catalog.ListTablesRequest{
55+
CatalogName: "",
56+
SchemaName: "",
57+
}).
58+
Return(nil, &apierr.APIError{
59+
ErrorCode: "BAD_REQUEST",
60+
Message: "Bad request: unable to list views",
61+
})
62+
},
5163
Resource: DataSourceViews(),
5264
Read: true,
5365
NonWritable: true,
5466
ID: "_",
55-
}.ExpectError(t, "i'm a teapot")
67+
}.ExpectError(t, "Bad request: unable to list views")
5668
}

0 commit comments

Comments
 (0)