DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.14k stars 407 forks source link

pq: cannot convert pq.Int64Array to Int64Array #156

Closed XieWeiXie closed 5 years ago

XieWeiXie commented 5 years ago

Hi, guys. Is there an easy way to solve this problem?

type PersonMockData struct {
}

func (p PersonMockData) GetPerson() models.People {
    return models.People{
        BaseModel: models.BaseModel{
            ID:        1,
            CreatedAt: time.Now(),
            UpdatedAt: time.Now(),
            DeletedAt: nil,
        },
        Name:       "xieWei",
        PersonUUID: "xieWei-Person-Uuid",
        UserID:     1,
        FaceIDs:    pq.Int64Array{1},
        Group:      []models.Group{},
    }
}

func (p PersonMockData) GetRowsForPerson(person models.People) *sqlmock.Rows {
    var personField = []string{"id", "created_at", "updated_at", "deleted_at", "name", "person_uuid", "user_id", "face_ids"}
    rows := sqlmock.NewRows(personField)
    rows.AddRow(person.ID, person.CreatedAt, person.UpdatedAt, person.DeletedAt, person.Name, person.PersonUUID, person.UserID, person.FaceIDs)
    return rows
}

and then:

mock.ExpectQuery(testdata.FixRule(sqlPeople)).WillReturnRows(personMockData.GetRowsForPerson(person))

error found:

name "face_ids": pq: cannot convert pq.Int64Array to Int64Array

So? what should i do?

l3pp4rd commented 5 years ago

Hi, by default sqlmock supports only standard go library value converter. You may customize it I guess, in order to support custom value converter not sure if pq exposes it, but that should allow to transform custom types to sql driver values. Note, you would need to create rows from Sqlmock instance (since only then it would use the custom value converter)

l3pp4rd commented 5 years ago

can now be managed with custom converter #195