session_schema_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 TestIsTableExist(t *testing.T) {
  91. assert.NoError(t, prepareEngine())
  92. exist, err := testEngine.IsTableExist(new(CustomTableName))
  93. assert.NoError(t, err)
  94. assert.False(t, exist)
  95. assert.NoError(t, testEngine.CreateTables(new(CustomTableName)))
  96. exist, err = testEngine.IsTableExist(new(CustomTableName))
  97. assert.NoError(t, err)
  98. assert.True(t, exist)
  99. }
  100. func TestIsTableEmpty(t *testing.T) {
  101. assert.NoError(t, prepareEngine())
  102. type NumericEmpty struct {
  103. Numeric float64 `xorm:"numeric(26,2)"`
  104. }
  105. type PictureEmpty struct {
  106. Id int64
  107. Url string `xorm:"unique"` //image's url
  108. Title string
  109. Description string
  110. Created time.Time `xorm:"created"`
  111. ILike int
  112. PageView int
  113. From_url string
  114. Pre_url string `xorm:"unique"` //pre view image's url
  115. Uid int64
  116. }
  117. assert.NoError(t, testEngine.DropTables(&PictureEmpty{}, &NumericEmpty{}))
  118. assert.NoError(t, testEngine.Sync2(new(PictureEmpty), new(NumericEmpty)))
  119. isEmpty, err := testEngine.IsTableEmpty(&PictureEmpty{})
  120. assert.NoError(t, err)
  121. assert.True(t, isEmpty)
  122. tbName := testEngine.GetTableMapper().Obj2Table("PictureEmpty")
  123. isEmpty, err = testEngine.IsTableEmpty(tbName)
  124. assert.NoError(t, err)
  125. assert.True(t, isEmpty)
  126. }
  127. type CustomTableName struct {
  128. Id int64
  129. Name string
  130. }
  131. func (c *CustomTableName) TableName() string {
  132. return "customtablename"
  133. }
  134. func TestCustomTableName(t *testing.T) {
  135. assert.NoError(t, prepareEngine())
  136. c := new(CustomTableName)
  137. assert.NoError(t, testEngine.DropTables(c))
  138. assert.NoError(t, testEngine.CreateTables(c))
  139. }
  140. func TestDump(t *testing.T) {
  141. assert.NoError(t, prepareEngine())
  142. fp := testEngine.Dialect().URI().DbName + ".sql"
  143. os.Remove(fp)
  144. assert.NoError(t, testEngine.DumpAllToFile(fp))
  145. }
  146. type IndexOrUnique struct {
  147. Id int64
  148. Index int `xorm:"index"`
  149. Unique int `xorm:"unique"`
  150. Group1 int `xorm:"index(ttt)"`
  151. Group2 int `xorm:"index(ttt)"`
  152. UniGroup1 int `xorm:"unique(lll)"`
  153. UniGroup2 int `xorm:"unique(lll)"`
  154. }
  155. func TestIndexAndUnique(t *testing.T) {
  156. assert.NoError(t, prepareEngine())
  157. assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
  158. assert.NoError(t, testEngine.DropTables(&IndexOrUnique{}))
  159. assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
  160. assert.NoError(t, testEngine.CreateIndexes(&IndexOrUnique{}))
  161. assert.NoError(t, testEngine.CreateUniques(&IndexOrUnique{}))
  162. assert.NoError(t, testEngine.DropIndexes(&IndexOrUnique{}))
  163. }
  164. func TestMetaInfo(t *testing.T) {
  165. assert.NoError(t, prepareEngine())
  166. assert.NoError(t, testEngine.Sync2(new(CustomTableName), new(IndexOrUnique)))
  167. tables, err := testEngine.DBMetas()
  168. assert.NoError(t, err)
  169. assert.EqualValues(t, 2, len(tables))
  170. tableNames := []string{tables[0].Name, tables[1].Name}
  171. assert.Contains(t, tableNames, "customtablename")
  172. assert.Contains(t, tableNames, "index_or_unique")
  173. }
  174. func TestCharst(t *testing.T) {
  175. assert.NoError(t, prepareEngine())
  176. err := testEngine.DropTables("user_charset")
  177. if err != nil {
  178. t.Error(err)
  179. panic(err)
  180. }
  181. err = testEngine.Charset("utf8").Table("user_charset").CreateTable(&Userinfo{})
  182. if err != nil {
  183. t.Error(err)
  184. panic(err)
  185. }
  186. }
  187. func TestSync2_1(t *testing.T) {
  188. type WxTest struct {
  189. Id int `xorm:"not null pk autoincr INT(64)`
  190. Passport_user_type int16 `xorm:"null int"`
  191. Id_delete int8 `xorm:"null int default 1"`
  192. }
  193. assert.NoError(t, prepareEngine())
  194. assert.NoError(t, testEngine.DropTables("wx_test"))
  195. assert.NoError(t, testEngine.Sync2(new(WxTest)))
  196. assert.NoError(t, testEngine.Sync2(new(WxTest)))
  197. }
  198. func TestUnique_1(t *testing.T) {
  199. type UserUnique struct {
  200. Id int64
  201. UserName string `xorm:"unique varchar(25) not null"`
  202. Password string `xorm:"varchar(255) not null"`
  203. Admin bool `xorm:"not null"`
  204. CreatedAt time.Time `xorm:"created"`
  205. UpdatedAt time.Time `xorm:"updated"`
  206. }
  207. assert.NoError(t, prepareEngine())
  208. assert.NoError(t, testEngine.DropTables("user_unique"))
  209. assert.NoError(t, testEngine.Sync2(new(UserUnique)))
  210. assert.NoError(t, testEngine.DropTables("user_unique"))
  211. assert.NoError(t, testEngine.CreateTables(new(UserUnique)))
  212. assert.NoError(t, testEngine.CreateUniques(new(UserUnique)))
  213. }
  214. func TestSync2_2(t *testing.T) {
  215. type TestSync2Index struct {
  216. Id int64
  217. UserId int64 `xorm:"index"`
  218. }
  219. assert.NoError(t, prepareEngine())
  220. var tableNames = make(map[string]bool)
  221. for i := 0; i < 10; i++ {
  222. tableName := fmt.Sprintf("test_sync2_index_%d", i)
  223. tableNames[tableName] = true
  224. assert.NoError(t, testEngine.Table(tableName).Sync2(new(TestSync2Index)))
  225. exist, err := testEngine.IsTableExist(tableName)
  226. assert.NoError(t, err)
  227. assert.True(t, exist)
  228. }
  229. tables, err := testEngine.DBMetas()
  230. assert.NoError(t, err)
  231. for _, table := range tables {
  232. assert.True(t, tableNames[table.Name])
  233. }
  234. }