예제 #1
0
func TestDeleteScope(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var id int64
	// Insert a Barack
	s.InsertInto("people").
		Columns("name", "email").
		Values("Barack", "*****@*****.**").
		Returning("id").
		QueryScalar(&id)

	scope := dat.NewScope("WHERE id = :id", dat.M{"id": 0})

	// Delete Barack
	res, err := s.
		DeleteFrom("people").
		ScopeMap(scope, dat.M{"id": id}).
		Exec()
	assert.NoError(t, err)

	// Ensure we only reflected one row and that the id no longer exists
	assert.EqualValues(t, res.RowsAffected, 1)

	var count int64
	err = s.Select("count(*)").
		From("people").
		Where("id = $1", id).
		QueryScalar(&count)
	assert.NoError(t, err)
	assert.EqualValues(t, count, 0)
}
예제 #2
0
func TestUpdateScope(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var id int64
	// Insert a George
	s.InsertInto("people").
		Columns("name", "email").
		Values("Scope", "*****@*****.**").
		Returning("id").
		QueryScalar(&id)

	scope := dat.NewScope("WHERE id = :id", dat.M{"id": 1000})

	// Rename our George to Barack
	_, err := s.
		Update("people").
		SetMap(map[string]interface{}{"name": "Barack", "email": "*****@*****.**"}).
		ScopeMap(scope, dat.M{"id": id}).
		Exec()

	assert.NoError(t, err)

	var person Person
	err = s.Select("*").From("people").Where("id = $1", id).QueryStruct(&person)
	assert.NoError(t, err)

	assert.Equal(t, person.ID, id)
	assert.Equal(t, person.Name, "Barack")
	assert.Equal(t, person.Email.Valid, true)
	assert.Equal(t, person.Email.String, "*****@*****.**")
}