xorm_test.go 11 KB

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