xorm_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package xorm
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "reflect"
  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. if err != nil {
  35. t.Fatal(err)
  36. }
  37. err = db.InitSqlMap()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. err = db.InitSqlTemplate()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. }
  46. func Test_GetFirst_Json(t *testing.T) {
  47. var article Article
  48. has, rows, err := db.Id(2).GetFirst(&article).Json()
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if !has {
  53. t.Log("[Test_GetFirst_Json]->rows: not exist\n")
  54. }
  55. t.Log("[Test_GetFirst_Json]->rows:\n" + rows)
  56. }
  57. func Test_GetFirst_Xml(t *testing.T) {
  58. var article Article
  59. has, rows, err := db.Where("userid =?", 3).GetFirst(&article).Xml()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if !has {
  64. t.Log("[Test_GetFirst_Xml]->rows: not exist\n")
  65. }
  66. t.Log("[Test_GetFirst_Xml]->rows:\n" + rows)
  67. }
  68. func Test_GetFirst_XmlIndent(t *testing.T) {
  69. var article Article
  70. has, rows, err := db.Where("userid =?", 3).GetFirst(&article).XmlIndent("", " ", "article")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if !has {
  75. t.Log("[Test_GetFirst_XmlIndent]->rows: not exist\n")
  76. }
  77. t.Log("[Test_GetFirst_XmlIndent]->rows:\n" + rows)
  78. }
  79. func Test_FindAll_Json(t *testing.T) {
  80. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).Query().Json()
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. t.Log("[Test_FindAll_Json]->rows:\n" + rows)
  85. }
  86. func Test_FindAll_ID(t *testing.T) {
  87. rows := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).Query()
  88. if rows.Error != nil {
  89. t.Fatal(rows.Error)
  90. }
  91. t.Log("[Test_FindAll_Json]->rows[0][\"id\"]:\n", rows.Result[0]["id"])
  92. t.Log("[Test_FindAll_Json]->reflect.TypeOf(rows.Result[0][\"id\"]):\n", reflect.TypeOf(rows.Result[0]["id"]))
  93. t.Log("[Test_FindAll_Json]->rows[0][\"title\"]:\n", rows.Result[0]["title"])
  94. t.Log("[Test_FindAll_Json]->reflect.TypeOf(rows.Result[0][\"title\"]):\n", reflect.TypeOf(rows.Result[0]["title"]))
  95. t.Log("[Test_FindAll_Json]->rows[0][\"createdatetime\"]:\n", rows.Result[0]["createdatetime"])
  96. t.Log("[Test_FindAll_Json]->reflect.TypeOf(rows.Result[0][\"createdatetime\"]):\n", reflect.TypeOf(rows.Result[0]["createdatetime"]))
  97. }
  98. func Test_FindAll_Xml(t *testing.T) {
  99. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).Query().Xml()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. t.Log("[Test_FindAll_Xml]->rows:\n" + rows)
  104. }
  105. func Test_FindAll_XmlIndent(t *testing.T) {
  106. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).Query().XmlIndent("", " ", "article")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. t.Log("[Test_FindAll_XmlIndent]->rows:\n" + rows)
  111. }
  112. func Test_FindAllWithDateFormat_Json(t *testing.T) {
  113. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).QueryWithDateFormat("20060102").Json()
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. t.Log("[Test_FindAllWithDateFormat_Json]->rows:\n" + rows)
  118. }
  119. func Test_FindAllWithDateFormat_Xml(t *testing.T) {
  120. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).QueryWithDateFormat("20060102").Xml()
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. t.Log("[Test_FindAllWithDateFormat_Xml]->rows:\n" + rows)
  125. }
  126. func Test_FindAllWithDateFormat_XmlIndent(t *testing.T) {
  127. rows, err := db.Sql("select id,title,createdatetime,content from article where id in (?,?)", 2, 5).QueryWithDateFormat("20060102").XmlIndent("", " ", "article")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. t.Log("[Test_FindAllWithDateFormat_XmlIndent]->rows:\n" + rows)
  132. }
  133. func Test_FindAllByParamMap_Json(t *testing.T) {
  134. paramMap := map[string]interface{}{"id": 4, "userid": 1}
  135. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().Json()
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. t.Log("[Test_FindAllByParamMap_Json]->rows:\n" + rows)
  140. }
  141. func Test_FindAllByParamMap_Xml(t *testing.T) {
  142. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  143. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().Xml()
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. t.Log("[Test_FindAllByParamMap_Xml]->rows:\n" + rows)
  148. }
  149. func Test_FindAllByParamMap_XmlIndent(t *testing.T) {
  150. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  151. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. t.Log("[Test_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  156. }
  157. func Test_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  158. paramMap := map[string]interface{}{"id": 5, "userid": 1}
  159. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. t.Log("[Test_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  164. }
  165. func Test_SqlMapClient_FindAllByParamMap_Json(t *testing.T) {
  166. paramMap := map[string]interface{}{"1": 2, "2": 5}
  167. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Json()
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. t.Log("[Test_SqlMapClient_FindAllByParamMap_Json]->rows:\n" + rows)
  172. }
  173. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_Json(t *testing.T) {
  174. paramMap := map[string]interface{}{"1": 2, "2": 5}
  175. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").Json()
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_Json]->rows:\n" + rows)
  180. }
  181. func Test_SqlMapClient_FindAllByParamMap_Xml(t *testing.T) {
  182. paramMap := map[string]interface{}{"1": 2, "2": 5}
  183. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Xml()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. t.Log("[Test_SqlMapClient_FindAllByParamMap_Xml]->rows:\n" + rows)
  188. }
  189. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_Xml(t *testing.T) {
  190. paramMap := map[string]interface{}{"1": 2, "2": 5}
  191. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").Xml()
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  196. }
  197. func Test_SqlMapClient_FindAllByParamMap_XmlIndent(t *testing.T) {
  198. paramMap := map[string]interface{}{"1": 2, "2": 5}
  199. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. t.Log("[Test_SqlMapClient_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  204. }
  205. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  206. paramMap := map[string]interface{}{"1": 2, "2": 5}
  207. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006-01-02 15:04").XmlIndent("", " ", "article")
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  212. }
  213. func Test_SqlTemplateClient_FindAllByParamMap_Json(t *testing.T) {
  214. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  215. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Json()
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_Json]->rows:\n" + rows)
  220. }
  221. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Json(t *testing.T) {
  222. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  223. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").Json()
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Json]->rows:\n" + rows)
  228. }
  229. func Test_SqlTemplateClient_FindAllByParamMap_Xml(t *testing.T) {
  230. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  231. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Xml()
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_Xml]->rows:\n" + rows)
  236. }
  237. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Xml(t *testing.T) {
  238. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  239. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").Xml()
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  244. }
  245. func Test_SqlTemplateClient_FindAllByParamMap_XmlIndent(t *testing.T) {
  246. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  247. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().XmlIndent("", " ", "article")
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  252. }
  253. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  254. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  255. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("01/02/2006").XmlIndent("", " ", "article")
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  260. }