Skip to content

Commit 8a9d83f

Browse files
author
George Starikov
committed
Add x-no-lock tests for postgres, pgx, pgx/v5, sqlserver, cockroachdb, yugabytedb
Add param validation unit test and integration lock/unlock test following the existing MySQL TestNoLockParamValidation/TestNoLockWorks pattern.
1 parent 35b2209 commit 8a9d83f

File tree

6 files changed

+298
-1
lines changed

6 files changed

+298
-1
lines changed

database/cockroachdb/cockroachdb_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ package cockroachdb
55
import (
66
"context"
77
"database/sql"
8+
"errors"
89
"fmt"
9-
"github.com/golang-migrate/migrate/v4"
1010
"log"
11+
"strconv"
1112
"strings"
1213
"testing"
14+
15+
"github.com/golang-migrate/migrate/v4"
1316
)
1417

1518
import (
@@ -155,6 +158,55 @@ func TestMultiStatement(t *testing.T) {
155158
})
156159
}
157160

161+
func TestNoLockParamValidation(t *testing.T) {
162+
c := &CockroachDb{}
163+
_, err := c.Open("cockroach://root@localhost/migrate?x-no-lock=not-a-bool")
164+
if !errors.Is(err, strconv.ErrSyntax) {
165+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
166+
}
167+
}
168+
169+
func TestNoLockWorks(t *testing.T) {
170+
dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) {
171+
createDB(t, ci)
172+
173+
ip, port, err := ci.Port(defaultPort)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
178+
addr := fmt.Sprintf("cockroach://root@%v:%v/migrate?sslmode=disable", ip, port)
179+
c := &CockroachDb{}
180+
d, err := c.Open(addr)
181+
if err != nil {
182+
t.Fatal(err)
183+
}
184+
185+
lock := d.(*CockroachDb)
186+
187+
c = &CockroachDb{}
188+
d, err = c.Open(addr + "&x-no-lock=true")
189+
if err != nil {
190+
t.Fatal(err)
191+
}
192+
193+
noLock := d.(*CockroachDb)
194+
195+
if err = lock.Lock(); err != nil {
196+
t.Fatal(err)
197+
}
198+
if err = noLock.Lock(); err != nil {
199+
t.Fatal(err)
200+
}
201+
if err = lock.Unlock(); err != nil {
202+
t.Fatal(err)
203+
}
204+
if err = noLock.Unlock(); err != nil {
205+
t.Fatal(err)
206+
}
207+
})
208+
}
209+
158210
func TestFilterCustomQuery(t *testing.T) {
159211
dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) {
160212
createDB(t, ci)

database/pgx/pgx_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,50 @@ func Test_computeLineFromPos(t *testing.T) {
784784
})
785785
}
786786
}
787+
788+
func TestNoLockParamValidation(t *testing.T) {
789+
p := &Postgres{}
790+
_, err := p.Open("pgx://postgres@localhost/postgres?x-no-lock=not-a-bool")
791+
if !errors.Is(err, strconv.ErrSyntax) {
792+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
793+
}
794+
}
795+
796+
func TestNoLockWorks(t *testing.T) {
797+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
798+
ip, port, err := c.FirstPort()
799+
if err != nil {
800+
t.Fatal(err)
801+
}
802+
803+
addr := pgConnectionString(ip, port)
804+
p := &Postgres{}
805+
d, err := p.Open(addr)
806+
if err != nil {
807+
t.Fatal(err)
808+
}
809+
810+
lock := d.(*Postgres)
811+
812+
p = &Postgres{}
813+
d, err = p.Open(pgConnectionString(ip, port, "x-no-lock=true"))
814+
if err != nil {
815+
t.Fatal(err)
816+
}
817+
818+
noLock := d.(*Postgres)
819+
820+
if err = lock.Lock(); err != nil {
821+
t.Fatal(err)
822+
}
823+
if err = noLock.Lock(); err != nil {
824+
t.Fatal(err)
825+
}
826+
if err = lock.Unlock(); err != nil {
827+
t.Fatal(err)
828+
}
829+
if err = noLock.Unlock(); err != nil {
830+
t.Fatal(err)
831+
}
832+
})
833+
}

