-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_db_test.go
67 lines (55 loc) · 1.74 KB
/
mock_db_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
package linq
import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// NewDB returns a DB instance with a non-nil db field when passed a non-nil gorm.DB instance
func Test_new_db_with_non_nil_gorm_db(t *testing.T) {
// Create a mock gorm.DB instance
mockDB, _, _ := sqlmock.New()
gormDB, _ := gorm.Open(mysql.New(mysql.Config{
Conn: mockDB,
}), &gorm.Config{})
// Call the code under test
db := NewDB[int](gormDB)
// Assert that the db field is not nil
if db.db == nil {
t.Errorf("Expected db field to be non-nil, but got nil")
}
}
// MockDB returns a non-nil DB instance, sqlmock.Sqlmock instance, and nil error when called
func Test_mock_db_returns_non_nil_instances_and_nil_error(t *testing.T) {
// Call the code under test
db, mock, err := MockDB[int]()
// Assert that the returned instances are not nil
if db == nil {
t.Errorf("Expected DB instance to be non-nil, but got nil")
}
if mock == nil {
t.Errorf("Expected sqlmock.Sqlmock instance to be non-nil, but got nil")
}
// Assert that the error is nil
if err != nil {
t.Errorf("Expected error to be nil, but got %v", err)
}
}
// MockDB returns a DB instance with a non-nil db field when called
func Test_mock_db_returns_db_with_non_nil_db_field(t *testing.T) {
// Call the code under test
db, _, _ := MockDB[int]()
// Assert that the db field is not nil
if db.db == nil {
t.Errorf("Expected db field to be non-nil, but got nil")
}
}
// NewDB returns a DB instance with a nil db field when passed a nil gorm.DB instance
func Test_new_db_with_nil_gorm_db(t *testing.T) {
// Call the code under test
db := NewDB[int](nil)
// Assert that the db field is nil
if db.db != nil {
t.Errorf("Expected db field to be nil, but got non-nil")
}
}