Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.6 KiB

  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. "fmt"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/go-xorm/builder"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestQueryString(t *testing.T) {
  14. assert.NoError(t, prepareEngine())
  15. type GetVar2 struct {
  16. Id int64 `xorm:"autoincr pk"`
  17. Msg string `xorm:"varchar(255)"`
  18. Age int
  19. Money float32
  20. Created time.Time `xorm:"created"`
  21. }
  22. assert.NoError(t, testEngine.Sync2(new(GetVar2)))
  23. var data = GetVar2{
  24. Msg: "hi",
  25. Age: 28,
  26. Money: 1.5,
  27. }
  28. _, err := testEngine.InsertOne(data)
  29. assert.NoError(t, err)
  30. records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var2", true))
  31. assert.NoError(t, err)
  32. assert.Equal(t, 1, len(records))
  33. assert.Equal(t, 5, len(records[0]))
  34. assert.Equal(t, "1", records[0]["id"])
  35. assert.Equal(t, "hi", records[0]["msg"])
  36. assert.Equal(t, "28", records[0]["age"])
  37. assert.Equal(t, "1.5", records[0]["money"])
  38. }
  39. func TestQueryString2(t *testing.T) {
  40. assert.NoError(t, prepareEngine())
  41. type GetVar3 struct {
  42. Id int64 `xorm:"autoincr pk"`
  43. Msg bool `xorm:"bit"`
  44. }
  45. assert.NoError(t, testEngine.Sync2(new(GetVar3)))
  46. var data = GetVar3{
  47. Msg: false,
  48. }
  49. _, err := testEngine.Insert(data)
  50. assert.NoError(t, err)
  51. records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var3", true))
  52. assert.NoError(t, err)
  53. assert.Equal(t, 1, len(records))
  54. assert.Equal(t, 2, len(records[0]))
  55. assert.Equal(t, "1", records[0]["id"])
  56. assert.True(t, "0" == records[0]["msg"] || "false" == records[0]["msg"])
  57. }
  58. func toString(i interface{}) string {
  59. switch i.(type) {
  60. case []byte:
  61. return string(i.([]byte))
  62. case string:
  63. return i.(string)
  64. }
  65. return fmt.Sprintf("%v", i)
  66. }
  67. func toInt64(i interface{}) int64 {
  68. switch i.(type) {
  69. case []byte:
  70. n, _ := strconv.ParseInt(string(i.([]byte)), 10, 64)
  71. return n
  72. case int:
  73. return int64(i.(int))
  74. case int64:
  75. return i.(int64)
  76. }
  77. return 0
  78. }
  79. func toFloat64(i interface{}) float64 {
  80. switch i.(type) {
  81. case []byte:
  82. n, _ := strconv.ParseFloat(string(i.([]byte)), 64)
  83. return n
  84. case float64:
  85. return i.(float64)
  86. case float32:
  87. return float64(i.(float32))
  88. }
  89. return 0
  90. }
  91. func TestQueryInterface(t *testing.T) {
  92. assert.NoError(t, prepareEngine())
  93. type GetVarInterface struct {
  94. Id int64 `xorm:"autoincr pk"`
  95. Msg string `xorm:"varchar(255)"`
  96. Age int
  97. Money float32
  98. Created time.Time `xorm:"created"`
  99. }
  100. assert.NoError(t, testEngine.Sync2(new(GetVarInterface)))
  101. var data = GetVarInterface{
  102. Msg: "hi",
  103. Age: 28,
  104. Money: 1.5,
  105. }
  106. _, err := testEngine.InsertOne(data)
  107. assert.NoError(t, err)
  108. records, err := testEngine.QueryInterface("select * from " + testEngine.TableName("get_var_interface", true))
  109. assert.NoError(t, err)
  110. assert.Equal(t, 1, len(records))
  111. assert.Equal(t, 5, len(records[0]))
  112. assert.EqualValues(t, 1, toInt64(records[0]["id"]))
  113. assert.Equal(t, "hi", toString(records[0]["msg"]))
  114. assert.EqualValues(t, 28, toInt64(records[0]["age"]))
  115. assert.EqualValues(t, 1.5, toFloat64(records[0]["money"]))
  116. }
  117. func TestQueryNoParams(t *testing.T) {
  118. assert.NoError(t, prepareEngine())
  119. type QueryNoParams struct {
  120. Id int64 `xorm:"autoincr pk"`
  121. Msg string `xorm:"varchar(255)"`
  122. Age int
  123. Money float32
  124. Created time.Time `xorm:"created"`
  125. }
  126. testEngine.ShowSQL(true)
  127. assert.NoError(t, testEngine.Sync2(new(QueryNoParams)))
  128. var q = QueryNoParams{
  129. Msg: "message",
  130. Age: 20,
  131. Money: 3000,
  132. }
  133. cnt, err := testEngine.Insert(&q)
  134. assert.NoError(t, err)
  135. assert.EqualValues(t, 1, cnt)
  136. assertResult := func(t *testing.T, results []map[string][]byte) {
  137. assert.EqualValues(t, 1, len(results))
  138. id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
  139. assert.NoError(t, err)
  140. assert.EqualValues(t, 1, id)
  141. assert.Equal(t, "message", string(results[0]["msg"]))
  142. age, err := strconv.Atoi(string(results[0]["age"]))
  143. assert.NoError(t, err)
  144. assert.EqualValues(t, 20, age)
  145. money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
  146. assert.NoError(t, err)
  147. assert.EqualValues(t, 3000, money)
  148. }
  149. results, err := testEngine.Table("query_no_params").Limit(10).Query()
  150. assert.NoError(t, err)
  151. assertResult(t, results)
  152. results, err = testEngine.SQL("select * from " + testEngine.TableName("query_no_params", true)).Query()
  153. assert.NoError(t, err)
  154. assertResult(t, results)
  155. }
  156. func TestQueryWithBuilder(t *testing.T) {
  157. assert.NoError(t, prepareEngine())
  158. type QueryWithBuilder struct {
  159. Id int64 `xorm:"autoincr pk"`
  160. Msg string `xorm:"varchar(255)"`
  161. Age int
  162. Money float32
  163. Created time.Time `xorm:"created"`
  164. }
  165. testEngine.ShowSQL(true)
  166. assert.NoError(t, testEngine.Sync2(new(QueryWithBuilder)))
  167. var q = QueryWithBuilder{
  168. Msg: "message",
  169. Age: 20,
  170. Money: 3000,
  171. }
  172. cnt, err := testEngine.Insert(&q)
  173. assert.NoError(t, err)
  174. assert.EqualValues(t, 1, cnt)
  175. assertResult := func(t *testing.T, results []map[string][]byte) {
  176. assert.EqualValues(t, 1, len(results))
  177. id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
  178. assert.NoError(t, err)
  179. assert.EqualValues(t, 1, id)
  180. assert.Equal(t, "message", string(results[0]["msg"]))
  181. age, err := strconv.Atoi(string(results[0]["age"]))
  182. assert.NoError(t, err)
  183. assert.EqualValues(t, 20, age)
  184. money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
  185. assert.NoError(t, err)
  186. assert.EqualValues(t, 3000, money)
  187. }
  188. results, err := testEngine.Query(builder.Select("*").From(testEngine.TableName("query_with_builder", true)))
  189. assert.NoError(t, err)
  190. assertResult(t, results)
  191. }