Skip to content

Commit 1496393

Browse files
committed
Refactor: move MockClient from production code to test file
1 parent b7e0953 commit 1496393

2 files changed

Lines changed: 52 additions & 26 deletions

File tree

internal/github/client.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"github.com/ntsk/gh-issue-bulk-create/pkg/models"
1313
)
1414

15+
// ClientInterface defines the interface for GitHub API operations
16+
type ClientInterface interface {
17+
CreateIssue(issue *models.Issue, repo string) (*models.IssueResponse, error)
18+
GetCurrentRepository() (string, error)
19+
}
20+
1521
// Client provides GitHub API functionality
1622
type Client struct {
1723
client *api.RESTClient
@@ -88,29 +94,3 @@ func (c *Client) GetCurrentRepository() (string, error) {
8894

8995
return fmt.Sprintf("%s/%s", info.Owner.Login, info.Name), nil
9096
}
91-
92-
// MockClient provides a mock GitHub client for testing
93-
type MockClient struct {
94-
CreateIssueFunc func(issue *models.Issue, repo string) (*models.IssueResponse, error)
95-
GetCurrentRepoFunc func() (string, error)
96-
CreatedIssues []*models.Issue
97-
GetCurrentRepoCounter int
98-
}
99-
100-
// CreateIssue implements the Client interface for testing
101-
func (m *MockClient) CreateIssue(issue *models.Issue, repo string) (*models.IssueResponse, error) {
102-
m.CreatedIssues = append(m.CreatedIssues, issue)
103-
if m.CreateIssueFunc != nil {
104-
return m.CreateIssueFunc(issue, repo)
105-
}
106-
return &models.IssueResponse{Number: 1, URL: "https://github.com/mock/repo/issues/1"}, nil
107-
}
108-
109-
// GetCurrentRepository implements the Client interface for testing
110-
func (m *MockClient) GetCurrentRepository() (string, error) {
111-
m.GetCurrentRepoCounter++
112-
if m.GetCurrentRepoFunc != nil {
113-
return m.GetCurrentRepoFunc()
114-
}
115-
return "mock/repo", nil
116-
}

internal/github/client_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ import (
66
"github.com/ntsk/gh-issue-bulk-create/pkg/models"
77
)
88

9+
// MockClient provides a mock GitHub client for testing
10+
type MockClient struct {
11+
CreateIssueFunc func(issue *models.Issue, repo string) (*models.IssueResponse, error)
12+
GetCurrentRepoFunc func() (string, error)
13+
CreatedIssues []*models.Issue
14+
GetCurrentRepoCounter int
15+
}
16+
17+
// CreateIssue implements the ClientInterface for testing
18+
func (m *MockClient) CreateIssue(issue *models.Issue, repo string) (*models.IssueResponse, error) {
19+
m.CreatedIssues = append(m.CreatedIssues, issue)
20+
if m.CreateIssueFunc != nil {
21+
return m.CreateIssueFunc(issue, repo)
22+
}
23+
return &models.IssueResponse{Number: 1, URL: "https://github.com/mock/repo/issues/1"}, nil
24+
}
25+
26+
// GetCurrentRepository implements the ClientInterface for testing
27+
func (m *MockClient) GetCurrentRepository() (string, error) {
28+
m.GetCurrentRepoCounter++
29+
if m.GetCurrentRepoFunc != nil {
30+
return m.GetCurrentRepoFunc()
31+
}
32+
return "mock/repo", nil
33+
}
34+
935
func TestMockClient(t *testing.T) {
1036
// Create mock client
1137
mockClient := &MockClient{}
@@ -70,3 +96,23 @@ func TestMockClient(t *testing.T) {
7096
t.Errorf("Expected custom repo 'custom/repo', got '%s'", repo)
7197
}
7298
}
99+
100+
// TestClientInterface demonstrates that both Client and MockClient implement ClientInterface
101+
func TestClientInterface(t *testing.T) {
102+
// Verify that both types implement the interface
103+
var client ClientInterface
104+
105+
// Test with MockClient
106+
mockClient := &MockClient{}
107+
client = mockClient
108+
if client == nil {
109+
t.Error("MockClient should implement ClientInterface")
110+
}
111+
112+
// Test with real Client (this would fail without proper initialization in tests)
113+
// realClient := &Client{}
114+
// client = realClient
115+
// if client == nil {
116+
// t.Error("Client should implement ClientInterface")
117+
// }
118+
}

0 commit comments

Comments
 (0)