Skip to content

Commit a346185

Browse files
authored
feat: build DetailResponse from DB (#121)
1 parent a2b3bb6 commit a346185

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

server/internal/data/data.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package data
2+
3+
import (
4+
"embed"
5+
6+
"github.com/codingpot/pr12er/server/pkg/pr12er"
7+
"google.golang.org/protobuf/encoding/prototext"
8+
)
9+
10+
//go:embed *.pbtxt
11+
var pbFiles embed.FS
12+
13+
// DB contains all PrVideos.
14+
var DB pr12er.Database
15+
16+
//nolint:gochecknoinits
17+
// we want to fail at the boot.
18+
func init() {
19+
bs, err := pbFiles.ReadFile("database.pbtxt")
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
err = prototext.Unmarshal(bs, &DB)
25+
if err != nil {
26+
panic(err)
27+
}
28+
}

server/internal/err/err.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package err
2+
3+
import "fmt"
4+
5+
// ErrorPrIDNotFound is used when PR ID is not found in Database.
6+
type ErrorPrIDNotFound struct {
7+
PrID int32
8+
}
9+
10+
// Error implements Error interface.
11+
func (e ErrorPrIDNotFound) Error() string {
12+
return fmt.Sprintf("PR-%d was not found", e.PrID)
13+
}

server/pkg/handlers/handlers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"sort"
55

6+
"github.com/codingpot/pr12er/server/internal/err"
67
"github.com/codingpot/pr12er/server/pkg/pr12er"
78
)
89

@@ -56,3 +57,18 @@ func getCategory(prVideo *pr12er.PrVideo) pr12er.Category {
5657
func getYouTubeLinkFromID(videoID string) string {
5758
return "https://youtu.be/" + videoID
5859
}
60+
61+
// DetailResponseFromDB builds DetailResponse from Database.
62+
func DetailResponseFromDB(prID int32, db *pr12er.Database) (*pr12er.GetDetailResponse, error) {
63+
video, ok := db.GetPrIdToVideo()[prID]
64+
if !ok {
65+
return nil, err.ErrorPrIDNotFound{PrID: prID}
66+
}
67+
68+
return &pr12er.GetDetailResponse{
69+
Detail: &pr12er.Detail{
70+
PrId: prID,
71+
Paper: video.GetPapers(),
72+
},
73+
}, nil
74+
}

server/pkg/handlers/handlers_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package handlers
33
import (
44
"testing"
55

6+
"github.com/codingpot/pr12er/server/internal/data"
67
"github.com/codingpot/pr12er/server/pkg/pr12er"
78
"github.com/google/go-cmp/cmp"
9+
"github.com/stretchr/testify/assert"
810
"google.golang.org/protobuf/testing/protocmp"
911
)
1012

@@ -86,3 +88,71 @@ func TestConvertToGet(t *testing.T) {
8688
})
8789
}
8890
}
91+
92+
func TestDetailResponseFromDB(t *testing.T) {
93+
type args struct {
94+
prID int32
95+
db *pr12er.Database
96+
}
97+
tests := []struct {
98+
name string
99+
args args
100+
want *pr12er.GetDetailResponse
101+
wantErr bool
102+
}{
103+
{
104+
name: "Returns a correct GetDetailResponse",
105+
args: args{
106+
prID: 1,
107+
db: &data.DB,
108+
},
109+
want: &pr12er.GetDetailResponse{Detail: &pr12er.Detail{
110+
PrId: 1,
111+
Paper: []*pr12er.Paper{
112+
{
113+
PaperId: "generative-adversarial-networks",
114+
Title: "Generative Adversarial Networks",
115+
ArxivId: "1406.2661",
116+
Abstract: "We propose a new framework for",
117+
Authors: []string{
118+
"Ian J. Goodfellow", "Jean Pouget-Abadie", "Mehdi Mirza", "Bing Xu",
119+
"David Warde-Farley", "Sherjil Ozair", "Aaron Courville", "Yoshua Bengio",
120+
},
121+
Repositories: nil,
122+
Methods: []*pr12er.Method{
123+
{Name: "GAN", FullName: "Generative Adversarial Network"},
124+
{Name: "Convolution", FullName: "Convolution"},
125+
},
126+
},
127+
},
128+
}},
129+
wantErr: false,
130+
},
131+
{
132+
name: "Returns an error when no PR is found",
133+
args: args{
134+
prID: 0,
135+
db: nil,
136+
},
137+
want: nil,
138+
wantErr: true,
139+
},
140+
}
141+
for _, tt := range tests {
142+
t.Run(tt.name, func(t *testing.T) {
143+
got, err := DetailResponseFromDB(tt.args.prID, tt.args.db)
144+
if tt.wantErr {
145+
assert.Error(t, err)
146+
} else {
147+
assert.NoError(t, err)
148+
}
149+
150+
if diff := cmp.Diff(tt.want, got,
151+
protocmp.IgnoreFields(&pr12er.Method{}, "description"),
152+
protocmp.IgnoreFields(&pr12er.Paper{}, "abstract", "pub_date", "repositories"),
153+
protocmp.Transform()); diff != "" {
154+
t.Error(diff)
155+
}
156+
})
157+
}
158+
}

0 commit comments

Comments
 (0)