Skip to content

Commit 37d5bb9

Browse files
committed
1. Refactoring code
2. Expanded documentation 3. Added linters
1 parent 49153d9 commit 37d5bb9

File tree

9 files changed

+133
-62
lines changed

9 files changed

+133
-62
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ linters:
5656
- wsl
5757
- ginkgolinter
5858
- interfacebloat
59+
- sloglint
60+
- nilnil
5961

6062
linters-settings:
6163
varnamelen:

README.adoc

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,67 @@
11
= test utils
2+
:toc:
23
---
4+
5+
36
image:https://img.shields.io/github/go-mod/go-version/itbasis/go-test-utils[GitHub go.mod Go version]
47
image:https://img.shields.io/badge/godoc-reference-blue.svg[link=https://pkg.go.dev/github.com/itbasis/go-test-utils]
58
image:https://img.shields.io/github/v/release/itbasis/go-test-utils[GitHub Release]
69
https://goreportcard.com/report/github.com/itbasis/go-test-utils[image:https://goreportcard.com/badge/github.com/itbasis/go-test-utils[Go Report Card]]
710

8-
. Used by https://onsi.github.io/ginkgo/[Ginkgo] as test framework
9-
. Used by https://github.com/DATA-DOG/go-sqlmock[sqlmock] with https://gorm.io/gorm[GORM] for SQL testing
11+
* Used by link:https://onsi.github.io/ginkgo/[Ginkgo] as test framework
12+
* Used by link:https://github.com/DATA-DOG/go-sqlmock[sqlmock] and link:https://gorm.io/gorm[GORM] for SQL testing
1013
11-
== Get module
14+
== Install module
1215

1316
```
14-
go get -u github.com/itbasis/go-test-utils/v2
17+
go get -u github.com/itbasis/go-test-utils/v4
1518
```
19+
20+
== File utils
21+
22+
* link:files/files.go[] - checks if the path is a regular file and reads its contents
23+
24+
== Database utils
25+
26+
* link:db/sql.go[] - Getting a DB mock and SqlMock without SqlMock options (example option: link:https://github.com/DATA-DOG/go-sqlmock?tab=readme-ov-file#customize-sql-query-matching[QueryMatcherOptions])
27+
* link:db/gorm.go[] - Getting a GORM and SqlMock instances
28+
29+
== Ginkgo utils
30+
31+
* link:ginkgo/ginkgo.go[] - Adds slog support to tests and code under test
32+
33+
[source,go]
34+
----
35+
package demo
36+
37+
import (
38+
"testing"
39+
40+
"github.com/itbasis/go-test-utils/v4/ginkgo"
41+
)
42+
43+
func TestSuite(t *testing.T) {
44+
ginkgo.InitGinkgoSuite(t, "Sample Suite")
45+
}
46+
----
47+
48+
Custom slog options
49+
50+
[source,go]
51+
----
52+
package demo
53+
54+
import (
55+
"log/slog"
56+
"testing"
57+
58+
"github.com/itbasis/go-test-utils/v4/ginkgo"
59+
)
60+
61+
func TestSuite(t *testing.T) {
62+
ginkgo.InitGinkgoSuiteWithSlogOptions(t, "Sample Suite", &slog.HandlerOptions{
63+
Level: slog.LevelDebug,
64+
AddSource: false,
65+
})
66+
}
67+
----

db/gorm.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package db
2+
3+
import (
4+
"github.com/DATA-DOG/go-sqlmock"
5+
"github.com/onsi/gomega"
6+
slogGorm "github.com/orandin/slog-gorm"
7+
pg "gorm.io/driver/postgres"
8+
"gorm.io/gorm"
9+
)
10+
11+
func NewMockForGorm(dialector gorm.Dialector) *gorm.DB {
12+
var gormDb, err = gorm.Open(dialector, &gorm.Config{Logger: slogGorm.New()})
13+
14+
gomega.Expect(err).To(gomega.Succeed())
15+
16+
return gormDb
17+
}
18+
19+
func NewMockForGormPostgreSQL() (sqlmock.Sqlmock, *gorm.DB) {
20+
var db, sqlMock = NewSQLMock()
21+
22+
return sqlMock, NewMockForGorm(pg.New(pg.Config{Conn: db}))
23+
}

db/mock-db.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

db/sql.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
6+
goSqlMock "github.com/DATA-DOG/go-sqlmock"
7+
"github.com/onsi/gomega"
8+
)
9+
10+
func NewSQLMock() (*sql.DB, goSqlMock.Sqlmock) {
11+
var db, sqlMock, err = goSqlMock.New()
12+
13+
gomega.Expect(err).To(gomega.Succeed())
14+
15+
return db, sqlMock
16+
}

ginkgo.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

ginkgo/ginkgo.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ginkgo
2+
3+
import (
4+
"log/slog"
5+
"testing"
6+
7+
"github.com/dusted-go/logging/prettylog"
8+
"github.com/onsi/ginkgo/v2"
9+
"github.com/onsi/gomega"
10+
)
11+
12+
func InitGinkgoSuite(t *testing.T, name string) {
13+
InitGinkgoSuiteWithSlogOptions(
14+
t, name,
15+
&slog.HandlerOptions{
16+
Level: slog.LevelDebug,
17+
AddSource: true,
18+
},
19+
)
20+
}
21+
22+
func InitGinkgoSuiteWithSlogOptions(t *testing.T, name string, slogOptions *slog.HandlerOptions) {
23+
gomega.RegisterFailHandler(ginkgo.Fail)
24+
25+
slog.SetDefault(
26+
slog.New(
27+
prettylog.New(slogOptions, prettylog.WithDestinationWriter(ginkgo.GinkgoWriter)),
28+
),
29+
)
30+
31+
ginkgo.RunSpecs(t, name)
32+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/itbasis/go-test-utils/v3
1+
module github.com/itbasis/go-test-utils/v4
22

33
go 1.22
44

@@ -7,7 +7,7 @@ require (
77
github.com/dusted-go/logging v1.3.0
88
github.com/onsi/ginkgo/v2 v2.20.0
99
github.com/onsi/gomega v1.34.1
10-
github.com/orandin/slog-gorm v1.3.2
10+
github.com/orandin/slog-gorm v1.4.0
1111
gorm.io/driver/postgres v1.5.9
1212
gorm.io/gorm v1.25.11
1313
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ github.com/onsi/ginkgo/v2 v2.20.0 h1:PE84V2mHqoT1sglvHc8ZdQtPcwmvvt29WLEEO3xmdZw
3535
github.com/onsi/ginkgo/v2 v2.20.0/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
3636
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
3737
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
38-
github.com/orandin/slog-gorm v1.3.2 h1:C0lKDQPAx/pF+8K2HL7bdShPwOEJpPM0Bn80zTzxU1g=
39-
github.com/orandin/slog-gorm v1.3.2/go.mod h1:MoZ51+b7xE9lwGNPYEhxcUtRNrYzjdcKvA8QXQQGEPA=
38+
github.com/orandin/slog-gorm v1.4.0 h1:FgA8hJufF9/jeNSYoEXmHPPBwET2gwlF3B85JdpsTUU=
39+
github.com/orandin/slog-gorm v1.4.0/go.mod h1:MoZ51+b7xE9lwGNPYEhxcUtRNrYzjdcKvA8QXQQGEPA=
4040
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
4141
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4242
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

0 commit comments

Comments
 (0)