Beispiel #1
0
func TestInsect(t *testing.T) {
	// Insert by specifying values
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var id int64
	err := s.Insect("people").
		Columns("name", "email").
		Values("mario", "*****@*****.**").
		Returning("id").
		QueryScalar(&id)
	assert.NoError(t, err)
	assert.True(t, id > 0)

	// Insert by specifying a record (ptr to struct)
	person := Person{Name: "Barack"}
	person.Email.Valid = true
	person.Email.String = "*****@*****.**"

	err = s.
		Insect("people").
		Columns("name", "email").
		Record(&person).
		Returning("id", "created_at").
		QueryStruct(&person)
	assert.NoError(t, err)
	assert.True(t, person.ID > 0)
	assert.NotEqual(t, person.CreatedAt, dat.NullTime{})
}
Beispiel #2
0
func TestInsertDoubleDollarQuote(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	expected := common.RandomString(16)
	var str string
	err := s.
		InsertInto("people").
		Columns("name", "key").
		Values("test", expected).
		Returning("key").
		QueryScalar(&str)
	assert.NoError(t, err)
	assert.Equal(t, expected, str)

	// ensure the tag cannot be escaped by user
	oldDollarTag := postgres.GetPgDollarTag()
	expected = common.RandomString(1024) + "'" + oldDollarTag
	builder := s.
		InsertInto("people").
		Columns("name", "key").
		Values("test", expected).
		Returning("key")

	sql, _, _ := builder.SetIsInterpolated(true).Interpolate()
	assert.NotEqual(t, oldDollarTag, postgres.GetPgDollarTag())
	assert.True(t, strings.Contains(sql, postgres.GetPgDollarTag()))

	builder.QueryScalar(&str)
	assert.NoError(t, err)
	assert.Equal(t, expected, str)
}
Beispiel #3
0
func TestTCPConsumerStartBadCases(t *testing.T) {
	consumer := NewMockConsumer()

	tcpConsumer := NewConsumer("qiuweyqjw", consumer)
	err := tcpConsumer.Start()
	assert.Regexp(t, regexp.MustCompile("missing port.*"), err.Error())

	tcpConsumer = NewConsumer("0.0.0.0:qwe", consumer)
	err = tcpConsumer.Start()
	assert.Regexp(t, regexp.MustCompile("unknown port.*"), err.Error())

	tcpConsumer = NewConsumer("0.0.0.0:22", consumer)
	err = tcpConsumer.Start()
	assert.NotEqual(t, nil, err.Error())

	_, err = NewClient(addr)
	assert.Regexp(t, regexp.MustCompile(".*connection refused.*"), err.Error())
}