-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.go
166 lines (126 loc) · 4.88 KB
/
event_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package libgorion
import (
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
)
var (
timeNow = time.Now().Local()
firstDate = timeNow.Format("02.01.2006")
lastDate = timeNow.AddDate(0, 0, 1).Format("02.01.2006")
workerName = "Worker"
companyName = "Company"
doorID = uint(22)
denied = true
)
func TestEvents(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
lib := &Database{DB: db}
column := []string{"Time", "firstName", "midName", "lastName", "Company", "Door", "Event"}
rows := sqlmock.NewRows(column).
AddRow("firstName", "midName", "lastName", "company", time.Now(), "door", "action")
mock.ExpectQuery(testQueryEvents).
WithArgs(firstDate, lastDate).
WillReturnRows(rows)
if _, err := lib.Events(firstDate, lastDate, "", 0, false); err != nil {
t.Errorf("error was not expected while gets all events %q ", err)
}
mock.ExpectQuery(testQueryEventsByEmployeeAndDoor).
WithArgs(firstDate, lastDate, workerName, doorID).
WillReturnRows(rows)
if _, err = lib.Events(firstDate, lastDate, workerName, doorID, false); err != nil {
t.Errorf("error was not expected while gets events by worker and door %q ", err)
}
mock.ExpectQuery(testQueryEventsByEmployee).
WithArgs(firstDate, lastDate, workerName).
WillReturnRows(rows)
if _, err = lib.Events(firstDate, lastDate, workerName, 0, false); err != nil {
t.Errorf("error was not expected while gets events by worker %q ", err)
}
mock.ExpectQuery(testQueryEventsByDoor).
WithArgs(firstDate, lastDate, doorID).
WillReturnRows(rows)
if _, err = lib.Events(firstDate, lastDate, "", doorID, false); err != nil {
t.Errorf("error was not expected while gets events by door %q ", err)
}
mock.ExpectQuery(testQueryEventsDenied).
WithArgs(firstDate, lastDate).
WillReturnRows(rows)
if _, err = lib.Events(firstDate, lastDate, "", 0, denied); err != nil {
t.Errorf("error was not expected while gets denied events %q ", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestWorkedTime(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
lib := &Database{DB: db}
column := []string{"Time", "firstName", "midName", "lastName", "Company", "Event"}
rows := sqlmock.NewRows(column).
AddRow("firstName", "midName", "lastName", "company", timeNow, timeNow)
mock.ExpectQuery(testQueryWorkedTime).WillReturnRows(rows)
if _, err = lib.WorkedTime(firstDate, lastDate, "", ""); err != nil {
t.Errorf("error was not expected while gets worked time %q ", err)
}
mock.ExpectQuery(testQueryWorkedTimeByCompany).WillReturnRows(rows)
if _, err = lib.WorkedTime(firstDate, lastDate, "", companyName); err != nil {
t.Errorf("error was not expected while gets worked time %q ", err)
}
mock.ExpectQuery(testQueryWorkedTimeByEmployee).WillReturnRows(rows)
if _, err = lib.WorkedTime(firstDate, lastDate, workerName, ""); err != nil {
t.Errorf("error was not expected while gets worked time %q ", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestEventsValue(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
lib := &Database{DB: db}
column := []string{"ID", "Value", "Comment"}
rows := sqlmock.NewRows(column).
AddRow("1", "alert", "alert event")
mock.ExpectQuery(testQueryEventsValues).WillReturnRows(rows)
if _, err := lib.EventsValues(); err != nil {
t.Errorf("error was not expected while gets events values %q ", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestEventsTail(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
lib := &Database{DB: db}
column := []string{"Time", "firstName", "midName", "lastName", "Company", "Door", "Event"}
rows := sqlmock.NewRows(column).
AddRow("firstName", "midName", "lastName", "company", time.Now(), "door", "action")
interval := time.Duration(5)
timeNow := time.Now().Local()
backForSeconds := timeNow.Add(time.Second * -interval)
mock.ExpectQuery(testQueryEvents).
WithArgs(backForSeconds.Format("02.01.2006 15:04:05"), timeNow.Format("02.01.2006 15:04:05")).
WillReturnRows(rows)
if err := lib.EventsTail(interval, ""); err != nil {
t.Errorf("error was not expected while gets all events %q ", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}