Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
feat: add test for get issue and sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
YCK1130 committed Jul 17, 2024
1 parent 83ca508 commit 20b5d64
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 35 deletions.
142 changes: 131 additions & 11 deletions application/jira/v0/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

qt "github.com/frankban/quicktest"
"github.com/instill-ai/component/base"
"github.com/instill-ai/component/internal/util/httpclient"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)
Expand Down Expand Up @@ -78,12 +77,123 @@ func TestComponent_ListBoardsTask(t *testing.T) {
StartAt: 1,
ProjectKeyOrID: "test",
},
wantErr: "unsuccessful HTTP response",
wantErr: "unsuccessful HTTP response.*",
},
}
taskTesting(testcases, taskListBoards, t)
}

func TestComponent_GetIssueTask(t *testing.T) {
testcases := []TaskCase[GetIssueInput, GetIssueOutput]{
{
_type: "ok",
name: "get issue-Task",
input: GetIssueInput{
IssueKeyOrID: "1",
UpdateHistory: true,
},
wantResp: GetIssueOutput{
Issue: Issue{
ID: "1",
Key: "TST-1",
Fields: map[string]interface{}{
"summary": "Test issue 1",
"description": "Test description 1",
"status": map[string]interface{}{
"name": "To Do",
},
"issuetype": map[string]interface{}{
"name": "Task",
},
},
Self: "https://test.atlassian.net/rest/agile/1.0/issue/1",
Summary: "Test issue 1",
Status: "To Do",
Description: "Test description 1",
IssueType: "Task",
},
},
},
{
_type: "ok",
name: "get issue-Epic",
input: GetIssueInput{
IssueKeyOrID: "4",
UpdateHistory: false,
},
wantResp: GetIssueOutput{
Issue: Issue{
ID: "4",
Key: "KAN-4",
Fields: map[string]interface{}{
"summary": "Test issue 4",
"status": map[string]interface{}{
"name": "Done",
},
"issuetype": map[string]interface{}{
"name": "Epic",
},
},
Self: "https://test.atlassian.net/rest/agile/1.0/issue/4",
Summary: "Test issue 4",
Status: "Done",
IssueType: "Epic",
},
},
},
{
_type: "nok",
name: "404 - Not Found",
input: GetIssueInput{
IssueKeyOrID: "5",
UpdateHistory: true,
},
wantErr: "unsuccessful HTTP response.*",
},
}
taskTesting(testcases, taskGetIssue, t)
}

func TestComponent_GetSprintTask(t *testing.T) {
testcases := []TaskCase[GetSprintInput, GetSprintOutput]{
{
_type: "ok",
name: "get sprint",
input: GetSprintInput{
SprintID: 1,
},
wantResp: GetSprintOutput{
ID: 1,
Self: "https://test.atlassian.net/rest/agile/1.0/sprint/1",
State: "active",
Name: "Sprint 1",
StartDate: "2021-01-01T00:00:00.000Z",
EndDate: "2021-01-15T00:00:00.000Z",
CompleteDate: "2021-01-15T00:00:00.000Z",
OriginBoardID: 1,
Goal: "Sprint goal",
},
},
{
_type: "nok",
name: "400 - Bad Request",
input: GetSprintInput{
SprintID: -1,
},
wantErr: "unsuccessful HTTP response.*",
},
{
_type: "nok",
name: "404 - Not Found",
input: GetSprintInput{
SprintID: 2,
},
wantErr: "unsuccessful HTTP response.*",
},
}
taskTesting(testcases, taskGetSprint, t)
}

func taskTesting[inType any, outType any](testcases []TaskCase[inType, outType], task string, t *testing.T) {
c := qt.New(t)
ctx := context.Background()
Expand All @@ -92,16 +202,24 @@ func taskTesting[inType any, outType any](testcases []TaskCase[inType, outType],

for _, tc := range testcases {
c.Run(tc._type+`-`+tc.name, func(c *qt.C) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/_edge/tenant_info" {
auth := base64.StdEncoding.EncodeToString([]byte(email + ":" + token))
c.Check(r.Header.Get("Authorization"), qt.Equals, "Basic "+auth)
authenticationMiddleware := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/_edge/tenant_info" {
auth := base64.StdEncoding.EncodeToString([]byte(email + ":" + token))
c.Check(r.Header.Get("Authorization"), qt.Equals, "Basic "+auth)
}
next.ServeHTTP(w, r)
}
w.Header().Set("Content-Type", httpclient.MIMETypeJSON)
router(w, r)
})

