示例#1
0
func TestSelectQueryStruct(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	// Found:
	var person Person
	err := s.
		Select("id", "name", "email").
		From("people").
		Where("email = $1", "*****@*****.**").
		QueryStruct(&person)
	assert.NoError(t, err)
	assert.True(t, person.ID > 0)
	assert.Equal(t, person.Name, "John")
	assert.True(t, person.Email.Valid)
	assert.Equal(t, person.Email.String, "*****@*****.**")

	// Not found:
	var person2 Person
	err = s.
		Select("id", "name", "email").
		From("people").Where("email = $1", "*****@*****.**").
		QueryStruct(&person2)
	assert.Contains(t, err.Error(), "no rows")
}
示例#2
0
func TestCommitWithNestedNestedCommit(t *testing.T) {
	log.Suppress(true)
	defer log.Suppress(false)
	installFixtures()
	tx, err := testDB.Begin()
	assert.NoError(t, err)
	err = nestedNestedCommit(tx)
	assert.NoError(t, err)
	err = tx.Commit()
	assert.NoError(t, err)

	var person Person
	err = testDB.
		Select("*").
		From("people").
		Where("email = $1", "*****@*****.**").
		QueryStruct(&person)
	assert.NoError(t, err)
	assert.True(t, person.ID > 0)

	err = testDB.
		Select("*").
		From("people").
		Where("email = $1", "*****@*****.**").
		QueryStruct(&person)
	assert.NoError(t, err)
	assert.True(t, person.ID > 0)
}
示例#3
0
func TestCacheSelectQueryStruct(t *testing.T) {
	Cache.FlushDB()
	for i := 0; i < 2; i++ {

		// Found:
		var person Person
		err := testDB.
			Select("id", "name", "email").
			From("people").
			Where("email = $1", "*****@*****.**").
			Cache("selectdoc.5", 1*time.Second, false).
			QueryStruct(&person)
		assert.NoError(t, err)
		assert.True(t, person.ID > 0)
		assert.Equal(t, person.Name, "John")
		assert.True(t, person.Email.Valid)
		assert.Equal(t, person.Email.String, "*****@*****.**")

		// Not found:
		var person2 Person
		err = testDB.
			Select("id", "name", "email").
			From("people").Where("email = $1", "*****@*****.**").
			Cache("selectdoc.6", 1*time.Second, false).
			QueryStruct(&person2)
		assert.Contains(t, err.Error(), "no rows")
	}
}
示例#4
0
func TestQueryObject(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var people jo.Object
	err := s.
		Select("id", "name", "email").
		From("people").
		OrderBy("id ASC").
		QueryObject(&people)

	assert.NoError(t, err)
	assert.Equal(t, len(people.AsSlice(".")), 6)

	// Make sure that the Ids are set. It's possible (maybe?) that different DBs set ids differently so
	// don't assume they're 1 and 2.
	assert.True(t, people.MustInt64("[0].id") > 0)
	assert.True(t, people.MustInt64("[1].id") > people.MustInt64("[0].id"))

	mario, _ := people.At("[0]")
	john, _ := people.At("[1]")
	assert.Equal(t, mario.MustString("name"), "Mario")
	assert.Equal(t, mario.MustString("email"), "*****@*****.**")
	assert.Equal(t, john.MustString("name"), "John")
	assert.Equal(t, john.MustString("email"), "*****@*****.**")
}
示例#5
0
func TestCacheSelectQueryStructs(t *testing.T) {
	Cache.FlushDB()
	for i := 0; i < 2; i++ {
		var people []Person
		err := testDB.
			Select("id", "name", "email").
			From("people").
			OrderBy("id ASC").
			Cache("selectdoc.4", 1*time.Second, false).
			QueryStructs(&people)

		assert.NoError(t, err)
		assert.Equal(t, len(people), 6)

		// Make sure that the Ids are set. It's possible (maybe?) that different DBs set ids differently so
		// don't assume they're 1 and 2.
		assert.True(t, people[0].ID > 0)
		assert.True(t, people[1].ID > people[0].ID)

		assert.Equal(t, people[0].Name, "Mario")
		assert.True(t, people[0].Email.Valid)
		assert.Equal(t, people[0].Email.String, "*****@*****.**")
		assert.Equal(t, people[1].Name, "John")
		assert.True(t, people[1].Email.Valid)
		assert.Equal(t, people[1].Email.String, "*****@*****.**")
	}
}
示例#6
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{})
}
示例#7
0
func TestUpdateBlacklist(t *testing.T) {
	installFixtures()

	// Insert by specifying a record (struct)
	p := Person{Name: "Barack"}
	p.Foo = "bar"
	var foo string
	var name string
	var id int64
	err := testDB.
		InsertInto("people").
		Whitelist("name", "foo").
		Record(p).
		Returning("id", "name", "foo").
		QueryScalar(&id, &name, &foo)
	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.Equal(t, name, "Barack")
	assert.Equal(t, foo, "bar")

	p2 := Person{Name: "oy"}
	p2.Foo = "bah"
	var name2 string
	var foo2 string
	err = testDB.
		Update("people").
		SetBlacklist(p2, "id", "name", "email", "key", "doc", "created_at").
		Where("id = $1", id).
		Returning("name", "foo").
		QueryScalar(&name2, &foo2)
	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.Equal(t, name2, "Barack")
	assert.Equal(t, foo2, "bah")
}
示例#8
0
func TestSelectQueryStructs(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var people []Person
	err := s.
		Select("id", "name", "email").
		From("people").
		OrderBy("id ASC").
		QueryStructs(&people)

	assert.NoError(t, err)
	assert.Equal(t, len(people), 6)

	// Make sure that the Ids are set. It's possible (maybe?) that different DBs set ids differently so
	// don't assume they're 1 and 2.
	assert.True(t, people[0].ID > 0)
	assert.True(t, people[1].ID > people[0].ID)

	assert.Equal(t, people[0].Name, "Mario")
	assert.True(t, people[0].Email.Valid)
	assert.Equal(t, people[0].Email.String, "*****@*****.**")
	assert.Equal(t, people[1].Name, "John")
	assert.True(t, people[1].Email.Valid)
	assert.Equal(t, people[1].Email.String, "*****@*****.**")

	// TODO: test map
}
示例#9
0
func TestDirSymlinkElementRelativePath(t *testing.T) {
	e := createElement(dirSymlink, pwd)

	assert.True(t, e.IsExists())
	assert.False(t, e.IsDir())
	assert.True(t, e.IsSymLink())
	assert.Equal(t, filepath.Join(pwd, dirSymlink), e.Path())
	assert.Equal(t, filepath.Dir(filepath.Join(pwd, dirSymlink)), e.Dirname())
}
示例#10
0
func TestDirSymlinkElementFullPath(t *testing.T) {
	fullPath := filepath.Join(pwd, dirSymlink)
	e := createElement(fullPath, "/path/to/dummy")

	assert.True(t, e.IsExists())
	assert.False(t, e.IsDir())
	assert.True(t, e.IsSymLink())
	assert.Equal(t, fullPath, e.Path())
	assert.Equal(t, filepath.Dir(fullPath), e.Dirname())
}
示例#11
0
func TestSelectDocDate(t *testing.T) {
	var comments []*Comment

	err := testDB.SelectDoc("id", "comment", `created_at as "CreatedAt"`).
		From("comments").
		QueryStructs(&comments)

	assert.NoError(t, err)
	assert.True(t, comments[0].CreatedAt.Valid)
	assert.True(t, comments[1].CreatedAt.Valid)
}
示例#12
0
func TestParallel(t *testing.T) {
	var result string
	tasks := func(p *Project) {
		p.Task1("A", func(*Context) {
			result += "A"
		})
		p.Task1("B", func(*Context) {
			time.Sleep(10 * time.Millisecond)
			result += "B"
		})
		p.Task("C", nil, func(*Context) {
			result += "C"
		})
		p.Task("D", nil, func(*Context) {
			result += "D"
		})
		p.Task("default", P{"A", "B", "C", "D"}, nil)
	}

	argv := []string{}
	ch := make(chan int)
	go func() {
		execCLI(tasks, argv, func(code int) {
			assert.Equal(t, code, 0)
			assert.True(t, len(result) == 4)
			ch <- code
			close(ch)
		})
	}()
	<-ch
}
示例#13
0
func TestInsertBlacklist(t *testing.T) {
	// type Person struct {
	// 	ID        int64          `db:"id"`
	// 	Name      string         `db:"name"`
	// 	Foo       string         `db:"foo"`
	// 	Email     dat.NullString `db:"email"`
	// 	Key       dat.NullString `db:"key"`
	// 	Doc       dat.NullString `db:"doc"`
	// 	CreatedAt dat.NullTime   `db:"created_at"`
	// }

	// Insert by specifying a record (struct)
	person2 := Person{Name: "Barack"}
	person2.Email.Valid = true
	person2.Email.String = "*****@*****.**"
	var email sql.NullString
	var name string
	var id int64

	err := testDB.
		InsertInto("people").
		Blacklist("id", "foo", "email", "key", "doc", "created_at").
		Record(person2).
		Returning("id", "name", "email").
		QueryScalar(&id, &name, &email)
	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.False(t, email.Valid)
	assert.Equal(t, name, "Barack")
}
示例#14
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)
}
示例#15
0
func TestNullInt64From(t *testing.T) {
	v := int64(400)
	n := NullInt64From(v)

	assert.True(t, n.Valid)
	assert.Equal(t, n.Int64, v)
}
示例#16
0
func TestSQLInjectionSQL(t *testing.T) {
	for _, fuzz := range strings.Split(fuzzList, "\n") {
		if fuzz == "" {
			continue
		}
		fuzz = strings.Trim(fuzz, " \t")

		var id int64
		var comment string
		err := testDB.
			SQL(`
				INSERT INTO comments (comment)
				VALUES ($1)
				RETURNING id, comment
			`, fuzz).
			SetIsInterpolated(true).
			QueryScalar(&id, &comment)

		assert.True(t, id > 0)
		assert.Equal(t, fuzz, comment)

		var result int
		err = testDB.SQL(`
			SELECT 42
			FROM comments
			WHERE id = $1 AND comment = $2
		`, id, comment).QueryScalar(&result)

		assert.NoError(t, err)
		assert.Equal(t, 42, result)
	}
}
示例#17
0
func TestNullTimeFrom(t *testing.T) {
	v := time.Now()
	n := NullTimeFrom(v)

	assert.True(t, n.Valid)
	assert.Equal(t, n.Time, v)
}
示例#18
0
func TestNullBoolFrom(t *testing.T) {
	v := false
	n := NullBoolFrom(v)

	assert.True(t, n.Valid)
	assert.Equal(t, n.Bool, v)
}
示例#19
0
func TestNullFloat64From(t *testing.T) {
	v := 42.2
	n := NullFloat64From(v)

	assert.True(t, n.Valid)
	assert.Equal(t, n.Float64, v)
}
示例#20
0
func TestCacheSelectQueryScalar(t *testing.T) {
	Cache.FlushDB()
	for i := 0; i < 2; i++ {

		var name string
		err := testDB.
			Select("name").
			From("people").
			Where("email = '*****@*****.**'").
			Cache("selectdoc.8", 1*time.Second, false).
			QueryScalar(&name)

		assert.NoError(t, err)
		assert.Equal(t, name, "John")

		var id int64
		err = testDB.
			Select("id").
			From("people").
			Limit(1).
			Cache("selectdoc.9", 1*time.Second, false).
			QueryScalar(&id)

		assert.NoError(t, err)
		assert.True(t, id > 0)
	}
}
示例#21
0
func TestNullStringFrom(t *testing.T) {
	v := "foo"
	n := NullStringFrom(v)

	assert.True(t, n.Valid)
	assert.Equal(t, n.String, v)
}
示例#22
0
func TestSQLInjectionBuilder(t *testing.T) {
	for _, fuzz := range strings.Split(fuzzList, "\n") {
		if fuzz == "" {
			continue
		}
		fuzz = strings.Trim(fuzz, " \t")

		var id int64
		var comment string
		err := testDB.
			InsertInto("comments").
			Columns("comment").
			Values(fuzz).
			SetIsInterpolated(true).
			Returning("id", "comment").
			QueryScalar(&id, &comment)

		assert.True(t, id > 0)
		assert.Equal(t, fuzz, comment)

		var result int
		err = testDB.SQL(`
			SELECT 42
			FROM comments
			WHERE id = $1 AND comment = $2
		`, id, comment).QueryScalar(&result)

		assert.NoError(t, err)
		assert.Equal(t, 42, result)
	}
}
示例#23
0
func TestTransactionReal(t *testing.T) {
	installFixtures()

	tx, err := testDB.Begin()
	assert.NoError(t, err)

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

	assert.True(t, id > 0)

	var person Person
	err = tx.
		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, "*****@*****.**")

	err = tx.Commit()
	assert.NoError(t, err)
}
示例#24
0
func TestSelectScoped(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var id int
	err := s.
		InsertInto("people").
		Columns("name").
		Values("mgutz").
		Returning("id").
		QueryScalar(&id)
	assert.NoError(t, err)
	assert.True(t, id > 0)

	var postID int
	err = s.
		InsertInto("posts").
		Columns("title", "state", "user_id").
		Values("my post", "published", id).
		Returning("id").
		QueryScalar(&postID)
	assert.NoError(t, err)
	assert.True(t, postID > 0)

	publishedByUser := `
		INNER JOIN people on (people.id = p.user_id)
		WHERE
			people.name = $1 AND
			p.state = 'published' AND
			p.deleted_at IS NULL
	`

	var posts []*Post
	err = s.
		Select("p.*").
		From("posts p").
		Scope(publishedByUser, "mgutz").
		QueryStructs(&posts)
	assert.NoError(t, err)
	assert.Equal(t, posts[0].Title, "my post")
}
示例#25
0
func TestEscapeSequences(t *testing.T) {
	installFixtures()

	dat.EnableInterpolation = true
	id := 0
	str := ""
	expect := "I said, \"a's \\ \\\b\f\n\r\t\x1a\"你好'; DELETE FROM people"

	err := testDB.InsertInto("people").
		Columns("name", "foo").
		Values("conn1", expect).
		Returning("id", "foo").
		QueryScalar(&id, &str)

	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.Equal(t, expect, str)
	dat.EnableInterpolation = false
}
示例#26
0
func TestInsertWhitelist(t *testing.T) {
	// Insert by specifying a record (struct)
	person2 := Person{Name: "Barack"}
	person2.Email.Valid = true
	person2.Email.String = "*****@*****.**"
	var email sql.NullString
	var name string
	var id int64
	err := testDB.
		InsertInto("people").
		Whitelist("name").
		Record(person2).
		Returning("id", "name", "email").
		QueryScalar(&id, &name, &email)
	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.False(t, email.Valid)
	assert.Equal(t, name, "Barack")
}
示例#27
0
func TestSelectQueryScalar(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var name string
	err := s.
		Select("name").
		From("people").
		Where("email = '*****@*****.**'").
		QueryScalar(&name)

	assert.NoError(t, err)
	assert.Equal(t, name, "John")

	var id int64
	err = s.Select("id").From("people").Limit(1).QueryScalar(&id)

	assert.NoError(t, err)
	assert.True(t, id > 0)
}
示例#28
0
func TestConnectionExec(t *testing.T) {
	installFixtures()

	id := 0
	str := ""
	err := testDB.InsertInto("people").
		Columns("name", "foo").
		Values("conn1", "---").
		Returning("id", "foo").
		QueryScalar(&id, &str)

	assert.NoError(t, err)
	assert.True(t, id > 0)
	assert.Equal(t, "---", str)

	dat.EnableInterpolation = true
	_, err = testDB.Update("people").
		Set("foo", dat.DEFAULT).
		Returning("foo").
		Exec()
	dat.EnableInterpolation = false
	assert.NoError(t, err)
}
示例#29
0
func TestVersion(t *testing.T) {
	// require at least 9.3+ for testing
	assert.True(t, testDB.Version > 90300)
}