-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
103 lines (86 loc) · 2.63 KB
/
database_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
package main
import (
"testing"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
func TestOpenDBIfClosed(t *testing.T) {
var err error
db, _, err = sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
result := OpenDBIfClosed()
if result != db {
t.Errorf("OpenDBIfClosed() returned a different database")
}
}
func TestViewRows(t *testing.T) {
var err error
var mock sqlmock.Sqlmock
db, mock, err = sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
var hashtag1, hashtag2 Hashtag
hashtag1.Name = "happy"
hashtag2.Name = "sad"
var hashtags []Hashtag
hashtags = append(hashtags, hashtag1)
hashtags = append(hashtags, hashtag2)
rows := sqlmock.NewRows([]string{"id", "hashtag", "sentiment", "created"}).AddRow(1, "happy", "positive", "2016-03-08 23:57:51.645176+00").AddRow(2, "sad", "negative", "2016-03-06 23:57:51.645176+00")
mock.ExpectQuery("^SELECT (.+) FROM hashtags$").WillReturnRows(rows)
result := ViewRows()
if result[0].Name != hashtags[0].Name && result[1].Name != hashtags[1].Name {
t.Fatalf("ViewRows() failed. Expected: %v. Got: %v", hashtags, result)
}
}
func TestIsInTable(t *testing.T) {
// var err error
// var mock sqlmock.Sqlmock
// db, mock, err = sqlmock.New()
// if err != nil {
// t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
// }
// defer db.Close()
//
// var trend1, trend2 anaconda.Trend
// trend1.Name = "happy"
// trend2.Name = "neutral"
//
// row1 := sqlmock.NewRows([]string{"id"}).AddRow(1)
// // // _ = sqlmock.NewRows([]string{"id"})
// //
// mock.ExpectQuery("^SELECT id FROM hashtags WHERE hashtag = (.+)$").WillReturnRows(row1)
// // // mock.ExpectQuery("^SELECT id FROM hashtags WHERE hashtag = 'neutral'").WillReturnRows(row2)
//
// result := IsInTable(trend1)
// if result != true {
// t.Fatalf("IsInTable failing")
// }
}
func TestInsertHashtag(t *testing.T) {
// var err error
// var mock sqlmock.Sqlmock
// db, mock, err = sqlmock.New()
// if err != nil {
// t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
// }
// defer db.Close()
//
// mock.ExpectBegin()
// mock.ExpectExec("^INSERT INTO hashtags$").WithArgs("happy", "positive")
// mock.ExpectCommit()
//
// InsertHashtag("happy", "positive")
//
// if err := mock.ExpectationsWereMet(); err != nil {
// t.Errorf("there were unfulfilled expections: %s", err)
// }
//
}
func TestSelectRandomHashtag(t *testing.T) {
}
func TestPurgeDB(t *testing.T) {
}