add more test for join find (#922)

This commit is contained in:
Lunny Xiao 2018-05-02 17:53:17 +08:00 committed by GitHub
parent a491afac18
commit ae0364a057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -26,10 +26,18 @@ var (
ErrNotImplemented = errors.New("Not implemented") ErrNotImplemented = errors.New("Not implemented")
// ErrConditionType condition type unsupported // ErrConditionType condition type unsupported
ErrConditionType = errors.New("Unsupported condition type") ErrConditionType = errors.New("Unsupported condition type")
// ErrFieldIsNotExist columns does not exist
ErrFieldIsNotExist = errors.New("Field does not exist")
) )
// ErrFieldIsNotExist columns does not exist
type ErrFieldIsNotExist struct {
FieldName string
TableName string
}
func (e ErrFieldIsNotExist) Error() string {
return fmt.Sprintf("field %s is not valid on table %s", e.FieldName, e.TableName)
}
// ErrFieldIsNotValid is not valid // ErrFieldIsNotValid is not valid
type ErrFieldIsNotValid struct { type ErrFieldIsNotValid struct {
FieldName string FieldName string

View File

@ -281,7 +281,7 @@ func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt,
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) (*reflect.Value, error) { func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) (*reflect.Value, error) {
var col *core.Column var col *core.Column
if col = table.GetColumnIdx(key, idx); col == nil { if col = table.GetColumnIdx(key, idx); col == nil {
return nil, ErrFieldIsNotExist return nil, ErrFieldIsNotExist{key, table.Name}
} }
fieldValue, err := col.ValueOfV(dataStruct) fieldValue, err := col.ValueOfV(dataStruct)

View File

@ -770,7 +770,7 @@ func TestFindCacheLimit(t *testing.T) {
func TestFindJoin(t *testing.T) { func TestFindJoin(t *testing.T) {
type SceneItem struct { type SceneItem struct {
Type int Type int
DeviceId int64 DeviceId int64
} }
@ -786,4 +786,9 @@ func TestFindJoin(t *testing.T) {
err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id"). err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id").
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes) Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
assert.NoError(t, err) assert.NoError(t, err)
scenes = make([]SceneItem, 0)
err = testEngine.Join("LEFT OUTER", new(DeviceUserPrivrels), "device_user_privrels.device_id=scene_item.device_id").
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
assert.NoError(t, err)
} }