xorm/session_cols_test.go

70 lines
1.3 KiB
Go
Raw Normal View History

2017-03-23 06:05:32 +00:00
// 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
2017-05-27 09:31:39 +00:00
import (
"testing"
"github.com/go-xorm/core"
2017-05-27 09:31:39 +00:00
"github.com/stretchr/testify/assert"
)
2017-03-23 06:05:32 +00:00
func TestSetExpr(t *testing.T) {
2017-05-27 09:31:39 +00:00
assert.NoError(t, prepareEngine())
type UserExpr struct {
2017-03-23 06:05:32 +00:00
Id int64
Show bool
}
assert.NoError(t, testEngine.Sync2(new(UserExpr)))
2017-05-27 09:31:39 +00:00
cnt, err := testEngine.Insert(&UserExpr{
2017-05-27 09:31:39 +00:00
Show: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var not = "NOT"
if testEngine.Dialect().DBType() == core.MSSQL {
not = "~"
}
cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
2017-05-27 09:31:39 +00:00
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
2017-03-23 06:05:32 +00:00
}
2017-07-24 14:09:47 +00:00
func TestCols(t *testing.T) {
assert.NoError(t, prepareEngine())
type ColsTable struct {
Id int64
Col1 string
Col2 string
}
assertSync(t, new(ColsTable))
_, err := testEngine.Insert(&ColsTable{
Col1: "1",
Col2: "2",
})
assert.NoError(t, err)
sess := testEngine.ID(1)
_, err = sess.Cols("col1").Cols("col2").Update(&ColsTable{
Col1: "",
Col2: "",
})
assert.NoError(t, err)
var tb ColsTable
has, err := testEngine.ID(1).Get(&tb)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "", tb.Col1)
assert.EqualValues(t, "", tb.Col2)
}