-
Notifications
You must be signed in to change notification settings - Fork 249
/
fake.go
60 lines (51 loc) · 1.51 KB
/
fake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package jira
import (
"errors"
"testing"
"github.com/andygrunwald/go-jira"
"github.com/sirupsen/logrus"
)
// IssueRequest describes a client call to file an issue
type IssueRequest struct {
IssueType, Title, Description, Reporter string
}
// IssueResponse describes a client response for filing an issue
type IssueResponse struct {
Issue *jira.Issue
Error error
}
// Fake is an injectable IssueFiler
type Fake struct {
behavior map[IssueRequest]IssueResponse
unwanted []IssueRequest
}
// FileIssue files the issue using injected behavior
func (f *Fake) FileIssue(issueType, title, description, reporter string, logger *logrus.Entry) (*jira.Issue, error) {
request := IssueRequest{
IssueType: issueType,
Title: title,
Description: description,
Reporter: reporter,
}
response, registered := f.behavior[request]
if !registered {
f.unwanted = append(f.unwanted, request)
return nil, errors.New("no such issue request behavior in fake")
}
delete(f.behavior, request)
return response.Issue, response.Error
}
// Validate ensures that all expected client calls happened
func (f *Fake) Validate(t *testing.T) {
for request := range f.behavior {
t.Errorf("fake issue filer did not get request: %v", request)
}
for _, request := range f.unwanted {
t.Errorf("fake issue filer got unwanted request: %v", request)
}
}
var _ IssueFiler = &Fake{}
// NewFake creates a new fake filer with the injected behavior
func NewFake(calls map[IssueRequest]IssueResponse) *Fake {
return &Fake{behavior: calls}
}