Skip to content

Commit e716aa6

Browse files
authored
Merge pull request #9 from ntsk/refactor/separate-mock-from-production
Move MockClient from production code to test file
2 parents b7e0953 + 9ca9f65 commit e716aa6

File tree

2 files changed

+62
-26
lines changed

2 files changed

+62
-26
lines changed

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: 56 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,33 @@ func TestMockClient(t *testing.T) {
7096
t.Errorf("Expected custom repo 'custom/repo', got '%s'", repo)
7197
}
7298
}
99+
100+
// TestClientInterface demonstrates that MockClient implements ClientInterface
101+
func TestClientInterface(t *testing.T) {
102+
// Verify that MockClient implements the interface by using it
103+
var client ClientInterface = &MockClient{}
104+
105+
// Test that we can call interface methods
106+
issue := &models.Issue{
107+
Title: "Interface Test",
108+
Body: "Testing interface implementation",
109+
}
110+
111+
response, err := client.CreateIssue(issue, "test/repo")
112+
if err != nil {
113+
t.Errorf("Expected no error, got: %v", err)
114+
}
115+
116+
if response.Number != 1 {
117+
t.Errorf("Expected issue number 1, got %d", response.Number)
118+
}
119+
120+
repo, err := client.GetCurrentRepository()
121+
if err != nil {
122+
t.Errorf("Expected no error, got: %v", err)
123+
}
124+
125+
if repo != "mock/repo" {
126+
t.Errorf("Expected repo 'mock/repo', got '%s'", repo)
127+
}
128+
}

0 commit comments

Comments
 (0)