Exemple #1
0
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",
	)
}
Exemple #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",
	)
}
Exemple #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
);`,
	)
}
Exemple #4
0
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`,
	)
}
Exemple #5
0
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")))
}