Exemple #1
0
func DoTestTransaction(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	defer mg.Close()
	defer q.Close()
	type txModel struct {
		Id int64
		A  string
	}
	table := txModel{
		A: "A",
	}
	mg.dropTableIfExists(&table)
	mg.CreateTableIfNotExists(&table)
	q.Begin()
	assert.NotNil(q.Tx)
	_, err := q.Save(&table)
	assert.Nil(err)
	err = q.Rollback()
	assert.Nil(err)
	out := new(txModel)
	err = q.Find(out)
	assert.Equal(sql.ErrNoRows, err)
	q.Begin()
	table.Id = 0
	_, err = q.Save(&table)
	assert.Nil(err)
	err = q.Commit()
	assert.Nil(err)
	out.Id = table.Id
	err = q.Find(out)
	assert.Nil(err)
	assert.Equal("A", out.A)
}
Exemple #2
0
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	defer mg.Close()
	defer q.Close()
	type user struct {
		Id   int64
		Name string
	}
	type post struct {
		Id       int64
		Title    string
		AuthorId int64
		Author   *user
	}
	aUser := &user{
		Name: "john",
	}
	aPost := &post{
		Title: "A Title",
	}
	mg.dropTableIfExists(aPost)
	mg.dropTableIfExists(aUser)
	mg.CreateTableIfNotExists(aUser)
	mg.CreateTableIfNotExists(aPost)

	affected, err := q.Save(aUser)
	assert.Nil(err)
	aPost.AuthorId = int64(aUser.Id)
	affected, err = q.Save(aPost)
	assert.Equal(1, affected)
	pst := new(post)
	pst.Id = aPost.Id
	err = q.Find(pst)
	assert.MustNil(err)
	assert.Equal(aPost.Id, pst.Id)
	assert.Equal("john", pst.Author.Name)

	pst.Author = nil
	err = q.OmitFields("Author").Find(pst)
	assert.MustNil(err)
	assert.MustNil(pst.Author)

	err = q.OmitJoin().Find(pst)
	assert.MustNil(err)
	assert.MustNil(pst.Author)

	var psts []*post
	err = q.FindAll(&psts)
	assert.MustNil(err)
	assert.OneLen(psts)
	assert.Equal("john", psts[0].Author.Name)
}
Exemple #3
0
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	type user struct {
		Id   Id
		Name string
	}
	type post struct {
		Id       Id
		Title    string
		AuthorId int64
		Author   *user
	}
	aUser := &user{
		Name: "john",
	}
	aPost := &post{
		Title: "A Title",
	}
	mg.dropTableIfExists(aPost)
	mg.dropTableIfExists(aUser)
	mg.CreateTableIfNotExists(aUser)
	mg.CreateTableIfNotExists(aPost)

	uid, err := q.Save(aUser)
	assert.Nil(err)
	aPost.AuthorId = int64(uid)
	affected, err := q.Save(aPost)
	assert.Equal(1, affected)
	pst := new(post)
	pst.Id = aPost.Id
	err = q.Find(pst)
	assert.MustNil(err)
	assert.MustNotNil(pst)
	assert.Equal(uid, pst.Id)
	assert.Equal("john", pst.Author.Name)
}
Exemple #4
0
func DoTestFind(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	now := time.Now()

	type types struct {
		Id    Id
		Str   string
		Intgr int64
		Flt   float64
		Bytes []byte
		Time  time.Time
	}
	modelData := &types{
		Str:   "string!",
		Intgr: -1,
		Flt:   3.8,
		Bytes: []byte("bytes!"),
		Time:  now,
	}

	mg.dropTableIfExists(modelData)
	mg.CreateTableIfNotExists(modelData)

	out := new(types)
	condition := NewCondition("str = ?", "string!").And("intgr = ?", -1)
	err := q.Condition(condition).Find(out)
	assert.Nil(err)
	assert.Zero(out.Id)

	affected, err := q.Save(modelData)
	assert.Nil(err)
	assert.Equal(1, affected)

	err = q.Condition(condition).Find(out)
	assert.Nil(err)
	assert.Equal(1, out.Id)
	assert.Equal("string!", out.Str)
	assert.Equal(-1, out.Intgr)
	assert.Equal(3.8, out.Flt)
	assert.Equal([]byte("bytes!"), out.Bytes)
	diff := now.Sub(out.Time)
	assert.True(diff < time.Second && diff > -time.Second)

	modelData.Id = 5
	modelData.Str = "New row"
	_, err = q.Save(modelData)
	assert.Nil(err)

	out = new(types)
	condition = NewCondition("str = ?", "New row").And("flt = ?", 3.8)
	err = q.Condition(condition).Find(out)
	assert.Nil(err)
	assert.Equal(5, out.Id)

	allOut := []*types{}
	err = q.Where("intgr = ?", -1).FindAll(&allOut)
	assert.Nil(err)
	assert.Equal(2, len(allOut))
}