THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

344 lines
7.4KB

  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. "xorm.io/core"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestArrayField(t *testing.T) {
  13. assert.NoError(t, prepareEngine())
  14. type ArrayStruct struct {
  15. Id int64
  16. Name [20]byte `xorm:"char(80)"`
  17. }
  18. assert.NoError(t, testEngine.Sync2(new(ArrayStruct)))
  19. var as = ArrayStruct{
  20. Name: [20]byte{
  21. 96, 96, 96, 96, 96,
  22. 96, 96, 96, 96, 96,
  23. 96, 96, 96, 96, 96,
  24. 96, 96, 96, 96, 96,
  25. },
  26. }
  27. cnt, err := testEngine.Insert(&as)
  28. assert.NoError(t, err)
  29. assert.EqualValues(t, 1, cnt)
  30. var arr ArrayStruct
  31. has, err := testEngine.ID(1).Get(&arr)
  32. assert.NoError(t, err)
  33. assert.Equal(t, true, has)
  34. assert.Equal(t, as.Name, arr.Name)
  35. var arrs []ArrayStruct
  36. err = testEngine.Find(&arrs)
  37. assert.NoError(t, err)
  38. assert.EqualValues(t, 1, len(arrs))
  39. assert.Equal(t, as.Name, arrs[0].Name)
  40. var newName = [20]byte{
  41. 90, 96, 96, 96, 96,
  42. 96, 96, 96, 96, 96,
  43. 96, 96, 96, 96, 96,
  44. 96, 96, 96, 96, 96,
  45. }
  46. cnt, err = testEngine.ID(1).Update(&ArrayStruct{
  47. Name: newName,
  48. })
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, 1, cnt)
  51. var newArr ArrayStruct
  52. has, err = testEngine.ID(1).Get(&newArr)
  53. assert.NoError(t, err)
  54. assert.Equal(t, true, has)
  55. assert.Equal(t, newName, newArr.Name)
  56. cnt, err = testEngine.ID(1).Delete(new(ArrayStruct))
  57. assert.NoError(t, err)
  58. assert.EqualValues(t, 1, cnt)
  59. var cfgArr ArrayStruct
  60. has, err = testEngine.ID(1).Get(&cfgArr)
  61. assert.NoError(t, err)
  62. assert.Equal(t, false, has)
  63. }
  64. func TestGetBytes(t *testing.T) {
  65. assert.NoError(t, prepareEngine())
  66. type Varbinary struct {
  67. Data []byte `xorm:"VARBINARY(250)"`
  68. }
  69. err := testEngine.Sync2(new(Varbinary))
  70. assert.NoError(t, err)
  71. cnt, err := testEngine.Insert(&Varbinary{
  72. Data: []byte("test"),
  73. })
  74. assert.NoError(t, err)
  75. assert.EqualValues(t, 1, cnt)
  76. var b Varbinary
  77. has, err := testEngine.Get(&b)
  78. assert.NoError(t, err)
  79. assert.Equal(t, true, has)
  80. assert.Equal(t, "test", string(b.Data))
  81. }
  82. type ConvString string
  83. func (s *ConvString) FromDB(data []byte) error {
  84. *s = ConvString("prefix---" + string(data))
  85. return nil
  86. }
  87. func (s *ConvString) ToDB() ([]byte, error) {
  88. return []byte(string(*s)), nil
  89. }
  90. type ConvConfig struct {
  91. Name string
  92. Id int64
  93. }
  94. func (s *ConvConfig) FromDB(data []byte) error {
  95. return DefaultJSONHandler.Unmarshal(data, s)
  96. }
  97. func (s *ConvConfig) ToDB() ([]byte, error) {
  98. return DefaultJSONHandler.Marshal(s)
  99. }
  100. type SliceType []*ConvConfig
  101. func (s *SliceType) FromDB(data []byte) error {
  102. return DefaultJSONHandler.Unmarshal(data, s)
  103. }
  104. func (s *SliceType) ToDB() ([]byte, error) {
  105. return DefaultJSONHandler.Marshal(s)
  106. }
  107. type ConvStruct struct {
  108. Conv ConvString
  109. Conv2 *ConvString
  110. Cfg1 ConvConfig
  111. Cfg2 *ConvConfig `xorm:"TEXT"`
  112. Cfg3 core.Conversion `xorm:"BLOB"`
  113. Slice SliceType
  114. }
  115. func (c *ConvStruct) BeforeSet(name string, cell Cell) {
  116. if name == "cfg3" || name == "Cfg3" {
  117. c.Cfg3 = new(ConvConfig)
  118. }
  119. }
  120. func TestConversion(t *testing.T) {
  121. assert.NoError(t, prepareEngine())
  122. c := new(ConvStruct)
  123. assert.NoError(t, testEngine.DropTables(c))
  124. assert.NoError(t, testEngine.Sync2(c))
  125. var s ConvString = "sssss"
  126. c.Conv = "tttt"
  127. c.Conv2 = &s
  128. c.Cfg1 = ConvConfig{"mm", 1}
  129. c.Cfg2 = &ConvConfig{"xx", 2}
  130. c.Cfg3 = &ConvConfig{"zz", 3}
  131. c.Slice = []*ConvConfig{{"yy", 4}, {"ff", 5}}
  132. _, err := testEngine.Insert(c)
  133. assert.NoError(t, err)
  134. c1 := new(ConvStruct)
  135. has, err := testEngine.Get(c1)
  136. assert.NoError(t, err)
  137. assert.True(t, has)
  138. assert.EqualValues(t, "prefix---tttt", string(c1.Conv))
  139. assert.NotNil(t, c1.Conv2)
  140. assert.EqualValues(t, "prefix---"+s, *c1.Conv2)
  141. assert.EqualValues(t, c.Cfg1, c1.Cfg1)
  142. assert.NotNil(t, c1.Cfg2)
  143. assert.EqualValues(t, *c.Cfg2, *c1.Cfg2)
  144. assert.NotNil(t, c1.Cfg3)
  145. assert.EqualValues(t, *c.Cfg3.(*ConvConfig), *c1.Cfg3.(*ConvConfig))
  146. assert.EqualValues(t, 2, len(c1.Slice))
  147. assert.EqualValues(t, *c.Slice[0], *c1.Slice[0])
  148. assert.EqualValues(t, *c.Slice[1], *c1.Slice[1])
  149. }
  150. type MyInt int
  151. type MyUInt uint
  152. type MyFloat float64
  153. type MyStruct struct {
  154. Type MyInt
  155. U MyUInt
  156. F MyFloat
  157. S MyString
  158. IA []MyInt
  159. UA []MyUInt
  160. FA []MyFloat
  161. SA []MyString
  162. NameArray []string
  163. Name string
  164. UIA []uint
  165. UIA8 []uint8
  166. UIA16 []uint16
  167. UIA32 []uint32
  168. UIA64 []uint64
  169. UI uint
  170. //C64 complex64
  171. MSS map[string]string
  172. }
  173. func TestCustomType1(t *testing.T) {
  174. assert.NoError(t, prepareEngine())
  175. err := testEngine.DropTables(&MyStruct{})
  176. assert.NoError(t, err)
  177. err = testEngine.CreateTables(&MyStruct{})
  178. assert.NoError(t, err)
  179. i := MyStruct{Name: "Test", Type: MyInt(1)}
  180. i.U = 23
  181. i.F = 1.34
  182. i.S = "fafdsafdsaf"
  183. i.UI = 2
  184. i.IA = []MyInt{1, 3, 5}
  185. i.UIA = []uint{1, 3}
  186. i.UIA16 = []uint16{2}
  187. i.UIA32 = []uint32{4, 5}
  188. i.UIA64 = []uint64{6, 7, 9}
  189. i.UIA8 = []uint8{1, 2, 3, 4}
  190. i.NameArray = []string{"ssss", "fsdf", "lllll, ss"}
  191. i.MSS = map[string]string{"s": "sfds,ss", "x": "lfjljsl"}
  192. cnt, err := testEngine.Insert(&i)
  193. assert.NoError(t, err)
  194. assert.EqualValues(t, 1, cnt)
  195. fmt.Println(i)
  196. i.NameArray = []string{}
  197. i.MSS = map[string]string{}
  198. i.F = 0
  199. has, err := testEngine.Get(&i)
  200. assert.NoError(t, err)
  201. assert.True(t, has)
  202. ss := []MyStruct{}
  203. err = testEngine.Find(&ss)
  204. assert.NoError(t, err)
  205. assert.EqualValues(t, 1, len(ss))
  206. assert.EqualValues(t, i, ss[0])
  207. sss := MyStruct{}
  208. has, err = testEngine.Get(&sss)
  209. assert.NoError(t, err)
  210. assert.True(t, has)
  211. sss.NameArray = []string{}
  212. sss.MSS = map[string]string{}
  213. cnt, err = testEngine.Delete(&sss)
  214. assert.NoError(t, err)
  215. assert.EqualValues(t, 1, cnt)
  216. }
  217. type Status struct {
  218. Name string
  219. Color string
  220. }
  221. var (
  222. _ core.Conversion = &Status{}
  223. Registed Status = Status{"Registed", "white"}
  224. Approved Status = Status{"Approved", "green"}
  225. Removed Status = Status{"Removed", "red"}
  226. Statuses map[string]Status = map[string]Status{
  227. Registed.Name: Registed,
  228. Approved.Name: Approved,
  229. Removed.Name: Removed,
  230. }
  231. )
  232. func (s *Status) FromDB(bytes []byte) error {
  233. if r, ok := Statuses[string(bytes)]; ok {
  234. *s = r
  235. return nil
  236. } else {
  237. return errors.New("no this data")
  238. }
  239. }
  240. func (s *Status) ToDB() ([]byte, error) {
  241. return []byte(s.Name), nil
  242. }
  243. type UserCus struct {
  244. Id int64
  245. Name string
  246. Status Status `xorm:"varchar(40)"`
  247. }
  248. func TestCustomType2(t *testing.T) {
  249. assert.NoError(t, prepareEngine())
  250. var uc UserCus
  251. err := testEngine.CreateTables(&uc)
  252. assert.NoError(t, err)
  253. tableName := testEngine.TableName(&uc, true)
  254. _, err = testEngine.Exec("delete from " + testEngine.Quote(tableName))
  255. assert.NoError(t, err)
  256. session := testEngine.NewSession()
  257. defer session.Close()
  258. if testEngine.Dialect().DBType() == core.MSSQL {
  259. err = session.Begin()
  260. assert.NoError(t, err)
  261. _, err = session.Exec("set IDENTITY_INSERT " + tableName + " on")
  262. assert.NoError(t, err)
  263. }
  264. cnt, err := session.Insert(&UserCus{1, "xlw", Registed})
  265. assert.NoError(t, err)
  266. assert.EqualValues(t, 1, cnt)
  267. if testEngine.Dialect().DBType() == core.MSSQL {
  268. err = session.Commit()
  269. assert.NoError(t, err)
  270. }
  271. user := UserCus{}
  272. exist, err := testEngine.ID(1).Get(&user)
  273. assert.NoError(t, err)
  274. assert.True(t, exist)
  275. fmt.Println(user)
  276. users := make([]UserCus, 0)
  277. err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Status")+"` = ?", "Registed").Find(&users)
  278. assert.NoError(t, err)
  279. assert.EqualValues(t, 1, len(users))
  280. fmt.Println(users)
  281. }