xorm_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package xorm
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/xormplus/xorm"
  7. _ "github.com/lib/pq"
  8. )
  9. type Article struct {
  10. Id int `xorm:"not null pk autoincr unique INTEGER"`
  11. Content string `xorm:"not null TEXT"`
  12. Title string `xorm:"not null VARCHAR(255)"`
  13. Categorysubid int `xorm:"not null INTEGER"`
  14. Remark string `xorm:"not null VARCHAR(2555)"`
  15. Userid int `xorm:"not null INTEGER"`
  16. Viewcount int `xorm:"not null default 0 INTEGER"`
  17. Replycount int `xorm:"not null default 0 INTEGER"`
  18. Tags string `xorm:"not null VARCHAR(300)"`
  19. Createdatetime JSONTime `xorm:"not null default 'now()' DATETIME"`
  20. Isdraft int `xorm:"SMALLINT"`
  21. Lastupdatetime time.Time `xorm:"not null default 'now()' DATETIME"`
  22. }
  23. type JSONTime time.Time
  24. func (t JSONTime) MarshalJSON() ([]byte, error) {
  25. //do your serializing here
  26. stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006/01/08 15:04:05"))
  27. return []byte(stamp), nil
  28. }
  29. var db *xorm.Engine
  30. func Test_InitDB(t *testing.T) {
  31. var err error
  32. db, err = xorm.NewPostgreSQL("postgres://postgres:root@localhost:5432/mblog?sslmode=disable")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. func Test_GetFirst_Json(t *testing.T) {
  38. var article Article
  39. has,rows, err := db.Id(2).GetFirst(&article).Json()
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if !has{
  44. t.Log("[Test_GetFirst_Json]->rows: not exist\n")
  45. }
  46. t.Log("[Test_GetFirst_Json]->rows:\n" + rows)
  47. }
  48. func Test_GetFirst_Xml(t *testing.T) {
  49. var article Article
  50. has,rows, err := db.Where("userid =?",3).GetFirst(&article).Xml()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if !has{
  55. t.Log("[Test_GetFirst_Xml]->rows: not exist\n")
  56. }
  57. t.Log("[Test_GetFirst_Xml]->rows:\n" + rows)
  58. }
  59. func Test_GetFirst_XmlIndent(t *testing.T) {
  60. var article Article
  61. has,rows, err := db.Where("userid =?",3).GetFirst(&article).XmlIndent(""," ","article")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if !has{
  66. t.Log("[Test_GetFirst_XmlIndent]->rows: not exist\n")
  67. }
  68. t.Log("[Test_GetFirst_XmlIndent]->rows:\n" + rows)
  69. }
  70. func Test_FindAll_Json(t *testing.T) {
  71. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAll().Json()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. t.Log("[Test_FindAll_Json]->rows:\n" + rows)
  76. }
  77. func Test_FindAll_ID(t *testing.T) {
  78. rows := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAll()
  79. if rows.Err != nil {
  80. t.Fatal(rows.Err)
  81. }
  82. t.Log("[Test_FindAll_Json]->rows[0][\"id\"]:\n" , rows.Result[0]["id"])
  83. }
  84. func Test_FindAll_Xml(t *testing.T) {
  85. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAll().Xml()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. t.Log("[Test_FindAll_Xml]->rows:\n" + rows)
  90. }
  91. func Test_FindAll_XmlIndent(t *testing.T) {
  92. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAll().XmlIndent("", " ", "article")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. t.Log("[Test_FindAll_XmlIndent]->rows:\n" + rows)
  97. }
  98. func Test_FindAllWithDateFormat_Json(t *testing.T) {
  99. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAllWithDateFormat("20060102").Json()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. t.Log("[Test_FindAllWithDateFormat_Json]->rows:\n" + rows)
  104. }
  105. func Test_FindAllWithDateFormat_Xml(t *testing.T) {
  106. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?", 2).FindAllWithDateFormat("20060102").Xml()
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. t.Log("[Test_FindAllWithDateFormat_Xml]->rows:\n" + rows)
  111. }
  112. func Test_FindAllWithDateFormat_XmlIndent(t *testing.T) {
  113. rows, err := db.Sql("select id,title,createdatetime,content from article where id in (?,?)", 2, 5).FindAllWithDateFormat("20060102").XmlIndent("", " ", "article")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. t.Log("[Test_FindAllWithDateFormat_XmlIndent]->rows:\n" + rows)
  118. }
  119. func Test_FindAllByParamMap_Json(t *testing.T) {
  120. paramMap := map[string]interface{}{"id": 4, "userid": 1}
  121. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).FindAllByParamMap().Json()
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. t.Log("[Test_FindAllByParamMap_Json]->rows:\n" + rows)
  126. }
  127. func Test_FindAllByParamMap_Xml(t *testing.T) {
  128. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  129. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).FindAllByParamMap().Xml()
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. t.Log("[Test_FindAllByParamMap_Xml]->rows:\n" + rows)
  134. }
  135. func Test_FindAllByParamMap_XmlIndent(t *testing.T) {
  136. paramMap := map[string]interface{}{"id": 6, "userid": 1}
  137. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).FindAllByParamMap().XmlIndent("", " ", "article")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. t.Log("[Test_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  142. }
  143. func Test_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  144. paramMap := map[string]interface{}{"id": 5, "userid": 1}
  145. rows, err := db.Sql("select id,title,createdatetime,content from article where id = ?id and userid=?userid", &paramMap).FindAllByParamMapWithDateFormat("2006/01/02").XmlIndent("", " ", "article")
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. t.Log("[Test_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  150. }
  151. func Test_SqlMapClient_FindAllByParamMap_Json(t *testing.T) {
  152. paramMap := map[string]interface{}{"1": 2, "2": 5}
  153. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMap().Json()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. t.Log("[Test_SqlMapClient_FindAllByParamMap_Json]->rows:\n" + rows)
  158. }
  159. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_Json(t *testing.T) {
  160. paramMap := map[string]interface{}{"1": 2, "2": 5}
  161. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMapWithDateFormat("2006-01-02 15:04").Json()
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_Json]->rows:\n" + rows)
  166. }
  167. func Test_SqlMapClient_FindAllByParamMap_Xml(t *testing.T) {
  168. paramMap := map[string]interface{}{"1": 2, "2": 5}
  169. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMap().Xml()
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. t.Log("[Test_SqlMapClient_FindAllByParamMap_Xml]->rows:\n" + rows)
  174. }
  175. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_Xml(t *testing.T) {
  176. paramMap := map[string]interface{}{"1": 2, "2": 5}
  177. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMapWithDateFormat("2006-01-02 15:04").Xml()
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  182. }
  183. func Test_SqlMapClient_FindAllByParamMap_XmlIndent(t *testing.T) {
  184. paramMap := map[string]interface{}{"1": 2, "2": 5}
  185. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMap().XmlIndent("", " ", "article")
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. t.Log("[Test_SqlMapClient_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  190. }
  191. func Test_SqlMapClient_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  192. paramMap := map[string]interface{}{"1": 2, "2": 5}
  193. rows, err := db.SqlMapClient("selectAllArticle", &paramMap).FindAllByParamMapWithDateFormat("2006-01-02 15:04").XmlIndent("", " ", "article")
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. t.Log("[Test_SqlMapClient_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  198. }
  199. func Test_SqlTemplateClient_FindAllByParamMap_Json(t *testing.T) {
  200. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  201. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMap().Json()
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_Json]->rows:\n" + rows)
  206. }
  207. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Json(t *testing.T) {
  208. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
  209. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMapWithDateFormat("01/02/2006").Json()
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Json]->rows:\n" + rows)
  214. }
  215. func Test_SqlTemplateClient_FindAllByParamMap_Xml(t *testing.T) {
  216. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  217. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMap().Xml()
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_Xml]->rows:\n" + rows)
  222. }
  223. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Xml(t *testing.T) {
  224. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  225. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMapWithDateFormat("01/02/2006").Xml()
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_Xml]->rows:\n" + rows)
  230. }
  231. func Test_SqlTemplateClient_FindAllByParamMap_XmlIndent(t *testing.T) {
  232. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  233. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMap().XmlIndent("", " ", "article")
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. t.Log("[Test_SqlTemplateClient_FindAllByParamMap_XmlIndent]->rows:\n" + rows)
  238. }
  239. func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent(t *testing.T) {
  240. paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 2}
  241. rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).FindAllByParamMapWithDateFormat("01/02/2006").XmlIndent("", " ", "article")
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
  246. }