-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_test.go
149 lines (134 loc) · 3.06 KB
/
book_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package goodreads
import (
"testing"
"github.com/ketabchi/util"
)
func TestGenres(t *testing.T) {
tests := []struct {
url string
exp []string
}{
{
"https://www.goodreads.com/book/show/7683075",
[]string{"Philosophy", "Nonfiction"},
},
{
"https://www.goodreads.com/book/show/178493._",
[]string{},
},
{
"https://www.goodreads.com/book/show/10541690",
[]string{"Classics", "Fiction", "Academic", "School",
"Literature", "Historical", "Historical Fiction"},
},
}
for i, test := range tests {
b, err := newBook(test.url)
if err != nil {
t.Errorf("Test %d: Error on creating book: %s", i, err)
continue
}
if genres := b.Genres(); !util.CheckSliceEq(genres, test.exp) {
t.Errorf("Test %d: Expected genres %q, but got %q", i, test.exp, genres)
}
}
}
func TestNewBook(t *testing.T) {
tests := []struct {
url string
expBestBookID string
}{
{
"https://www.goodreads.com/book/show/19060872-the-monk-who-sold-his-ferrari",
"43877",
},
}
for i, test := range tests {
book, err := NewBook(test.url)
if err != nil {
t.Errorf("Test %d: Error on creating book: %s", i, err)
continue
}
if book.Work.BestBookID != test.expBestBookID {
t.Errorf("Test %d: Expected book best book id %s, but got %s",
i, test.expBestBookID, book.Work.BestBookID)
}
}
}
func TestBookID(t *testing.T) {
tests := []struct {
url string
exp string
}{
{
"https://www.goodreads.com/book/show/38750904-the-accidental-further-adventures-of-the-hundred-year-old-man",
"38750904",
},
{
"https://www.goodreads.com/book/show/1.Harry_Potter_and_the_Half_Blood_Prince",
"1",
},
{
"https://www.goodreads.com/book/show/3",
"3",
},
}
for i, test := range tests {
id := bookID(test.url)
if id != test.exp {
t.Errorf("Test %d: Expected id %s, but got %s", i, test.exp, id)
}
}
}
func TestHasAuthor(t *testing.T) {
tests := []struct {
url string
author string
exp bool
}{
{
"https://www.goodreads.com/book/show/38750904-the-accidental-further-adventures-of-the-hundred-year-old-man",
"Jonas Jonasson",
true,
},
}
for i, test := range tests {
book, err := NewBook(test.url)
if err != nil {
t.Errorf("Test %d: Couldnt create book from %s: %s", i, test.url, err)
continue
}
if exists := book.HasAuthor(test.author); exists != test.exp {
t.Errorf("Test %d: Expected has author %v, but got %v", i, test.exp, exists)
}
}
}
func TestSerie(t *testing.T) {
tests := []struct {
url string
exp string
}{
{
"https://www.goodreads.com/book/show/17347634-me-before-you",
"Me Before You",
},
{
"https://www.goodreads.com/book/show/2195738._04_",
"Tintin",
},
{
"https://www.goodreads.com/book/show/26272491-reason-and-responsibility",
"",
},
}
for i, test := range tests {
book, err := NewBook(test.url)
if err != nil {
t.Errorf("Test %d: Couldnt create book from %s: %s", i, test.url, err)
continue
}
if serie := book.Serie(); serie != test.exp {
t.Errorf("Test %d: Expected serie %s, but got %s", i, test.exp, serie)
}
}
}