xorm_test.go 11 KB

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