xorm_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package xorm
  2. import (
  3. "fmt"
  4. "log"
  5. "reflect"
  6. "testing"
  7. "time"
  8. "github.com/xormplus/xorm"
  9. _ "github.com/lib/pq"
  10. )
  11. type Article struct {
  12. Id int `xorm:"not null pk autoincr unique INTEGER"`
  13. Content string `xorm:"not null TEXT"`
  14. Title string `xorm:"not null VARCHAR(255)"`
  15. Categorysubid int `xorm:"not null INTEGER"`
  16. Remark string `xorm:"not null VARCHAR(2555)"`
  17. Userid int `xorm:"not null INTEGER"`
  18. Viewcount int `xorm:"not null default 0 INTEGER"`
  19. Replycount int `xorm:"not null default 0 INTEGER"`
  20. Tags string `xorm:"not null VARCHAR(300)"`
  21. Createdatetime JSONTime `xorm:"not null default 'now()' DATETIME"`
  22. Isdraft int `xorm:"SMALLINT"`
  23. Lastupdatetime time.Time `xorm:"not null default 'now()' DATETIME"`
  24. }
  25. type Category struct {
  26. Id int `xorm:"not null pk autoincr unique INTEGER"`
  27. Name string `xorm:"not null VARCHAR(200)"`
  28. Counts int `xorm:"not null default 0 INTEGER"`
  29. Orders int `xorm:"not null default 0 INTEGER"`
  30. Createtime time.Time `xorm:"not null default 'now()' created DATETIME"`
  31. Pid int `xorm:"not null default 0 INTEGER"`
  32. Lastupdatetime time.Time `xorm:"not null default 'now()' updated DATETIME"`
  33. Status int `xorm:"not null default 1 SMALLINT"`
  34. }
  35. type JSONTime time.Time
  36. func (t JSONTime) MarshalJSON() ([]byte, error) {
  37. //do your serializing here
  38. stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006/01/08 15:04:05"))
  39. return []byte(stamp), nil
  40. }
  41. var db *xorm.Engine
  42. func Test_InitDB(t *testing.T) {
  43. var err error
  44. db, err = xorm.NewPostgreSQL("postgres://postgres:root@localhost:5432/mblog?sslmode=disable")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. err = db.SetSqlMapRootDir("./sql/oracle").InitSqlMap()
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. err = db.SetSqlTemplateRootDir("./sql/oracle").InitSqlTemplate(xorm.SqlTemplateOptions{Extension: ".xx"})
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. err = db.StartFSWatcher()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. db.ShowSQL(true)
  61. log.Println(db)
  62. // db.NewSession().SqlMapClient().Execute()
  63. }
  64. func Test_Get_Struct(t *testing.T) {
  65. var article Article
  66. has, err := db.Id(2).Get(&article)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if !has {
  71. t.Log("[Test_Get_Struct]->rows: not exist\n")
  72. }
  73. t.Log("[Test_Get_Struct]->rows:\n", article)
  74. }
  75. func Test_GetFirst_Json(t *testing.T) {
  76. var article Article
  77. has, rows, err := db.Id(2).GetFirst(&article).Json()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if !has {
  82. t.Log("[Test_GetFirst_Json]->rows: not exist\n")
  83. }
  84. t.Log("[Test_GetFirst_Json]->rows:\n" + rows)
  85. }
  86. func Test_GetFirst_Xml(t *testing.T) {
  87. var article Article
  88. has, rows, err := db.Where("userid =?", 2).GetFirst(&article).Xml()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. if !has {
  93. t.Log("[Test_GetFirst_Xml]->rows: not exist\n")
  94. }
  95. t.Log("[Test_GetFirst_Xml]->rows:\n" + rows)
  96. }
  97. func Test_GetFirst_XmlIndent(t *testing.T) {
  98. var article Article
  99. has, rows, err := db.Where("userid =?", 2).GetFirst(&article).XmlIndent("", " ", "article")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if !has {
  104. t.Log("[Test_GetFirst_XmlIndent]->rows: not exist\n")
  105. }
  106. t.Log("[Test_GetFirst_XmlIndent]->rows:\n" + rows)
  107. }
  108. func Test_Search(t *testing.T) {
  109. var article []Article
  110. result := db.Sql("select id,title,createdatetime,content from article where id = ?", 25).Search(&article)
  111. if result.Error != nil {
  112. t.Fatal(result.Error)
  113. }
  114. t.Log("[Test_Find]->article[0].Id:\n", article[0].Id)
  115. t.Log("[Test_Find]->article[0].Content:\n", article[0].Content)
  116. t.Log("[Test_Find]->article[0].Title:\n", article[0].Title)
  117. t.Log("[Test_Find]->article[0].Categorysubid:\n", article[0].Categorysubid)
  118. t.Log("[Test_Find]->article[0].Createdatetime:\n", article[0].Createdatetime)
  119. t.Log("[Test_Find]->article[0].Isdraft:\n", article[0].Isdraft)
  120. t.Log("[Test_Find]->article[0].Lastupdatetime:\n", article[0].Lastupdatetime)
  121. t.Log("[Test_Find]->article[0].Remark:\n", article[0].Remark)
  122. t.Log("[Test_Find]->article[0].Replycount:\n", article[0].Replycount)
  123. t.Log("[Test_Find]->article[0].Tags:\n", article[0].Tags)
  124. t.Log("[Test_Find]->article[0].Userid:\n", article[0].Userid)
  125. t.Log("[Test_Find]->article[0].Viewcount:\n", article[0].Viewcount)
  126. t.Log("[Test_Find]-> result.Result:\n", result.Result)
  127. resultJson, err := result.Json()
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. t.Log("[Test_Search]-> result.Json():\n", resultJson)
  132. }
  133. func Test_Query_Json(t *testing.T) {
  134. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query().Json()
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. t.Log("[Test_Query_Json]->rows:\n" + rows)
  139. }
  140. func Test_Query_Result(t *testing.T) {
  141. rows := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query()
  142. if rows.Error != nil {
  143. t.Fatal(rows.Error)
  144. }
  145. t.Log("[Test_Query_Result]->rows[0][\"id\"]:\n", rows.Results[0]["id"])
  146. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"id\"]):\n", reflect.TypeOf(rows.Results[0]["id"]))
  147. t.Log("[Test_Query_Result]->rows[0][\"title\"]:\n", rows.Results[0]["title"])
  148. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"title\"]):\n", reflect.TypeOf(rows.Results[0]["title"]))
  149. t.Log("[Test_Query_Result]->rows[0][\"createdatetime\"]:\n", rows.Results[0]["createdatetime"])
  150. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"createdatetime\"]):\n", reflect.TypeOf(rows.Results[0]["createdatetime"]))
  151. }
  152. func Test_Query_Xml(t *testing.T) {
  153. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query().Xml()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. t.Log("[Test_Query_Xml]->rows:\n" + rows)
  158. }
  159. func Test_Query_XmlIndent(t *testing.T) {
  160. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).Query().XmlIndent("", " ", "article")
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. t.Log("[Test_Query_XmlIndent]->rows:\n" + rows)
  165. }
  166. func Test_QueryWithDateFormat_Json(t *testing.T) {
  167. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).QueryWithDateFormat("20060102").Json()
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. t.Log("[Test_QueryWithDateFormat_Json]->rows:\n" + rows)
  172. }
  173. func Test_QueryWithDateFormat_Xml(t *testing.T) {
  174. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).QueryWithDateFormat("20060102").Xml()
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. t.Log("[Test_QueryWithDateFormat_Xml]->rows:\n" + rows)
  179. }
  180. func Test_QueryWithDateFormat_XmlIndent(t *testing.T) {
  181. rows, err := db.Sql("select id,title,createdatetime,content from article where id in (?,?)", 27, 33).QueryWithDateFormat("20060102").XmlIndent("", " ", "article")
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. t.Log("[Test_QueryWithDateFormat_XmlIndent]->rows:\n" + rows)
  186. }
  187. func Test_QueryByParamMap_Json(t *testing.T) {
  188. paramMap := map[string]interface{}{"id": 32, "userid": 1}
  189. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).Query().Json()
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. t.Log("[Test_QueryByParamMap_Json]->rows:\n" + rows)
  194. }
  195. func Test_QueryByParamMap_Xml(t *testing.T) {
  196. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  197. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).Query().Xml()
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. t.Log("[Test_QueryByParamMap_Xml]->rows:\n" + rows)
  202. }
  203. func Test_QueryByParamMap_XmlIndent(t *testing.T) {
  204. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  205. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).Query().XmlIndent("", " ", "article")
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. t.Log("[Test_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  210. }
  211. func Test_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  212. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  213. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. t.Log("[Test_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  218. }
  219. func Test_SqlMapClient_QueryByParamMap_Json(t *testing.T) {
  220. paramMap := map[string]interface{}{"1": 2, "2": 5}
  221. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).Query().Json()
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. t.Log("[Test_SqlMapClient_QueryByParamMap_Json]->rows:\n" + rows)
  226. }
  227. func Test_SqlMapClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
  228. paramMap := map[string]interface{}{"1": 2, "2": 5}
  229. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryWithDateFormat("2006-01-02 15:04").Json()
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
  234. }
  235. func Test_SqlMapClient_QueryByParamMap_Xml(t *testing.T) {
  236. paramMap := map[string]interface{}{"1": 2, "2": 5}
  237. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).Query().Xml()
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. t.Log("[Test_SqlMapClient_QueryByParamMap_Xml]->rows:\n" + rows)
  242. }
  243. func Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
  244. paramMap := map[string]interface{}{"1": 2, "2": 5}
  245. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryWithDateFormat("2006-01-02 15:04").Xml()
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  250. }
  251. func Test_SqlMapClient_QueryByParamMap_XmlIndent(t *testing.T) {
  252. paramMap := map[string]interface{}{"1": 2, "2": 5}
  253. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).Query().XmlIndent("", " ", "article")
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. t.Log("[Test_SqlMapClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  258. }
  259. func Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  260. paramMap := map[string]interface{}{"1": 2, "2": 5}
  261. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryWithDateFormat("2006-01-02 15:04").XmlIndent("", " ", "article")
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  266. }
  267. func Test_SqlTemplateClient_QueryByParamMap_Json(t *testing.T) {
  268. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  269. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).Query().Json()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. t.Log("[Test_SqlTemplateClient_QueryByParamMap_Json]->rows:\n" + rows)
  274. }
  275. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
  276. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  277. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).QueryWithDateFormat("01/02/2006").Json()
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
  282. }
  283. func Test_SqlTemplateClient_QueryByParamMap_Xml(t *testing.T) {
  284. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  285. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).Query().Xml()
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. t.Log("[Test_SqlTemplateClient_QueryByParamMap_Xml]->rows:\n" + rows)
  290. }
  291. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
  292. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  293. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).QueryWithDateFormat("01/02/2006").Xml()
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  298. }
  299. func Test_SqlTemplateClient_QueryByParamMap_XmlIndent(t *testing.T) {
  300. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  301. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).Query().XmlIndent("", " ", "article")
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. t.Log("[Test_SqlTemplateClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  306. }
  307. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  308. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  309. rows, err := db.SqlTemplateClient("select.example.stpl", &paramMap).QueryWithDateFormat("01/02/2006").XmlIndent("", " ", "article")
  310. if err != nil {
  311. t.Fatal(err)
  312. }
  313. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  314. }
  315. func Test_Where_Search_Structs_Json(t *testing.T) {
  316. var articles []Article
  317. json, err := db.Where("id=?", 6).Search(&articles).Json()
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. t.Log("[Test_Where_Search_Structs_Json]->rows:\n" + json)
  322. }
  323. func Test_Search_Structs_Xml(t *testing.T) {
  324. var articles []Article
  325. xml, err := db.Where("id=?", 6).Search(&articles).Xml()
  326. if err != nil {
  327. t.Fatal(err)
  328. }
  329. t.Log("[Test_Search_Structs_Xml]->rows:\n" + xml)
  330. }
  331. func Test_Search_Structs_XmlIndent(t *testing.T) {
  332. var articles []Article
  333. xml, err := db.Where("id=?", 6).Search(&articles).XmlIndent("", " ", "Article")
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. t.Log("[Test_Search_Structs_XmlIndent]->rows:\n" + xml)
  338. }
  339. func Test_Search_Structs_Json(t *testing.T) {
  340. var categories []Category
  341. Json, err := db.Select("id").Search(&categories).Json()
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. t.Log("[Test_Search_Structs_Json]->rows:\n", Json)
  346. }
  347. func Test_Sql_Find_Structs(t *testing.T) {
  348. var categories2 []Category
  349. err := db.Sql("select * from category where id =?", 16).Find(&categories2)
  350. if err != nil {
  351. t.Fatal(err)
  352. }
  353. t.Log("[Test_Sql_Find_Structs]->rows:\n", categories2)
  354. }
  355. func Test_SqlMapClient_Find_Structs(t *testing.T) {
  356. var categories2 []Category
  357. db.AddSql("1", "select * from category where id =?")
  358. err := db.SqlMapClient("1", 16).Find(&categories2)
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. t.Log("[Test_SqlMapClient_Find_Structs]->rows:\n", categories2)
  363. }
  364. func Test_SqlTemplateClient_Find_Structs(t *testing.T) {
  365. var categories2 []Category
  366. db.AddSqlTemplate("1", "select * from category where id =?id")
  367. err := db.SqlTemplateClient("1", &map[string]interface{}{"id": 25}).Find(&categories2)
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. t.Log("[Test_SqlTemplateClient_Find_Structs]->rows:\n", categories2)
  372. }
  373. func Test_Sql_Search_Json(t *testing.T) {
  374. var categories2 []Category
  375. json, err := db.Sql("select * from category where id =?", 16).Search(&categories2).Json()
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. t.Log("[Test_Sql_Search_Json]->rows:\n", json)
  380. }
  381. func Test_SqlMapClient_Search_Json(t *testing.T) {
  382. var categories2 []Category
  383. db.AddSql("1", "select * from category where id =?")
  384. json, err := db.SqlMapClient("1", 16).Search(&categories2).Json()
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. t.Log("[Test_SqlMapClient_Search_Json]->rows:\n", json)
  389. }
  390. func Test_SqlTemplateClient_Search_Json(t *testing.T) {
  391. var categories2 []Category
  392. db.AddSqlTemplate("1", "select * from category where id =?id")
  393. json, err := db.SqlTemplateClient("1", &map[string]interface{}{"id": 25}).Search(&categories2).Json()
  394. if err != nil {
  395. t.Fatal(err)
  396. }
  397. t.Log("[Test_SqlTemplateClient_Search_Json]->rows:\n", json)
  398. }