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.

348 lines
8.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. "fmt"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestStoreEngine(t *testing.T) {
  13. assert.NoError(t, prepareEngine())
  14. assert.NoError(t, testEngine.DropTables("user_store_engine"))
  15. type UserinfoStoreEngine struct {
  16. Id int64
  17. Name string
  18. }
  19. assert.NoError(t, testEngine.StoreEngine("InnoDB").Table("user_store_engine").CreateTable(&UserinfoStoreEngine{}))
  20. }
  21. func TestCreateTable(t *testing.T) {
  22. assert.NoError(t, prepareEngine())
  23. assert.NoError(t, testEngine.DropTables("user_user"))
  24. type UserinfoCreateTable struct {
  25. Id int64
  26. Name string
  27. }
  28. assert.NoError(t, testEngine.Table("user_user").CreateTable(&UserinfoCreateTable{}))
  29. }
  30. func TestCreateMultiTables(t *testing.T) {
  31. assert.NoError(t, prepareEngine())
  32. session := testEngine.NewSession()
  33. defer session.Close()
  34. type UserinfoMultiTable struct {
  35. Id int64
  36. Name string
  37. }
  38. user := &UserinfoMultiTable{}
  39. assert.NoError(t, session.Begin())
  40. for i := 0; i < 10; i++ {
  41. tableName := fmt.Sprintf("user_%v", i)
  42. assert.NoError(t, session.DropTable(tableName))
  43. assert.NoError(t, session.Table(tableName).CreateTable(user))
  44. }
  45. assert.NoError(t, session.Commit())
  46. }
  47. type SyncTable1 struct {
  48. Id int64
  49. Name string
  50. Dev int `xorm:"index"`
  51. }
  52. type SyncTable2 struct {
  53. Id int64
  54. Name string `xorm:"unique"`
  55. Number string `xorm:"index"`
  56. Dev int
  57. Age int
  58. }
  59. func (SyncTable2) TableName() string {
  60. return "sync_table1"
  61. }
  62. type SyncTable3 struct {
  63. Id int64
  64. Name string `xorm:"unique"`
  65. Number string `xorm:"index"`
  66. Dev int
  67. Age int
  68. }
  69. func (s *SyncTable3) TableName() string {
  70. return "sync_table1"
  71. }
  72. func TestSyncTable(t *testing.T) {
  73. assert.NoError(t, prepareEngine())
  74. assert.NoError(t, testEngine.Sync2(new(SyncTable1)))
  75. tables, err := testEngine.DBMetas()
  76. assert.NoError(t, err)
  77. assert.EqualValues(t, 1, len(tables))
  78. assert.EqualValues(t, "sync_table1", tables[0].Name)
  79. assert.NoError(t, testEngine.Sync2(new(SyncTable2)))
  80. tables, err = testEngine.DBMetas()
  81. assert.NoError(t, err)
  82. assert.EqualValues(t, 1, len(tables))
  83. assert.EqualValues(t, "sync_table1", tables[0].Name)
  84. assert.NoError(t, testEngine.Sync2(new(SyncTable3)))
  85. tables, err = testEngine.DBMetas()
  86. assert.NoError(t, err)
  87. assert.EqualValues(t, 1, len(tables))
  88. assert.EqualValues(t, "sync_table1", tables[0].Name)
  89. }
  90. func TestSyncTable2(t *testing.T) {
  91. assert.NoError(t, prepareEngine())
  92. assert.NoError(t, testEngine.Table("sync_tablex").Sync2(new(SyncTable1)))
  93. tables, err := testEngine.DBMetas()
  94. assert.NoError(t, err)
  95. assert.EqualValues(t, 1, len(tables))
  96. assert.EqualValues(t, "sync_tablex", tables[0].Name)
  97. assert.EqualValues(t, 3, len(tables[0].Columns()))
  98. type SyncTable4 struct {
  99. SyncTable1 `xorm:"extends"`
  100. NewCol string
  101. }
  102. assert.NoError(t, testEngine.Table("sync_tablex").Sync2(new(SyncTable4)))
  103. tables, err = testEngine.DBMetas()
  104. assert.NoError(t, err)
  105. assert.EqualValues(t, 1, len(tables))
  106. assert.EqualValues(t, "sync_tablex", tables[0].Name)
  107. assert.EqualValues(t, 4, len(tables[0].Columns()))
  108. assert.EqualValues(t, colMapper.Obj2Table("NewCol"), tables[0].Columns()[3].Name)
  109. }
  110. func TestIsTableExist(t *testing.T) {
  111. assert.NoError(t, prepareEngine())
  112. exist, err := testEngine.IsTableExist(new(CustomTableName))
  113. assert.NoError(t, err)
  114. assert.False(t, exist)
  115. assert.NoError(t, testEngine.CreateTables(new(CustomTableName)))
  116. exist, err = testEngine.IsTableExist(new(CustomTableName))
  117. assert.NoError(t, err)
  118. assert.True(t, exist)
  119. }
  120. func TestIsTableEmpty(t *testing.T) {
  121. assert.NoError(t, prepareEngine())
  122. type NumericEmpty struct {
  123. Numeric float64 `xorm:"numeric(26,2)"`
  124. }
  125. type PictureEmpty struct {
  126. Id int64
  127. Url string `xorm:"unique"` //image's url
  128. Title string
  129. Description string
  130. Created time.Time `xorm:"created"`
  131. ILike int
  132. PageView int
  133. From_url string
  134. Pre_url string `xorm:"unique"` //pre view image's url
  135. Uid int64
  136. }
  137. assert.NoError(t, testEngine.DropTables(&PictureEmpty{}, &NumericEmpty{}))
  138. assert.NoError(t, testEngine.Sync2(new(PictureEmpty), new(NumericEmpty)))
  139. isEmpty, err := testEngine.IsTableEmpty(&PictureEmpty{})
  140. assert.NoError(t, err)
  141. assert.True(t, isEmpty)
  142. tbName := testEngine.GetTableMapper().Obj2Table("PictureEmpty")
  143. isEmpty, err = testEngine.IsTableEmpty(tbName)
  144. assert.NoError(t, err)
  145. assert.True(t, isEmpty)
  146. }
  147. type CustomTableName struct {
  148. Id int64
  149. Name string
  150. }
  151. func (c *CustomTableName) TableName() string {
  152. return "customtablename"
  153. }
  154. func TestCustomTableName(t *testing.T) {
  155. assert.NoError(t, prepareEngine())
  156. c := new(CustomTableName)
  157. assert.NoError(t, testEngine.DropTables(c))
  158. assert.NoError(t, testEngine.CreateTables(c))
  159. }
  160. func TestDump(t *testing.T) {
  161. assert.NoError(t, prepareEngine())
  162. fp := testEngine.Dialect().URI().DbName + ".sql"
  163. os.Remove(fp)
  164. assert.NoError(t, testEngine.DumpAllToFile(fp))
  165. }
  166. type IndexOrUnique struct {
  167. Id int64
  168. Index int `xorm:"index"`
  169. Unique int `xorm:"unique"`
  170. Group1 int `xorm:"index(ttt)"`
  171. Group2 int `xorm:"index(ttt)"`
  172. UniGroup1 int `xorm:"unique(lll)"`
  173. UniGroup2 int `xorm:"unique(lll)"`
  174. }
  175. func TestIndexAndUnique(t *testing.T) {
  176. assert.NoError(t, prepareEngine())
  177. assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
  178. assert.NoError(t, testEngine.DropTables(&IndexOrUnique{}))
  179. assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
  180. assert.NoError(t, testEngine.CreateIndexes(&IndexOrUnique{}))
  181. assert.NoError(t, testEngine.CreateUniques(&IndexOrUnique{}))
  182. assert.NoError(t, testEngine.DropIndexes(&IndexOrUnique{}))
  183. }
  184. func TestMetaInfo(t *testing.T) {
  185. assert.NoError(t, prepareEngine())
  186. assert.NoError(t, testEngine.Sync2(new(CustomTableName), new(IndexOrUnique)))
  187. tables, err := testEngine.DBMetas()
  188. assert.NoError(t, err)
  189. assert.EqualValues(t, 2, len(tables))
  190. tableNames := []string{tables[0].Name, tables[1].Name}
  191. assert.Contains(t, tableNames, "customtablename")
  192. assert.Contains(t, tableNames, "index_or_unique")
  193. }
  194. func TestCharst(t *testing.T) {
  195. assert.NoError(t, prepareEngine())
  196. err := testEngine.DropTables("user_charset")
  197. if err != nil {
  198. t.Error(err)
  199. panic(err)
  200. }
  201. err = testEngine.Charset("utf8").Table("user_charset").CreateTable(&Userinfo{})
  202. if err != nil {
  203. t.Error(err)
  204. panic(err)
  205. }
  206. }
  207. func TestSync2_1(t *testing.T) {
  208. type WxTest struct {
  209. Id int `xorm:"not null pk autoincr INT(64)"`
  210. Passport_user_type int16 `xorm:"null int"`
  211. Id_delete int8 `xorm:"null int default 1"`
  212. }
  213. assert.NoError(t, prepareEngine())
  214. assert.NoError(t, testEngine.DropTables("wx_test"))
  215. assert.NoError(t, testEngine.Sync2(new(WxTest)))
  216. assert.NoError(t, testEngine.Sync2(new(WxTest)))
  217. }
  218. func TestUnique_1(t *testing.T) {
  219. type UserUnique struct {
  220. Id int64
  221. UserName string `xorm:"unique varchar(25) not null"`
  222. Password string `xorm:"varchar(255) not null"`
  223. Admin bool `xorm:"not null"`
  224. CreatedAt time.Time `xorm:"created"`
  225. UpdatedAt time.Time `xorm:"updated"`
  226. }
  227. assert.NoError(t, prepareEngine())
  228. assert.NoError(t, testEngine.DropTables("user_unique"))
  229. assert.NoError(t, testEngine.Sync2(new(UserUnique)))
  230. assert.NoError(t, testEngine.DropTables("user_unique"))
  231. assert.NoError(t, testEngine.CreateTables(new(UserUnique)))
  232. assert.NoError(t, testEngine.CreateUniques(new(UserUnique)))
  233. }
  234. func TestSync2_2(t *testing.T) {
  235. type TestSync2Index struct {
  236. Id int64
  237. UserId int64 `xorm:"index"`
  238. }
  239. assert.NoError(t, prepareEngine())
  240. var tableNames = make(map[string]bool)
  241. for i := 0; i < 10; i++ {
  242. tableName := fmt.Sprintf("test_sync2_index_%d", i)
  243. tableNames[tableName] = true
  244. assert.NoError(t, testEngine.Table(tableName).Sync2(new(TestSync2Index)))
  245. exist, err := testEngine.IsTableExist(tableName)
  246. assert.NoError(t, err)
  247. assert.True(t, exist)
  248. }
  249. tables, err := testEngine.DBMetas()
  250. assert.NoError(t, err)
  251. for _, table := range tables {
  252. assert.True(t, tableNames[table.Name])
  253. }
  254. }
  255. func TestSync2_Default(t *testing.T) {
  256. type TestSync2Default struct {
  257. Id int64
  258. UserId int64 `xorm:"default(1)"`
  259. IsMember bool `xorm:"default(true)"`
  260. Name string `xorm:"default('my_name')"`
  261. }
  262. assert.NoError(t, prepareEngine())
  263. assertSync(t, new(TestSync2Default))
  264. assert.NoError(t, testEngine.Sync2(new(TestSync2Default)))
  265. }