Skip to content

Commit 9bf7d6a

Browse files
authored
Fix history module still use string to insert/select (#112)
1 parent fda5d8f commit 9bf7d6a

File tree

6 files changed

+44
-43
lines changed

6 files changed

+44
-43
lines changed

internal/application/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (a *App) saveToHistory(c *comicinfo.ComicInfo) error {
8686
s = strings.Split(c.Genre, ",")
8787
for _, item := range s {
8888
values = append(values, history.HistoryVal{
89-
Category: history.Genre_Text,
89+
Category: history.CategoryGenre,
9090
Value: item,
9191
})
9292
}
@@ -96,7 +96,7 @@ func (a *App) saveToHistory(c *comicinfo.ComicInfo) error {
9696
s = strings.Split(c.Publisher, ",")
9797
for _, item := range s {
9898
values = append(values, history.HistoryVal{
99-
Category: history.Publisher_Text,
99+
Category: history.CategoryPublisher,
100100
Value: item,
101101
})
102102
}

internal/history/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
)
1515

1616
// Insert value into database. This function is allowed to insert multiple values at once.
17-
func insertValue(db *lazydb.LazyDB, category string, value ...string) error {
17+
func insertValue(db *lazydb.LazyDB, category categoryType, value ...string) error {
1818
// Prevent nil database
1919
if db == nil {
2020
return ErrDatabaseNil
@@ -42,7 +42,7 @@ func insertValue(db *lazydb.LazyDB, category string, value ...string) error {
4242
}
4343

4444
// Get inputted list from database, by given category.
45-
func getHistory(db *lazydb.LazyDB, category string) ([]string, error) {
45+
func getHistory(db *lazydb.LazyDB, category categoryType) ([]string, error) {
4646
// Prevent nil database
4747
if db == nil {
4848
return []string{}, ErrDatabaseNil

internal/history/core_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Function to check how many rows in db has given category & value.
14-
func checkRowCount(a *lazydb.LazyDB, category string, value string) (int, error) {
14+
func checkRowCount(a *lazydb.LazyDB, category categoryType, value string) (int, error) {
1515
// Get Inserted rows
1616
rows, err := a.Query("SELECT COUNT(*) FROM list_inputted WHERE category=? AND input=?", category, value)
1717
if err != nil {
@@ -47,7 +47,7 @@ func createTestDB(path string, withData bool) (*lazydb.LazyDB, error) {
4747
}
4848

4949
// Insert data rows
50-
sql := `INSERT INTO list_inputted (category, input) VALUES ('abc','123'), ('def', '123'), ('def', '456')`
50+
sql := `INSERT INTO list_inputted (category, input) VALUES (45,'123'), (56, '123'), (56, '456')`
5151
_, err = a.Exec(sql)
5252
if err != nil {
5353
return nil, err
@@ -70,31 +70,31 @@ func TestInsertValue(t *testing.T) {
7070
// Test case
7171
type testCase struct {
7272
dbPath string
73-
category string
73+
category categoryType
7474
value []string
7575
wantErr bool
7676
insertedRow []int // Should have same order as `value`
7777
}
7878

7979
tests := []testCase{
80-
// Normal test in same db
81-
{db1, "abc", []string{"123"}, false, []int{1}},
82-
{db1, "abc", []string{"123"}, false, []int{1}},
83-
{db1, "def", []string{"123"}, false, []int{1}},
80+
// Normal test in same db
81+
{db1, 45, []string{"123"}, false, []int{1}},
82+
{db1, 45, []string{"123"}, false, []int{1}},
83+
{db1, 56, []string{"123"}, false, []int{1}},
8484

8585
// Duplicate Test
86-
{"test2.db", "abc", []string{"123", "123"}, false, []int{1, 1}},
87-
{"test2.db", "abc", []string{"123", "456"}, false, []int{1, 1}},
86+
{"test2.db", 45, []string{"123", "123"}, false, []int{1, 1}},
87+
{"test2.db", 45, []string{"123", "456"}, false, []int{1, 1}},
8888

8989
// Empty value
90-
{"test3.db", "abc", []string{}, false, []int{}},
91-
{"test4.db", "", []string{"123"}, false, []int{1}},
90+
{"test3.db", 45, []string{}, false, []int{}},
91+
{"test4.db", 0, []string{"123"}, false, []int{1}},
9292

9393
// Empty string value
94-
{"test5.db", "abc", []string{"123", ""}, false, []int{1, 0}},
94+
{"test5.db", 45, []string{"123", ""}, false, []int{1, 0}},
9595

9696
// Nil database
97-
{"", "abc", []string{"123"}, true, []int{1}},
97+
{"", 45, []string{"123"}, true, []int{1}},
9898
}
9999

100100
// Start testing
@@ -149,15 +149,15 @@ func TestGetHistory(t *testing.T) {
149149

150150
// Prepare test case
151151
type testCase struct {
152-
category string
152+
category categoryType
153153
result []string
154154
wantErr bool
155155
}
156156

157157
tests := []testCase{
158-
{"abc", []string{"123"}, false},
159-
{"def", []string{"123", "456"}, false},
160-
{"kk", []string{}, false},
158+
{45, []string{"123"}, false},
159+
{56, []string{"123", "456"}, false},
160+
{77, []string{}, false},
161161
}
162162

163163
// Start Testing
@@ -178,13 +178,13 @@ func TestGetHistoryNilDB(t *testing.T) {
178178

179179
// Prepare test case
180180
type testCase struct {
181-
category string
181+
category categoryType
182182
result []string
183183
wantErr bool
184184
}
185185

186186
tests := []testCase{
187-
{"abc", []string{"123"}, true},
187+
{45, []string{"123"}, true},
188188
}
189189

190190
// Start Testing

internal/history/impl.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@ import (
44
"github.com/dark-person/lazydb"
55
)
66

7-
// Database value for Genre.
8-
const Genre_Text = "Genre"
7+
type categoryType int
98

10-
// Database value for Publisher.
11-
const Publisher_Text = "Publisher"
9+
const (
10+
CategoryGenre categoryType = iota + 1 // Database value for Genre.
11+
CategoryPublisher // Database value for Publisher.
12+
CategoryTranslator // Database value for Translator.
13+
)
1214

1315
// Insert genre value from database.
1416
func InsertGenre(db *lazydb.LazyDB, value ...string) error {
15-
return insertValue(db, Genre_Text, value...)
17+
return insertValue(db, CategoryGenre, value...)
1618
}
1719

1820
// Get all genre value that from database.
1921
func GetGenreList(db *lazydb.LazyDB) ([]string, error) {
20-
return getHistory(db, Genre_Text)
22+
return getHistory(db, CategoryGenre)
2123
}
2224

2325
// Insert publisher value from database.
2426
func InsertPublisher(db *lazydb.LazyDB, value ...string) error {
25-
return insertValue(db, Publisher_Text, value...)
27+
return insertValue(db, CategoryPublisher, value...)
2628
}
2729

2830
// Get all publisher value that from database.
2931
func GetPublisherList(db *lazydb.LazyDB) ([]string, error) {
30-
return getHistory(db, Publisher_Text)
32+
return getHistory(db, CategoryPublisher)
3133
}

internal/history/multiple_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,25 @@ func TestInsertMultiple(t *testing.T) {
2727

2828
tests := []testCase{
2929
// Normal test in same db
30-
{db1, []HistoryVal{{"abc", "123"}}, false, []int{1}},
31-
{db1, []HistoryVal{{"abc", "123"}}, false, []int{1}},
32-
{db1, []HistoryVal{{"def", "123"}}, false, []int{1}},
30+
{db1, []HistoryVal{{34, "123"}}, false, []int{1}},
31+
{db1, []HistoryVal{{34, "123"}}, false, []int{1}},
32+
{db1, []HistoryVal{{45, "123"}}, false, []int{1}},
3333

3434
// Different values in same time
35-
{"test2.db", []HistoryVal{{"abc", "123"}, {"def", "123"}}, false, []int{1, 1}},
35+
{"test2.db", []HistoryVal{{34, "123"}, {45, "123"}}, false, []int{1, 1}},
3636

3737
// Duplicate Test
38-
{"test3.db", []HistoryVal{{"abc", "123"}, {"abc", "123"}}, false, []int{1, 1}},
39-
{"test3.db", []HistoryVal{{"abc", "123"}, {"abc", "456"}}, false, []int{1, 1}},
38+
{"test3.db", []HistoryVal{{34, "123"}, {34, "123"}}, false, []int{1, 1}},
39+
{"test3.db", []HistoryVal{{34, "123"}, {34, "456"}}, false, []int{1, 1}},
4040

4141
// Empty value
42-
{"test4.db", []HistoryVal{{"abc", ""}}, false, []int{0}},
43-
{"test5.db", []HistoryVal{{"", "123"}}, false, []int{1}},
42+
{"test4.db", []HistoryVal{{34, ""}}, false, []int{0}},
4443

4544
// Empty string value
46-
{"test6.db", []HistoryVal{{"abc", "123"}, {"abc", ""}}, false, []int{1, 0}},
45+
{"test6.db", []HistoryVal{{34, "123"}, {34, ""}}, false, []int{1, 0}},
4746

4847
// Nil database
49-
{"", []HistoryVal{{"abc", "123"}}, true, []int{1}},
48+
{"", []HistoryVal{{34, "123"}}, true, []int{1}},
5049
}
5150

5251
// Start testing

internal/history/struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ package history
55
//
66
// This type is designed for insert value with different category at a time.
77
type HistoryVal struct {
8-
Category string // category to be inserted
9-
Value string // value to be inserted
8+
Category categoryType // category to be inserted
9+
Value string // value to be inserted
1010
}

0 commit comments

Comments
 (0)