forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_test.go
More file actions
80 lines (76 loc) · 2.34 KB
/
result_test.go
File metadata and controls
80 lines (76 loc) · 2.34 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package search
import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepositoryExportData(t *testing.T) {
var createdAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC)
tests := []struct {
name string
fields []string
repo Repository
output string
}{
{
name: "exports requested fields",
fields: []string{"createdAt", "description", "fullName", "isArchived", "isFork", "isPrivate", "pushedAt"},
repo: Repository{
CreatedAt: createdAt,
Description: "description",
FullName: "cli/cli",
IsArchived: true,
IsFork: false,
IsPrivate: false,
PushedAt: createdAt,
},
output: `{"createdAt":"2021-02-28T12:30:00Z","description":"description","fullName":"cli/cli","isArchived":true,"isFork":false,"isPrivate":false,"pushedAt":"2021-02-28T12:30:00Z"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exported := tt.repo.ExportData(tt.fields)
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
require.NoError(t, enc.Encode(exported))
assert.Equal(t, tt.output, strings.TrimSpace(buf.String()))
})
}
}
func TestIssueExportData(t *testing.T) {
var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC)
tests := []struct {
name string
fields []string
issue Issue
output string
}{
{
name: "exports requested fields",
fields: []string{"assignees", "body", "commentsCount", "labels", "isLocked", "title", "updatedAt"},
issue: Issue{
Assignees: []User{{Login: "test"}},
Body: "body",
CommentsCount: 1,
Labels: []Label{{Name: "label1"}, {Name: "label2"}},
IsLocked: true,
Title: "title",
UpdatedAt: updatedAt,
},
output: `{"assignees":[{"id":"","login":"test","type":""}],"body":"body","commentsCount":1,"isLocked":true,"labels":[{"color":"","description":"","id":"","name":"label1"},{"color":"","description":"","id":"","name":"label2"}],"title":"title","updatedAt":"2021-02-28T12:30:00Z"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exported := tt.issue.ExportData(tt.fields)
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
require.NoError(t, enc.Encode(exported))
assert.Equal(t, tt.output, strings.TrimSpace(buf.String()))
})
}
}