Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

improve tests #1250

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,39 @@ jobs:

- run: go test -v -race -db="sqlite3" -conn_str="./test.db" -coverprofile=coverage1-1.txt -covermode=atomic
- run: go test -v -race -db="sqlite3" -conn_str="./test.db" -cache=true -coverprofile=coverage1-2.txt -covermode=atomic
- run: go test -v -race -db="sqlite3" -conn_str="./test.db" -map_type=same -coverprofile=coverage1-3.txt -covermode=atomic
- run: go test -v -race -db="sqlite3" -conn_str="./test.db" -map_type=same -cache=true -coverprofile=coverage1-4.txt -covermode=atomic

- run: go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -coverprofile=coverage2-1.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -cache=true -coverprofile=coverage2-2.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -map_type=same -coverprofile=coverage2-3.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@/xorm_test" -map_type=same -cache=true -coverprofile=coverage2-4.txt -covermode=atomic

- run: go test -v -race -db="mymysql" -conn_str="xorm_test/root/" -coverprofile=coverage3-1.txt -covermode=atomic
- run: go test -v -race -db="mymysql" -conn_str="xorm_test/root/" -cache=true -coverprofile=coverage3-2.txt -covermode=atomic
- run: go test -v -race -db="mymysql" -conn_str="xorm_test/root/" -map_type=same -coverprofile=coverage3-3.txt -covermode=atomic
- run: go test -v -race -db="mymysql" -conn_str="xorm_test/root/" -map_type=same -cache=true -coverprofile=coverage3-4.txt -covermode=atomic

- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -coverprofile=coverage4-1.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -cache=true -coverprofile=coverage4-2.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -map_type=same -coverprofile=coverage4-3.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -map_type=same -cache=true -coverprofile=coverage4-4.txt -covermode=atomic

- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -schema=xorm -coverprofile=coverage5-1.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -map_type=same -schema=xorm -coverprofile=coverage5-3.txt -covermode=atomic
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -map_type=same -schema=xorm -cache=true -coverprofile=coverage5-4.txt -covermode=atomic

- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -coverprofile=coverage6-1.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -cache=true -coverprofile=coverage6-2.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -map_type=same -coverprofile=coverage6-3.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -map_type=same -cache=true -coverprofile=coverage6-4.txt -covermode=atomic

- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true -coverprofile=coverage7-1.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true -cache=true -coverprofile=coverage7-2.txt -covermode=atomic
- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt coverage6-1.txt coverage6-2.txt coverage7-1.txt coverage7-2.txt > coverage.txt
- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -map_type=same -ignore_select_update=true -coverprofile=coverage7-3.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -map_type=same -ignore_select_update=true -cache=true -coverprofile=coverage7-4.txt -covermode=atomic

- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage1-3.txt coverage1-4.txt coverage2-1.txt coverage2-2.txt coverage2-3.txt coverage2-4.txt coverage3-1.txt coverage3-2.txt coverage3-3.txt coverage3-4.txt coverage4-1.txt coverage4-2.txt coverage4-3.txt coverage4-4.txt coverage5-1.txt coverage5-2.txt coverage5-3.txt coverage5-4.txt coverage6-1.txt coverage6-2.txt coverage6-3.txt coverage6-4.txt coverage7-1.txt coverage7-2.txt coverage7-3.txt coverage7-4.txt > coverage.txt