database/pgx/v5/pgx_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,50 @@ func Test_computeLineFromPos(t *testing.T) {
759759
})
760760
}
761761
}
762+
763+
func TestNoLockParamValidation(t *testing.T) {
764+
p := &Postgres{}
765+
_, err := p.Open("pgx5://postgres@localhost/postgres?x-no-lock=not-a-bool")
766+
if !errors.Is(err, strconv.ErrSyntax) {
767+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
768+
}
769+
}
770+
771+
func TestNoLockWorks(t *testing.T) {
772+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
773+
ip, port, err := c.FirstPort()
774+
if err != nil {
775+
t.Fatal(err)
776+
}
777+
778+
addr := pgConnectionString(ip, port)
779+
p := &Postgres{}
780+
d, err := p.Open(addr)
781+
if err != nil {
782+
t.Fatal(err)
783+
}
784+
785+
lock := d.(*Postgres)
786+
787+
p = &Postgres{}
788+
d, err = p.Open(pgConnectionString(ip, port, "x-no-lock=true"))
789+
if err != nil {
790+
t.Fatal(err)
791+
}
792+
793+
noLock := d.(*Postgres)
794+
795+
if err = lock.Lock(); err != nil {
796+
t.Fatal(err)
797+
}
798+
if err = noLock.Lock(); err != nil {
799+
t.Fatal(err)
800+
}
801+
if err = lock.Unlock(); err != nil {
802+
t.Fatal(err)
803+
}
804+
if err = noLock.Unlock(); err != nil {
805+
t.Fatal(err)
806+
}
807+
})
808+
}

