Ejemplo n.º 1
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")
}
Ejemplo n.º 2
0
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`)
}
Ejemplo n.º 3
0
func (me *postgresTest) TestInsertReturning() {
	t := me.T()
	ds := me.db.From("entry")
	now := time.Now()
	e := entry{Int: 10, Float: 1.000000, String: "1.000000", Time: now, Bool: true, Bytes: []byte("1.000000")}
	found, err := ds.Returning(goqu.Star()).Insert(e).ScanStruct(&e)
	assert.NoError(t, err)
	assert.True(t, found)
	assert.True(t, e.Id > 0)

	var ids []uint32
	assert.NoError(t, ds.Returning("id").Insert([]entry{
		{Int: 11, Float: 1.100000, String: "1.100000", Time: now, Bool: false, Bytes: []byte("1.100000")},
		{Int: 12, Float: 1.200000, String: "1.200000", Time: now, Bool: true, Bytes: []byte("1.200000")},
		{Int: 13, Float: 1.300000, String: "1.300000", Time: now, Bool: false, Bytes: []byte("1.300000")},
		{Int: 14, Float: 1.400000, String: "1.400000", Time: now, Bool: true, Bytes: []byte("1.400000")},
	}).ScanVals(&ids))
	assert.Len(t, ids, 4)
	for _, id := range ids {
		assert.True(t, id > 0)
	}

	var ints []int64
	assert.NoError(t, ds.Returning("int").Insert(
		entry{Int: 15, Float: 1.500000, String: "1.500000", Time: now, Bool: false, Bytes: []byte("1.500000")},
		entry{Int: 16, Float: 1.600000, String: "1.600000", Time: now, Bool: true, Bytes: []byte("1.600000")},
		entry{Int: 17, Float: 1.700000, String: "1.700000", Time: now, Bool: false, Bytes: []byte("1.700000")},
		entry{Int: 18, Float: 1.800000, String: "1.800000", Time: now, Bool: true, Bytes: []byte("1.800000")},
	).ScanVals(&ints))
	assert.True(t, found)
	assert.Equal(t, ints, []int64{15, 16, 17, 18})
}
Ejemplo n.º 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)
}
Ejemplo n.º 5
0
func (me *databaseTest) TestLogger() {
	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.ExpectExec(`SELECT \* FROM "items" WHERE "id" = ?`).
		WithArgs(1).
		WillReturnResult(sqlmock.NewResult(0, 0))

	db := New("db-mock", mDb)
	logger := new(dbTestMockLogger)
	db.Logger(logger)
	var items []testActionItem
	assert.NoError(t, db.ScanStructs(&items, `SELECT * FROM "items"`))
	_, err = db.Exec(`SELECT * FROM "items" WHERE "id" = ?`, 1)
	assert.NoError(t, err)
	db.Trace("TEST", "")
	assert.Equal(t, logger.Messages, []string{
		"[goqu] QUERY [query:=`SELECT * FROM \"items\"`]",
		"[goqu] EXEC [query:=`SELECT * FROM \"items\" WHERE \"id\" = ?` args:=[1]]",
		"[goqu] TEST",
	})
}
Ejemplo n.º 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`)
}
Ejemplo n.º 7
0
func (me *datasetTest) TestPreparedInsertWithGoquPkTagSql() {
	t := me.T()
	ds1 := From("items")
	type item struct {
		Id      uint32 `db:"id" goqu:"pk,skipinsert"`
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	sql, args, err := ds1.Prepared(true).ToInsertSql(item{Name: "Test", Address: "111 Test Addr"})
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{"111 Test Addr", "Test"})
	assert.Equal(t, sql, `INSERT INTO "items" ("address", "name") VALUES (?, ?)`)

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

	sql, args, err = ds1.Prepared(true).ToInsertSql(
		item{Name: "Test1", Address: "111 Test Addr"},
		item{Name: "Test2", Address: "211 Test Addr"},
		item{Name: "Test3", Address: "311 Test Addr"},
		item{Name: "Test4", Address: "411 Test Addr"},
	)
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{"111 Test Addr", "Test1", "211 Test Addr", "Test2", "311 Test Addr", "Test3", "411 Test Addr", "Test4"})
	assert.Equal(t, sql, `INSERT INTO "items" ("address", "name") VALUES (?, ?), (?, ?), (?, ?), (?, ?)`)
}
Ejemplo n.º 8
0
func (me *datasetTest) TestHaving() {
	t := me.T()
	ds1 := From("test")

	b := ds1.Having(Ex{
		"a": Op{"gt": 1},
	}).GroupBy("created")
	sql, _, err := b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" GROUP BY "created" HAVING ("a" > 1)`)

	b = ds1.Where(Ex{"b": true}).
		Having(Ex{"a": Op{"gt": 1}}).
		GroupBy("created")
	sql, _, err = b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("b" IS TRUE) GROUP BY "created" HAVING ("a" > 1)`)

	b = ds1.Having(Ex{"a": Op{"gt": 1}})
	sql, _, err = b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" HAVING ("a" > 1)`)

	b = ds1.Having(Ex{"a": Op{"gt": 1}}).Having(Ex{"b": 2})
	sql, _, err = b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" HAVING (("a" > 1) AND ("b" = 2))`)
}
Ejemplo n.º 9
0
func (me *datasetTest) TestGroupBy() {
	t := me.T()
	ds1 := From("test")

	b := ds1.Where(
		I("a").Gt(1),
	).GroupBy("created")
	sql, _, err := b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("a" > 1) GROUP BY "created"`)

	b = ds1.Where(
		I("a").Gt(1),
	).GroupBy(Literal("created::DATE"))
	sql, _, err = b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("a" > 1) GROUP BY created::DATE`)

	b = ds1.Where(
		I("a").Gt(1),
	).GroupBy("name", Literal("created::DATE"))
	sql, _, err = b.ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("a" > 1) GROUP BY "name", created::DATE`)
}
Ejemplo n.º 10
0
func (me *datasetTest) TestPreparedWhere() {
	t := me.T()
	ds1 := From("test")

	b := ds1.Where(Ex{
		"a": true,
		"b": Op{"neq": true},
		"c": false,
		"d": Op{"neq": false},
	})
	sql, args, err := b.Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{})
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE (("a" IS TRUE) AND ("b" IS NOT TRUE) AND ("c" IS FALSE) AND ("d" IS NOT FALSE))`)

	b = ds1.Where(Ex{
		"a": "a",
		"b": Op{"neq": "b"},
		"c": Op{"gt": "c"},
		"d": Op{"gte": "d"},
		"e": Op{"lt": "e"},
		"f": Op{"lte": "f"},
	})
	sql, args, err = b.Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{"a", "b", "c", "d", "e", "f"})
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE (("a" = ?) AND ("b" != ?) AND ("c" > ?) AND ("d" >= ?) AND ("e" < ?) AND ("f" <= ?))`)
}
Ejemplo n.º 11
0
func (me *datasetTest) TestJoin() {
	t := me.T()
	ds1 := From("items")

	sql, _, err := ds1.Join(I("players").As("p"), On(Ex{"p.id": I("items.playerId")})).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "items" INNER JOIN "players" AS "p" ON ("p"."id" = "items"."playerId")`)

	sql, _, err = ds1.Join(ds1.From("players").As("p"), On(Ex{"p.id": I("items.playerId")})).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "items" INNER JOIN (SELECT * FROM "players") AS "p" ON ("p"."id" = "items"."playerId")`)

	sql, _, err = ds1.Join(I("v1").Table("test"), On(Ex{"v1.test.id": I("items.playerId")})).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "items" INNER JOIN "v1"."test" ON ("v1"."test"."id" = "items"."playerId")`)

	sql, _, err = ds1.Join(I("test"), Using(I("name"), I("common_id"))).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "items" INNER JOIN "test" USING ("name", "common_id")`)

	sql, _, err = ds1.Join(I("test"), Using("name", "common_id")).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `SELECT * FROM "items" INNER JOIN "test" USING ("name", "common_id")`)

}
Ejemplo n.º 12
0
func (me *datasetTest) TestPreparedHaving() {
	t := me.T()
	ds1 := From("test")

	b := ds1.Having(I("a").Gt(1)).GroupBy("created")
	sql, args, err := b.Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1})
	assert.Equal(t, sql, `SELECT * FROM "test" GROUP BY "created" HAVING ("a" > ?)`)

	b = ds1.
		Where(I("b").IsTrue()).
		Having(I("a").Gt(1)).
		GroupBy("created")
	sql, args, err = b.Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1})
	assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("b" IS TRUE) GROUP BY "created" HAVING ("a" > ?)`)

	b = ds1.Having(I("a").Gt(1))
	sql, args, err = b.Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1})
	assert.Equal(t, sql, `SELECT * FROM "test" HAVING ("a" > ?)`)
}
Ejemplo n.º 13
0
func (me *datasetTest) TestLiteralIdentifierExpression() {
	t := me.T()
	buf := NewSqlBuilder(false)
	ds := From("test")
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a")))
	assert.Equal(t, buf.String(), `"a"`)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b")))
	assert.Equal(t, buf.String(), `"a"."b"`)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b.c")))
	assert.Equal(t, buf.String(), `"a"."b"."c"`)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b.*")))
	assert.Equal(t, buf.String(), `"a"."b".*`)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.*")))
	assert.Equal(t, buf.String(), `"a".*`)

	buf = NewSqlBuilder(true)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a")))
	assert.Equal(t, buf.String(), `"a"`)
	assert.Equal(t, buf.args, []interface{}{})
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b")))
	assert.Equal(t, buf.String(), `"a"."b"`)
	assert.Equal(t, buf.args, []interface{}{})
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b.c")))
	assert.Equal(t, buf.String(), `"a"."b"."c"`)
	assert.Equal(t, buf.args, []interface{}{})
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.b.*")))
	assert.Equal(t, buf.String(), `"a"."b".*`)
	assert.Equal(t, buf.args, []interface{}{})
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a.*")))
	assert.Equal(t, buf.String(), `"a".*`)
	assert.Equal(t, buf.args, []interface{}{})
}
Ejemplo n.º 14
0
func (me *datasetTest) TestPreparedUnion() {
	t := me.T()
	a := From("invoice").Select("id", "amount").Where(I("amount").Gt(1000))
	b := From("invoice").Select("id", "amount").Where(I("amount").Lt(10))

	sql, args, err := a.Union(b).Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1000, 10})
	assert.Equal(t, sql, `SELECT "id", "amount" FROM "invoice" WHERE ("amount" > ?) UNION (SELECT "id", "amount" FROM "invoice" WHERE ("amount" < ?))`)

	sql, args, err = a.Limit(1).Union(b).Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1000, 1, 10})
	assert.Equal(t, sql, `SELECT * FROM (SELECT "id", "amount" FROM "invoice" WHERE ("amount" > ?) LIMIT ?) AS "t1" UNION (SELECT "id", "amount" FROM "invoice" WHERE ("amount" < ?))`)

	sql, args, err = a.Union(b.Limit(1)).Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1000, 10, 1})
	assert.Equal(t, sql, `SELECT "id", "amount" FROM "invoice" WHERE ("amount" > ?) UNION (SELECT * FROM (SELECT "id", "amount" FROM "invoice" WHERE ("amount" < ?) LIMIT ?) AS "t1")`)

	sql, args, err = a.Union(b).Union(b.Where(I("id").Lt(50))).Prepared(true).ToSql()
	assert.NoError(t, err)
	assert.Equal(t, args, []interface{}{1000, 10, 10, 50})
	assert.Equal(t, sql, `SELECT "id", "amount" FROM "invoice" WHERE ("amount" > ?) UNION (SELECT "id", "amount" FROM "invoice" WHERE ("amount" < ?)) UNION (SELECT "id", "amount" FROM "invoice" WHERE (("amount" < ?) AND ("id" < ?)))`)

}
Ejemplo n.º 15
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")
}
Ejemplo n.º 16
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)
}
Ejemplo n.º 17
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`)
}
Ejemplo n.º 18
0
func (me *datasetTest) TestInsertWithGoquPkTagSql() {
	t := me.T()
	ds1 := From("items")
	type item struct {
		Id      uint32 `db:"id" goqu:"pk,skipinsert"`
		Address string `db:"address"`
		Name    string `db:"name"`
	}
	sql, _, err := ds1.ToInsertSql(item{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{}{"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(
		item{Name: "Test1", Address: "111 Test Addr"},
		item{Name: "Test2", Address: "211 Test Addr"},
		item{Name: "Test3", Address: "311 Test Addr"},
		item{Name: "Test4", Address: "411 Test Addr"},
	)
	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')`)
}
Ejemplo n.º 19
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")
}
Ejemplo n.º 20
0
func (me *databaseTest) TestPrepare() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectPrepare()
	db := New("mock", mDb)
	stmt, err := db.Prepare("SELECT * FROM test WHERE id = ?")
	assert.NoError(t, err)
	assert.NotNil(t, stmt)
}
Ejemplo n.º 21
0
func (me *txDatabaseTest) TestRollback() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectBegin()
	sqlmock.ExpectRollback()
	db := New("mock", mDb)
	tx, err := db.Begin()
	assert.NoError(t, err)
	assert.NoError(t, tx.Rollback())
}
Ejemplo n.º 22
0
func (me *datasetTest) TestDeleteSqlWithReturning() {
	t := me.T()
	ds1 := From("items")
	sql, _, err := ds1.Returning("id").ToDeleteSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `DELETE FROM "items" RETURNING "id"`)

	sql, _, err = ds1.Returning("id").Where(I("id").IsNotNull()).ToDeleteSql()
	assert.NoError(t, err)
	assert.Equal(t, sql, `DELETE FROM "items" WHERE ("id" IS NOT NULL) RETURNING "id"`)
}
Ejemplo n.º 23
0
func (me *datasetTest) TestLiteralColumnList() {
	t := me.T()
	buf := NewSqlBuilder(false)
	ds := From("test")
	assert.NoError(t, ds.Literal(me.Truncate(buf), cols("a", Literal("true"))))
	assert.Equal(t, buf.String(), `"a", true`)

	buf = NewSqlBuilder(true)
	assert.NoError(t, ds.Literal(me.Truncate(buf), cols("a", Literal("true"))))
	assert.Equal(t, buf.args, []interface{}{})
	assert.Equal(t, buf.String(), `"a", true`)
}
Ejemplo n.º 24
0
func (me *datasetTest) TestLiteralUpdateExpression() {
	t := me.T()
	buf := NewSqlBuilder(false)
	ds := From("test")
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Set(1)))
	assert.Equal(t, buf.String(), `"a"=1`)

	buf = NewSqlBuilder(true)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Set(1)))
	assert.Equal(t, buf.args, []interface{}{1})
	assert.Equal(t, buf.String(), `"a"=?`)
}
Ejemplo n.º 25
0
func (me *datasetTest) TestLiteralCastExpression() {
	t := me.T()
	buf := NewSqlBuilder(false)
	ds := From("test")
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Cast("DATE")))
	assert.Equal(t, buf.String(), `CAST("a" AS DATE)`)

	buf = NewSqlBuilder(true)
	assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Cast("DATE")))
	assert.Equal(t, buf.args, []interface{}{})
	assert.Equal(t, buf.String(), `CAST("a" AS DATE)`)
}
Ejemplo n.º 26
0
func (me *datasetTest) TestUpdate() {
	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))

	db := New("mock", mDb)
	_, err = db.From("items").Where(I("name").IsNull()).Update(Record{"address": "111 Test Addr", "name": "Test1"}).Exec()
	assert.NoError(t, err)
}
Ejemplo n.º 27
0
func (me *datasetTest) TestInsert() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectExec(`INSERT INTO "items" \("address", "name"\) VALUES \('111 Test Addr', 'Test1'\)`).
		WithArgs().
		WillReturnResult(sqlmock.NewResult(0, 0))

	db := New("mock", mDb)
	_, err = db.From("items").Insert(Record{"address": "111 Test Addr", "name": "Test1"}).Exec()
	assert.NoError(t, err)
}
Ejemplo n.º 28
0
func (me *datasetTest) TestDelete() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectExec(`DELETE FROM "items" WHERE \("id" > 10\)`).
		WithArgs().
		WillReturnResult(sqlmock.NewResult(0, 0))

	db := New("mock", mDb)
	_, err = db.From("items").Where(I("id").Gt(10)).Delete().Exec()
	assert.NoError(t, err)
}
Ejemplo n.º 29
0
func (me *datasetTest) TestDelete_WithPreparedStatment() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectExec(`DELETE FROM "items" WHERE \("id" > \?\)`).
		WithArgs(10).
		WillReturnResult(sqlmock.NewResult(0, 0))

	db := New("mock", mDb)
	_, err = db.From("items").Prepared(true).Where(Ex{"id": Op{"gt": 10}}).Delete().Exec()
	assert.NoError(t, err)
}
Ejemplo n.º 30
0
func (me *txDatabaseTest) TestFrom() {
	t := me.T()
	mDb, err := sqlmock.New()
	assert.NoError(t, err)
	sqlmock.ExpectBegin()
	sqlmock.ExpectCommit()
	db := New("mock", mDb)
	tx, err := db.Begin()
	assert.NoError(t, err)
	assert.NotNil(t, tx.From("test"))
	assert.NoError(t, tx.Commit())
}