- run: bash <(curl -s https://codecov.io/bash)
14 changes: 10 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ func TestCacheFind(t *testing.T) {
assert.Equal(t, inserts[i].Password, box.Password)
}

idName := mapper.Obj2Table("Id")

boxes = make([]MailBox, 0, 2)
assert.NoError(t, testEngine.Alias("a").Where("a.id > -1").Asc("a.id").Find(&boxes))
assert.NoError(t, testEngine.Alias("a").Where("`a`.`"+idName+"` > -1").Asc("a."+idName).Find(&boxes))
assert.EqualValues(t, 2, len(boxes))
for i, box := range boxes {
assert.Equal(t, inserts[i].Id, box.Id)
Expand All @@ -74,8 +76,10 @@ func TestCacheFind(t *testing.T) {
Password string
}

tableName := mapper.Obj2Table("MailBox")

boxes2 := make([]MailBox4, 0, 2)
assert.NoError(t, testEngine.Table("mail_box").Where("mail_box.id > -1").Asc("mail_box.id").Find(&boxes2))
assert.NoError(t, testEngine.Table(tableName).Where("`"+tableName+"`.`"+idName+"` > -1").Asc(tableName+"."+idName).Find(&boxes2))
assert.EqualValues(t, 2, len(boxes2))
for i, box := range boxes2 {
assert.Equal(t, inserts[i].Id, box.Id)
Expand Down Expand Up @@ -161,15 +165,17 @@ func TestCacheGet(t *testing.T) {
_, err := testEngine.Insert(inserts[0])
assert.NoError(t, err)

idName := mapper.Obj2Table("Id")

var box1 MailBox3
has, err := testEngine.Where("id = ?", inserts[0].Id).Get(&box1)
has, err := testEngine.Where("`"+idName+"` = ?", inserts[0].Id).Get(&box1)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "user1", box1.Username)
assert.EqualValues(t, "pass1", box1.Password)

var box2 MailBox3
has, err = testEngine.Where("id = ?", inserts[0].Id).Get(&box2)
has, err = testEngine.Where("`"+idName+"` = ?", inserts[0].Id).Get(&box2)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "user1", box2.Username)
Expand Down
2 changes: 1 addition & 1 deletion engine_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (engine *Engine) tbNameWithSchema(v string) string {
engine.dialect.URI().Schema != "" &&
engine.dialect.URI().Schema != postgresPublicSchema &&
strings.Index(v, ".") == -1 {
return engine.dialect.URI().Schema + "." + v
return engine.Quote(engine.dialect.URI().Schema) + "." + v
}
return v
}
Expand Down
10 changes: 7 additions & 3 deletions session_cols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func TestSetExpr(t *testing.T) {
if testEngine.Dialect().DBType() == core.MSSQL {
not = "~"
}
cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
showName := mapper.Obj2Table("Show")
cnt, err = testEngine.SetExpr(showName, not+" `"+showName+"`").ID(1).Update(new(UserExpr))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

Expand Down Expand Up @@ -81,7 +82,9 @@ func TestCols(t *testing.T) {
assert.NoError(t, err)

sess := testEngine.ID(1)
_, err = sess.Cols("col1").Cols("col2").Update(&ColsTable{
col1Name := mapper.Obj2Table("Col1")
col2Name := mapper.Obj2Table("Col2")
_, err = sess.Cols(col1Name).Cols(col2Name).Update(&ColsTable{
Col1: "",
Col2: "",
})
Expand Down Expand Up @@ -130,7 +133,8 @@ func TestMustCol(t *testing.T) {
}

customer.ParentId = 0
affected, err := testEngine.MustCols("parent_id").Update(&customer, &CustomerOnlyId{Id: customer.Id})
parentIDName := mapper.Obj2Table("ParentId")
affected, err := testEngine.MustCols(parentIDName).Update(&customer, &CustomerOnlyId{Id: customer.Id})
assert.NoError(t, err)
assert.EqualValues(t, 1, affected)
}
35 changes: 20 additions & 15 deletions session_cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,67 @@ func TestBuilder(t *testing.T) {
_, err = testEngine.Insert(&Condition{TableName: "table1", ColName: "col1", Op: OpEqual, Value: "1"})
assert.NoError(t, err)

colNameName := "`" + mapper.Obj2Table("ColName") + "`"
opName := "`" + mapper.Obj2Table("Op") + "`"
valueName := "`" + mapper.Obj2Table("Value") + "`"

var cond Condition
has, err := testEngine.Where(builder.Eq{"col_name": "col1"}).Get(&cond)
has, err := testEngine.Where(builder.Eq{colNameName: "col1"}).Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")

has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Eq{"op": OpEqual})).
has, err = testEngine.Where(builder.Eq{colNameName: "col1"}.
And(builder.Eq{opName: OpEqual})).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")

has, err = testEngine.Where(builder.Eq{"col_name": "col1", "op": OpEqual, "value": "1"}).
has, err = testEngine.Where(builder.Eq{colNameName: "col1", opName: OpEqual, valueName: "1"}).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")

has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Neq{"op": OpEqual})).
has, err = testEngine.Where(builder.Eq{colNameName: "col1"}.
And(builder.Neq{opName: OpEqual})).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, false, has, "records should not exist")

var conds []Condition
err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Eq{"op": OpEqual})).
err = testEngine.Where(builder.Eq{colNameName: "col1"}.
And(builder.Eq{opName: OpEqual})).
Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")

