Skip to content

Commit

Permalink
test: fix failing test post FSM refactor for generator
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Sep 10, 2023
1 parent 934a9e9 commit 7717eb3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
11 changes: 5 additions & 6 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const (
Initial = State("initial")
CommitsFetched = State("commits_fetched")
JiraIssuesFetched = State("jira_issues_fetched")
ChangelogRecored = State("changelog_recorded")
ChangesRecorded = State("changes_recorded")

FetchCommits = Event("fetch_commits")
FetchJiraIssues = Event("fetch_jira_issues")
RecordChangelog = Event("record_changelog")
RecordChanges = Event("record_changes")
)

func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Generator {
Expand All @@ -51,7 +51,7 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener
fsm.Events{
{Name: FetchCommits, Src: []string{Initial}, Dst: CommitsFetched},
{Name: FetchJiraIssues, Src: []string{CommitsFetched}, Dst: JiraIssuesFetched},
{Name: RecordChangelog, Src: []string{JiraIssuesFetched}, Dst: ChangelogRecored},
{Name: RecordChanges, Src: []string{JiraIssuesFetched}, Dst: ChangesRecorded},
},
fsm.Callbacks{
Before(FetchCommits): func(ctx context.Context, e *fsm.Event) {
Expand All @@ -71,7 +71,7 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener
}
g.jiraIssues = lo.Uniq(issues)
},
RecordChangelog: func(ctx context.Context, e *fsm.Event) {
RecordChanges: func(ctx context.Context, e *fsm.Event) {
slog.Debug("Total jira issues ids", "count", len(g.jiraIssues))
slog.Debug("Recroding changelog")

Expand All @@ -82,7 +82,6 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener
},
},
)

return g
}

Expand All @@ -95,7 +94,7 @@ func panicIfErr(err error) {
func (c *Generator) Generate(ctx context.Context) *Changelog {
panicIfErr(c.FSM.Event(ctx, FetchCommits))
panicIfErr(c.FSM.Event(ctx, FetchJiraIssues))
panicIfErr(c.FSM.Event(ctx, RecordChangelog))
panicIfErr(c.FSM.Event(ctx, RecordChanges))

slog.Debug("Total changelog items", "count", len(c.changes))

Expand Down
56 changes: 45 additions & 11 deletions pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jira_changelog

import (
"context"
"testing"
"time"

Expand All @@ -10,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestChangelogFromCommits(t *testing.T) {
func TestFetchJiraIssuesEvent(t *testing.T) {
commits := []git.Commit{
{Time: time.Now(), Message: "[TEST-1234] commit message1", Sha: "3245vw"},
{Time: time.Now(), Message: "[TEST-4546] commit message sample1", Sha: "3245vw"},
Expand All @@ -21,6 +22,42 @@ func TestChangelogFromCommits(t *testing.T) {
{Time: time.Now(), Message: "foobar commit message random", Sha: "3245vw"},
}

jiraIssues := []jira.Issue{
jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"),
jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"),
jira.NewIssue("TEST-12345", "Ticket description of another card from same epic", "done", "Epic1"),
jira.NewIssue("", "[NO-CARD] commit message random (3245vw)", "done", ""),
jira.NewIssue("", "foobar commit message random (3245vw)", "done", ""),
}

mockedClient := mocks.NewClient(t)
mockedClient.On("FetchIssue", "TEST-1234").Return(jiraIssues[0], nil).Twice()
mockedClient.On("FetchIssue", "TEST-4546").Return(jiraIssues[1], nil).Twice()
mockedClient.On("FetchIssue", "TEST-12345").Return(jiraIssues[2], nil)

// Setup
generator := NewGenerator(jira.Config{}, "fromRef", "toRef", "http://example-repo.com")
generator.client = mockedClient
generator.commits = commits
generator.FSM.SetState(CommitsFetched)

// invoke event
err := generator.FSM.Event(context.Background(), FetchJiraIssues)

assert.NoError(t, err)
assert.Equal(t, len(jiraIssues), len(generator.jiraIssues))
assert.Equal(t, jiraIssues, generator.jiraIssues)
assert.Equal(t, generator.FSM.Current(), JiraIssuesFetched)
}

func TestRecordChangeLogEvent(t *testing.T) {
issues := []jira.Issue{
jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"),
jira.NewIssue("TEST-12345", "Ticket description of another from same epic", "done", "Epic1"),
jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"),
jira.NewIssue("", "[NO-CARD] commit message random (3245vw)", "done", ""),
jira.NewIssue("", "foobar commit message random (3245vw)", "done", ""),
}
expected := Changes{
"Epic1": {
jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"),
Expand All @@ -35,15 +72,12 @@ func TestChangelogFromCommits(t *testing.T) {
},
}

mockedClient := mocks.NewClient(t)
mockedClient.On("FetchIssue", "TEST-1234").Return(jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"), nil).Twice()
mockedClient.On("FetchIssue", "TEST-4546").Return(jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"), nil).Twice()
mockedClient.On("FetchIssue", "TEST-12345").Return(jira.NewIssue("TEST-12345", "Ticket description of another from same epic", "done", "Epic1"), nil)
generator := Generator{}
generator.client = mockedClient

result, err := generator.fetchJiraIssues(commits)
generator := NewGenerator(jira.Config{}, "fromRef", "toRef", "http://example-repo.com")
generator.jiraIssues = issues
generator.FSM.SetState(JiraIssuesFetched)
generator.FSM.Event(context.Background(), RecordChanges)

assert.NoError(t, err)
assert.Equal(t, expected, result)
actual := generator.changes
assert.Equal(t, expected, actual)
assert.Equal(t, generator.FSM.Current(), ChangesRecorded)
}

0 comments on commit 7717eb3

Please sign in to comment.