-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
147 lines (112 loc) · 3.62 KB
/
main_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
package main
import (
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
)
func TestGetInstanceID_WithOK(t *testing.T) {
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()
expected := 1
rows := sqlmock.NewRows([]string{"instance_id"}).
AddRow(expected)
mock.ExpectQuery("SELECT instance_id FROM icinga_instances WHERE instance_name = ?").WillReturnRows(rows)
id, err := getInstanceID(db, "default")
if err != nil {
t.Errorf("error was not expected while getting instance: %s", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
if id != expected {
t.Errorf("actual: %d, expected: %d", id, expected)
}
}
func TestGetInstanceID_WithError(t *testing.T) {
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()
_, DbErr := getInstanceID(db, "default")
if err != nil {
t.Errorf("error was not expected while getting instance: %s", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
if DbErr == nil {
t.Errorf("expected an error got nil")
}
}
func TestOldestTime_WithOK(t *testing.T) {
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()
expected := "2024-07-02 08:25:44"
rows := sqlmock.NewRows([]string{"start_time"}).
AddRow(expected)
mock.ExpectQuery("SELECT start_time FROM icinga_notifications WHERE (.+) ORDER BY start_time ASC LIMIT 1").WillReturnRows(rows)
table := Table{"notifications", "start_time"}
oldest, err := table.OldestTime(db, 1)
if "2024-07-02 08:25:44 +0000 UTC" != oldest.String() {
t.Errorf("returned timestamp not expected: %s", oldest.String())
}
if err != nil {
t.Errorf("error was not expected while getting oldest time: %s", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestCleanup_WithOK(t *testing.T) {
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()
ts := time.Now()
mock.ExpectExec("DELETE FROM icinga_notifications WHERE (.+) AND start_time (.+) LIMIT 10").
WithArgs(1, ts).
WillReturnResult(sqlmock.NewResult(1, 1))
table := Table{"notifications", "start_time"}
r, err := table.Cleanup(db, 1, ts, 10)
if 1 != r {
t.Errorf("returned rows not expected: %d", r)
}
if err != nil {
t.Errorf("error was not expected while cleanup: %s", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestCount_WithOK(t *testing.T) {
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()
rows := sqlmock.NewRows([]string{"count(*)"}).
AddRow(123)
ts := time.Now()
mock.ExpectQuery("SELECT count(.+) FROM icinga_notifications WHERE instance_id = (.+) AND start_time < (.+)").
WithArgs(1, ts).
WillReturnRows(rows)
table := Table{"notifications", "start_time"}
r, err := table.Count(db, 1, ts)
if 123 != r {
t.Errorf("returned rows not expected: %d", r)
}
if err != nil {
t.Errorf("error was not expected while cleanup: %s", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}