示例#1
0
文件: column_test.go 项目: aodin/sol
func TestColumn(t *testing.T) {
	expect := sol.NewTester(t, Dialect())

	expect.SQL(
		meetings.Select().Where(meetings.C("time").Contains("today")),
		`SELECT meetings.uuid, meetings.time FROM meetings WHERE meetings.time @> $1`,
		"today",
	)
}
示例#2
0
func TestColumn(t *testing.T) {
	expect := sol.NewTester(t, &PostGres{})

	expect.SQL(
		`SELECT "meetings"."uuid", "meetings"."time" FROM "meetings" WHERE "meetings"."time" @> $1`,
		meetings.Select().Where(meetings.C("time").Contains("today")),
		"today",
	)
}
示例#3
0
func TestPostGres_Create(t *testing.T) {
	expect := sol.NewTester(t, &PostGres{})

	expect.SQL(
		itemsFK.Create(),
		`CREATE TABLE items_fk (
  id INTEGER NOT NULL REFERENCES items_b(id),
  name VARCHAR
);`,
	)
}
示例#4
0
文件: select_test.go 项目: aodin/sol
func TestSelect(t *testing.T) {
	expect := sol.NewTester(t, &PostGres{})

	// Build a GROUP BY statement using an aggregate
	expect.SQL(
		sol.Select(
			things.C("name"),
			sol.Max(things.C("created_at")),
		).GroupBy(
			things.C("name"),
		).OrderBy(
			sol.Max(things.C("created_at")).Desc(),
		),
		`SELECT things.name, MAX(things.created_at) FROM things GROUP BY things.name ORDER BY MAX(things.created_at) DESC`,
	)
}
示例#5
0
文件: insert_test.go 项目: aodin/sol
func TestInsert(t *testing.T) {
	expect := sol.NewTester(t, &PostGres{})

	// By default, an INSERT without values will assume a single entry
	expect.SQL(
		meetings.Insert().Returning(meetings),
		`INSERT INTO meetings (uuid, time) VALUES ($1, $2) RETURNING meetings.uuid, meetings.time`,
		nil, nil,
	)

	// If no parameters are given to Returning(), it will default to the
	// INSERT statement's table
	expect.SQL(
		meetings.Insert().Returning(),
		`INSERT INTO meetings (uuid, time) VALUES ($1, $2) RETURNING meetings.uuid, meetings.time`,
		nil, nil,
	)

	// UPSERT
	now := time.Now()
	expect.SQL(
		meetings.Insert().OnConflict().DoUpdate(
			sol.Values{"time": now},
		).Where(meetings.C("time").GTE(now)),
		`INSERT INTO meetings (uuid, time) VALUES ($1, $2) ON CONFLICT DO UPDATE SET time = $3 WHERE meetings.time >= $4`,
		nil, nil, now, now,
	)

	expect.SQL(
		meetings.Insert().OnConflict().DoNothing(),
		`INSERT INTO meetings (uuid, time) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
		nil, nil,
	)

	// Selecting a column or table that is not part of the insert table
	// should produce an error
	expect.Error(meetings.Insert().Returning(things))
	expect.Error(meetings.Insert().Returning(things.C("id")))
}