Skip to content

Commit 36802d5

Browse files
committed
update tests to use new mock pattern
1 parent 2c8823d commit 36802d5

File tree

1 file changed

+46
-108
lines changed

1 file changed

+46
-108
lines changed

pkg/github/projects_test.go

Lines changed: 46 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,15 +1565,9 @@ func Test_ProjectsList_ListProjects(t *testing.T) {
15651565
}{
15661566
{
15671567
name: "success organization",
1568-
mockedClient: mock.NewMockedHTTPClient(
1569-
mock.WithRequestMatchHandler(
1570-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2", Method: http.MethodGet},
1571-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1572-
w.WriteHeader(http.StatusOK)
1573-
_, _ = w.Write(mock.MustMarshal(orgProjects))
1574-
}),
1575-
),
1576-
),
1568+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1569+
GetOrgsProjectsV2: mockResponse(t, http.StatusOK, orgProjects),
1570+
}),
15771571
requestArgs: map[string]any{
15781572
"method": "list_projects",
15791573
"owner": "octo-org",
@@ -1584,15 +1578,9 @@ func Test_ProjectsList_ListProjects(t *testing.T) {
15841578
},
15851579
{
15861580
name: "success user",
1587-
mockedClient: mock.NewMockedHTTPClient(
1588-
mock.WithRequestMatchHandler(
1589-
mock.EndpointPattern{Pattern: "/users/{username}/projectsV2", Method: http.MethodGet},
1590-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1591-
w.WriteHeader(http.StatusOK)
1592-
_, _ = w.Write(mock.MustMarshal(userProjects))
1593-
}),
1594-
),
1595-
),
1581+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1582+
GetUsersProjectsV2ByUsername: mockResponse(t, http.StatusOK, userProjects),
1583+
}),
15961584
requestArgs: map[string]any{
15971585
"method": "list_projects",
15981586
"owner": "octocat",
@@ -1603,7 +1591,7 @@ func Test_ProjectsList_ListProjects(t *testing.T) {
16031591
},
16041592
{
16051593
name: "missing required parameter method",
1606-
mockedClient: mock.NewMockedHTTPClient(),
1594+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
16071595
requestArgs: map[string]any{
16081596
"owner": "octo-org",
16091597
"owner_type": "org",
@@ -1613,7 +1601,7 @@ func Test_ProjectsList_ListProjects(t *testing.T) {
16131601
},
16141602
{
16151603
name: "unknown method",
1616-
mockedClient: mock.NewMockedHTTPClient(),
1604+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
16171605
requestArgs: map[string]any{
16181606
"method": "unknown_method",
16191607
"owner": "octo-org",
@@ -1662,15 +1650,9 @@ func Test_ProjectsList_ListProjectFields(t *testing.T) {
16621650
fields := []map[string]any{{"id": 101, "name": "Status", "data_type": "single_select"}}
16631651

16641652
t.Run("success organization", func(t *testing.T) {
1665-
mockedClient := mock.NewMockedHTTPClient(
1666-
mock.WithRequestMatchHandler(
1667-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/fields", Method: http.MethodGet},
1668-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1669-
w.WriteHeader(http.StatusOK)
1670-
_, _ = w.Write(mock.MustMarshal(fields))
1671-
}),
1672-
),
1673-
)
1653+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1654+
GetOrgsProjectsV2FieldsByProject: mockResponse(t, http.StatusOK, fields),
1655+
})
16741656

16751657
client := gh.NewClient(mockedClient)
16761658
deps := BaseDeps{
@@ -1698,7 +1680,7 @@ func Test_ProjectsList_ListProjectFields(t *testing.T) {
16981680
})
16991681

17001682
t.Run("missing project_number", func(t *testing.T) {
1701-
mockedClient := mock.NewMockedHTTPClient()
1683+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
17021684
client := gh.NewClient(mockedClient)
17031685
deps := BaseDeps{
17041686
Client: client,
@@ -1724,15 +1706,9 @@ func Test_ProjectsList_ListProjectItems(t *testing.T) {
17241706
items := []map[string]any{{"id": 1001, "archived_at": nil, "content": map[string]any{"title": "Issue 1"}}}
17251707

17261708
t.Run("success organization", func(t *testing.T) {
1727-
mockedClient := mock.NewMockedHTTPClient(
1728-
mock.WithRequestMatchHandler(
1729-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/items", Method: http.MethodGet},
1730-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1731-
w.WriteHeader(http.StatusOK)
1732-
_, _ = w.Write(mock.MustMarshal(items))
1733-
}),
1734-
),
1735-
)
1709+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1710+
GetOrgsProjectsV2ItemsByProject: mockResponse(t, http.StatusOK, items),
1711+
})
17361712

17371713
client := gh.NewClient(mockedClient)
17381714
deps := BaseDeps{
@@ -1783,15 +1759,9 @@ func Test_ProjectsGet_GetProject(t *testing.T) {
17831759
project := map[string]any{"id": 123, "title": "Project Title"}
17841760

17851761
t.Run("success organization", func(t *testing.T) {
1786-
mockedClient := mock.NewMockedHTTPClient(
1787-
mock.WithRequestMatchHandler(
1788-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1", Method: http.MethodGet},
1789-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1790-
w.WriteHeader(http.StatusOK)
1791-
_, _ = w.Write(mock.MustMarshal(project))
1792-
}),
1793-
),
1794-
)
1762+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1763+
GetOrgsProjectsV2ByProject: mockResponse(t, http.StatusOK, project),
1764+
})
17951765

17961766
client := gh.NewClient(mockedClient)
17971767
deps := BaseDeps{
@@ -1817,7 +1787,7 @@ func Test_ProjectsGet_GetProject(t *testing.T) {
18171787
})
18181788

18191789
t.Run("unknown method", func(t *testing.T) {
1820-
mockedClient := mock.NewMockedHTTPClient()
1790+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
18211791
client := gh.NewClient(mockedClient)
18221792
deps := BaseDeps{
18231793
Client: client,
@@ -1844,15 +1814,9 @@ func Test_ProjectsGet_GetProjectField(t *testing.T) {
18441814
field := map[string]any{"id": 101, "name": "Status", "data_type": "single_select"}
18451815

18461816
t.Run("success organization", func(t *testing.T) {
1847-
mockedClient := mock.NewMockedHTTPClient(
1848-
mock.WithRequestMatchHandler(
1849-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/fields/101", Method: http.MethodGet},
1850-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1851-
w.WriteHeader(http.StatusOK)
1852-
_, _ = w.Write(mock.MustMarshal(field))
1853-
}),
1854-
),
1855-
)
1817+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1818+
GetOrgsProjectsV2FieldsByProjectByFieldID: mockResponse(t, http.StatusOK, field),
1819+
})
18561820

18571821
client := gh.NewClient(mockedClient)
18581822
deps := BaseDeps{
@@ -1879,7 +1843,7 @@ func Test_ProjectsGet_GetProjectField(t *testing.T) {
18791843
})
18801844

18811845
t.Run("missing field_id", func(t *testing.T) {
1882-
mockedClient := mock.NewMockedHTTPClient()
1846+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
18831847
client := gh.NewClient(mockedClient)
18841848
deps := BaseDeps{
18851849
Client: client,
@@ -1906,15 +1870,9 @@ func Test_ProjectsGet_GetProjectItem(t *testing.T) {
19061870
item := map[string]any{"id": 1001, "archived_at": nil, "content": map[string]any{"title": "Issue 1"}}
19071871

19081872
t.Run("success organization", func(t *testing.T) {
1909-
mockedClient := mock.NewMockedHTTPClient(
1910-
mock.WithRequestMatchHandler(
1911-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/items/1001", Method: http.MethodGet},
1912-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1913-
w.WriteHeader(http.StatusOK)
1914-
_, _ = w.Write(mock.MustMarshal(item))
1915-
}),
1916-
),
1917-
)
1873+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1874+
GetOrgsProjectsV2ItemsByProjectByItemID: mockResponse(t, http.StatusOK, item),
1875+
})
19181876

19191877
client := gh.NewClient(mockedClient)
19201878
deps := BaseDeps{
@@ -1941,7 +1899,7 @@ func Test_ProjectsGet_GetProjectItem(t *testing.T) {
19411899
})
19421900

19431901
t.Run("missing item_id", func(t *testing.T) {
1944-
mockedClient := mock.NewMockedHTTPClient()
1902+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
19451903
client := gh.NewClient(mockedClient)
19461904
deps := BaseDeps{
19471905
Client: client,
@@ -1991,23 +1949,12 @@ func Test_ProjectsWrite_AddProjectItem(t *testing.T) {
19911949
addedItem := map[string]any{"id": 2001, "archived_at": nil}
19921950

19931951
t.Run("success organization", func(t *testing.T) {
1994-
mockedClient := mock.NewMockedHTTPClient(
1995-
mock.WithRequestMatchHandler(
1996-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/items", Method: http.MethodPost},
1997-
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1998-
body, _ := io.ReadAll(r.Body)
1999-
var payload map[string]any
2000-
_ = json.Unmarshal(body, &payload)
2001-
if payload["id"] == nil || payload["type"] == nil {
2002-
w.WriteHeader(http.StatusBadRequest)
2003-
_, _ = w.Write([]byte(`{"message":"bad request"}`))
2004-
return
2005-
}
2006-
w.WriteHeader(http.StatusCreated)
2007-
_, _ = w.Write(mock.MustMarshal(addedItem))
2008-
}),
2009-
),
2010-
)
1952+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1953+
PostOrgsProjectsV2ItemsByProject: expectRequestBody(t, map[string]any{
1954+
"type": "Issue",
1955+
"id": float64(123),
1956+
}).andThen(mockResponse(t, http.StatusCreated, addedItem)),
1957+
})
20111958

20121959
client := gh.NewClient(mockedClient)
20131960
deps := BaseDeps{
@@ -2035,7 +1982,7 @@ func Test_ProjectsWrite_AddProjectItem(t *testing.T) {
20351982
})
20361983

20371984
t.Run("missing item_type", func(t *testing.T) {
2038-
mockedClient := mock.NewMockedHTTPClient()
1985+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
20391986
client := gh.NewClient(mockedClient)
20401987
deps := BaseDeps{
20411988
Client: client,
@@ -2057,7 +2004,7 @@ func Test_ProjectsWrite_AddProjectItem(t *testing.T) {
20572004
})
20582005

20592006
t.Run("invalid item_type", func(t *testing.T) {
2060-
mockedClient := mock.NewMockedHTTPClient()
2007+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
20612008
client := gh.NewClient(mockedClient)
20622009
deps := BaseDeps{
20632010
Client: client,
@@ -2080,7 +2027,7 @@ func Test_ProjectsWrite_AddProjectItem(t *testing.T) {
20802027
})
20812028

20822029
t.Run("unknown method", func(t *testing.T) {
2083-
mockedClient := mock.NewMockedHTTPClient()
2030+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
20842031
client := gh.NewClient(mockedClient)
20852032
deps := BaseDeps{
20862033
Client: client,
@@ -2107,15 +2054,9 @@ func Test_ProjectsWrite_UpdateProjectItem(t *testing.T) {
21072054
updatedItem := map[string]any{"id": 1001, "archived_at": nil}
21082055

21092056
t.Run("success organization", func(t *testing.T) {
2110-
mockedClient := mock.NewMockedHTTPClient(
2111-
mock.WithRequestMatchHandler(
2112-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/items/1001", Method: http.MethodPatch},
2113-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2114-
w.WriteHeader(http.StatusOK)
2115-
_, _ = w.Write(mock.MustMarshal(updatedItem))
2116-
}),
2117-
),
2118-
)
2057+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
2058+
PatchOrgsProjectsV2ItemsByProjectByItemID: mockResponse(t, http.StatusOK, updatedItem),
2059+
})
21192060

21202061
client := gh.NewClient(mockedClient)
21212062
deps := BaseDeps{
@@ -2146,7 +2087,7 @@ func Test_ProjectsWrite_UpdateProjectItem(t *testing.T) {
21462087
})
21472088

21482089
t.Run("missing updated_field", func(t *testing.T) {
2149-
mockedClient := mock.NewMockedHTTPClient()
2090+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
21502091
client := gh.NewClient(mockedClient)
21512092
deps := BaseDeps{
21522093
Client: client,
@@ -2172,14 +2113,11 @@ func Test_ProjectsWrite_DeleteProjectItem(t *testing.T) {
21722113
toolDef := ProjectsWrite(translations.NullTranslationHelper)
21732114

21742115
t.Run("success organization", func(t *testing.T) {
2175-
mockedClient := mock.NewMockedHTTPClient(
2176-
mock.WithRequestMatchHandler(
2177-
mock.EndpointPattern{Pattern: "/orgs/{org}/projectsV2/1/items/1001", Method: http.MethodDelete},
2178-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2179-
w.WriteHeader(http.StatusNoContent)
2180-
}),
2181-
),
2182-
)
2116+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
2117+
DeleteOrgsProjectsV2ItemsByProjectByItemID: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
2118+
w.WriteHeader(http.StatusNoContent)
2119+
}),
2120+
})
21832121

21842122
client := gh.NewClient(mockedClient)
21852123
deps := BaseDeps{
@@ -2203,7 +2141,7 @@ func Test_ProjectsWrite_DeleteProjectItem(t *testing.T) {
22032141
})
22042142

22052143
t.Run("missing item_id", func(t *testing.T) {
2206-
mockedClient := mock.NewMockedHTTPClient()
2144+
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{})
22072145
client := gh.NewClient(mockedClient)
22082146
deps := BaseDeps{
22092147
Client: client,

0 commit comments

Comments
 (0)