conds = make([]Condition, 0)
err = testEngine.Where(builder.Like{"col_name", "col"}).Find(&conds)
err = testEngine.Where(builder.Like{colNameName, "col"}).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")

conds = make([]Condition, 0)
err = testEngine.Where(builder.Expr("col_name = ?", "col1")).Find(&conds)
err = testEngine.Where(builder.Expr(colNameName+" = ?", "col1")).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")

conds = make([]Condition, 0)
err = testEngine.Where(builder.In("col_name", "col1", "col2")).Find(&conds)
err = testEngine.Where(builder.In(colNameName, "col1", "col2")).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")

conds = make([]Condition, 0)
err = testEngine.NotIn("col_name", "col1", "col2").Find(&conds)
err = testEngine.NotIn(colNameName, "col1", "col2").Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 0, len(conds), "records should not exist")

// complex condtions
var where = builder.NewCond()
if true {
where = where.And(builder.Eq{"col_name": "col1"})
where = where.Or(builder.And(builder.In("col_name", "col1", "col2"), builder.Expr("col_name = ?", "col1")))
where = where.And(builder.Eq{colNameName: "col1"})
where = where.Or(builder.And(builder.In(colNameName, "col1", "col2"), builder.Expr(colNameName+" = ?", "col1")))
}

conds = make([]Condition, 0)
Expand Down Expand Up @@ -219,7 +223,8 @@ func TestFindAndCount(t *testing.T) {
assert.NoError(t, err)

var results []FindAndCount
sess := testEngine.Where("name = ?", "test1")
nameName := mapper.Obj2Table("Name")
sess := testEngine.Where("`"+nameName+"` = ?", "test1")
conds := sess.Conds()
err = sess.Find(&results)
assert.NoError(t, err)
Expand Down
18 changes: 11 additions & 7 deletions session_exist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,35 @@ func TestExistStruct(t *testing.T) {
assert.NoError(t, err)
assert.False(t, has)

has, err = testEngine.Where("name = ?", "test1").Exist(&RecordExist{})
nameName := "`" + mapper.Obj2Table("Name") + "`"

has, err = testEngine.Where(nameName+" = ?", "test1").Exist(&RecordExist{})
assert.NoError(t, err)
assert.True(t, has)

has, err = testEngine.Where("name = ?", "test2").Exist(&RecordExist{})
has, err = testEngine.Where(nameName+" = ?", "test2").Exist(&RecordExist{})
assert.NoError(t, err)
assert.False(t, has)

has, err = testEngine.SQL("select * from "+testEngine.TableName("record_exist", true)+" where name = ?", "test1").Exist()
tableName := "`" + mapper.Obj2Table("RecordExist") + "`"

has, err = testEngine.SQL("select * from "+testEngine.TableName(tableName, true)+" where "+nameName+" = ?", "test1").Exist()
assert.NoError(t, err)
assert.True(t, has)

has, err = testEngine.SQL("select * from "+testEngine.TableName("record_exist", true)+" where name = ?", "test2").Exist()
has, err = testEngine.SQL("select * from "+testEngine.TableName(tableName, true)+" where "+nameName+" = ?", "test2").Exist()
assert.NoError(t, err)
assert.False(t, has)

has, err = testEngine.Table("record_exist").Exist()
has, err = testEngine.Table(tableName).Exist()
assert.NoError(t, err)
assert.True(t, has)

has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist()
has, err = testEngine.Table(tableName).Where(nameName+" = ?", "test1").Exist()
assert.NoError(t, err)
assert.True(t, has)

has, err = testEngine.Table("record_exist").Where("name = ?", "test2").Exist()
has, err = testEngine.Table(tableName).Where(nameName+" = ?", "test2").Exist()
assert.NoError(t, err)
assert.False(t, has)
}
Loading