Ejemplo n.º 1
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))
}
Ejemplo 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(),
	)
}
Ejemplo n.º 3
0
func DoTestBoolType(assert *assrt.Assert, info dialectInfo) {
	type BoolType struct {
		Id     int64
		Active bool
	}
	bt := new(BoolType)
	mg, q := setupDb(assert, info)
	defer mg.Close()
	defer q.Close()
	mg.dropTableIfExists(bt)
	mg.CreateTableIfNotExists(bt)
	bt.Active = true
	q.Save(bt)
	bt.Active = false
	q.WhereEqual("active", true).Find(bt)
	assert.True(bt.Active)
}
Ejemplo n.º 4
0
func DoTestCreateTable(assert *assrt.Assert, info dialectInfo) {
	assert.Logf("Dialect %T\n", info.dialect)
	mg, _ := setupDb(assert, info)
	{
		type AddColumn struct {
			Prim Id
		}
		table := &AddColumn{}
		mg.dropTableIfExists(table)
		mg.CreateTableIfNotExists(table)
		columns := mg.Dialect.ColumnsInTable(mg, table)
		assert.OneLen(columns)
		assert.True(columns["prim"])
	}
	table := &AddColumn{}
	mg.CreateTableIfNotExists(table)
	columns := mg.Dialect.ColumnsInTable(mg, table)
	assert.Equal(4, len(columns))
}
Ejemplo n.º 5
0
func DoTestCreateTable(assert *assrt.Assert, info dialectInfo) {
	assert.Logf("Dialect %T\n", info.dialect)
	mg, _ := setupDb(assert, info)
	defer mg.Close()
	{
		type AddColumn struct {
			Prim int64 `qbs:"pk"`
		}
		table := &AddColumn{}
		mg.dropTableIfExists(table)
		mg.CreateTableIfNotExists(table)
		columns := mg.Dialect.ColumnsInTable(mg, table)
		assert.OneLen(columns)
		assert.True(columns["prim"])
	}
	table := &AddColumn{}
	mg.CreateTableIfNotExists(table)
	assert.True(mg.Dialect.IndexExists(mg, "add_column", "add_column_first_last"))
	columns := mg.Dialect.ColumnsInTable(mg, table)
	assert.Equal(4, len(columns))
}
Ejemplo n.º 6
0
func DoTestSaveAndDelete(assert *assrt.Assert, info dialectInfo) {
	x := time.Now()
	assert.MustZero(x.Sub(x.UTC()))
	now := time.Now()
	mg, q := setupDb(assert, info)
	type saveModel struct {
		Id      Id
		A       string
		B       int
		Updated time.Time
		Created time.Time
	}
	model1 := saveModel{
		A: "banana",
		B: 5,
	}
	model2 := saveModel{
		A: "orange",
		B: 4,
	}

	mg.dropTableIfExists(&model1)
	mg.CreateTableIfNotExists(&model1)
	affected, err := q.Save(&model1)
	assert.MustNil(err)
	assert.Equal(1, affected)
	assert.True(model1.Created.Sub(now) > 0)
	assert.True(model1.Updated.Sub(now) > 0)

	// make sure created/updated values match the db
	var model1r []*saveModel
	err = q.Where("id = ?", model1.Id).FindAll(&model1r)
	assert.MustNil(err)
	assert.MustOneLen(model1r)
	assert.Equal(model1.Created.Unix(), model1r[0].Created.Unix())
	assert.Equal(model1.Updated.Unix(), model1r[0].Updated.Unix())

	oldCreate := model1.Created
	oldUpdate := model1.Updated
	model1.A = "grape"
	model1.B = 9

	time.Sleep(time.Second * 1) // sleep for 1 sec

	affected, err = q.Save(&model1)
	assert.MustNil(err)
	assert.MustEqual(1, affected)
	assert.True(model1.Created.Equal(oldCreate))
	assert.True(model1.Updated.Sub(oldUpdate) > 0)

	// make sure created/updated values match the db
	var model1r2 []*saveModel
	err = q.Where("id = ?", model1.Id).FindAll(&model1r2)
	assert.MustNil(err)
	assert.MustOneLen(model1r2)
	assert.True(model1r2[0].Updated.Sub(model1r2[0].Created) >= 1)
	assert.Equal(model1.Created.Unix(), model1r2[0].Created.Unix())
	assert.Equal(model1.Updated.Unix(), model1r2[0].Updated.Unix())

	affected, err = q.Save(&model2)
	assert.MustNil(err)
	assert.Equal(1, affected)

	affected, err = q.Delete(&model2)
	assert.MustNil(err)
	assert.Equal(1, affected)
}