-
Notifications
You must be signed in to change notification settings - Fork 1
/
goso_test.go
126 lines (119 loc) · 2.73 KB
/
goso_test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package goso
import (
"encoding/json"
"fmt"
"maps"
netUrl "net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
func openFile(path string) (*os.File, func(), error) {
f, err := os.Open(filepath.FromSlash(fmt.Sprintf("testdata/%s.json", path)))
if err != nil {
return nil, nil, err
}
return f, func() { f.Close() }, nil
}
func fetchGoogle(conf *Config, results map[int]*Result) error {
var gsResp GoogleSearchResult
f, close, err := openFile("goso")
if err != nil {
return err
}
defer close()
err = json.NewDecoder(f).Decode(&gsResp)
if err != nil {
return err
}
for _, item := range gsResp.Items {
var upvoteCount int
var dateCreated time.Time
if len(item.Pagemap.Question) > 0 {
question := item.Pagemap.Question[0]
answerCount, _ := strconv.Atoi(question.Answercount)
if answerCount == 0 {
continue
}
upvoteCount, _ = strconv.Atoi(question.Upvotecount)
dateCreated, _ = time.Parse("2006-01-02T15:04:05", question.Datecreated)
}
u, _ := netUrl.Parse(item.Link)
questionId, _ := strconv.Atoi(strings.Split(u.Path, "/")[2])
results[questionId] = &Result{
Title: item.Title,
Link: item.Link,
QuestionId: questionId,
UpvoteCount: upvoteCount,
Date: dateCreated,
}
}
return nil
}
func fetchStackOverflow(conf *Config, results map[int]*Result) error {
questions := make([]string, len(results))
var idx int
for question := range maps.Keys(results) {
questions[idx] = strconv.Itoa(question)
idx++
}
_ = strings.Join(questions, ";")
var soResp StackOverflowResult
f, close, err := openFile("answers")
if err != nil {
return err
}
defer close()
err = json.NewDecoder(f).Decode(&soResp)
if err != nil {
return err
}
for _, item := range soResp.Items {
result, ok := results[item.QuestionID]
if !ok {
continue
}
result.Answers = append(result.Answers,
&Answer{
Title: result.Title,
Author: item.Owner.DisplayName,
Score: item.Score,
Body: item.Body,
Link: fmt.Sprintf("https://stackoverflow.com/a/%d", item.AnswerID),
IsAccepted: item.IsAccepted,
Date: time.Unix(int64(item.CreationDate), 0).UTC(),
})
}
return nil
}
func BenchmarkGetAnswers(b *testing.B) {
b.ResetTimer()
conf := &Config{
Style: "onedark",
Lexer: "c",
QuestionNum: 10,
AnswerNum: 10,
}
for i := 0; i < b.N; i++ {
_, err := GetAnswers(conf, fetchGoogle, fetchStackOverflow)
if err != nil {
b.Fatal(err)
}
}
}
func TestOutput(t *testing.T) {
conf := &Config{
Style: "onedark",
Lexer: "c",
QuestionNum: 10,
AnswerNum: 10,
}
answers, err := GetAnswers(conf, fetchGoogle, fetchStackOverflow)
if err != nil {
t.Fatal(err)
}
fmt.Println(answers)
}