xorm_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. }
  48. func Test_Get_Struct(t *testing.T) {
  49. var article Article
  50. has, err := db.Id(2).Get(&article)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if !has {
  55. t.Log("[Test_Get_Struct]->rows: not exist\n")
  56. }
  57. t.Log("[Test_Get_Struct]->rows:\n", article)
  58. }
  59. func Test_GetFirst_Json(t *testing.T) {
  60. var article Article
  61. has, rows, err := db.Id(2).GetFirst(&article).Json()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if !has {
  66. t.Log("[Test_GetFirst_Json]->rows: not exist\n")
  67. }
  68. t.Log("[Test_GetFirst_Json]->rows:\n" + rows)
  69. }
  70. func Test_GetFirst_Xml(t *testing.T) {
  71. var article Article
  72. has, rows, err := db.Where("userid =?", 2).GetFirst(&article).Xml()
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. if !has {
  77. t.Log("[Test_GetFirst_Xml]->rows: not exist\n")
  78. }
  79. t.Log("[Test_GetFirst_Xml]->rows:\n" + rows)
  80. }
  81. func Test_GetFirst_XmlIndent(t *testing.T) {
  82. var article Article
  83. has, rows, err := db.Where("userid =?", 2).GetFirst(&article).XmlIndent("", " ", "article")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if !has {
  88. t.Log("[Test_GetFirst_XmlIndent]->rows: not exist\n")
  89. }
  90. t.Log("[Test_GetFirst_XmlIndent]->rows:\n" + rows)
  91. }
  92. func Test_Find(t *testing.T) {
  93. var article []Article
  94. result := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Find(&article)
  95. if result.Error != nil {
  96. t.Fatal(result.Error)
  97. }
  98. t.Log("[Test_Find]->article[0].Id:\n", article[0].Id)
  99. t.Log("[Test_Find]->article[0].Content:\n", article[0].Content)
  100. t.Log("[Test_Find]->article[0].Title:\n", article[0].Title)
  101. t.Log("[Test_Find]->article[0].Categorysubid:\n", article[0].Categorysubid)
  102. t.Log("[Test_Find]->article[0].Createdatetime:\n", article[0].Createdatetime)
  103. t.Log("[Test_Find]->article[0].Isdraft:\n", article[0].Isdraft)
  104. t.Log("[Test_Find]->article[0].Lastupdatetime:\n", article[0].Lastupdatetime)
  105. t.Log("[Test_Find]->article[0].Remark:\n", article[0].Remark)
  106. t.Log("[Test_Find]->article[0].Replycount:\n", article[0].Replycount)
  107. t.Log("[Test_Find]->article[0].Tags:\n", article[0].Tags)
  108. t.Log("[Test_Find]->article[0].Userid:\n", article[0].Userid)
  109. t.Log("[Test_Find]->article[0].Viewcount:\n", article[0].Viewcount)
  110. t.Log("[Test_Find]-> result.Result:\n", result.Result)
  111. resultJson, err := result.Json()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. t.Log("[Test_Find]-> result.Json():\n", resultJson)
  116. }
  117. func Test_Query_Json(t *testing.T) {
  118. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query().Json()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. t.Log("[Test_Query_Json]->rows:\n" + rows)
  123. }
  124. func Test_Query_Result(t *testing.T) {
  125. rows := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query()
  126. if rows.Error != nil {
  127. t.Fatal(rows.Error)
  128. }
  129. t.Log("[Test_Query_Result]->rows[0][\"id\"]:\n", rows.Result[0]["id"])
  130. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"id\"]):\n", reflect.TypeOf(rows.Result[0]["id"]))
  131. t.Log("[Test_Query_Result]->rows[0][\"title\"]:\n", rows.Result[0]["title"])
  132. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"title\"]):\n", reflect.TypeOf(rows.Result[0]["title"]))
  133. t.Log("[Test_Query_Result]->rows[0][\"createdatetime\"]:\n", rows.Result[0]["createdatetime"])
  134. t.Log("[Test_Query_Result]->reflect.TypeOf(rows.Result[0][\"createdatetime\"]):\n", reflect.TypeOf(rows.Result[0]["createdatetime"]))
  135. }
  136. func Test_Query_Xml(t *testing.T) {
  137. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 27).Query().Xml()
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. t.Log("[Test_Query_Xml]->rows:\n" + rows)
  142. }
  143. func Test_Query_XmlIndent(t *testing.T) {
  144. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).Query().XmlIndent("", " ", "article")
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. t.Log("[Test_Query_XmlIndent]->rows:\n" + rows)
  149. }
  150. func Test_QueryWithDateFormat_Json(t *testing.T) {
  151. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).QueryWithDateFormat("20060102").Json()
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. t.Log("[Test_QueryWithDateFormat_Json]->rows:\n" + rows)
  156. }
  157. func Test_QueryWithDateFormat_Xml(t *testing.T) {
  158. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 33).QueryWithDateFormat("20060102").Xml()
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. t.Log("[Test_QueryWithDateFormat_Xml]->rows:\n" + rows)
  163. }
  164. func Test_QueryWithDateFormat_XmlIndent(t *testing.T) {
  165. rows, err := db.Sql("select id,title,createdatetime,content from article where id in (?,?)", 27, 33).QueryWithDateFormat("20060102").XmlIndent("", " ", "article")
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. t.Log("[Test_QueryWithDateFormat_XmlIndent]->rows:\n" + rows)
  170. }
  171. func Test_QueryByParamMap_Json(t *testing.T) {
  172. paramMap := map[string]interface{}{"id": 32, "userid": 1}
  173. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().Json()
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. t.Log("[Test_QueryByParamMap_Json]->rows:\n" + rows)
  178. }
  179. func Test_QueryByParamMap_Xml(t *testing.T) {
  180. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  181. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().Xml()
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. t.Log("[Test_QueryByParamMap_Xml]->rows:\n" + rows)
  186. }
  187. func Test_QueryByParamMap_XmlIndent(t *testing.T) {
  188. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  189. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. t.Log("[Test_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  194. }
  195. func Test_QueryByParamMapWithDateFormat_XmlIndent(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).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. t.Log("[Test_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  202. }
  203. func Test_SqlMapClient_QueryByParamMap_Json(t *testing.T) {
  204. paramMap := map[string]interface{}{"1": 2, "2": 5}
  205. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Json()
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. t.Log("[Test_SqlMapClient_QueryByParamMap_Json]->rows:\n" + rows)
  210. }
  211. func Test_SqlMapClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
  212. paramMap := map[string]interface{}{"1": 2, "2": 5}
  213. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").Json()
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
  218. }
  219. func Test_SqlMapClient_QueryByParamMap_Xml(t *testing.T) {
  220. paramMap := map[string]interface{}{"1": 2, "2": 5}
  221. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Xml()
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. t.Log("[Test_SqlMapClient_QueryByParamMap_Xml]->rows:\n" + rows)
  226. }
  227. func Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
  228. paramMap := map[string]interface{}{"1": 2, "2": 5}
  229. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").Xml()
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  234. }
  235. func Test_SqlMapClient_QueryByParamMap_XmlIndent(t *testing.T) {
  236. paramMap := map[string]interface{}{"1": 2, "2": 5}
  237. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. t.Log("[Test_SqlMapClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  242. }
  243. func Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  244. paramMap := map[string]interface{}{"1": 2, "2": 5}
  245. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").XmlIndent("", " ", "article")
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. t.Log("[Test_SqlMapClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  250. }
  251. func Test_SqlTemplateClient_QueryByParamMap_Json(t *testing.T) {
  252. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  253. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Json()
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. t.Log("[Test_SqlTemplateClient_QueryByParamMap_Json]->rows:\n" + rows)
  258. }
  259. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json(t *testing.T) {
  260. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  261. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").Json()
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Json]->rows:\n" + rows)
  266. }
  267. func Test_SqlTemplateClient_QueryByParamMap_Xml(t *testing.T) {
  268. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  269. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Xml()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. t.Log("[Test_SqlTemplateClient_QueryByParamMap_Xml]->rows:\n" + rows)
  274. }
  275. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml(t *testing.T) {
  276. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  277. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").Xml()
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  282. }
  283. func Test_SqlTemplateClient_QueryByParamMap_XmlIndent(t *testing.T) {
  284. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  285. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. t.Log("[Test_SqlTemplateClient_QueryByParamMap_XmlIndent]->rows:\n" + rows)
  290. }
  291. func Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  292. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  293. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").XmlIndent("", " ", "article")
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. t.Log("[Test_SqlTemplateClient_QueryByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  298. }
  299. func Test_Find_Structs_Json(t *testing.T) {
  300. articles := make([]Article, 0)
  301. json, err := db.Where("id=?", 6).Find(&articles).Json()
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. t.Log("[Test_Find_Structs_Json]->rows:\n" + json)
  306. }
  307. func Test_Find_Structs_Xml(t *testing.T) {
  308. articles := make([]Article, 0)
  309. xml, err := db.Where("id=?", 6).Find(&articles).Xml()
  310. if err != nil {
  311. t.Fatal(err)
  312. }
  313. t.Log("[Test_Find_Structs_Xml]->rows:\n" + xml)
  314. }
  315. func Test_Find_Structs_XmlIndent(t *testing.T) {
  316. articles := make([]Article, 0)
  317. xml, err := db.Where("id=?", 6).Find(&articles).XmlIndent("", " ", "Article")
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. t.Log("[Test_Find_Structs_XmlIndent]->rows:\n" + xml)
  322. }