Exemple #1
0
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 {}")
}
Exemple #2
0
func (me *crudExecTest) TestScanVal() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)

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

	mock.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)
}
Exemple #3
0
func (me *crudExecTest) TestScanVals() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)

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

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

	mock.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)
}
Exemple #4
0
func (me *databaseTest) TestScanStructs() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	mock.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`)
}
Exemple #5
0
func (me *datasetTest) TestScanStructs_WithPreparedStatements() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.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"))

	mock.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`)
}
Exemple #6
0
func (me *databaseTest) TestScanStruct() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectQuery(`SELECT \* FROM "items" LIMIT 1`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))

	mock.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`)
}
Exemple #7
0
func (me *crudExecTest) TestScanStruct() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)

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

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

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

	mock.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")
}
Exemple #8
0
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")
}
Exemple #9
0
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")
}
Exemple #10
0
func (me *databaseTest) TestScanVals() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.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")
}
Exemple #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"]`)
}
Exemple #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)
}
Exemple #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")
}
Exemple #14
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")
}
Exemple #15
0
func (me *databaseTest) TestScanVal() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.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")
}
Exemple #16
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")
}
Exemple #17
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")
}
Exemple #18
0
func (me *datasetTest) TestScanVals_WithPreparedStatment() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.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")
}
Exemple #19
0
func (me *databaseTest) TestBegin() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectBegin()
	mock.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")
}
Exemple #20
0
func (me *datasetTest) TestScanVal_WithPreparedStatement() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.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")
}
Exemple #21
0
func (me *databaseTest) TestExec() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectExec(`UPDATE "items" SET "address"='111 Test Addr',"name"='Test1' WHERE \("name" IS NULL\)`).
		WithArgs().
		WillReturnResult(sqlmock.NewResult(0, 0))

	mock.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")
}
Exemple #22
0
func (me *txDatabaseTest) TestWrap() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectBegin()
	mock.ExpectCommit()
	mock.ExpectBegin()
	mock.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")
}
Exemple #23
0
func (me *databaseTest) TestQuery() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	mock.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")
}
Exemple #24
0
func (me *txDatabaseTest) TestQueryRow() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)
	mock.ExpectBegin()
	mock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

	mock.ExpectQuery(`SELECT \* FROM "items"`).
		WithArgs().
		WillReturnError(NewGoquError("mock error"))
	mock.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())
}
Exemple #25
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")
}
Exemple #26
0
func (me *crudExecTest) TestScanStructs() {
	t := me.T()
	mDb, mock, err := sqlmock.New()
	assert.NoError(t, err)

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

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

	mock.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"))

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

	mock.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"))

	mock.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")

}
Exemple #27
0
// EqualError asserts that a function returned an error (i.e. not `nil`)
// and that it is equal to the provided error.
//
//   actualObj, err := SomeFunction()
//   require.Error(t, err, "An error was expected")
//   require.Equal(t, err, expectedError)
//   }
func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) {
	if !assert.EqualError(t, theError, errString, msgAndArgs...) {
		t.FailNow()
	}
}
Exemple #28
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")
}
Exemple #29
0
func (me *datasetTest) TestLiteralUnsupportedExpression() {
	t := me.T()
	assert.EqualError(t, From("test").Literal(NewSqlBuilder(false), unknowExpression{}), "goqu: Unsupported expression type goqu.unknowExpression")
}
Exemple #30
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")
}