/
bootstrap_test.go
130 lines (112 loc) · 4.8 KB
/
bootstrap_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
package pgengine_test
import (
"context"
"database/sql"
"errors"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
pgx "github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
)
func TestInitAndTestMock(t *testing.T) {
initmockdb(t)
pgengine.OpenDB = func(driverName string, dataSourceName string) (*sql.DB, error) {
return db, nil
}
defer db.Close()
t.Run("Check bootstrap if everything fine", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), pgengine.WaitTime*time.Second+2)
defer cancel()
mock.ExpectPing()
mock.ExpectQuery("SELECT EXISTS").WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
assert.True(t, pgengine.InitAndTestConfigDBConnection(ctx, *cmdOpts))
})
t.Run("Check bootstrap if ping failed", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), pgengine.WaitTime*time.Second+2)
defer cancel()
mock.ExpectPing().WillReturnError(errors.New("ping error"))
assert.False(t, pgengine.InitAndTestConfigDBConnection(ctx, *cmdOpts))
})
t.Run("Check bootstrap if executeSchemaScripts failed", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), pgengine.WaitTime*time.Second+2)
defer cancel()
mock.ExpectPing()
mock.ExpectQuery("SELECT EXISTS").WillReturnError(errors.New("internal error"))
assert.False(t, pgengine.InitAndTestConfigDBConnection(ctx, *cmdOpts))
})
t.Run("Check bootstrap if startup file doesn't exist", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), pgengine.WaitTime*time.Second+2)
defer cancel()
mock.ExpectPing()
mock.ExpectQuery("SELECT EXISTS").WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
cmdOpts.File = "foo"
assert.False(t, pgengine.InitAndTestConfigDBConnection(ctx, *cmdOpts))
})
pgengine.OpenDB = sql.Open
assert.NoError(t, mock.ExpectationsWereMet(), "there were unfulfilled expectations")
}
func TestExecuteSchemaScripts(t *testing.T) {
initmockdb(t)
defer db.Close()
pgengine.ConfigDb = xdb
t.Run("Check schema scripts if error returned for SELECT EXISTS", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
mock.ExpectQuery("SELECT EXISTS").WillReturnError(errors.New("expected"))
assert.False(t, pgengine.ExecuteSchemaScripts(ctx))
})
t.Run("Check schema scripts if error returned", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
mock.ExpectQuery("SELECT EXISTS").WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
mock.ExpectExec("CREATE SCHEMA timetable").WillReturnError(errors.New("expected"))
mock.ExpectExec("DROP SCHEMA IF EXISTS timetable CASCADE").WillReturnError(errors.New("expected"))
assert.False(t, pgengine.ExecuteSchemaScripts(ctx))
})
t.Run("Check schema scripts if everything fine", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
mock.ExpectQuery("SELECT EXISTS").WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
for i := 0; i < 4; i++ {
mock.ExpectExec(".*").WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("INSERT INTO timetable\\.log").WillReturnResult(sqlmock.NewResult(0, 1))
}
mock.ExpectExec("INSERT INTO timetable\\.log").WillReturnResult(sqlmock.NewResult(0, 1))
assert.True(t, pgengine.ExecuteSchemaScripts(ctx))
})
}
func TestReconnectAndFixLeftovers(t *testing.T) {
initmockdb(t)
defer db.Close()
pgengine.ConfigDb = xdb
t.Run("Check ReconnectAndFixLeftovers if everything fine", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
mock.ExpectPing()
mock.ExpectExec("INSERT INTO timetable\\.log").WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("INSERT INTO timetable\\.run_status").WillReturnResult(sqlmock.NewResult(0, 0))
assert.True(t, pgengine.ReconnectDbAndFixLeftovers(ctx))
})
t.Run("Check ReconnectAndFixLeftovers if error returned", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), (pgengine.WaitTime+2)*time.Second)
defer cancel()
mock.ExpectPing().WillReturnError(errors.New("expected"))
mock.ExpectPing().WillDelayFor(pgengine.WaitTime * time.Second * 2)
assert.False(t, pgengine.ReconnectDbAndFixLeftovers(ctx))
})
assert.NoError(t, mock.ExpectationsWereMet())
}
func TestLogger(t *testing.T) {
l := pgengine.Logger{}
for level := pgx.LogLevelNone; level <= pgx.LogLevelTrace; level++ {
l.Log(context.Background(), pgx.LogLevel(level), "", nil)
}
}
func TestFinalizeConnection(t *testing.T) {
initmockdb(t)
pgengine.ConfigDb = xdb
mock.ExpectClose().WillReturnError(errors.New("expected"))
pgengine.FinalizeConfigDBConnection()
}