Skip to content

Commit

Permalink
add more test for join find (go-xorm#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored May 2, 2018
1 parent a491afa commit ae0364a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
12 changes: 10 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ var (
ErrNotImplemented = errors.New("Not implemented")
// ErrConditionType condition type unsupported
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
type ErrFieldIsNotValid struct {
FieldName string
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var col *core.Column
if col = table.GetColumnIdx(key, idx); col == nil {
return nil, ErrFieldIsNotExist
return nil, ErrFieldIsNotExist{key, table.Name}
}

fieldValue, err := col.ValueOfV(dataStruct)
Expand Down
7 changes: 6 additions & 1 deletion session_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func TestFindCacheLimit(t *testing.T) {

func TestFindJoin(t *testing.T) {
type SceneItem struct {
Type int
Type int
DeviceId int64
}

Expand All @@ -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").
Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
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)
}

0 comments on commit ae0364a

Please sign in to comment.