Browse Source

Add mssql ci test (#1203)

* add mssql ci test

* fix mssql ci

* check test databases

* fix ci

* fix postgres schema tests

* fix ci on postgres

* fix insert map bug

* fix insert map bug

* fix bug

* fix insert map bug

* fix create schema on tests

* fix mssql tests

* fix insert return id bugs on mssql

* add head comments
tags/v.0.7.3
Lunny Xiao GitHub 10 months ago
parent
commit
17b9cc7330
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 667 additions and 728 deletions
  1. +10
    -2
      .circleci/config.yml
  2. +1
    -1
      engine_table.go
  3. +432
    -548
      processors_test.go
  4. +19
    -2
      session_delete_test.go
  5. +2
    -2
      session_exist.go
  6. +16
    -3
      session_get_test.go
  7. +33
    -10
      session_insert.go
  8. +10
    -35
      session_insert_test.go
  9. +4
    -4
      session_query_test.go
  10. +1
    -1
      statement.go
  11. +70
    -111
      tag_extends_test.go
  12. +1
    -1
      test_mssql.sh
  13. +4
    -0
      types.go
  14. +14
    -6
      types_test.go
  15. +50
    -2
      xorm_test.go

+ 10
- 2
.circleci/config.yml View File

@@ -19,9 +19,15 @@ jobs:
# CircleCI PostgreSQL images available at: https://hub.docker.com/r/circleci/postgres/
- image: circleci/postgres:9.6.2-alpine
environment:
POSTGRES_USER: root
POSTGRES_USER: circleci
POSTGRES_DB: xorm_test

- image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
MSSQL_PID: Developer

working_directory: /go/src/github.com/go-xorm/xorm
steps:
- checkout
@@ -43,6 +49,8 @@ jobs:
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -cache=true -coverprofile=coverage4-2.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -schema=xorm -coverprofile=coverage5-1.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic
- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt > coverage.txt
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -coverprofile=coverage6-1.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -cache=true -coverprofile=coverage6-2.txt -covermode=atomic
- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt coverage6-1.txt coverage6-2.txt > coverage.txt

- run: bash <(curl -s https://codecov.io/bash)

+ 1
- 1
engine_table.go View File

@@ -12,7 +12,7 @@ import (
"github.com/go-xorm/core"
)

// TableNameWithSchema will automatically add schema prefix on table name
// tbNameWithSchema will automatically add schema prefix on table name
func (engine *Engine) tbNameWithSchema(v string) string {
// Add schema name as prefix of table name.
// Only for postgres database.


+ 432
- 548
processors_test.go
File diff suppressed because it is too large
View File


+ 19
- 2
session_delete_test.go View File

@@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
)

@@ -21,11 +22,27 @@ func TestDelete(t *testing.T) {

assert.NoError(t, testEngine.Sync2(new(UserinfoDelete)))

session := testEngine.NewSession()
defer session.Close()

var err error
if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("SET IDENTITY_INSERT userinfo_delete ON")
assert.NoError(t, err)
}

user := UserinfoDelete{Uid: 1}
cnt, err := testEngine.Insert(&user)
cnt, err := session.Insert(&user)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

cnt, err = testEngine.Delete(&UserinfoDelete{Uid: user.Uid})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
@@ -40,7 +57,7 @@ func TestDelete(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.Where("id=?", user.Uid).Delete(&UserinfoDelete{})
cnt, err = testEngine.Where("`id`=?", user.Uid).Delete(&UserinfoDelete{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)



+ 2
- 2
session_exist.go View File

@@ -41,14 +41,14 @@ func (session *Session) Exist(bean ...interface{}) (bool, error) {
}

if session.engine.dialect.DBType() == core.MSSQL {
sqlStr = fmt.Sprintf("SELECT top 1 * FROM %s WHERE %s", tableName, condSQL)
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s WHERE %s", tableName, condSQL)
} else {
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT 1", tableName, condSQL)
}
args = condArgs
} else {
if session.engine.dialect.DBType() == core.MSSQL {
sqlStr = fmt.Sprintf("SELECT top 1 * FROM %s", tableName)
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s", tableName)
} else {
sqlStr = fmt.Sprintf("SELECT * FROM %s LIMIT 1", tableName)
}


+ 16
- 3
session_get_test.go View File

@@ -84,7 +84,11 @@ func TestGetVar(t *testing.T) {
assert.Equal(t, "1.5", fmt.Sprintf("%.1f", money))

var money2 float64
has, err = testEngine.SQL("SELECT money FROM " + testEngine.TableName("get_var", true) + " LIMIT 1").Get(&money2)
if testEngine.Dialect().DBType() == core.MSSQL {
has, err = testEngine.SQL("SELECT TOP 1 money FROM " + testEngine.TableName("get_var", true)).Get(&money2)
} else {
has, err = testEngine.SQL("SELECT money FROM " + testEngine.TableName("get_var", true) + " LIMIT 1").Get(&money2)
}
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1.5", fmt.Sprintf("%.1f", money2))
@@ -156,14 +160,23 @@ func TestGetStruct(t *testing.T) {

assert.NoError(t, testEngine.Sync2(new(UserinfoGet)))

session := testEngine.NewSession()
defer session.Close()

var err error
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT userinfo_get ON")
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("SET IDENTITY_INSERT userinfo_get ON")
assert.NoError(t, err)
}
cnt, err := testEngine.Insert(&UserinfoGet{Uid: 2})
cnt, err := session.Insert(&UserinfoGet{Uid: 2})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

user := UserinfoGet{Uid: 2}
has, err := testEngine.Get(&user)


+ 33
- 10
session_insert.go View File

@@ -373,21 +373,30 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {

var sqlStr string
var tableName = session.statement.TableName()
var output string
if session.engine.dialect.DBType() == core.MSSQL && len(table.AutoIncrement) > 0 {
output = fmt.Sprintf(" OUTPUT Inserted.%s", table.AutoIncrement)
}
if len(colPlaces) > 0 {
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v)%s VALUES (%v)",
session.engine.Quote(tableName),
session.engine.QuoteStr(),
strings.Join(colNames, session.engine.Quote(", ")),
session.engine.QuoteStr(),
output,
colPlaces)
} else {
if session.engine.dialect.DBType() == core.MYSQL {
sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", session.engine.Quote(tableName))
} else {
sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", session.engine.Quote(tableName))
sqlStr = fmt.Sprintf("INSERT INTO %s%s DEFAULT VALUES", session.engine.Quote(tableName), output)
}
}

if len(table.AutoIncrement) > 0 && session.engine.dialect.DBType() == core.POSTGRES {
sqlStr = sqlStr + " RETURNING " + session.engine.Quote(table.AutoIncrement)
}

handleAfterInsertProcessorFunc := func(bean interface{}) {
if session.isAutoCommit {
for _, closure := range session.afterClosures {
@@ -459,9 +468,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
aiValue.Set(int64ToIntValue(id, aiValue.Type()))

return 1, nil
} else if session.engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 {
//assert table.AutoIncrement != ""
sqlStr = sqlStr + " RETURNING " + session.engine.Quote(table.AutoIncrement)
} else if len(table.AutoIncrement) > 0 && (session.engine.dialect.DBType() == core.POSTGRES || session.engine.dialect.DBType() == core.MSSQL) {
res, err := session.queryBytes(sqlStr, args...)

if err != nil {
@@ -481,7 +488,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
}

if len(res) < 1 {
return 0, errors.New("insert no error but not returned id")
return 0, errors.New("insert successfully but not returned id")
}

idByte := res[0][table.AutoIncrement]
@@ -660,6 +667,10 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
}

func (session *Session) insertMapInterface(m map[string]interface{}) (int64, error) {
if len(m) == 0 {
return 0, ErrParamsType
}

var columns = make([]string, 0, len(m))
for k := range m {
columns = append(columns, k)
@@ -669,8 +680,12 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err
qm := strings.Repeat("?,", len(columns))
qm = "(" + qm[:len(qm)-1] + ")"

tableName := session.statement.AltTableName
var sql = "INSERT INTO `" + tableName + "` (`" + strings.Join(columns, "`,`") + "`) VALUES " + qm
tableName := session.statement.TableName()
if len(tableName) <= 0 {
return 0, ErrTableNotFound
}

var sql = fmt.Sprintf("INSERT INTO %s (`%s`) VALUES %s", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)
var args = make([]interface{}, 0, len(m))
for _, colName := range columns {
args = append(args, m[colName])
@@ -692,6 +707,10 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err
}

func (session *Session) insertMapString(m map[string]string) (int64, error) {
if len(m) == 0 {
return 0, ErrParamsType
}

var columns = make([]string, 0, len(m))
for k := range m {
columns = append(columns, k)
@@ -701,8 +720,12 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) {
qm := strings.Repeat("?,", len(columns))
qm = "(" + qm[:len(qm)-1] + ")"

tableName := session.statement.AltTableName
var sql = "INSERT INTO `" + tableName + "` (`" + strings.Join(columns, "`,`") + "`) VALUES " + qm
tableName := session.statement.TableName()
if len(tableName) <= 0 {
return 0, ErrTableNotFound
}

var sql = fmt.Sprintf("INSERT INTO %s (`%s`) VALUES %s", session.engine.Quote(tableName), strings.Join(columns, "`,`"), qm)
var args = make([]interface{}, 0, len(m))
for _, colName := range columns {
args = append(args, m[colName])


+ 10
- 35
session_insert_test.go View File

@@ -145,41 +145,22 @@ func TestInsert(t *testing.T) {
user := Userinfo{0, "xiaolunwen", "dev", "lunny", time.Now(),
Userdetail{Id: 1}, 1.78, []byte{1, 2, 3}, true}
cnt, err := testEngine.Insert(&user)
fmt.Println(user.Uid)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
}

if user.Uid <= 0 {
err = errors.New("not return id error")
t.Error(err)
panic(err)
}
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt, "insert not returned 1")
assert.True(t, user.Uid > 0, "not return id error")

user.Uid = 0
cnt, err = testEngine.Insert(&user)
// Username is unique, so this should return error
assert.Error(t, err, "insert should fail but no error returned")
assert.EqualValues(t, 0, cnt, "insert not returned 1")
if err == nil {
err = errors.New("insert failed but no return error")
t.Error(err)
panic(err)
}
if cnt != 0 {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
return
panic("should return err")
}
}

func TestInsertAutoIncr(t *testing.T) {
assert.NoError(t, prepareEngine())

assertSync(t, new(Userinfo))

// auto increment insert
@@ -214,20 +195,14 @@ func TestInsertDefault(t *testing.T) {

di := new(DefaultInsert)
err := testEngine.Sync2(di)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)

var di2 = DefaultInsert{Name: "test"}
_, err = testEngine.Omit(testEngine.GetColumnMapper().Obj2Table("Status")).Insert(&di2)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)

has, err := testEngine.Desc("(id)").Get(di)
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
if !has {
err = errors.New("error with no data")
t.Error(err)


+ 4
- 4
session_query_test.go View File

@@ -207,7 +207,7 @@ func TestQueryStringNoParam(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0]["id"])
if testEngine.Dialect().URI().DbType == core.POSTGRES {
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0]["msg"])
} else {
assert.EqualValues(t, "0", records[0]["msg"])
@@ -217,7 +217,7 @@ func TestQueryStringNoParam(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0]["id"])
if testEngine.Dialect().URI().DbType == core.POSTGRES {
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0]["msg"])
} else {
assert.EqualValues(t, "0", records[0]["msg"])
@@ -244,7 +244,7 @@ func TestQuerySliceStringNoParam(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0][0])
if testEngine.Dialect().URI().DbType == core.POSTGRES {
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0][1])
} else {
assert.EqualValues(t, "0", records[0][1])
@@ -254,7 +254,7 @@ func TestQuerySliceStringNoParam(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0][0])
if testEngine.Dialect().URI().DbType == core.POSTGRES {
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0][1])
} else {
assert.EqualValues(t, "0", records[0][1])


+ 1
- 1
statement.go View File

@@ -1089,7 +1089,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n

if dialect.DBType() == core.MSSQL {
if statement.LimitN > 0 {
top = fmt.Sprintf(" TOP %d ", statement.LimitN)
top = fmt.Sprintf("TOP %d ", statement.LimitN)
}
if statement.Start > 0 {
var column string


+ 70
- 111
tag_extends_test.go View File

@@ -60,63 +60,37 @@ func TestExtends(t *testing.T) {
assert.NoError(t, prepareEngine())

err := testEngine.DropTables(&tempUser2{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.CreateTables(&tempUser2{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu := &tempUser2{tempUser{0, "extends"}, "dev depart"}
_, err = testEngine.Insert(tu)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu2 := &tempUser2{}
_, err = testEngine.Get(tu2)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu3 := &tempUser2{tempUser{0, "extends update"}, ""}
_, err = testEngine.ID(tu2.TempUser.Id).Update(tu3)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.DropTables(&tempUser4{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.CreateTables(&tempUser4{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu8 := &tempUser4{tempUser2{tempUser{0, "extends"}, "dev depart"}}
_, err = testEngine.Insert(tu8)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu9 := &tempUser4{}
_, err = testEngine.Get(tu9)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

if tu9.TempUser2.TempUser.Username != tu8.TempUser2.TempUser.Username || tu9.TempUser2.Departname != tu8.TempUser2.Departname {
err = errors.New(fmt.Sprintln("not equal for", tu8, tu9))
t.Error(err)
@@ -125,36 +99,22 @@ func TestExtends(t *testing.T) {

tu10 := &tempUser4{tempUser2{tempUser{0, "extends update"}, ""}}
_, err = testEngine.ID(tu9.TempUser2.TempUser.Id).Update(tu10)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.DropTables(&tempUser3{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.CreateTables(&tempUser3{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu4 := &tempUser3{&tempUser{0, "extends"}, "dev depart"}
_, err = testEngine.Insert(tu4)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

tu5 := &tempUser3{}
_, err = testEngine.Get(tu5)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

if tu5.Temp == nil {
err = errors.New("error get data extends")
t.Error(err)
@@ -169,22 +129,12 @@ func TestExtends(t *testing.T) {

tu6 := &tempUser3{&tempUser{0, "extends update"}, ""}
_, err = testEngine.ID(tu5.Temp.Id).Update(tu6)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

users := make([]tempUser3, 0)
err = testEngine.Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
if len(users) != 1 {
err = errors.New("error get data not 1")
t.Error(err)
panic(err)
}
assert.NoError(t, err)
assert.EqualValues(t, 1, len(users), "error get data not 1")

assertSync(t, new(Userinfo), new(Userdetail))

@@ -249,10 +199,7 @@ func TestExtends(t *testing.T) {
Join("LEFT", qt(ud), qt(ui)+"."+qt("detail_id")+" = "+qt(ud)+"."+qt(uiid)).
NoCascade().
Find(&infos2)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)
fmt.Println(infos2)
}

@@ -297,25 +244,16 @@ func TestExtends2(t *testing.T) {
assert.NoError(t, prepareEngine())

err := testEngine.DropTables(&Message{}, &MessageUser{}, &MessageType{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

err = testEngine.CreateTables(&Message{}, &MessageUser{}, &MessageType{})
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

var sender = MessageUser{Name: "sender"}
var receiver = MessageUser{Name: "receiver"}
var msgtype = MessageType{Name: "type"}
_, err = testEngine.Insert(&sender, &receiver, &msgtype)
if err != nil {
t.Error(err)
panic(err)
}
assert.NoError(t, err)

msg := Message{
MessageBase: MessageBase{
@@ -326,15 +264,24 @@ func TestExtends2(t *testing.T) {
Uid: sender.Id,
ToUid: receiver.Id,
}

session := testEngine.NewSession()
defer session.Close()

// MSSQL deny insert identity column excep declare as below
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
cnt, err := session.Insert(&msg)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

_, err = testEngine.Insert(&msg)
if err != nil {
t.Error(err)
panic(err)
if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

var mapper = testEngine.GetTableMapper().Obj2Table
@@ -344,23 +291,14 @@ func TestExtends2(t *testing.T) {
msgTableName := quote(testEngine.TableName(mapper("Message"), true))

list := make([]Message, 0)
err = testEngine.Table(msgTableName).Join("LEFT", []string{userTableName, "sender"}, "`sender`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
err = session.Table(msgTableName).Join("LEFT", []string{userTableName, "sender"}, "`sender`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
Join("LEFT", []string{userTableName, "receiver"}, "`receiver`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("ToUid")+"`").
Join("LEFT", []string{typeTableName, "type"}, "`type`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Id")+"`").
Find(&list)
assert.NoError(t, err)

if len(list) != 1 {
err = errors.New(fmt.Sprintln("should have 1 message, got", len(list)))
t.Error(err)
panic(err)
}

if list[0].Id != msg.Id {
err = errors.New(fmt.Sprintln("should message equal", list[0], msg))
t.Error(err)
panic(err)
}
assert.EqualValues(t, 1, len(list), fmt.Sprintln("should have 1 message, got", len(list)))
assert.EqualValues(t, msg.Id, list[0].Id, fmt.Sprintln("should message equal", list[0], msg))
}

func TestExtends3(t *testing.T) {
@@ -396,13 +334,25 @@ func TestExtends3(t *testing.T) {
Uid: sender.Id,
ToUid: receiver.Id,
}

session := testEngine.NewSession()
defer session.Close()

// MSSQL deny insert identity column excep declare as below
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
_, err = testEngine.Insert(&msg)
_, err = session.Insert(&msg)
assert.NoError(t, err)

if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

var mapper = testEngine.GetTableMapper().Obj2Table
var quote = testEngine.Quote
userTableName := quote(testEngine.TableName(mapper("MessageUser"), true))
@@ -410,7 +360,7 @@ func TestExtends3(t *testing.T) {
msgTableName := quote(testEngine.TableName(mapper("Message"), true))

list := make([]MessageExtend3, 0)
err = testEngine.Table(msgTableName).Join("LEFT", []string{userTableName, "sender"}, "`sender`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
err = session.Table(msgTableName).Join("LEFT", []string{userTableName, "sender"}, "`sender`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
Join("LEFT", []string{userTableName, "receiver"}, "`receiver`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("ToUid")+"`").
Join("LEFT", []string{typeTableName, "type"}, "`type`.`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Id")+"`").
Find(&list)
@@ -478,14 +428,23 @@ func TestExtends4(t *testing.T) {
Content: "test",
Uid: sender.Id,
}

session := testEngine.NewSession()
defer session.Close()

// MSSQL deny insert identity column excep declare as below
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
_, err = testEngine.Insert(&msg)
if err != nil {
t.Error(err)
panic(err)
_, err = session.Insert(&msg)
assert.NoError(t, err)

if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

var mapper = testEngine.GetTableMapper().Obj2Table
@@ -495,7 +454,7 @@ func TestExtends4(t *testing.T) {
msgTableName := quote(testEngine.TableName(mapper("Message"), true))

list := make([]MessageExtend4, 0)
err = testEngine.Table(msgTableName).Join("LEFT", userTableName, userTableName+".`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
err = session.Table(msgTableName).Join("LEFT", userTableName, userTableName+".`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Uid")+"`").
Join("LEFT", typeTableName, typeTableName+".`"+mapper("Id")+"`="+msgTableName+".`"+mapper("Id")+"`").
Find(&list)
if err != nil {


+ 1
- 1
test_mssql.sh View File

@@ -1 +1 @@
go test -db=mssql -conn_str="server=192.168.1.58;user id=sa;password=123456;database=xorm_test"
go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test"

+ 4
- 0
types.go View File

@@ -1,3 +1,7 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (


+ 14
- 6
types_test.go View File

@@ -309,16 +309,24 @@ func TestCustomType2(t *testing.T) {
_, err = testEngine.Exec("delete from " + testEngine.Quote(tableName))
assert.NoError(t, err)

session := testEngine.NewSession()
defer session.Close()

if testEngine.Dialect().DBType() == core.MSSQL {
return
/*_, err = engine.Exec("set IDENTITY_INSERT " + tableName + " on")
if err != nil {
t.Fatal(err)
}*/
err = session.Begin()
assert.NoError(t, err)
_, err = session.Exec("set IDENTITY_INSERT " + tableName + " on")
assert.NoError(t, err)
}

_, err = testEngine.Insert(&UserCus{1, "xlw", Registed})
cnt, err := session.Insert(&UserCus{1, "xlw", Registed})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

if testEngine.Dialect().DBType() == core.MSSQL {
err = session.Commit()
assert.NoError(t, err)
}

user := UserCus{}
exist, err := testEngine.ID(1).Get(&user)


+ 50
- 2
xorm_test.go View File

@@ -1,8 +1,14 @@
// Copyright 2018 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"strings"
"testing"
@@ -35,6 +41,48 @@ func createEngine(dbType, connStr string) error {
var err error

if !*cluster {
// create databases if not exist
var db *sql.DB
var err error
if strings.ToLower(dbType) != core.MSSQL {
db, err = sql.Open(dbType, connStr)
} else {
db, err = sql.Open(dbType, strings.Replace(connStr, "xorm_test", "master", -1))
}

if err != nil {
return err
}

switch strings.ToLower(dbType) {
case core.MSSQL:
if _, err = db.Exec("If(db_id(N'xorm_test') IS NULL) BEGIN CREATE DATABASE xorm_test; END;"); err != nil {
return fmt.Errorf("db.Exec: %v", err)
}
case core.POSTGRES:
rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = 'xorm_test'"))
if err != nil {
return fmt.Errorf("db.Query: %v", err)
}
defer rows.Close()

if !rows.Next() {
if _, err = db.Exec("CREATE DATABASE xorm_test"); err != nil {
return fmt.Errorf("CREATE DATABASE: %v", err)
}
}
if *schema != "" {
if _, err = db.Exec("CREATE SCHEMA IF NOT EXISTS " + *schema); err != nil {
return fmt.Errorf("CREATE SCHEMA: %v", err)
}
}
case core.MYSQL:
if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS xorm_test"); err != nil {
return fmt.Errorf("db.Exec: %v", err)
}
}
db.Close()

testEngine, err = NewEngine(dbType, connStr)
} else {
testEngine, err = NewEngineGroup(dbType, strings.Split(connStr, *splitter))
@@ -95,7 +143,7 @@ func TestMain(m *testing.M) {
}
} else {
if ptrConnStr == nil {
fmt.Println("you should indicate conn string")
log.Fatal("you should indicate conn string")
return
}
connString = *ptrConnStr
@@ -112,7 +160,7 @@ func TestMain(m *testing.M) {
fmt.Println("testing", dbType, connString)

if err := prepareEngine(); err != nil {
fmt.Println(err)
log.Fatal(err)
return
}



Loading…
Cancel
Save