Skip to content

Commit 97118aa

Browse files
Merge pull request #25 from oracle-samples/add_connection_tests
Add connection tests
2 parents 13a88fd + 2811e86 commit 97118aa

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

tests/connection_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ package tests
4040

4141
import (
4242
"fmt"
43+
"log"
44+
"os"
45+
"sync"
4346
"testing"
4447

4548
"gorm.io/gorm"
@@ -65,3 +68,91 @@ func TestWithSingleConnection(t *testing.T) {
6568
t.Errorf("WithSingleConnection() method should get correct value, expect: %v, got %v", expectedString, actualString)
6669
}
6770
}
71+
72+
func TestConnectionWithInvalidQuery(t *testing.T) {
73+
err := DB.Connection(func(tx *gorm.DB) error {
74+
return tx.Exec("SELECT * FROM non_existent_table").Error
75+
})
76+
if err == nil {
77+
t.Fatalf("Expected error for invalid query in Connection, got nil")
78+
}
79+
}
80+
81+
func TestMultipleSequentialConnections(t *testing.T) {
82+
for i := 0; i < 20; i++ {
83+
var val int
84+
err := DB.Connection(func(tx *gorm.DB) error {
85+
return tx.Raw("SELECT 1 FROM dual").Scan(&val).Error
86+
})
87+
if err != nil {
88+
t.Fatalf("Sequential Connection #%d failed: %v", i+1, err)
89+
}
90+
if val != 1 {
91+
t.Fatalf("Sequential Connection #%d got wrong result: %v", i+1, val)
92+
}
93+
}
94+
}
95+
96+
func TestConnectionAfterDBClose(t *testing.T) {
97+
sqlDB, err := DB.DB()
98+
if err != nil {
99+
t.Fatalf("DB.DB() should not fail, got: %v", err)
100+
}
101+
err = sqlDB.Close()
102+
if err != nil {
103+
t.Fatalf("sqlDB.Close() failed: %v", err)
104+
}
105+
cerr := DB.Connection(func(tx *gorm.DB) error {
106+
var v int
107+
return tx.Raw("SELECT 1 FROM dual").Scan(&v).Error
108+
})
109+
if cerr == nil {
110+
t.Fatalf("Expected error when calling Connection after DB closed, got nil")
111+
}
112+
if DB, err = OpenTestConnection(&gorm.Config{Logger: newLogger}); err != nil {
113+
log.Printf("failed to connect database, got error %v", err)
114+
os.Exit(1)
115+
}
116+
}
117+
118+
func TestConnectionHandlesPanic(t *testing.T) {
119+
defer func() {
120+
if r := recover(); r == nil {
121+
t.Fatalf("Expected panic inside Connection, but none occurred")
122+
}
123+
}()
124+
DB.Connection(func(tx *gorm.DB) error {
125+
panic("panic in connection callback")
126+
})
127+
t.Fatalf("Should have panicked inside connection callback")
128+
}
129+
130+
func TestConcurrentConnections(t *testing.T) {
131+
const goroutines = 10
132+
var wg sync.WaitGroup
133+
wg.Add(goroutines)
134+
errChan := make(chan error, goroutines)
135+
136+
for i := 0; i < goroutines; i++ {
137+
go func(i int) {
138+
defer wg.Done()
139+
var val int
140+
err := DB.Connection(func(tx *gorm.DB) error {
141+
return tx.Raw("SELECT ? FROM dual", i).Scan(&val).Error
142+
})
143+
if err != nil {
144+
errChan <- fmt.Errorf("goroutine #%d: connection err: %v", i, err)
145+
return
146+
}
147+
if val != i {
148+
errChan <- fmt.Errorf("goroutine #%d: got wrong result: %v", i, val)
149+
}
150+
}(i)
151+
}
152+
153+
wg.Wait()
154+
close(errChan)
155+
for err := range errChan {
156+
t.Error(err)
157+
}
158+
}

tests/passed-tests.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ TestPluginCallbacks
3838
TestCallbacksGet
3939
TestCallbacksRemove
4040
TestWithSingleConnection
41+
TestConnectionWithInvalidQuery
42+
TestNestedConnection
43+
TestMultipleSequentialConnections
44+
TestConnectionAfterDBClose
45+
TestConnectionHandlesPanic
46+
TestConcurrentConnections
4147
TestCountWithGroup
4248
TestCount
4349
TestCountOnEmptyTable

0 commit comments

Comments
 (0)