Exemplo n.º 1
0
func DoTestTransaction(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	type txModel struct {
		Id Id
		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.Nil(err)
	assert.Zero(out.Id)
	q.Begin()
	table.Id = 0
	_, err = q.Save(&table)
	assert.Nil(err)
	err = q.Commit()
	assert.Nil(err)
	err = q.Find(out)
	assert.Nil(err)
	assert.Equal("A", out.A)
}
Exemplo n.º 2
0
func assertLegitGraph(assert *assrt.Assert, g *Graph) {
	assert.NotNil(g)

	gstat, _ := os.Stat(filepath.Join(g.dir))
	assert.True(gstat.IsDir())

	assert.True(g.HasBranch("hroot/init"))

	assert.Equal(
		"",
		g.cmd("ls-tree")("HEAD").Output(),
	)
}
Exemplo n.º 3
0
func DoTestFind(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	defer mg.Close()
	defer q.Close()
	now := time.Now()

	type types struct {
		Id    int64
		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.Equal(sql.ErrNoRows, err)

	affected, err := q.Save(modelData)
	assert.Nil(err)
	assert.Equal(1, affected)
	out.Id = modelData.Id
	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)

	out = new(types)
	out.Id = 100
	err = q.Find(out)
	assert.NotNil(err)

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