예제 #1
0
func (me *crudExecTest) TestScanVal() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)

	sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
		WillReturnError(fmt.Errorf("query error"))

	sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1"))

	db := New("db-mock", mDb)
	exec := newCrudExec(db, nil, `SELECT "id" FROM "items"`)

	var id int64
	var ids []int64
	found, err := exec.ScanVal(id)
	assert.EqualError(t, err, "goqu: Type must be a pointer when calling ScanVal")
	assert.False(t, found)
	found, err = exec.ScanVal(&ids)
	assert.EqualError(t, err, "goqu: Cannot scan into a slice when calling ScanVal")
	assert.False(t, found)
	found, err = exec.ScanVal(&id)
	assert.EqualError(t, err, "query error")
	assert.False(t, found)

	var ptrId int64
	found, err = exec.ScanVal(&ptrId)
	assert.NoError(t, err)
	assert.Equal(t, ptrId, 1)
}
예제 #2
0
func (me *crudExecTest) TestScanStruct() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WillReturnError(fmt.Errorf("query error"))

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name", "phone_number", "age"}).FromCSVString("111 Test Addr,Test1,111-111-1111,20"))

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

	db := New("db-mock", mDb)
	exec := newCrudExec(db, nil, `SELECT * FROM "items"`)

	var slicePtr []testCrudActionItem
	var item testCrudActionItem
	found, err := exec.ScanStruct(item)
	assert.EqualError(t, err, "goqu: Type must be a pointer to a struct when calling ScanStruct")
	assert.False(t, found)
	found, err = exec.ScanStruct(&slicePtr)
	assert.EqualError(t, err, "goqu: Type must be a pointer to a struct when calling ScanStruct")
	assert.False(t, found)
	found, err = exec.ScanStruct(&item)
	assert.EqualError(t, err, "query error")
	assert.False(t, found)

	found, err = exec.ScanStruct(&item)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, item.Address, "111 Test Addr")
	assert.Equal(t, item.Name, "Test1")

	var composed testComposedCrudActionItem
	found, err = exec.ScanStruct(&composed)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, composed.Address, "111 Test Addr")
	assert.Equal(t, composed.Name, "Test1")
	assert.Equal(t, composed.PhoneNumber, "111-111-1111")
	assert.Equal(t, composed.Age, 20)

	var noTag testCrudActionNoTagsItem
	found, err = exec.ScanStruct(&noTag)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, noTag.Address, "111 Test Addr")
	assert.Equal(t, noTag.Name, "Test1")
}
예제 #3
0
func (me *postgresTest) TestDelete() {
	t := me.T()
	ds := me.db.From("entry")
	var e entry
	found, err := ds.Where(goqu.I("int").Eq(9)).Select("id").ScanStruct(&e)
	assert.NoError(t, err)
	assert.True(t, found)
	_, err = ds.Where(goqu.I("id").Eq(e.Id)).Delete().Exec()
	assert.NoError(t, err)

	count, err := ds.Count()
	assert.NoError(t, err)
	assert.Equal(t, count, 9)

	var id uint32
	found, err = ds.Where(goqu.I("id").Eq(e.Id)).ScanVal(&id)
	assert.NoError(t, err)
	assert.False(t, found)

	e = entry{}
	found, err = ds.Where(goqu.I("int").Eq(8)).Select("id").ScanStruct(&e)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.NotEqual(t, e.Id, 0)

	id = 0
	_, err = ds.Where(goqu.I("id").Eq(e.Id)).Returning("id").Delete().ScanVal(&id)
	assert.NoError(t, err)
	assert.Equal(t, id, e.Id)
}
예제 #4
0
func (me *mysqlTest) TestDelete() {
	t := me.T()
	ds := me.db.From("entry")
	var e entry
	found, err := ds.Where(goqu.I("int").Eq(9)).Select("id").ScanStruct(&e)
	assert.NoError(t, err)
	assert.True(t, found)
	_, err = ds.Where(goqu.I("id").Eq(e.Id)).Delete().Exec()
	assert.NoError(t, err)

	count, err := ds.Count()
	assert.NoError(t, err)
	assert.Equal(t, count, 9)

	var id uint32
	found, err = ds.Where(goqu.I("id").Eq(e.Id)).ScanVal(&id)
	assert.NoError(t, err)
	assert.False(t, found)

	e = entry{}
	found, err = ds.Where(goqu.I("int").Eq(8)).Select("id").ScanStruct(&e)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.NotEqual(t, e.Id, 0)

	id = 0
	_, err = ds.Where(goqu.I("id").Eq(e.Id)).Returning("id").Delete().ScanVal(&id)
	assert.Equal(t, err.Error(), "goqu: Adapter does not support RETURNING clause")
}
예제 #5
0
func (me *adapterTest) TestHasAdapter() {
	t := me.T()
	assert.False(t, HasAdapter("test"))
	RegisterAdapter("test", func(ds *Dataset) Adapter {
		return NewDefaultAdapter(ds)
	})
	assert.True(t, HasAdapter("test"))
	removeAdapter("test")
}
예제 #6
0
func (me *datasetTest) TestPrepared() {
	t := me.T()
	ds := From("test")
	preparedDs := ds.Prepared(true)
	assert.True(t, preparedDs.isPrepared)
	assert.False(t, ds.isPrepared)

	//should apply the prepared to any datasets created from the root
	assert.True(t, preparedDs.Where(Ex{"a": 1}).isPrepared)
}
예제 #7
0
func (me *crudExecTest) TestWithError() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	db := New("db-mock", mDb)
	expectedErr := fmt.Errorf("crud exec error")
	exec := newCrudExec(db, expectedErr, `SELECT * FROM "items"`)
	var items []testCrudActionItem
	assert.EqualError(t, exec.ScanStructs(&items), expectedErr.Error())
	found, err := exec.ScanStruct(&testCrudActionItem{})
	assert.EqualError(t, err, expectedErr.Error())
	assert.False(t, found)
	var vals []string
	assert.EqualError(t, exec.ScanVals(&vals), expectedErr.Error())
	var val string
	found, err = exec.ScanVal(&val)
	assert.EqualError(t, err, expectedErr.Error())
	assert.False(t, found)
}
예제 #8
0
func (me *datasetAdapterTest) TestSupportsReturn() {
	t := me.T()
	dsAdapter := me.GetDs("test").Adapter()
	assert.False(t, dsAdapter.SupportsReturn())
}