Skip to content

Commit b63be72

Browse files
author
Roman Dvoskin
committed
Fix rex and add now to tests
1 parent 0794cb1 commit b63be72

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

named.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper)
210210
return bound, arglist, nil
211211
}
212212

213-
var valueBracketReg = regexp.MustCompile(`\([^(]*\?+[^)]*\)`)
213+
var valueBracketReg = regexp.MustCompile(`\([^(]*.[^(]\)$`)
214214

215215
func fixBound(bound string, loop int) string {
216216
loc := valueBracketReg.FindStringIndex(bound)

named_context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestNamedContextQueries(t *testing.T) {
12-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
12+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
1313
loadDefaultFixture(db, t)
1414
test := Test{t}
1515
var ns *NamedStmt

named_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlx
22

33
import (
44
"database/sql"
5+
"fmt"
56
"testing"
67
)
78

@@ -119,7 +120,7 @@ func (t Test) Errorf(err error, format string, args ...interface{}) {
119120
}
120121

121122
func TestNamedQueries(t *testing.T) {
122-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
123+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
123124
loadDefaultFixture(db, t)
124125
test := Test{t}
125126
var ns *NamedStmt
@@ -191,8 +192,8 @@ func TestNamedQueries(t *testing.T) {
191192
{FirstName: "Ngani", LastName: "Laumape", Email: "[email protected]"},
192193
}
193194

194-
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
195-
VALUES (:first_name, :last_name, :email)`, sls)
195+
insert := fmt.Sprintf("INSERT INTO person (first_name, last_name, email, added_at) VALUES (:first_name, :last_name, :email, %v)", now)
196+
_, err = db.NamedExec(insert, sls)
196197
test.Error(err)
197198

198199
for _, p := range sls {

sqlx_context_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func MultiExecContext(ctx context.Context, e ExecerContext, query string) {
4242
}
4343

4444
func RunWithSchemaContext(ctx context.Context, schema Schema, t *testing.T, test func(ctx context.Context, db *DB, t *testing.T)) {
45-
runner := func(ctx context.Context, db *DB, t *testing.T, create, drop string) {
45+
runner := func(ctx context.Context, db *DB, t *testing.T, create, drop, now string) {
4646
defer func() {
4747
MultiExecContext(ctx, db, drop)
4848
}()
@@ -52,16 +52,16 @@ func RunWithSchemaContext(ctx context.Context, schema Schema, t *testing.T, test
5252
}
5353

5454
if TestPostgres {
55-
create, drop := schema.Postgres()
56-
runner(ctx, pgdb, t, create, drop)
55+
create, drop, now := schema.Postgres()
56+
runner(ctx, pgdb, t, create, drop, now)
5757
}
5858
if TestSqlite {
59-
create, drop := schema.Sqlite3()
60-
runner(ctx, sldb, t, create, drop)
59+
create, drop, now := schema.Sqlite3()
60+
runner(ctx, sldb, t, create, drop, now)
6161
}
6262
if TestMysql {
63-
create, drop := schema.MySQL()
64-
runner(ctx, mysqldb, t, create, drop)
63+
create, drop, now := schema.MySQL()
64+
runner(ctx, mysqldb, t, create, drop, now)
6565
}
6666
}
6767

sqlx_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ type Schema struct {
9898
drop string
9999
}
100100

101-
func (s Schema) Postgres() (string, string) {
102-
return s.create, s.drop
101+
func (s Schema) Postgres() (string, string, string) {
102+
return s.create, s.drop, `now()`
103103
}
104104

105-
func (s Schema) MySQL() (string, string) {
106-
return strings.Replace(s.create, `"`, "`", -1), s.drop
105+
func (s Schema) MySQL() (string, string, string) {
106+
return strings.Replace(s.create, `"`, "`", -1), s.drop, `now()`
107107
}
108108

109-
func (s Schema) Sqlite3() (string, string) {
110-
return strings.Replace(s.create, `now()`, `CURRENT_TIMESTAMP`, -1), s.drop
109+
func (s Schema) Sqlite3() (string, string, string) {
110+
return strings.Replace(s.create, `now()`, `CURRENT_TIMESTAMP`, -1), s.drop, `CURRENT_TIMESTAMP`
111111
}
112112

113113
var defaultSchema = Schema{
@@ -218,27 +218,27 @@ func MultiExec(e Execer, query string) {
218218
}
219219
}
220220

221-
func RunWithSchema(schema Schema, t *testing.T, test func(db *DB, t *testing.T)) {
222-
runner := func(db *DB, t *testing.T, create, drop string) {
221+
func RunWithSchema(schema Schema, t *testing.T, test func(db *DB, t *testing.T, now string)) {
222+
runner := func(db *DB, t *testing.T, create, drop, now string) {
223223
defer func() {
224224
MultiExec(db, drop)
225225
}()
226226

227227
MultiExec(db, create)
228-
test(db, t)
228+
test(db, t, now)
229229
}
230230

231231
if TestPostgres {
232-
create, drop := schema.Postgres()
233-
runner(pgdb, t, create, drop)
232+
create, drop, now := schema.Postgres()
233+
runner(pgdb, t, create, drop, now)
234234
}
235235
if TestSqlite {
236-
create, drop := schema.Sqlite3()
237-
runner(sldb, t, create, drop)
236+
create, drop, now := schema.Sqlite3()
237+
runner(sldb, t, create, drop, now)
238238
}
239239
if TestMysql {
240-
create, drop := schema.MySQL()
241-
runner(mysqldb, t, create, drop)
240+
create, drop, now := schema.MySQL()
241+
runner(mysqldb, t, create, drop, now)
242242
}
243243
}
244244

@@ -263,7 +263,7 @@ func loadDefaultFixture(db *DB, t *testing.T) {
263263
// Test a new backwards compatible feature, that missing scan destinations
264264
// will silently scan into sql.RawText rather than failing/panicing
265265
func TestMissingNames(t *testing.T) {
266-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
266+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
267267
loadDefaultFixture(db, t)
268268
type PersonPlus struct {
269269
FirstName string `db:"first_name"`
@@ -383,7 +383,7 @@ func TestEmbeddedStructs(t *testing.T) {
383383
type Loop2 struct{ Loop1 }
384384
type Loop3 struct{ Loop2 }
385385

386-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
386+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
387387
loadDefaultFixture(db, t)
388388
peopleAndPlaces := []PersonPlace{}
389389
err := db.Select(
@@ -476,7 +476,7 @@ func TestJoinQuery(t *testing.T) {
476476
}
477477
type Boss Employee
478478

479-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
479+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
480480
loadDefaultFixture(db, t)
481481

482482
var employees []struct {
@@ -512,7 +512,7 @@ func TestJoinQueryNamedPointerStructs(t *testing.T) {
512512
}
513513
type Boss Employee
514514

515-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
515+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
516516
loadDefaultFixture(db, t)
517517

518518
var employees []struct {
@@ -544,7 +544,7 @@ func TestJoinQueryNamedPointerStructs(t *testing.T) {
544544
}
545545

546546
func TestSelectSliceMapTime(t *testing.T) {
547-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
547+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
548548
loadDefaultFixture(db, t)
549549
rows, err := db.Queryx("SELECT * FROM person")
550550
if err != nil {
@@ -573,7 +573,7 @@ func TestSelectSliceMapTime(t *testing.T) {
573573
}
574574

575575
func TestNilReceiver(t *testing.T) {
576-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
576+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
577577
loadDefaultFixture(db, t)
578578
var p *Person
579579
err := db.Get(p, "SELECT * FROM person LIMIT 1")
@@ -619,7 +619,7 @@ func TestNamedQuery(t *testing.T) {
619619
`,
620620
}
621621

622-
RunWithSchema(schema, t, func(db *DB, t *testing.T) {
622+
RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
623623
type Person struct {
624624
FirstName sql.NullString `db:"first_name"`
625625
LastName sql.NullString `db:"last_name"`
@@ -829,7 +829,7 @@ func TestNilInserts(t *testing.T) {
829829
drop: "drop table tt;",
830830
}
831831

832-
RunWithSchema(schema, t, func(db *DB, t *testing.T) {
832+
RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
833833
type TT struct {
834834
ID int
835835
Value *string
@@ -874,7 +874,7 @@ func TestScanError(t *testing.T) {
874874
drop: `drop table kv;`,
875875
}
876876

877-
RunWithSchema(schema, t, func(db *DB, t *testing.T) {
877+
RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
878878
type WrongTypes struct {
879879
K int
880880
V string
@@ -902,7 +902,7 @@ func TestScanError(t *testing.T) {
902902
// loading and reloading the schema..
903903

904904
func TestUsage(t *testing.T) {
905-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
905+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
906906
loadDefaultFixture(db, t)
907907
slicemembers := []SliceMember{}
908908
err := db.Select(&slicemembers, "SELECT * FROM place ORDER BY telcode ASC")
@@ -1421,7 +1421,7 @@ func TestEmbeddedMaps(t *testing.T) {
14211421
drop: `drop table message;`,
14221422
}
14231423

1424-
RunWithSchema(schema, t, func(db *DB, t *testing.T) {
1424+
RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
14251425
messages := []Message{
14261426
{"Hello, World", PropertyMap{"one": "1", "two": "2"}},
14271427
{"Thanks, Joy", PropertyMap{"pull": "request"}},
@@ -1465,7 +1465,7 @@ func TestIssue197(t *testing.T) {
14651465
type Var struct{ Raw json.RawMessage }
14661466
type Var2 struct{ Raw []byte }
14671467
type Var3 struct{ Raw mybyte }
1468-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
1468+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
14691469
var err error
14701470
var v, q Var
14711471
if err = db.Get(&v, `SELECT '{"a": "b"}' AS raw`); err != nil {
@@ -1565,7 +1565,7 @@ func TestIn(t *testing.T) {
15651565
t.Error("Expected an error, but got nil.")
15661566
}
15671567
}
1568-
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) {
1568+
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
15691569
loadDefaultFixture(db, t)
15701570
//tx.MustExec(tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
15711571
//tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
@@ -1689,7 +1689,7 @@ func TestEmbeddedLiterals(t *testing.T) {
16891689
drop: `drop table x;`,
16901690
}
16911691

1692-
RunWithSchema(schema, t, func(db *DB, t *testing.T) {
1692+
RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
16931693
type t1 struct {
16941694
K *string
16951695
}

0 commit comments

Comments
 (0)