srv := httptest.NewServer(h)
return http.HandlerFunc(fn)
}
setContentTypeMiddleware := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
srv := httptest.NewServer(router(authenticationMiddleware, setContentTypeMiddleware))
c.Cleanup(srv.Close)

setup, err := structpb.NewStruct(map[string]any{
Expand All @@ -121,8 +239,10 @@ func taskTesting[inType any, outType any](testcases []TaskCase[inType, outType],
c.Assert(err, qt.ErrorMatches, tc.wantErr)
return
}
c.Assert(err, qt.IsNil)
wantJSON, err := json.Marshal(tc.wantResp)
c.Assert(err, qt.IsNil)
c.Assert(got, qt.HasLen, 1)
c.Check(wantJSON, qt.JSONEquals, got[0].AsMap())
})
}
Expand Down
1 change: 0 additions & 1 deletion application/jira/v0/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type SprintOrEpic struct {
type GetIssueInput struct {
IssueKeyOrID string `json:"issue-id-or-key,omitempty" struct:"issueIdOrKey"`
UpdateHistory bool `json:"update-history,omitempty" struct:"updateHistory"`
FromBacklog bool `json:"from-backlog,omitempty" struct:"fromBacklog"`
}
type GetIssueOutput struct {
Issue
Expand Down
116 changes: 111 additions & 5 deletions application/jira/v0/mock_database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package jira

import "fmt"
import (
"fmt"
)

type FakeBoard struct {
Board
}

func (f *FakeBoard) getSelf() string {
if f.Self == "" {
f.Self = fmt.Sprintf("https://test.atlassian.net/rest/agile/1.0/board/%d", f.ID)
}
return f.Self
}

var fakeBoards = []FakeBoard{
{
Expand All @@ -26,13 +39,106 @@ var fakeBoards = []FakeBoard{
},
}

type FakeBoard struct {
Board
type FakeIssue struct {
ID string `json:"id"`
Key string `json:"key"`
Self string `json:"self"`
Fields map[string]interface{} `json:"fields"`
}

func (f *FakeBoard) getSelf() string {
func (f *FakeIssue) getSelf() string {
if f.Self == "" {
f.Self = fmt.Sprintf("https://test.atlassian.net/rest/agile/1.0/board/%d", f.ID)
f.Self = fmt.Sprintf("https://test.atlassian.net/rest/agile/1.0/issue/%s", f.ID)
}
return f.Self
}

var fakeIssues = []FakeIssue{
{
ID: "1",
Key: "TST-1",
Fields: map[string]interface{}{
"summary": "Test issue 1",
"description": "Test description 1",
"status": map[string]interface{}{
"name": "To Do",
},
"issuetype": map[string]interface{}{
"name": "Task",
},
},
},
{
ID: "2",
Key: "TST-2",
Fields: map[string]interface{}{
"summary": "Test issue 2",
"description": "Test description 2",
"status": map[string]interface{}{
"name": "In Progress",
},
"issuetype": map[string]interface{}{
"name": "Task",
},
},
},
{
ID: "3",
Key: "TST-3",
Fields: map[string]interface{}{
"summary": "Test issue 3",
"description": "Test description 3",
"status": map[string]interface{}{
"name": "Done",
},
"issuetype": map[string]interface{}{
"name": "Task",
},
},
},
{
ID: "4",
Key: "KAN-4",
Fields: map[string]interface{}{
"summary": "Test issue 4",
"status": map[string]interface{}{
"name": "Done",
},
"issuetype": map[string]interface{}{
"name": "Epic",
},
},
},
}

type FakeSprint struct {
ID int `json:"id"`
Self string `json:"self"`
State string `json:"state"`
Name string `json:"name"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
CompleteDate string `json:"completeDate"`
OriginBoardID int `json:"originBoardId"`
Goal string `json:"goal"`
}

func (f *FakeSprint) getSelf() string {
if f.Self == "" {
f.Self = fmt.Sprintf("https://test.atlassian.net/rest/agile/1.0/sprint/%d", f.ID)
}
return f.Self
}

var fakeSprints = []FakeSprint{
{
ID: 1,
State: "active",
Name: "Sprint 1",
StartDate: "2021-01-01T00:00:00.000Z",
EndDate: "2021-01-15T00:00:00.000Z",
CompleteDate: "2021-01-15T00:00:00.000Z",
OriginBoardID: 1,
Goal: "Sprint goal",
},
}
Loading

0 comments on commit 20b5d64

Please sign in to comment.