xorm_test.go 12 KB

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