Skip to content

Commit

Permalink
test: use fake branch provider instead of mock in pull request use case
Browse files Browse the repository at this point in the history
  • Loading branch information
hielfx committed May 28, 2024
1 parent 2bf8d09 commit a0ffd24
Showing 1 changed file with 31 additions and 49 deletions.
80 changes: 31 additions & 49 deletions internal/use_cases/create_pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ type CreatePullRequestExecutionTestSuite struct {
userInteractionProvider *domainMocks.MockUserInteractionProvider
pullRequestProvider *domainFakes.FakePullRequestProvider
// issueTracker *domainFakes.FakeIssueTracker
branchProvider *domainMocks.MockBranchProvider
branchProvider *domainFakes.FakeBranchProvider
repositoryProvider *domainFakes.FakeRepositoryProvider
}

type CreateGithubPullRequestExecutionTestSuite struct {
CreatePullRequestExecutionTestSuite
}

func (s *CreatePullRequestExecutionTestSuite) setGetBranchName(branchName string) {
s.branchProvider.EXPECT().GetBranchName(mock.Anything, mock.Anything).Return(branchName, nil).Once()
}

func TestCreateGitHubPullRequestExecutionTestSuite(t *testing.T) {
suite.Run(t, new(CreateGithubPullRequestExecutionTestSuite))
}
Expand Down Expand Up @@ -67,7 +63,10 @@ func (s *CreateGithubPullRequestExecutionTestSuite) SetupSubTest() {

s.userInteractionProvider = s.initializeUserInteractionProvider()
s.pullRequestProvider = domainFakes.NewFakePullRequestProvider()
s.branchProvider = s.initializeBranchProvider()

s.branchProvider = domainFakes.NewFakeBranchProvider()
s.branchProvider.SetBranchName(s.defaultBranchName)

s.repositoryProvider = domainFakes.NewRepositoryProvider()

defaultConfig := use_cases.CreatePullRequestConfiguration{
Expand Down Expand Up @@ -110,7 +109,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti

s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -121,7 +120,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti

s.Run("should exit if user does not confirm current branch", func() {
s.gitProvider.CurrentBranch = s.defaultBranchName
s.setGetBranchName(s.defaultBranchName)

mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation(mock.Anything, mock.Anything).Return(false, nil).Once()
Expand All @@ -137,7 +135,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.pullRequestProvider.AddPullRequest(branchName, domain.PullRequest{})
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -149,7 +147,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)
s.uc.Cfg.IsInteractive = false

err := s.uc.Execute()
Expand All @@ -162,7 +160,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -173,7 +171,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-1-sample-issue"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -185,7 +183,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)
s.gitProvider.CommitsToPush[branchName] = []string{"commit 1", "commit 2"}

s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue pushing all pending commits in this branch and create the pull request", true).Return(false, nil).Once()
Expand All @@ -201,7 +199,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-4-with-commit-error"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.BranchWithCommitError = []string{branchName}
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -211,7 +209,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
s.Run("should error if could not push branch", func() {
branchName := "feature/GH-5-with-no-local-branch"
s.gitProvider.CurrentBranch = branchName
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -223,7 +221,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
s.gitProvider.CurrentBranch = branchName
s.gitProvider.LocalBranches = append(s.gitProvider.LocalBranches, branchName)
s.pullRequestProvider.PullRequestsWithErrors = []string{branchName}
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -234,8 +232,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to use this branch to create the pull request", true).Return(true, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "1"

err := s.uc.Execute()
Expand All @@ -245,8 +241,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
})

s.Run("should error if branch already exists when using default and issue flags", func() {
s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IsInteractive = false
s.uc.Cfg.IssueID = "1"

Expand All @@ -259,8 +253,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to use this branch to create the pull request", true).Return(false, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "1"

err := s.uc.Execute()
Expand All @@ -277,8 +269,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue?", true).Return(true, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "1"

err := s.uc.Execute()
Expand All @@ -294,8 +284,6 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue?", true).Return(false, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "1"

err := s.uc.Execute()
Expand All @@ -322,7 +310,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-1-sample-issue"
s.gitProvider.CurrentBranch = branchName

s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand Down Expand Up @@ -350,7 +338,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
branchName := "feature/GH-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

s.uc.Cfg.CloseIssue = false

Expand All @@ -363,7 +351,7 @@ func (s *CreateGithubPullRequestExecutionTestSuite) TestCreatePullRequestExecuti
s.Run("should error if could not get issue", func() {
branchName := "feature/GH-6-with-no-remote-branch"
s.gitProvider.CurrentBranch = branchName
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand Down Expand Up @@ -439,7 +427,10 @@ func (s *CreateJiraPullRequestExecutionTestSuite) SetupSubTest() {

s.userInteractionProvider = s.initializeUserInteractionProvider()
s.pullRequestProvider = domainFakes.NewFakePullRequestProvider()
s.branchProvider = s.initializeBranchProvider()

s.branchProvider = domainFakes.NewFakeBranchProvider()
s.branchProvider.SetBranchName(s.defaultBranchName)

s.repositoryProvider = domainFakes.NewRepositoryProvider()

defaultConfig := use_cases.CreatePullRequestConfiguration{
Expand Down Expand Up @@ -491,7 +482,6 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution

s.Run("should exit if user does not confirm current branch", func() {
s.gitProvider.CurrentBranch = s.defaultBranchName
s.setGetBranchName(s.defaultBranchName)

mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation(mock.Anything, mock.Anything).Return(false, nil).Once()
Expand All @@ -507,7 +497,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.pullRequestProvider.AddPullRequest(branchName, domain.PullRequest{})
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -519,7 +509,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
branchName := "feature/PROJECTKEY-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)
s.uc.Cfg.IsInteractive = false

err := s.uc.Execute()
Expand All @@ -532,7 +522,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
branchName := "feature/PROJECTKEY-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -542,7 +532,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
s.Run("should return error if remote branch already exists", func() {
branchName := "feature/PROJECTKEY-1-sample-issue"
s.gitProvider.CurrentBranch = branchName
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -554,7 +544,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
branchName := "feature/PROJECTKEY-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)
s.gitProvider.CommitsToPush[branchName] = []string{"commit 1", "commit 2"}

s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue pushing all pending commits in this branch and create the pull request", true).Return(false, nil).Once()
Expand All @@ -570,7 +560,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
branchName := "feature/PROJECTKEY-4-with-commit-error"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.BranchWithCommitError = []string{branchName}
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -580,7 +570,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
s.Run("should error if could not push branch", func() {
branchName := "feature/PROJECTKEY-5-with-no-local-branch"
s.gitProvider.CurrentBranch = branchName
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -592,7 +582,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
s.gitProvider.CurrentBranch = branchName
s.gitProvider.LocalBranches = append(s.gitProvider.LocalBranches, branchName)
s.pullRequestProvider.PullRequestsWithErrors = []string{branchName}
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand All @@ -603,8 +593,6 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to use this branch to create the pull request", true).Return(true, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "PROJECTKEY-1"

err := s.uc.Execute()
Expand All @@ -626,8 +614,6 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to use this branch to create the pull request", true).Return(false, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "PROJECTKEY-1"

err := s.uc.Execute()
Expand All @@ -644,8 +630,6 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue?", true).Return(true, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "PROJECTKEY-1"

err := s.uc.Execute()
Expand All @@ -661,8 +645,6 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
mocks.UnsetExpectedCall(&s.userInteractionProvider.Mock, s.userInteractionProvider.AskUserForConfirmation)
s.userInteractionProvider.EXPECT().AskUserForConfirmation("Do you want to continue?", true).Return(false, nil).Once()

s.setGetBranchName(s.defaultBranchName)

s.uc.Cfg.IssueID = "PROJECTKEY-1"

err := s.uc.Execute()
Expand Down Expand Up @@ -714,7 +696,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
branchName := "feature/PROJECTKEY-3-local-branch"
s.gitProvider.CurrentBranch = branchName
s.gitProvider.AddLocalBranches(branchName)
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

s.expectNoPrFound()

Expand All @@ -728,7 +710,7 @@ func (s *CreateJiraPullRequestExecutionTestSuite) TestCreatePullRequestExecution
s.Run("should error if could not get issue", func() {
branchName := "feature/PROJECTKEY-6-with-no-remote-branch"
s.gitProvider.CurrentBranch = branchName
s.setGetBranchName(branchName)
s.branchProvider.SetBranchName(branchName)

err := s.uc.Execute()

Expand Down

0 comments on commit a0ffd24

Please sign in to comment.