func (me *datasetTest) TestScanStructs_WithPreparedStatements() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT "address", "name" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`).
		WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	sqlmock.ExpectQuery(`SELECT "test" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`).
		WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
		WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))

	db := New("mock", mDb)
	var items []dsTestActionItem
	assert.NoError(t, db.From("items").Prepared(true).Where(Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).ScanStructs(&items))
	assert.Len(t, items, 2)
	assert.Equal(t, items[0].Address, "111 Test Addr")
	assert.Equal(t, items[0].Name, "Test1")

	assert.Equal(t, items[1].Address, "211 Test Addr")
	assert.Equal(t, items[1].Name, "Test2")

	items = items[0:0]
	assert.EqualError(t, db.From("items").ScanStructs(items), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, db.From("items").ScanStructs(&dsTestActionItem{}), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, db.From("items").
		Prepared(true).
		Select("test").
		Where(Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
		ScanStructs(&items), `goqu: Unable to find corresponding field to column "test" returned by query`)
}
示例#2
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)
}
func (me *datasetTest) TestInsertSqlDifferentTypes() {
	t := me.T()
	ds1 := From("items")
	type item struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	type item2 struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	_, _, err := ds1.ToInsertSql(
		item{Address: "111 Test Addr", Name: "Test1"},
		item2{Address: "211 Test Addr", Name: "Test2"},
		item{Address: "311 Test Addr", Name: "Test3"},
		item2{Address: "411 Test Addr", Name: "Test4"},
	)
	assert.EqualError(t, err, "goqu: Rows must be all the same type expected goqu.item got goqu.item2")

	_, _, err = ds1.ToInsertSql(
		item{Address: "111 Test Addr", Name: "Test1"},
		map[string]interface{}{"address": "211 Test Addr", "name": "Test2"},
		item{Address: "311 Test Addr", Name: "Test3"},
		map[string]interface{}{"address": "411 Test Addr", "name": "Test4"},
	)
	assert.EqualError(t, err, "goqu: Rows must be all the same type expected goqu.item got map[string]interface {}")
}
示例#4
0
func (me *crudExecTest) TestScanVals() {
	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\n2"))

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

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

	var id int64
	var ids []int64
	assert.EqualError(t, exec.ScanVals(ids), "goqu: Type must be a pointer to a slice when calling ScanVals")
	assert.EqualError(t, exec.ScanVals(&id), "goqu: Type must be a pointer to a slice when calling ScanVals")
	assert.EqualError(t, exec.ScanVals(&ids), "query error")

	assert.NoError(t, exec.ScanVals(&ids))
	assert.Equal(t, ids, []int64{1, 2})

	var pointers []*int64
	assert.NoError(t, exec.ScanVals(&pointers))
	assert.Len(t, pointers, 2)
	assert.Equal(t, *pointers[0], 1)
	assert.Equal(t, *pointers[1], 2)
}
示例#5
0
func (me *databaseTest) TestScanStructs() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	sqlmock.ExpectQuery(`SELECT "test" FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))

	db := New("db-mock", mDb)
	var items []testActionItem
	assert.NoError(t, db.ScanStructs(&items, `SELECT * FROM "items"`))
	assert.Len(t, items, 2)
	assert.Equal(t, items[0].Address, "111 Test Addr")
	assert.Equal(t, items[0].Name, "Test1")

	assert.Equal(t, items[1].Address, "211 Test Addr")
	assert.Equal(t, items[1].Name, "Test2")

	items = items[0:0]
	assert.EqualError(t, db.ScanStructs(items, `SELECT * FROM "items"`), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, db.ScanStructs(&testActionItem{}, `SELECT * FROM "items"`), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, db.ScanStructs(&items, `SELECT "test" FROM "items"`), `goqu: Unable to find corresponding field to column "test" returned by query`)
}
示例#6
0
func (me *databaseTest) TestScanStruct() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT \* FROM "items" LIMIT 1`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

	sqlmock.ExpectQuery(`SELECT "test" FROM "items" LIMIT 1`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))

	db := New("mock", mDb)
	var item testActionItem
	found, err := db.ScanStruct(&item, `SELECT * FROM "items" LIMIT 1`)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.Equal(t, item.Address, "111 Test Addr")
	assert.Equal(t, item.Name, "Test1")

	_, err = db.ScanStruct(item, `SELECT * FROM "items" LIMIT 1`)
	assert.EqualError(t, err, "goqu: Type must be a pointer to a struct when calling ScanStruct")
	_, err = db.ScanStruct([]testActionItem{}, `SELECT * FROM "items" LIMIT 1`)
	assert.EqualError(t, err, "goqu: Type must be a pointer to a struct when calling ScanStruct")
	_, err = db.ScanStruct(&item, `SELECT "test" FROM "items" LIMIT 1`)
	assert.EqualError(t, err, `goqu: Unable to find corresponding field to column "test" returned by query`)
}
示例#7
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")
}
func (me *datasetTest) TestInsertSqlNoReturning() {
	t := me.T()
	mDb, _ := sqlmock.New()
	ds1 := New("no-return", mDb).From("items")
	type item struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	_, _, err := ds1.Returning("id").ToInsertSql(item{Name: "Test", Address: "111 Test Addr"})
	assert.EqualError(t, err, "goqu: Adapter does not support RETURNING clause")

	_, _, err = ds1.Returning("id").ToInsertSql(From("test2"))
	assert.EqualError(t, err, "goqu: Adapter does not support RETURNING clause")
}
func (me *datasetTest) TestInsertSqlWithMaps() {
	t := me.T()
	ds1 := From("items")

	sql, _, err := ds1.ToInsertSql(map[string]interface{}{"name": "Test", "address": "111 Test Addr"})
	assert.NoError(t, err)
	assert.Equal(t, sql, `INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test')`)

	sql, _, err = ds1.ToInsertSql(
		map[string]interface{}{"address": "111 Test Addr", "name": "Test1"},
		map[string]interface{}{"address": "211 Test Addr", "name": "Test2"},
		map[string]interface{}{"address": "311 Test Addr", "name": "Test3"},
		map[string]interface{}{"address": "411 Test Addr", "name": "Test4"},
	)
	assert.NoError(t, err)
	assert.Equal(t, sql, `INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('211 Test Addr', 'Test2'), ('311 Test Addr', 'Test3'), ('411 Test Addr', 'Test4')`)

	_, _, err = ds1.ToInsertSql(
		map[string]interface{}{"address": "111 Test Addr", "name": "Test1"},
		map[string]interface{}{"address": "211 Test Addr"},
		map[string]interface{}{"address": "311 Test Addr", "name": "Test3"},
		map[string]interface{}{"address": "411 Test Addr", "name": "Test4"},
	)
	assert.EqualError(t, err, "goqu: Rows with different value length expected 2 got 1")
}
示例#10
0
func (me *databaseTest) TestScanVals() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))

	db := New("mock", mDb)
	var ids []uint32
	assert.NoError(t, db.ScanVals(&ids, `SELECT "id" FROM "items"`))
	assert.Len(t, ids, 5)

	assert.EqualError(t, db.ScanVals([]uint32{}, `SELECT "id" FROM "items"`), "goqu: Type must be a pointer to a slice when calling ScanVals")
	assert.EqualError(t, db.ScanVals(testActionItem{}, `SELECT "id" FROM "items"`), "goqu: Type must be a pointer to a slice when calling ScanVals")
}
示例#11
0
func (me *datasetTest) TestInsertSqlWitDifferentKeys() {
	t := me.T()
	ds1 := From("items")
	_, _, err := ds1.ToInsertSql(
		map[string]interface{}{"address": "111 Test Addr", "name": "test"},
		map[string]interface{}{"phoneNumber": 10, "address": "111 Test Addr"},
	)
	assert.EqualError(t, err, `goqu: Rows with different keys expected ["address","name"] got ["address","phoneNumber"]`)
}
示例#12
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)
}
示例#13
0
func (me *datasetTest) TestUpdateSqlWithNoSources() {
	t := me.T()
	ds1 := From("items")
	type item struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	_, _, err := ds1.From().ToUpdateSql(item{Name: "Test", Address: "111 Test Addr"})
	assert.EqualError(t, err, "goqu: No source found when generating update sql")
}
示例#14
0
func (me *databaseTest) TestScanVal() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT "id" FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("10"))

	db := New("mock", mDb)
	var id int64
	found, err := db.ScanVal(&id, `SELECT "id" FROM "items"`)
	assert.NoError(t, err)
	assert.Equal(t, id, 10)
	assert.True(t, found)

	found, err = db.ScanVal([]int64{}, `SELECT "id" FROM "items"`)
	assert.EqualError(t, err, "goqu: Type must be a pointer when calling ScanVal")
	found, err = db.ScanVal(10, `SELECT "id" FROM "items"`)
	assert.EqualError(t, err, "goqu: Type must be a pointer when calling ScanVal")
}
func (me *datasetTest) TestScanVals_WithPreparedStatment() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT "id" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`).
		WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
		WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))

	db := New("mock", mDb)
	var ids []uint32
	assert.NoError(t, db.From("items").
		Prepared(true).
		Select("id").
		Where(Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
		ScanVals(&ids))
	assert.Len(t, ids, 5)

	assert.EqualError(t, db.From("items").ScanVals([]uint32{}), "goqu: Type must be a pointer to a slice when calling ScanVals")
	assert.EqualError(t, db.From("items").ScanVals(dsTestActionItem{}), "goqu: Type must be a pointer to a slice when calling ScanVals")
}
示例#16
0
func (me *datasetTest) TestInsert_InvalidValue() {
	t := me.T()
	mDb, _ := sqlmock.New()
	ds1 := New("no-return", mDb).From("items")
	type item struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	_, _, err := ds1.ToInsertSql(true)
	assert.EqualError(t, err, "goqu: Unsupported insert must be map, goqu.Record, or struct type got: bool")
}
示例#17
0
func (me *datasetTest) TestDeleteSqlNoReturning() {
	t := me.T()
	mDb, _ := sqlmock.New()
	ds1 := New("no-return", mDb).From("items")
	type item struct {
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	_, _, err := ds1.Returning("id").ToDeleteSql()
	assert.EqualError(t, err, "goqu: Adapter does not support RETURNING clause")
}
示例#18
0
func (me *datasetTest) TestInsertSqlWithMapsWithDifferentLengths() {
	t := me.T()
	ds1 := From("items")
	_, _, err := ds1.ToInsertSql(
		map[string]interface{}{"address": "111 Test Addr", "name": "Test1"},
		map[string]interface{}{"address": "211 Test Addr"},
		map[string]interface{}{"address": "311 Test Addr", "name": "Test3"},
		map[string]interface{}{"address": "411 Test Addr", "name": "Test4"},
	)
	assert.EqualError(t, err, "goqu: Rows with different value length expected 2 got 1")
}
示例#19
0
func (me *databaseTest) TestBegin() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectBegin()
	sqlmock.ExpectBegin().WillReturnError(NewGoquError("transaction error"))
	db := New("mock", mDb)
	tx, err := db.Begin()
	assert.NoError(t, err)
	assert.Equal(t, tx.Dialect, "mock")

	_, err = db.Begin()
	assert.EqualError(t, err, "goqu: transaction error")
}
func (me *datasetTest) TestScanVal_WithPreparedStatement() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT "id" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\) LIMIT ?`).
		WithArgs("111 Test Addr", "Bob", "Sally", "Billy", 1).
		WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("10"))

	db := New("mock", mDb)
	var id int64
	found, err := db.From("items").
		Prepared(true).
		Select("id").
		Where(Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
		ScanVal(&id)
	assert.NoError(t, err)
	assert.Equal(t, id, 10)
	assert.True(t, found)

	found, err = db.From("items").ScanVal([]int64{})
	assert.EqualError(t, err, "goqu: Type must be a pointer when calling ScanVal")
	found, err = db.From("items").ScanVal(10)
	assert.EqualError(t, err, "goqu: Type must be a pointer when calling ScanVal")
}
示例#21
0
func (me *databaseTest) TestExec() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectExec(`UPDATE "items" SET "address"='111 Test Addr',"name"='Test1' WHERE \("name" IS NULL\)`).
		WithArgs().
		WillReturnResult(sqlmock.NewResult(0, 0))

	sqlmock.ExpectExec(`UPDATE "items" SET "address"='111 Test Addr',"name"='Test1' WHERE \("name" IS NULL\)`).
		WithArgs().
		WillReturnError(NewGoquError("mock error"))

	db := New("mock", mDb)
	_, err = db.Exec(`UPDATE "items" SET "address"='111 Test Addr',"name"='Test1' WHERE ("name" IS NULL)`)
	assert.NoError(t, err)
	_, err = db.Exec(`UPDATE "items" SET "address"='111 Test Addr',"name"='Test1' WHERE ("name" IS NULL)`)
	assert.EqualError(t, err, "goqu: mock error")
}
示例#22
0
func (me *txDatabaseTest) TestWrap() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectBegin()
	sqlmock.ExpectCommit()
	sqlmock.ExpectBegin()
	sqlmock.ExpectRollback()
	tx, err := New("mock", mDb).Begin()
	assert.NoError(t, err)
	assert.NoError(t, tx.Wrap(func() error {
		return nil
	}))
	tx, err = New("mock", mDb).Begin()
	assert.NoError(t, err)
	assert.EqualError(t, tx.Wrap(func() error {
		return NewGoquError("tx error")
	}), "goqu: tx error")
}
示例#23
0
func (me *databaseTest) TestQuery() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnError(NewGoquError("mock error"))

	db := New("mock", mDb)
	_, err = db.Query(`SELECT * FROM "items"`)
	assert.NoError(t, err, "goqu - mock error")

	_, err = db.Query(`SELECT * FROM "items"`)
	assert.EqualError(t, err, "goqu: mock error")
}
示例#24
0
func (me *txDatabaseTest) TestQueryRow() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectBegin()
	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	sqlmock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnError(NewGoquError("mock error"))
	sqlmock.ExpectCommit()
	tx, err := New("mock", mDb).Begin()
	assert.NoError(t, err)
	rows := tx.QueryRow(`SELECT * FROM "items"`)
	var address string
	var name string
	assert.NoError(t, rows.Scan(&address, &name))

	rows = tx.QueryRow(`SELECT * FROM "items"`)
	assert.EqualError(t, rows.Scan(&address, &name), "goqu: mock error")
	assert.NoError(t, tx.Commit())
}
示例#25
0
func (me *crudExecTest) TestScanStructs() {
	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\n211 Test Addr,Test2"))

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

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

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

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

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

	var items []testCrudActionItem
	assert.EqualError(t, exec.ScanStructs(items), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, exec.ScanStructs(&testCrudActionItem{}), "goqu: Type must be a pointer to a slice when calling ScanStructs")
	assert.EqualError(t, exec.ScanStructs(&items), "query error")

	assert.NoError(t, exec.ScanStructs(&items))
	assert.Len(t, items, 2)
	assert.Equal(t, items[0].Address, "111 Test Addr")
	assert.Equal(t, items[0].Name, "Test1")

	assert.Equal(t, items[1].Address, "211 Test Addr")
	assert.Equal(t, items[1].Name, "Test2")

	var composed []testComposedCrudActionItem
	assert.NoError(t, exec.ScanStructs(&composed))
	assert.Len(t, composed, 2)
	assert.Equal(t, composed[0].Address, "111 Test Addr")
	assert.Equal(t, composed[0].Name, "Test1")
	assert.Equal(t, composed[0].PhoneNumber, "111-111-1111")
	assert.Equal(t, composed[0].Age, 20)

	assert.Equal(t, composed[1].Address, "211 Test Addr")
	assert.Equal(t, composed[1].Name, "Test2")
	assert.Equal(t, composed[1].PhoneNumber, "222-222-2222")
	assert.Equal(t, composed[1].Age, 30)

	var pointers []*testCrudActionItem
	assert.NoError(t, exec.ScanStructs(&pointers))
	assert.Len(t, pointers, 2)
	assert.Equal(t, pointers[0].Address, "111 Test Addr")
	assert.Equal(t, pointers[0].Name, "Test1")

	assert.Equal(t, pointers[1].Address, "211 Test Addr")
	assert.Equal(t, pointers[1].Name, "Test2")

	var composedPointers []*testComposedCrudActionItem
	assert.NoError(t, exec.ScanStructs(&composedPointers))
	assert.Len(t, composedPointers, 2)
	assert.Equal(t, composedPointers[0].Address, "111 Test Addr")
	assert.Equal(t, composedPointers[0].Name, "Test1")
	assert.Equal(t, composedPointers[0].PhoneNumber, "111-111-1111")
	assert.Equal(t, composedPointers[0].Age, 20)

	assert.Equal(t, composedPointers[1].Address, "211 Test Addr")
	assert.Equal(t, composedPointers[1].Name, "Test2")
	assert.Equal(t, composedPointers[1].PhoneNumber, "222-222-2222")
	assert.Equal(t, composedPointers[1].Age, 30)

	var noTags []testCrudActionNoTagsItem
	assert.NoError(t, exec.ScanStructs(&noTags))
	assert.Len(t, noTags, 2)
	assert.Equal(t, noTags[0].Address, "111 Test Addr")
	assert.Equal(t, noTags[0].Name, "Test1")

	assert.Equal(t, noTags[1].Address, "211 Test Addr")
	assert.Equal(t, noTags[1].Name, "Test2")

}
示例#26
0
func (me *datasetTest) TestTruncateSqlNoSources() {
	t := me.T()
	ds1 := From("items")
	_, _, err := ds1.From().ToTruncateSql()
	assert.EqualError(t, err, "goqu: No source found when generating truncate sql")
}
示例#27
0
func (me *datasetTest) TestLiteralUnsupportedType() {
	t := me.T()
	assert.EqualError(t, From("test").Literal(NewSqlBuilder(false), struct{}{}), "goqu: Unable to encode value {}")
}
示例#28
0
func (me *datasetTest) TestLiteralUnsupportedExpression() {
	t := me.T()
	assert.EqualError(t, From("test").Literal(NewSqlBuilder(false), unknowExpression{}), "goqu: Unsupported expression type goqu.unknowExpression")
}
示例#29
0
func (me *datasetTest) TestUpdateSqlWithUnsupportedType() {
	t := me.T()
	ds1 := From("items")
	_, _, err := ds1.ToUpdateSql([]string{"HELLO"})
	assert.EqualError(t, err, "goqu: Unsupported update interface type []string")
}
示例#30
0
func (me *datasetTest) TestInsertSqlWithNoFrom() {
	t := me.T()
	ds1 := From("test").From()
	_, _, err := ds1.ToInsertSql(map[string]interface{}{"address": "111 Test Addr", "name": "Test1"})
	assert.EqualError(t, err, "goqu: No source found when generating insert sql")
}