Skip to content

Commit

Permalink
Merge branch 'eatigo-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 11, 2018
2 parents 3c70f83 + 7a8c2bb commit 0cc4d47
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 106 deletions.
6 changes: 4 additions & 2 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func TestCreate(t *testing.T) {
}

var newUser User
DB.First(&newUser, user.Id)
if err := DB.First(&newUser, user.Id).Error; err != nil {
t.Errorf("No error should happen, but got %v", err)
}

if !reflect.DeepEqual(newUser.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
t.Errorf("User's PasswordHash should be saved ([]byte)")
Expand All @@ -38,7 +40,7 @@ func TestCreate(t *testing.T) {
}

if newUser.UserNum != Num(111) {
t.Errorf("User's UserNum should be saved (custom type)")
t.Errorf("User's UserNum should be saved (custom type), but got %v", newUser.UserNum)
}

if newUser.Latitude != float {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB {
// Exec execute raw sql
func (s *DB) Exec(sql string, values ...interface{}) *DB {
scope := s.NewScope(nil)
generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
generatedSQL := scope.buildCondition(map[string]interface{}{"query": sql, "args": values}, true)
generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
scope.Raw(generatedSQL)
return scope.Exec().db
Expand Down
41 changes: 41 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,47 @@ func TestQueryBuilderSubselectInWhere(t *testing.T) {
}
}

func TestQueryBuilderRawQueryWithSubquery(t *testing.T) {
user := User{Name: "subquery_test_user1", Age: 10}
DB.Save(&user)
user = User{Name: "subquery_test_user2", Age: 11}
DB.Save(&user)
user = User{Name: "subquery_test_user3", Age: 12}
DB.Save(&user)

var count int
err := DB.Raw("select count(*) from (?) tmp",
DB.Table("users").
Select("name").
Where("age >= ? and name in (?)", 10, []string{"subquery_test_user1", "subquery_test_user2"}).
Group("name").
QueryExpr(),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
if count != 2 {
t.Errorf("Row count must be 2, instead got %d", count)
}

err = DB.Raw("select count(*) from (?) tmp",
DB.Table("users").
Select("name").
Where("name LIKE ?", "subquery_test%").
Not("age <= ?", 10).Not("name in (?)", []string{"subquery_test_user1", "subquery_test_user2"}).
Group("name").
QueryExpr(),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
if count != 1 {
t.Errorf("Row count must be 1, instead got %d", count)
}
}

func TestQueryBuilderSubselectInHaving(t *testing.T) {
user := User{Name: "query_expr_having_ruser1", Email: "[email protected]", Age: 64}
DB.Save(&user)
Expand Down
3 changes: 3 additions & 0 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"reflect"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -168,6 +169,8 @@ type Num int64
func (i *Num) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
n, _ := strconv.Atoi(string(s))
*i = Num(n)
case int64:
*i = Num(s)
default:
Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestStringPrimaryKeyForNumericValueStartingWithZero(t *testing.T) {
var address AddressByZipCode
DB.First(&address, "00501")
if address.ZipCode != "00501" {
t.Errorf("Fetch a record from with a string primary key for a numeric value starting with zero should work, but failed")
t.Errorf("Fetch a record from with a string primary key for a numeric value starting with zero should work, but failed, zip code is %v", address.ZipCode)
}
}

Expand Down
Loading

0 comments on commit 0cc4d47

Please sign in to comment.