database_sql_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package test
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/pkg/errors"
  6. "math/rand"
  7. "testing"
  8. "time"
  9. )
  10. func TestDatabaseSqlConditions(t *testing.T) {
  11. conditions := sql.NewConditions().
  12. AddCondition("name1 = ?", "foo").
  13. Equal("age", 20).
  14. Like("name2", "%foo%").
  15. In("id1", []string{"111", "222"}).
  16. NotIn("id2", []string{"33", "444"}).
  17. Not("id3", "555").
  18. LessThan("num1", 10).
  19. LessThanAndEqual("num2", 20).
  20. GreaterThan("num3", 30).
  21. GreaterThanAndEqual("num4", 40)
  22. exceptQueries := []string{
  23. `name1 = ?`,
  24. `"age" = ?`,
  25. `"name2" LIKE ?`,
  26. `"id1" IN ?`,
  27. `"id2" NOT IN ?`,
  28. `"id3" != ?`,
  29. `"num1" < ?`,
  30. `"num2" <= ?`,
  31. `"num3" > ?`,
  32. `"num4" >= ?`,
  33. }
  34. exceptArgs := []any{
  35. "foo",
  36. 20,
  37. "%foo%",
  38. []string{"111", "222"},
  39. []string{"33", "444"},
  40. "555",
  41. 10,
  42. 20,
  43. 30,
  44. 40,
  45. }
  46. exceptAnd := `WHERE name1 = ? AND "age" = ? AND "name2" LIKE ? AND "id1" IN ? AND "id2" NOT IN ? AND "id3" != ? AND "num1" < ? AND "num2" <= ? AND "num3" > ? AND "num4" >= ?`
  47. for i, query := range conditions.Queries() {
  48. if exceptQueries[i] != query {
  49. t.Fatalf("%+v\n", errors.Errorf("Query Error: except %v, actural %v",
  50. exceptQueries[i], query))
  51. }
  52. }
  53. for i, arg := range conditions.Args() {
  54. argStringSlice, ok := arg.([]string)
  55. if ok {
  56. for j, stringArg := range argStringSlice {
  57. exceptArg, ok := exceptArgs[i].([]string)
  58. if !ok {
  59. t.Fatalf("%+v\n", errors.New("参数类型不匹配"))
  60. }
  61. if exceptArg[j] != stringArg {
  62. t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
  63. exceptQueries[i], stringArg))
  64. }
  65. }
  66. continue
  67. }
  68. if exceptArgs[i] != arg {
  69. t.Fatalf("%+v\n", errors.Errorf("Args Error: except %v, actural %v",
  70. exceptQueries[i], arg))
  71. }
  72. }
  73. if conditions.And() != exceptAnd {
  74. t.Fatalf("%+v\n", errors.Errorf("And Error:\nexcept %v\nactural %v\n",
  75. exceptAnd, conditions.And()))
  76. }
  77. }
  78. func TestDatabaseSqlTableRow(t *testing.T) {
  79. tableRow := sql.NewTableRow().
  80. Add("name", "test").
  81. Add("age", 20)
  82. exceptColumns := []string{
  83. `"name"`,
  84. `"age"`,
  85. }
  86. exceptValues := []any{
  87. "test",
  88. 20,
  89. }
  90. for i, column := range tableRow.Columns() {
  91. if exceptColumns[i] != column {
  92. t.Fatalf("%+v\n", errors.Errorf("Column Error: except %v, actural %v",
  93. exceptColumns[i], column))
  94. }
  95. }
  96. for i, value := range tableRow.Values() {
  97. if exceptValues[i] != value {
  98. t.Fatalf("%+v\n", errors.Errorf("Value Error: except %v, actural %v",
  99. exceptValues[i], value))
  100. }
  101. }
  102. }
  103. func TestDatabaseSqlResult(t *testing.T) {
  104. timeResult := time.Now().Local().Format("2006-01-02T15:04:05") + "+08:00"
  105. stringResult := strutils.SimpleUUID()
  106. boolResult := rand.Intn(2) == 0
  107. intResult := rand.Int()
  108. int8Result := int8(rand.Int())
  109. int16Result := int16(rand.Int())
  110. int32Result := int32(rand.Int())
  111. int64Result := int64(rand.Int())
  112. uintResult := uint(rand.Int())
  113. uint8Result := uint8(rand.Int())
  114. uint16Result := uint16(rand.Int())
  115. uint32Result := uint32(rand.Int())
  116. uint64Result := uint64(rand.Int())
  117. float32Result := rand.Float32()
  118. float64Result := rand.Float64()
  119. result := sql.Result{
  120. "time": timeResult,
  121. "string": stringResult,
  122. "bool": boolResult,
  123. "int": intResult,
  124. "int8": int8Result,
  125. "int16": int16Result,
  126. "int32": int32Result,
  127. "int64": int64Result,
  128. "uint": uintResult,
  129. "uint8": uint8Result,
  130. "uint16": uint16Result,
  131. "uint32": uint32Result,
  132. "uint64": uint64Result,
  133. "float32": float32Result,
  134. "float64": float64Result,
  135. }
  136. if result.ColumnValueStringAsTime("time").Format("2006-01-02T15:04:05")+"+08:00" != timeResult {
  137. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  138. timeResult, result.ColumnValueStringAsTime("time")))
  139. }
  140. if result.ColumnValueString("string") != stringResult {
  141. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  142. stringResult, result.ColumnValueString("string")))
  143. }
  144. if result.ColumnValueBool("bool") != boolResult {
  145. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  146. boolResult, result.ColumnValueBool("bool")))
  147. }
  148. if result.ColumnValueInt("int") != intResult {
  149. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  150. intResult, result.ColumnValueInt("int")))
  151. }
  152. if result.ColumnValueInt8("int8") != int8Result {
  153. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  154. int8Result, result.ColumnValueInt8("int8")))
  155. }
  156. if result.ColumnValueInt16("int16") != int16Result {
  157. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  158. int16Result, result.ColumnValueInt16("int16")))
  159. }
  160. if result.ColumnValueInt32("int32") != int32Result {
  161. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  162. int32Result, result.ColumnValueInt32("int32")))
  163. }
  164. if result.ColumnValueInt64("int64") != int64Result {
  165. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  166. int64Result, result.ColumnValueInt64("int64")))
  167. }
  168. if result.ColumnValueUint("uint") != uintResult {
  169. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  170. uintResult, result.ColumnValueUint("uint")))
  171. }
  172. if result.ColumnValueUint8("uint8") != uint8Result {
  173. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  174. uint8Result, result.ColumnValueUint8("uint8")))
  175. }
  176. if result.ColumnValueUint16("uint16") != uint16Result {
  177. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  178. uint16Result, result.ColumnValueUint16("uint16")))
  179. }
  180. if result.ColumnValueUint32("uint32") != uint32Result {
  181. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  182. uint32Result, result.ColumnValueUint32("uint32")))
  183. }
  184. if result.ColumnValueUint64("uint64") != uint64Result {
  185. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  186. uint64Result, result.ColumnValueUint64("uint64")))
  187. }
  188. if result.ColumnValueFloat32("float32") != float32Result {
  189. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  190. float32Result, result.ColumnValueFloat32("float32")))
  191. }
  192. if result.ColumnValueFloat64("float64") != float64Result {
  193. t.Fatalf("%+v\n", errors.Errorf("Result Error: except %v, actural %v",
  194. float64Result, result.ColumnValueFloat64("float64")))
  195. }
  196. }