當前位置: 首頁>>代碼示例>>Golang>>正文


Golang assert.Equal函數代碼示例

本文整理匯總了Golang中gopkg/in/stretchr/testify/v1/assert.Equal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Equal函數的具體用法?Golang Equal怎麽用?Golang Equal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Equal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestUpdateReal

func TestUpdateReal(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	var id int64
	// Insert a George
	s.InsertInto("people").Columns("name", "email").
		Values("George", "[email protected]").
		Returning("id").
		QueryScalar(&id)

	// Rename our George to Barack
	_, err := s.Update("people").SetMap(map[string]interface{}{"name": "Barack", "email": "[email protected]"}).Where("id = $1", 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, "[email protected]")
}
開發者ID:iPowow,項目名稱:lbManager,代碼行數:25,代碼來源:update_exec_test.go

示例2: TestInterpolateJSON

func TestInterpolateJSON(t *testing.T) {
	j, _ := NewJSON([]int{1, 3, 10})
	sql, args, err := Interpolate("SELECT $1", []interface{}{j})
	assert.NoError(t, err)
	assert.Equal(t, "SELECT '[1,3,10]'", sql)
	assert.Equal(t, 0, len(args))
}
開發者ID:iPowow,項目名稱:lbManager,代碼行數:7,代碼來源:interpolate_test.go

示例3: TestQueryObject

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"), "[email protected]")
	assert.Equal(t, john.MustString("name"), "John")
	assert.Equal(t, john.MustString("email"), "[email protected]")
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:26,代碼來源:query_object_test.go

示例4: TestUpdateKeywordColumnName

func TestUpdateKeywordColumnName(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	// Insert a user with a key
	res, err := s.
		InsertInto("people").
		Columns("name", "email", "key").
		Values("Benjamin", "[email protected]", "6").
		Exec()
	assert.NoError(t, err)

	// Update the key
	res, err = s.Update("people").Set("key", "6-revoked").Where(dat.Eq{"key": "6"}).Exec()
	assert.NoError(t, err)

	// Assert our record was updated (and only our record)
	assert.EqualValues(t, res.RowsAffected, 1)

	var person Person
	err = s.Select("*").From("people").Where(dat.Eq{"email": "[email protected]"}).QueryStruct(&person)
	assert.NoError(t, err)

	assert.Equal(t, person.Name, "Benjamin")
	assert.Equal(t, person.Key.String, "6-revoked")
}
開發者ID:iPowow,項目名稱:lbManager,代碼行數:26,代碼來源:update_exec_test.go

示例5: TestSQLInjectionBuilder

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)
	}
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:31,代碼來源:sqli_test.go

示例6: TestUpsertSQLRecord

func TestUpsertSQLRecord(t *testing.T) {
	var rec = struct {
		B int `db:"b"`
		C int `db:"c"`
	}{1, 2}

	sql, args := Upsert("tab").
		Columns("b", "c").
		Record(rec).
		Where("d=$1", 4).
		Returning("f", "g").
		ToSQL()

	expected := `
	WITH
		upd AS (
			UPDATE "tab"
			SET "b" = $1, "c" = $2
			WHERE (d=$3)
			RETURNING "f","g"
		), ins AS (
			INSERT INTO "tab"("b","c")
			SELECT $1,$2
			WHERE NOT EXISTS (SELECT 1 FROM upd)
			RETURNING "f","g"
		)
	SELECT * FROM ins UNION ALL SELECT * FROM upd
	`

	assert.Equal(t, stripWS(expected), stripWS(sql))
	assert.Equal(t, []interface{}{1, 2, 4}, args)
}
開發者ID:iPowow,項目名稱:lbManager,代碼行數:32,代碼來源:upsert_test.go

示例7: TestSelectDocRow

