session_cond_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/xormplus/builder"
  11. )
  12. func TestBuilder(t *testing.T) {
  13. assert.NoError(t, prepareEngine())
  14. const (
  15. OpEqual int = iota
  16. OpGreatThan
  17. OpLessThan
  18. )
  19. type Condition struct {
  20. Id int64
  21. TableName string
  22. ColName string
  23. Op int
  24. Value string
  25. }
  26. err := testEngine.CreateTables(&Condition{})
  27. assert.NoError(t, err)
  28. _, err = testEngine.Insert(&Condition{TableName: "table1", ColName: "col1", Op: OpEqual, Value: "1"})
  29. assert.NoError(t, err)
  30. var cond Condition
  31. has, err := testEngine.Where(builder.Eq{"col_name": "col1"}).Get(&cond)
  32. assert.NoError(t, err)
  33. assert.Equal(t, true, has, "records should exist")
  34. has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
  35. And(builder.Eq{"op": OpEqual})).
  36. NoAutoCondition().
  37. Get(&cond)
  38. assert.NoError(t, err)
  39. assert.Equal(t, true, has, "records should exist")
  40. has, err = testEngine.Where(builder.Eq{"col_name": "col1", "op": OpEqual, "value": "1"}).
  41. NoAutoCondition().
  42. Get(&cond)
  43. assert.NoError(t, err)
  44. assert.Equal(t, true, has, "records should exist")
  45. has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
  46. And(builder.Neq{"op": OpEqual})).
  47. NoAutoCondition().
  48. Get(&cond)
  49. assert.NoError(t, err)
  50. assert.Equal(t, false, has, "records should not exist")
  51. var conds []Condition
  52. err = testEngine.Where(builder.Eq{"col_name": "col1"}.
  53. And(builder.Eq{"op": OpEqual})).
  54. Find(&conds)
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, 1, len(conds), "records should exist")
  57. conds = make([]Condition, 0)
  58. err = testEngine.Where(builder.Like{"col_name", "col"}).Find(&conds)
  59. assert.NoError(t, err)
  60. assert.EqualValues(t, 1, len(conds), "records should exist")
  61. conds = make([]Condition, 0)
  62. err = testEngine.Where(builder.Expr("col_name = ?", "col1")).Find(&conds)
  63. assert.NoError(t, err)
  64. assert.EqualValues(t, 1, len(conds), "records should exist")
  65. conds = make([]Condition, 0)
  66. err = testEngine.Where(builder.In("col_name", "col1", "col2")).Find(&conds)
  67. assert.NoError(t, err)
  68. assert.EqualValues(t, 1, len(conds), "records should exist")
  69. conds = make([]Condition, 0)
  70. err = testEngine.NotIn("col_name", "col1", "col2").Find(&conds)
  71. assert.NoError(t, err)
  72. assert.EqualValues(t, 0, len(conds), "records should not exist")
  73. // complex condtions
  74. var where = builder.NewCond()
  75. if true {
  76. where = where.And(builder.Eq{"col_name": "col1"})
  77. where = where.Or(builder.And(builder.In("col_name", "col1", "col2"), builder.Expr("col_name = ?", "col1")))
  78. }
  79. conds = make([]Condition, 0)
  80. err = testEngine.Where(where).Find(&conds)
  81. assert.NoError(t, err)
  82. assert.EqualValues(t, 1, len(conds), "records should exist")
  83. }
  84. func TestIn(t *testing.T) {
  85. assert.NoError(t, prepareEngine())
  86. assert.NoError(t, testEngine.Sync2(new(Userinfo)))
  87. cnt, err := testEngine.Insert([]Userinfo{
  88. {
  89. Username: "user1",
  90. Departname: "dev",
  91. },
  92. {
  93. Username: "user2",
  94. Departname: "dev",
  95. },
  96. {
  97. Username: "user3",
  98. Departname: "dev",
  99. },
  100. })
  101. assert.NoError(t, err)
  102. assert.EqualValues(t, 3, cnt)
  103. department := "`" + testEngine.GetColumnMapper().Obj2Table("Departname") + "`"
  104. var usrs []Userinfo
  105. err = testEngine.Where(department+" = ?", "dev").Limit(3).Find(&usrs)
  106. assert.NoError(t, err)
  107. assert.EqualValues(t, 3, len(usrs))
  108. var ids []int64
  109. var idsStr string
  110. for _, u := range usrs {
  111. ids = append(ids, u.Uid)
  112. idsStr = fmt.Sprintf("%d,", u.Uid)
  113. }
  114. idsStr = idsStr[:len(idsStr)-1]
  115. users := make([]Userinfo, 0)
  116. err = testEngine.In("(id)", ids[0], ids[1], ids[2]).Find(&users)
  117. assert.NoError(t, err)
  118. fmt.Println(users)
  119. assert.EqualValues(t, 3, len(users))
  120. users = make([]Userinfo, 0)
  121. err = testEngine.In("(id)", ids).Find(&users)
  122. assert.NoError(t, err)
  123. fmt.Println(users)
  124. assert.EqualValues(t, 3, len(users))
  125. for _, user := range users {
  126. if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
  127. err = errors.New("in uses should be " + idsStr + " total 3")
  128. assert.NoError(t, err)
  129. }
  130. }
  131. users = make([]Userinfo, 0)
  132. var idsInterface []interface{}
  133. for _, id := range ids {
  134. idsInterface = append(idsInterface, id)
  135. }
  136. err = testEngine.Where(department+" = ?", "dev").In("(id)", idsInterface...).Find(&users)
  137. assert.NoError(t, err)
  138. fmt.Println(users)
  139. assert.EqualValues(t, 3, len(users))
  140. for _, user := range users {
  141. if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
  142. err = errors.New("in uses should be " + idsStr + " total 3")
  143. assert.NoError(t, err)
  144. }
  145. }
  146. dev := testEngine.GetColumnMapper().Obj2Table("Dev")
  147. err = testEngine.In("(id)", 1).In("(id)", 2).In(department, dev).Find(&users)
  148. assert.NoError(t, err)
  149. fmt.Println(users)
  150. cnt, err = testEngine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev-"})
  151. assert.NoError(t, err)
  152. assert.EqualValues(t, 1, cnt)
  153. user := new(Userinfo)
  154. has, err := testEngine.ID(ids[0]).Get(user)
  155. assert.NoError(t, err)
  156. assert.True(t, has)
  157. assert.EqualValues(t, "dev-", user.Departname)
  158. cnt, err = testEngine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev"})
  159. assert.NoError(t, err)
  160. assert.EqualValues(t, 1, cnt)
  161. cnt, err = testEngine.In("(id)", ids[1]).Delete(&Userinfo{})
  162. assert.NoError(t, err)
  163. assert.EqualValues(t, 1, cnt)
  164. }
  165. func TestFindAndCount(t *testing.T) {
  166. assert.NoError(t, prepareEngine())
  167. type FindAndCount struct {
  168. Id int64
  169. Name string
  170. }
  171. assert.NoError(t, testEngine.Sync2(new(FindAndCount)))
  172. _, err := testEngine.Insert([]FindAndCount{
  173. {
  174. Name: "test1",
  175. },
  176. {
  177. Name: "test2",
  178. },
  179. })
  180. assert.NoError(t, err)
  181. var results []FindAndCount
  182. sess := testEngine.Where("name = ?", "test1")
  183. conds := sess.Conds()
  184. err = sess.Find(&results)
  185. assert.NoError(t, err)
  186. assert.EqualValues(t, 1, len(results))
  187. total, err := testEngine.Where(conds).Count(new(FindAndCount))
  188. assert.NoError(t, err)
  189. assert.EqualValues(t, 1, total)
  190. }