database/postgres/postgres_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func Test(t *testing.T) {
9999
t.Run("testPostgresLock", testPostgresLock)
100100
t.Run("testWithInstanceConcurrent", testWithInstanceConcurrent)
101101
t.Run("testWithConnection", testWithConnection)
102+
t.Run("testNoLockWorks", testNoLockWorks)
102103

103104
t.Cleanup(func() {
104105
for _, spec := range specs {
@@ -748,6 +749,53 @@ func testWithConnection(t *testing.T) {
748749
})
749750
}
750751

752+
func TestNoLockParamValidation(t *testing.T) {
753+
p := &Postgres{}
754+
_, err := p.Open("postgres://postgres@localhost/postgres?x-no-lock=not-a-bool")
755+
if !errors.Is(err, strconv.ErrSyntax) {
756+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
757+
}
758+
}
759+
760+
func testNoLockWorks(t *testing.T) {
761+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
762+
ip, port, err := c.FirstPort()
763+
if err != nil {
764+
t.Fatal(err)
765+
}
766+
767+
addr := pgConnectionString(ip, port)
768+
p := &Postgres{}
769+
d, err := p.Open(addr)
770+
if err != nil {
771+
t.Fatal(err)
772+
}
773+
774+
lock := d.(*Postgres)
775+
776+
p = &Postgres{}
777+
d, err = p.Open(pgConnectionString(ip, port, "x-no-lock=true"))
778+
if err != nil {
779+
t.Fatal(err)
780+
}
781+
782+
noLock := d.(*Postgres)
783+
784+
if err = lock.Lock(); err != nil {
785+
t.Fatal(err)
786+
}
787+
if err = noLock.Lock(); err != nil {
788+
t.Fatal(err)
789+
}
790+
if err = lock.Unlock(); err != nil {
791+
t.Fatal(err)
792+
}
793+
if err = noLock.Unlock(); err != nil {
794+
t.Fatal(err)
795+
}
796+
})
797+
}
798+
751799
func Test_computeLineFromPos(t *testing.T) {
752800
testcases := []struct {
753801
pos int

database/sqlserver/sqlserver_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"database/sql"
66
sqldriver "database/sql/driver"
7+
"errors"
78
"fmt"
89
"log"
910
"runtime"
11+
"strconv"
1012
"strings"
1113
"testing"
1214
"time"
@@ -92,6 +94,7 @@ func Test(t *testing.T) {
9294
t.Run("testMsiTrue", testMsiTrue)
9395
t.Run("testOpenWithPasswordAndMSI", testOpenWithPasswordAndMSI)
9496
t.Run("testMsiFalse", testMsiFalse)
97+
t.Run("testNoLockWorks", testNoLockWorks)
9598

9699
t.Cleanup(func() {
97100
for _, spec := range specs {
@@ -311,6 +314,54 @@ func testOpenWithPasswordAndMSI(t *testing.T) {
311314
})
312315
}
313316

317+
func TestNoLockParamValidation(t *testing.T) {
318+
p := &SQLServer{}
319+
_, err := p.Open("sqlserver://sa:password@localhost?x-no-lock=not-a-bool")
320+
if !errors.Is(err, strconv.ErrSyntax) {
321+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
322+
}
323+
}
324+
325+
func testNoLockWorks(t *testing.T) {
326+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
327+
SkipIfUnsupportedArch(t, c)
328+
ip, port, err := c.Port(defaultPort)
329+
if err != nil {
330+
t.Fatal(err)
331+
}
332+
333+
addr := msConnectionString(ip, port)
334+
p := &SQLServer{}
335+
d, err := p.Open(addr)
336+
if err != nil {
337+
t.Fatal(err)
338+
}
339+
340+
lock := d.(*SQLServer)
341+
342+
p = &SQLServer{}
343+
d, err = p.Open(addr + "&x-no-lock=true")
344+
if err != nil {
345+
t.Fatal(err)
346+
}
347+
348+
noLock := d.(*SQLServer)
349+
350+
if err = lock.Lock(); err != nil {
351+
t.Fatal(err)
352+
}
353+
if err = noLock.Lock(); err != nil {
354+
t.Fatal(err)
355+
}
356+
if err = lock.Unlock(); err != nil {
357+
t.Fatal(err)
358+
}
359+
if err = noLock.Unlock(); err != nil {
360+
t.Fatal(err)
361+
}
362+
})
363+
}
364+
314365
func testMsiFalse(t *testing.T) {
315366
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
316367
SkipIfUnsupportedArch(t, c)

database/yugabytedb/yugabytedb_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package yugabytedb
55
import (
66
"context"
77
"database/sql"
8+
"errors"
89
"fmt"
910
"log"
11+
"strconv"
1012
"strings"
1113
"testing"
1214
"time"
@@ -95,6 +97,7 @@ func Test(t *testing.T) {
9597
t.Run("testMigrate", testMigrate)
9698
t.Run("testMultiStatement", testMultiStatement)
9799
t.Run("testFilterCustomQuery", testFilterCustomQuery)
100+
t.Run("testNoLockWorks", testNoLockWorks)
98101

99102
t.Cleanup(func() {
100103
for _, spec := range specs {
@@ -179,6 +182,55 @@ func testMultiStatement(t *testing.T) {
179182
})
180183
}
181184

185+
func TestNoLockParamValidation(t *testing.T) {
186+
c := &YugabyteDB{}
187+
_, err := c.Open("yugabyte://yugabyte@localhost/migrate?x-no-lock=not-a-bool")
188+
if !errors.Is(err, strconv.ErrSyntax) {
189+
t.Fatal("Expected syntax error when passing a non-bool as x-no-lock parameter")
190+
}
191+
}
192+
193+
func testNoLockWorks(t *testing.T) {
194+
dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) {
195+
createDB(t, ci)
196+
197+
ip, port, err := ci.Port(defaultPort)
198+
if err != nil {
199+
t.Fatal(err)
200+
}
201+
202+
addr := getConnectionString(ip, port)
203+
c := &YugabyteDB{}
204+
d, err := c.Open(addr)
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
209+
lock := d.(*YugabyteDB)
210+
211+
c = &YugabyteDB{}
212+
d, err = c.Open(getConnectionString(ip, port, "x-no-lock=true"))
213+
if err != nil {
214+
t.Fatal(err)
215+
}
216+
217+
noLock := d.(*YugabyteDB)
218+
219+
if err = lock.Lock(); err != nil {
220+
t.Fatal(err)
221+
}
222+
if err = noLock.Lock(); err != nil {
223+
t.Fatal(err)
224+
}
225+
if err = lock.Unlock(); err != nil {
226+
t.Fatal(err)
227+
}
228+
if err = noLock.Unlock(); err != nil {
229+
t.Fatal(err)
230+
}
231+
})
232+
}
233+
182234
func testFilterCustomQuery(t *testing.T) {
183235
dktesting.ParallelTest(t, specs, func(t *testing.T, ci dktest.ContainerInfo) {
184236
createDB(t, ci)

0 commit comments

Comments
 (0)