func TestSelectDocRow(t *testing.T) {
	assert := assert.New(t)
	type Post struct {
		ID    int
		Title string
	}

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var person Person
	err := testDB.
		SelectDoc("id", "name").
		Many("posts", `SELECT id, title FROM posts WHERE user_id = people.id`).
		From("people").
		Where("id = $1", 1).
		QueryStruct(&person)

	assert.NoError(err)
	assert.Equal("Mario", person.Name)
	assert.Equal(1, person.ID)

	assert.Equal(2, len(person.Posts))
	assert.Equal("Day 1", person.Posts[0].Title)
	assert.Equal("Day 2", person.Posts[1].Title)
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:29,代碼來源:select_doc_test.go

示例8: TestSelectDocNested

func TestSelectDocNested(t *testing.T) {
	assert := assert.New(t)

	var obj jo.Object

	posts := dat.SelectDoc("id", "title").
		Many("comments", `SELECT * FROM comments WHERE comments.id = posts.id`).
		From("posts").
		Where("user_id = people.id")

	err := testDB.
		SelectDoc("id", "name").
		Many("posts", posts).
		From("people").
		Where("id = $1", 1).
		SetIsInterpolated(true).
		QueryStruct(&obj)

	assert.NoError(err)
	assert.Equal("Mario", obj.AsString("name"))
	assert.Equal(1, obj.AsInt("id"))

	assert.Equal("A very good day", obj.AsString("posts[0].comments[0].comment"))
	assert.Equal("Yum. Apple pie.", obj.AsString("posts[1].comments[0].comment"))
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:25,代碼來源:select_doc_test.go

示例9: TestInsectSqlRecord

func TestInsectSqlRecord(t *testing.T) {
	var rec = struct {
		B int
		C int
	}{1, 2}

	sql, args := Insect("tab").
		Columns("b", "c").
		Record(rec).
		Where("d = $1", 3).
		Returning("id", "f", "g").
		ToSQL()

	expected := `
	WITH
		sel AS (SELECT id, f, g FROM tab WHERE (d = $1)),
		ins AS (
			INSERT INTO "tab"("b","c")
			SELECT $2,$3
			WHERE NOT EXISTS (SELECT 1 FROM sel)
			RETURNING "id","f","g"
		)
	SELECT * FROM ins UNION ALL SELECT * FROM sel
	`
	assert.Equal(t, stripWS(expected), stripWS(sql))
	assert.Equal(t, args, []interface{}{3, 1, 2})
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:27,代碼來源:insect_test.go

示例10: TestInsertDoubleDollarQuote

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)
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:32,代碼來源:insert_exec_test.go

示例11: TestLegacyIn

func TestLegacyIn(t *testing.T) {

	var cat = "cat"
	if isWindows {
		cat = "cmd /c type"
	}
	//// Run

	// in V2 BashOutput accepts an options map

	out, err := RunOutput(cat+" foo.txt", M{"$in": "test"})
	assert.NoError(t, err)
	assert.Equal(t, "foo", str.Clean(out))

	if isWindows {
		return
	}

	//// Bash

	// in V2 BashOutput accepts an options map
	out, err = BashOutput("cat foo.txt", M{"$in": "test"})
	assert.NoError(t, err)
	assert.Equal(t, "foo", str.Clean(out))
}
開發者ID:coreroller,項目名稱:coreroller,代碼行數:25,代碼來源:project_test.go

示例12: TestCacheSelectQuerySliceByHash

func TestCacheSelectQuerySliceByHash(t *testing.T) {
	Cache.FlushDB()
	for i := 0; i < 2; i++ {
		var names []string
		err := testDB.
			Select("name").
			From("people").
			Cache("", 1*time.Second, false).
			QuerySlice(&names)

		assert.NoError(t, err)
		assert.Equal(t, len(names), 6)
		assert.Equal(t, names, []string{"Mario", "John", "Grant", "Tony", "Ester", "Reggie"})

		var ids []int64
		err = testDB.
			Select("id").
			From("people").
			Limit(1).
			Cache("", 1*time.Second, false).
			QuerySlice(&ids)

		assert.NoError(t, err)
		assert.Equal(t, len(ids), 1)
		assert.Equal(t, ids, []int64{1})
	}
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:27,代碼來源:cache_test.go

示例13: TestCacheSelectQueryStruct

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", "[email protected]").
			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, "[email protected]")

		// Not found:
		var person2 Person
		err = testDB.
			Select("id", "name", "email").
			From("people").Where("email = $1", "[email protected]").
			Cache("selectdoc.6", 1*time.Second, false).
			QueryStruct(&person2)
		assert.Contains(t, err.Error(), "no rows")
	}
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:28,代碼來源:cache_test.go

示例14: TestSQLInjectionSQL

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)
	}
}
開發者ID:syreclabs,項目名稱:dat,代碼行數:32,代碼來源:sqli_test.go

示例15: TestUpdateBlacklist

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")
}
開發者ID:iPowow,項目名稱:lbManager,代碼行數:35,代碼來源:update_exec_test.go


注:本文中的gopkg/in/stretchr/testify/v1/assert.Equal函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。