forked from thomas-tacquet/gormv2-logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gormlog_test.go
69 lines (51 loc) · 1.81 KB
/
gormlog_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
package gormv2logrus_test
import (
"fmt"
"testing"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormv2logrus "github.com/thomas-tacquet/gormv2-logrus"
)
func TestWithLogrus(t *testing.T) {
// create a logrusEntry entry to giveit to gormv2_logrus
logger, hook := test.NewNullLogger()
// create the gorm compatible logger with logrusEntry instance
gormLog := gormv2logrus.NewGormlog(gormv2logrus.WithLogrus(logger))
db, err := gorm.Open(sqlite.Open(
generateTestingSqliteString(t)),
&gorm.Config{Logger: gormLog},
)
// check if database correctly created
require.NoError(t, err)
require.NotNil(t, db)
sqlDB, err := db.DB()
require.NoError(t, err)
require.NotNil(t, sqlDB)
defer func() {
assert.NoError(t, sqlDB.Close())
}()
// NotExistingTable is a simple empty struct that does not exist in current database,
// so if we try to create a new entry of this struct, gorm must return an error
// telling us that this table does not exists
type NotExistingTable struct{}
errCreate := db.Create(&NotExistingTable{}).Error
t.Log(errCreate.Error())
// testing gorm is not a purprose of this test, but to ensure consistency we
// must check if errCreate is not empty
require.NotEmpty(t, errCreate)
require.Contains(t, errCreate.Error(), "no such table")
require.Contains(t, errCreate.Error(), "not_existing_tables")
assert.Equal(t, 1, len(hook.Entries))
require.NotNil(t, hook.LastEntry())
lastLogEntry, err := hook.LastEntry().String()
require.NoError(t, err)
assert.Contains(t, lastLogEntry, errCreate.Error())
}
func generateTestingSqliteString(t *testing.T) string {
t.Helper()
const sqliteConnString = "file:%s?mode=memory&cache=shared"
return fmt.Sprintf(sqliteConnString, t.Name())
}