add test and remove unused warn log (#886)

This commit is contained in:
Lunny Xiao 2018-04-11 15:39:04 +08:00 committed by GitHub
parent c8ae6fa95b
commit 5c2af83817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 437 additions and 374 deletions

View File

@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/go-xorm/builder"
@ -51,7 +52,9 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
fieldValuePtr, err := col.ValueOf(bean)
if err != nil {
engine.logger.Error(err)
if !strings.Contains(err.Error(), "is not valid") {
engine.logger.Warn(err)
}
continue
}

View File

@ -6,6 +6,7 @@ package xorm
import (
"errors"
"fmt"
)
var (
@ -25,4 +26,16 @@ var (
ErrNotImplemented = errors.New("Not implemented")
// ErrConditionType condition type unsupported
ErrConditionType = errors.New("Unsupported conditon type")
// ErrColumnIsNotExist columns is not exist
ErrFieldIsNotExist = errors.New("Field is not exist")
)
// ErrFieldIsNotValid is not valid
type ErrFieldIsNotValid struct {
FieldName string
TableName string
}
func (e ErrFieldIsNotValid) Error() string {
return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName)
}

View File

@ -42,6 +42,7 @@ type Interface interface {
IsTableExist(beanOrTableName interface{}) (bool, error)
Iterate(interface{}, IterFunc) error
Limit(int, ...int) *Session
MustCols(columns ...string) *Session
NoAutoCondition(...bool) *Session
NotIn(string, ...interface{}) *Session
Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session

View File

@ -278,24 +278,22 @@ func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt,
return
}
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value {
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) (*reflect.Value, error) {
var col *core.Column
if col = table.GetColumnIdx(key, idx); col == nil {
//session.engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq())
return nil
return nil, ErrFieldIsNotExist
}
fieldValue, err := col.ValueOfV(dataStruct)
if err != nil {
session.engine.logger.Error(err)
return nil
return nil, err
}
if !fieldValue.IsValid() || !fieldValue.CanSet() {
session.engine.logger.Warnf("table %v's column %v is not valid or cannot set", table.Name, key)
return nil
return nil, ErrFieldIsNotValid{key, table.Name}
}
return fieldValue
return fieldValue, nil
}
// Cell cell is a result of one column field
@ -407,7 +405,16 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
}
tempMap[lKey] = idx
if fieldValue := session.getField(dataStruct, key, table, idx); fieldValue != nil {
fieldValue, err := session.getField(dataStruct, key, table, idx)
if err != nil {
if !strings.Contains(err.Error(), "is not valid") {
session.engine.logger.Warn(err)
}
continue
}
if fieldValue == nil {
continue
}
rawValue := reflect.Indirect(reflect.ValueOf(scanResults[ii]))
// if row is null then ignore
@ -812,7 +819,6 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
}
}
}
}
return pk, nil
}

View File

@ -67,3 +67,43 @@ func TestCols(t *testing.T) {
assert.EqualValues(t, "", tb.Col1)
assert.EqualValues(t, "", tb.Col2)
}
func TestMustCol(t *testing.T) {
assert.NoError(t, prepareEngine())
type CustomerUpdate struct {
Id int64 `form:"id" json:"id"`
Username string `form:"username" json:"username" binding:"required"`
Email string `form:"email" json:"email"`
Sex int `form:"sex" json:"sex"`
Name string `form:"name" json:"name" binding:"required"`
Telephone string `form:"telephone" json:"telephone"`
Type int `form:"type" json:"type" binding:"required"`
ParentId int64 `form:"parent_id" json:"parent_id" xorm:"int null"`
Remark string `form:"remark" json:"remark"`
Status int `form:"status" json:"status" binding:"required"`
Age int `form:"age" json:"age"`
CreatedAt int64 `xorm:"created" form:"created_at" json:"created_at"`
UpdatedAt int64 `xorm:"updated" form:"updated_at" json:"updated_at"`
BirthDate int64 `form:"birth_date" json:"birth_date"`
Password string `xorm:"varchar(200)" form:"password" json:"password"`
}
assertSync(t, new(CustomerUpdate))
var customer = CustomerUpdate{
ParentId: 1,
}
cnt, err := testEngine.Insert(&customer)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
type CustomerOnlyId struct {
Id int64
}
customer.ParentId = 0
affected, err := testEngine.MustCols("parent_id").Update(&customer, &CustomerOnlyId{Id: customer.Id})
assert.NoError(t, err)
assert.EqualValues(t, 1, affected)
}