From 2ba7d73eec439639f302c77f5fe0e31b37c9e708 Mon Sep 17 00:00:00 2001 From: Jason Moiron Date: Tue, 12 Jan 2016 00:35:08 -0500 Subject: [PATCH] some simple lint fixes for naming in tests, add a test that shows #197 failing and a comment about why it fails --- reflectx/reflect_test.go | 8 ++--- sqlx_test.go | 63 +++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/reflectx/reflect_test.go b/reflectx/reflect_test.go index 7a76dadd..28c1aec2 100644 --- a/reflectx/reflect_test.go +++ b/reflectx/reflect_test.go @@ -220,7 +220,7 @@ func TestInlineStruct(t *testing.T) { type Employee struct { Name string - Id int + ID int } type Boss Employee type person struct { @@ -229,7 +229,7 @@ func TestInlineStruct(t *testing.T) { } // employees columns: (employee.name employee.id boss.name boss.id) - em := person{Employee: Employee{Name: "Joe", Id: 2}, Boss: Boss{Name: "Dick", Id: 1}} + em := person{Employee: Employee{Name: "Joe", ID: 2}, Boss: Boss{Name: "Dick", ID: 1}} ev := reflect.ValueOf(em) fields := m.TypeMap(reflect.TypeOf(em)) @@ -242,8 +242,8 @@ func TestInlineStruct(t *testing.T) { t.Errorf("Expecting %s, got %s", em.Employee.Name, v.Interface().(string)) } v = m.FieldByName(ev, "boss.id") - if ival(v) != em.Boss.Id { - t.Errorf("Expecting %s, got %s", em.Boss.Id, ival(v)) + if ival(v) != em.Boss.ID { + t.Errorf("Expecting %s, got %s", em.Boss.ID, ival(v)) } } diff --git a/sqlx_test.go b/sqlx_test.go index c392685f..bc5683bc 100644 --- a/sqlx_test.go +++ b/sqlx_test.go @@ -470,9 +470,9 @@ func TestEmbeddedStructs(t *testing.T) { func TestJoinQuery(t *testing.T) { type Employee struct { Name string - Id int64 - // BossId is an id into the employee table - BossId sql.NullInt64 `db:"boss_id"` + ID int64 + // BossID is an id into the employee table + BossID sql.NullInt64 `db:"boss_id"` } type Boss Employee @@ -496,7 +496,7 @@ func TestJoinQuery(t *testing.T) { if len(em.Employee.Name) == 0 { t.Errorf("Expected non zero lengthed name.") } - if em.Employee.BossId.Int64 != em.Boss.Id { + if em.Employee.BossID.Int64 != em.Boss.ID { t.Errorf("Expected boss ids to match") } } @@ -506,9 +506,9 @@ func TestJoinQuery(t *testing.T) { func TestJoinQueryNamedPointerStructs(t *testing.T) { type Employee struct { Name string - Id int64 - // BossId is an id into the employee table - BossId sql.NullInt64 `db:"boss_id"` + ID int64 + // BossID is an id into the employee table + BossID sql.NullInt64 `db:"boss_id"` } type Boss Employee @@ -536,7 +536,7 @@ func TestJoinQueryNamedPointerStructs(t *testing.T) { if len(em.Emp1.Name) == 0 || len(em.Emp2.Name) == 0 { t.Errorf("Expected non zero lengthed name.") } - if em.Emp1.BossId.Int64 != em.Boss.Id || em.Emp2.BossId.Int64 != em.Boss.Id { + if em.Emp1.BossID.Int64 != em.Boss.ID || em.Emp2.BossID.Int64 != em.Boss.ID { t.Errorf("Expected boss ids to match") } } @@ -1341,6 +1341,53 @@ func TestEmbeddedMaps(t *testing.T) { }) } +func TestIssue197(t *testing.T) { + // this test actually tests for a bug in database/sql: + // https://github.com/golang/go/issues/13905 + // this potentially makes _any_ named type that is an alias for []byte + // unsafe to use in a lot of different ways (basically, unsafe to hold + // onto after loading from the database). + t.Skip() + + type mybyte []byte + type Var struct{ Raw json.RawMessage } + type Var2 struct{ Raw []byte } + type Var3 struct{ Raw mybyte } + RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) { + var err error + var v, q Var + if err = db.Get(&v, `SELECT '{"a": "b"}' AS raw`); err != nil { + t.Fatal(err) + } + fmt.Printf("%s: v %s\n", db.DriverName(), v.Raw) + if err = db.Get(&q, `SELECT 'null' AS raw`); err != nil { + t.Fatal(err) + } + fmt.Printf("%s: v %s\n", db.DriverName(), v.Raw) + + var v2, q2 Var2 + if err = db.Get(&v2, `SELECT '{"a": "b"}' AS raw`); err != nil { + t.Fatal(err) + } + fmt.Printf("%s: v2 %s\n", db.DriverName(), v2.Raw) + if err = db.Get(&q2, `SELECT 'null' AS raw`); err != nil { + t.Fatal(err) + } + fmt.Printf("%s: v2 %s\n", db.DriverName(), v2.Raw) + + var v3, q3 Var3 + if err = db.QueryRow(`SELECT '{"a": "b"}' AS raw`).Scan(&v3.Raw); err != nil { + t.Fatal(err) + } + fmt.Printf("v3 %s\n", v3.Raw) + if err = db.QueryRow(`SELECT '{"c": "d"}' AS raw`).Scan(&q3.Raw); err != nil { + t.Fatal(err) + } + fmt.Printf("v3 %s\n", v3.Raw) + t.Fail() + }) +} + func TestIn(t *testing.T) { // some quite normal situations type tr struct {