Support mock has one
This commit is contained in:
parent
8ae27d7ec7
commit
a0b70669e7
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
|
||||
)
|
||||
|
||||
@ -69,7 +70,25 @@ func getRelationRows(rVal reflect.Value, fieldName string, relation *Relationshi
|
||||
)
|
||||
|
||||
switch relation.Kind {
|
||||
case "has_one":
|
||||
// just a plain struct
|
||||
scope := &Scope{Value: rVal.Interface()}
|
||||
|
||||
for _, field := range scope.GetModelStruct().StructFields {
|
||||
if field.IsNormal {
|
||||
columns = append(columns, field.DBName)
|
||||
}
|
||||
}
|
||||
|
||||
rows = sqlmock.NewRows(columns)
|
||||
|
||||
// we don't have a slice
|
||||
row := getRowForFields(scope.Fields())
|
||||
rows = rows.AddRow(row...)
|
||||
|
||||
return rows, true
|
||||
case "has_many", "many_to_many":
|
||||
// in this case, we're guarnateed to have a slice
|
||||
elem := rVal.Type().Elem()
|
||||
scope := &Scope{Value: reflect.New(elem).Interface()}
|
||||
|
||||
@ -165,6 +184,7 @@ func (q *SqlmockQuery) Returns(out interface{}) ExpectedQuery {
|
||||
|
||||
for i, query := range q.queries {
|
||||
query.WillReturnRows(rows[i])
|
||||
spew.Dump(query)
|
||||
}
|
||||
|
||||
return q
|
||||
|
@ -145,6 +145,31 @@ func TestMockPreloadHasMany(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockPreloadHasOne(t *testing.T) {
|
||||
db, expect, err := gorm.NewDefaultExpecter()
|
||||
defer func() {
|
||||
db.Close()
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
in := User{Id: 1}
|
||||
out := User{Id: 1, CreditCard: CreditCard{Number: "12345678"}}
|
||||
|
||||
expect.Preload("CreditCard").Find(&in).Returns(out)
|
||||
db.Preload("CreditCard").Find(&in)
|
||||
|
||||
if err := expect.AssertExpectations(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(in, out) {
|
||||
t.Error("In and out are not equal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockPreloadMany2Many(t *testing.T) {
|
||||
db, expect, err := gorm.NewDefaultExpecter()
|
||||
defer func() {
|
||||
@ -166,7 +191,7 @@ func TestMockPreloadMany2Many(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// if !reflect.DeepEqual(in, out) {
|
||||
// t.Error("In and out are not equal")
|
||||
// }
|
||||
// if !reflect.DeepEqual(in, out) {
|
||||
// t.Error("In and out are not equal")
|
||||
// }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user