orm_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. package sqlx
  2. import (
  3. "database/sql"
  4. "errors"
  5. "testing"
  6. "git.i2edu.net/i2/go-zero/core/logx"
  7. "github.com/DATA-DOG/go-sqlmock"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestUnmarshalRowBool(t *testing.T) {
  11. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  12. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1")
  13. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  14. var value bool
  15. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  16. return unmarshalRow(&value, rows, true)
  17. }, "select value from users where user=?", "anyone"))
  18. assert.True(t, value)
  19. })
  20. }
  21. func TestUnmarshalRowBoolNotSettable(t *testing.T) {
  22. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  23. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1")
  24. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  25. var value bool
  26. assert.NotNil(t, query(db, func(rows *sql.Rows) error {
  27. return unmarshalRow(value, rows, true)
  28. }, "select value from users where user=?", "anyone"))
  29. })
  30. }
  31. func TestUnmarshalRowInt(t *testing.T) {
  32. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  33. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2")
  34. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  35. var value int
  36. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  37. return unmarshalRow(&value, rows, true)
  38. }, "select value from users where user=?", "anyone"))
  39. assert.EqualValues(t, 2, value)
  40. })
  41. }
  42. func TestUnmarshalRowInt8(t *testing.T) {
  43. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  44. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("3")
  45. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  46. var value int8
  47. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  48. return unmarshalRow(&value, rows, true)
  49. }, "select value from users where user=?", "anyone"))
  50. assert.EqualValues(t, int8(3), value)
  51. })
  52. }
  53. func TestUnmarshalRowInt16(t *testing.T) {
  54. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  55. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("4")
  56. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  57. var value int16
  58. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  59. return unmarshalRow(&value, rows, true)
  60. }, "select value from users where user=?", "anyone"))
  61. assert.Equal(t, int16(4), value)
  62. })
  63. }
  64. func TestUnmarshalRowInt32(t *testing.T) {
  65. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  66. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("5")
  67. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  68. var value int32
  69. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  70. return unmarshalRow(&value, rows, true)
  71. }, "select value from users where user=?", "anyone"))
  72. assert.Equal(t, int32(5), value)
  73. })
  74. }
  75. func TestUnmarshalRowInt64(t *testing.T) {
  76. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  77. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("6")
  78. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  79. var value int64
  80. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  81. return unmarshalRow(&value, rows, true)
  82. }, "select value from users where user=?", "anyone"))
  83. assert.EqualValues(t, int64(6), value)
  84. })
  85. }
  86. func TestUnmarshalRowUint(t *testing.T) {
  87. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  88. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2")
  89. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  90. var value uint
  91. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  92. return unmarshalRow(&value, rows, true)
  93. }, "select value from users where user=?", "anyone"))
  94. assert.EqualValues(t, uint(2), value)
  95. })
  96. }
  97. func TestUnmarshalRowUint8(t *testing.T) {
  98. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  99. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("3")
  100. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  101. var value uint8
  102. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  103. return unmarshalRow(&value, rows, true)
  104. }, "select value from users where user=?", "anyone"))
  105. assert.EqualValues(t, uint8(3), value)
  106. })
  107. }
  108. func TestUnmarshalRowUint16(t *testing.T) {
  109. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  110. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("4")
  111. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  112. var value uint16
  113. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  114. return unmarshalRow(&value, rows, true)
  115. }, "select value from users where user=?", "anyone"))
  116. assert.EqualValues(t, uint16(4), value)
  117. })
  118. }
  119. func TestUnmarshalRowUint32(t *testing.T) {
  120. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  121. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("5")
  122. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  123. var value uint32
  124. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  125. return unmarshalRow(&value, rows, true)
  126. }, "select value from users where user=?", "anyone"))
  127. assert.EqualValues(t, uint32(5), value)
  128. })
  129. }
  130. func TestUnmarshalRowUint64(t *testing.T) {
  131. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  132. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("6")
  133. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  134. var value uint64
  135. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  136. return unmarshalRow(&value, rows, true)
  137. }, "select value from users where user=?", "anyone"))
  138. assert.EqualValues(t, uint16(6), value)
  139. })
  140. }
  141. func TestUnmarshalRowFloat32(t *testing.T) {
  142. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  143. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("7")
  144. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  145. var value float32
  146. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  147. return unmarshalRow(&value, rows, true)
  148. }, "select value from users where user=?", "anyone"))
  149. assert.EqualValues(t, float32(7), value)
  150. })
  151. }
  152. func TestUnmarshalRowFloat64(t *testing.T) {
  153. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  154. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("8")
  155. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  156. var value float64
  157. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  158. return unmarshalRow(&value, rows, true)
  159. }, "select value from users where user=?", "anyone"))
  160. assert.EqualValues(t, float64(8), value)
  161. })
  162. }
  163. func TestUnmarshalRowString(t *testing.T) {
  164. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  165. const expect = "hello"
  166. rs := sqlmock.NewRows([]string{"value"}).FromCSVString(expect)
  167. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  168. var value string
  169. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  170. return unmarshalRow(&value, rows, true)
  171. }, "select value from users where user=?", "anyone"))
  172. assert.EqualValues(t, expect, value)
  173. })
  174. }
  175. func TestUnmarshalRowStruct(t *testing.T) {
  176. value := new(struct {
  177. Name string
  178. Age int
  179. })
  180. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  181. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
  182. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  183. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  184. return unmarshalRow(value, rows, true)
  185. }, "select name, age from users where user=?", "anyone"))
  186. assert.Equal(t, "liao", value.Name)
  187. assert.Equal(t, 5, value.Age)
  188. })
  189. }
  190. func TestUnmarshalRowStructWithTags(t *testing.T) {
  191. value := new(struct {
  192. Age int `db:"age"`
  193. Name string `db:"name"`
  194. })
  195. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  196. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
  197. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  198. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  199. return unmarshalRow(value, rows, true)
  200. }, "select name, age from users where user=?", "anyone"))
  201. assert.Equal(t, "liao", value.Name)
  202. assert.Equal(t, 5, value.Age)
  203. })
  204. }
  205. func TestUnmarshalRowStructWithTagsWrongColumns(t *testing.T) {
  206. value := new(struct {
  207. Age *int `db:"age"`
  208. Name string `db:"name"`
  209. })
  210. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  211. rs := sqlmock.NewRows([]string{"name"}).FromCSVString("liao")
  212. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  213. assert.NotNil(t, query(db, func(rows *sql.Rows) error {
  214. return unmarshalRow(value, rows, true)
  215. }, "select name, age from users where user=?", "anyone"))
  216. })
  217. }
  218. func TestUnmarshalRowsBool(t *testing.T) {
  219. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  220. expect := []bool{true, false}
  221. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1\n0")
  222. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  223. var value []bool
  224. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  225. return unmarshalRows(&value, rows, true)
  226. }, "select value from users where user=?", "anyone"))
  227. assert.EqualValues(t, expect, value)
  228. })
  229. }
  230. func TestUnmarshalRowsInt(t *testing.T) {
  231. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  232. expect := []int{2, 3}
  233. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  234. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  235. var value []int
  236. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  237. return unmarshalRows(&value, rows, true)
  238. }, "select value from users where user=?", "anyone"))
  239. assert.EqualValues(t, expect, value)
  240. })
  241. }
  242. func TestUnmarshalRowsInt8(t *testing.T) {
  243. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  244. expect := []int8{2, 3}
  245. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  246. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  247. var value []int8
  248. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  249. return unmarshalRows(&value, rows, true)
  250. }, "select value from users where user=?", "anyone"))
  251. assert.EqualValues(t, expect, value)
  252. })
  253. }
  254. func TestUnmarshalRowsInt16(t *testing.T) {
  255. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  256. expect := []int16{2, 3}
  257. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  258. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  259. var value []int16
  260. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  261. return unmarshalRows(&value, rows, true)
  262. }, "select value from users where user=?", "anyone"))
  263. assert.EqualValues(t, expect, value)
  264. })
  265. }
  266. func TestUnmarshalRowsInt32(t *testing.T) {
  267. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  268. expect := []int32{2, 3}
  269. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  270. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  271. var value []int32
  272. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  273. return unmarshalRows(&value, rows, true)
  274. }, "select value from users where user=?", "anyone"))
  275. assert.EqualValues(t, expect, value)
  276. })
  277. }
  278. func TestUnmarshalRowsInt64(t *testing.T) {
  279. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  280. expect := []int64{2, 3}
  281. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  282. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  283. var value []int64
  284. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  285. return unmarshalRows(&value, rows, true)
  286. }, "select value from users where user=?", "anyone"))
  287. assert.EqualValues(t, expect, value)
  288. })
  289. }
  290. func TestUnmarshalRowsUint(t *testing.T) {
  291. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  292. expect := []uint{2, 3}
  293. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  294. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  295. var value []uint
  296. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  297. return unmarshalRows(&value, rows, true)
  298. }, "select value from users where user=?", "anyone"))
  299. assert.EqualValues(t, expect, value)
  300. })
  301. }
  302. func TestUnmarshalRowsUint8(t *testing.T) {
  303. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  304. expect := []uint8{2, 3}
  305. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  306. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  307. var value []uint8
  308. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  309. return unmarshalRows(&value, rows, true)
  310. }, "select value from users where user=?", "anyone"))
  311. assert.EqualValues(t, expect, value)
  312. })
  313. }
  314. func TestUnmarshalRowsUint16(t *testing.T) {
  315. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  316. expect := []uint16{2, 3}
  317. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  318. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  319. var value []uint16
  320. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  321. return unmarshalRows(&value, rows, true)
  322. }, "select value from users where user=?", "anyone"))
  323. assert.EqualValues(t, expect, value)
  324. })
  325. }
  326. func TestUnmarshalRowsUint32(t *testing.T) {
  327. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  328. expect := []uint32{2, 3}
  329. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  330. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  331. var value []uint32
  332. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  333. return unmarshalRows(&value, rows, true)
  334. }, "select value from users where user=?", "anyone"))
  335. assert.EqualValues(t, expect, value)
  336. })
  337. }
  338. func TestUnmarshalRowsUint64(t *testing.T) {
  339. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  340. expect := []uint64{2, 3}
  341. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  342. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  343. var value []uint64
  344. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  345. return unmarshalRows(&value, rows, true)
  346. }, "select value from users where user=?", "anyone"))
  347. assert.EqualValues(t, expect, value)
  348. })
  349. }
  350. func TestUnmarshalRowsFloat32(t *testing.T) {
  351. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  352. expect := []float32{2, 3}
  353. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  354. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  355. var value []float32
  356. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  357. return unmarshalRows(&value, rows, true)
  358. }, "select value from users where user=?", "anyone"))
  359. assert.EqualValues(t, expect, value)
  360. })
  361. }
  362. func TestUnmarshalRowsFloat64(t *testing.T) {
  363. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  364. expect := []float64{2, 3}
  365. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  366. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  367. var value []float64
  368. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  369. return unmarshalRows(&value, rows, true)
  370. }, "select value from users where user=?", "anyone"))
  371. assert.EqualValues(t, expect, value)
  372. })
  373. }
  374. func TestUnmarshalRowsString(t *testing.T) {
  375. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  376. expect := []string{"hello", "world"}
  377. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("hello\nworld")
  378. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  379. var value []string
  380. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  381. return unmarshalRows(&value, rows, true)
  382. }, "select value from users where user=?", "anyone"))
  383. assert.EqualValues(t, expect, value)
  384. })
  385. }
  386. func TestUnmarshalRowsBoolPtr(t *testing.T) {
  387. yes := true
  388. no := false
  389. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  390. expect := []*bool{&yes, &no}
  391. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1\n0")
  392. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  393. var value []*bool
  394. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  395. return unmarshalRows(&value, rows, true)
  396. }, "select value from users where user=?", "anyone"))
  397. assert.EqualValues(t, expect, value)
  398. })
  399. }
  400. func TestUnmarshalRowsIntPtr(t *testing.T) {
  401. two := 2
  402. three := 3
  403. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  404. expect := []*int{&two, &three}
  405. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  406. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  407. var value []*int
  408. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  409. return unmarshalRows(&value, rows, true)
  410. }, "select value from users where user=?", "anyone"))
  411. assert.EqualValues(t, expect, value)
  412. })
  413. }
  414. func TestUnmarshalRowsInt8Ptr(t *testing.T) {
  415. two := int8(2)
  416. three := int8(3)
  417. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  418. expect := []*int8{&two, &three}
  419. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  420. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  421. var value []*int8
  422. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  423. return unmarshalRows(&value, rows, true)
  424. }, "select value from users where user=?", "anyone"))
  425. assert.EqualValues(t, expect, value)
  426. })
  427. }
  428. func TestUnmarshalRowsInt16Ptr(t *testing.T) {
  429. two := int16(2)
  430. three := int16(3)
  431. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  432. expect := []*int16{&two, &three}
  433. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  434. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  435. var value []*int16
  436. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  437. return unmarshalRows(&value, rows, true)
  438. }, "select value from users where user=?", "anyone"))
  439. assert.EqualValues(t, expect, value)
  440. })
  441. }
  442. func TestUnmarshalRowsInt32Ptr(t *testing.T) {
  443. two := int32(2)
  444. three := int32(3)
  445. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  446. expect := []*int32{&two, &three}
  447. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  448. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  449. var value []*int32
  450. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  451. return unmarshalRows(&value, rows, true)
  452. }, "select value from users where user=?", "anyone"))
  453. assert.EqualValues(t, expect, value)
  454. })
  455. }
  456. func TestUnmarshalRowsInt64Ptr(t *testing.T) {
  457. two := int64(2)
  458. three := int64(3)
  459. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  460. expect := []*int64{&two, &three}
  461. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  462. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  463. var value []*int64
  464. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  465. return unmarshalRows(&value, rows, true)
  466. }, "select value from users where user=?", "anyone"))
  467. assert.EqualValues(t, expect, value)
  468. })
  469. }
  470. func TestUnmarshalRowsUintPtr(t *testing.T) {
  471. two := uint(2)
  472. three := uint(3)
  473. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  474. expect := []*uint{&two, &three}
  475. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  476. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  477. var value []*uint
  478. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  479. return unmarshalRows(&value, rows, true)
  480. }, "select value from users where user=?", "anyone"))
  481. assert.EqualValues(t, expect, value)
  482. })
  483. }
  484. func TestUnmarshalRowsUint8Ptr(t *testing.T) {
  485. two := uint8(2)
  486. three := uint8(3)
  487. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  488. expect := []*uint8{&two, &three}
  489. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  490. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  491. var value []*uint8
  492. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  493. return unmarshalRows(&value, rows, true)
  494. }, "select value from users where user=?", "anyone"))
  495. assert.EqualValues(t, expect, value)
  496. })
  497. }
  498. func TestUnmarshalRowsUint16Ptr(t *testing.T) {
  499. two := uint16(2)
  500. three := uint16(3)
  501. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  502. expect := []*uint16{&two, &three}
  503. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  504. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  505. var value []*uint16
  506. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  507. return unmarshalRows(&value, rows, true)
  508. }, "select value from users where user=?", "anyone"))
  509. assert.EqualValues(t, expect, value)
  510. })
  511. }
  512. func TestUnmarshalRowsUint32Ptr(t *testing.T) {
  513. two := uint32(2)
  514. three := uint32(3)
  515. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  516. expect := []*uint32{&two, &three}
  517. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  518. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  519. var value []*uint32
  520. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  521. return unmarshalRows(&value, rows, true)
  522. }, "select value from users where user=?", "anyone"))
  523. assert.EqualValues(t, expect, value)
  524. })
  525. }
  526. func TestUnmarshalRowsUint64Ptr(t *testing.T) {
  527. two := uint64(2)
  528. three := uint64(3)
  529. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  530. expect := []*uint64{&two, &three}
  531. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  532. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  533. var value []*uint64
  534. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  535. return unmarshalRows(&value, rows, true)
  536. }, "select value from users where user=?", "anyone"))
  537. assert.EqualValues(t, expect, value)
  538. })
  539. }
  540. func TestUnmarshalRowsFloat32Ptr(t *testing.T) {
  541. two := float32(2)
  542. three := float32(3)
  543. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  544. expect := []*float32{&two, &three}
  545. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  546. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  547. var value []*float32
  548. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  549. return unmarshalRows(&value, rows, true)
  550. }, "select value from users where user=?", "anyone"))
  551. assert.EqualValues(t, expect, value)
  552. })
  553. }
  554. func TestUnmarshalRowsFloat64Ptr(t *testing.T) {
  555. two := float64(2)
  556. three := float64(3)
  557. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  558. expect := []*float64{&two, &three}
  559. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
  560. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  561. var value []*float64
  562. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  563. return unmarshalRows(&value, rows, true)
  564. }, "select value from users where user=?", "anyone"))
  565. assert.EqualValues(t, expect, value)
  566. })
  567. }
  568. func TestUnmarshalRowsStringPtr(t *testing.T) {
  569. hello := "hello"
  570. world := "world"
  571. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  572. expect := []*string{&hello, &world}
  573. rs := sqlmock.NewRows([]string{"value"}).FromCSVString("hello\nworld")
  574. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  575. var value []*string
  576. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  577. return unmarshalRows(&value, rows, true)
  578. }, "select value from users where user=?", "anyone"))
  579. assert.EqualValues(t, expect, value)
  580. })
  581. }
  582. func TestUnmarshalRowsStruct(t *testing.T) {
  583. expect := []struct {
  584. Name string
  585. Age int64
  586. }{
  587. {
  588. Name: "first",
  589. Age: 2,
  590. },
  591. {
  592. Name: "second",
  593. Age: 3,
  594. },
  595. }
  596. var value []struct {
  597. Name string
  598. Age int64
  599. }
  600. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  601. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
  602. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  603. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  604. return unmarshalRows(&value, rows, true)
  605. }, "select name, age from users where user=?", "anyone"))
  606. for i, each := range expect {
  607. assert.Equal(t, each.Name, value[i].Name)
  608. assert.Equal(t, each.Age, value[i].Age)
  609. }
  610. })
  611. }
  612. func TestUnmarshalRowsStructWithNullStringType(t *testing.T) {
  613. expect := []struct {
  614. Name string
  615. NullString sql.NullString
  616. }{
  617. {
  618. Name: "first",
  619. NullString: sql.NullString{
  620. String: "firstnullstring",
  621. Valid: true,
  622. },
  623. },
  624. {
  625. Name: "second",
  626. NullString: sql.NullString{
  627. String: "",
  628. Valid: false,
  629. },
  630. },
  631. }
  632. var value []struct {
  633. Name string `db:"name"`
  634. NullString sql.NullString `db:"value"`
  635. }
  636. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  637. rs := sqlmock.NewRows([]string{"name", "value"}).AddRow(
  638. "first", "firstnullstring").AddRow("second", nil)
  639. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  640. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  641. return unmarshalRows(&value, rows, true)
  642. }, "select name, age from users where user=?", "anyone"))
  643. for i, each := range expect {
  644. assert.Equal(t, each.Name, value[i].Name)
  645. assert.Equal(t, each.NullString.String, value[i].NullString.String)
  646. assert.Equal(t, each.NullString.Valid, value[i].NullString.Valid)
  647. }
  648. })
  649. }
  650. func TestUnmarshalRowsStructWithTags(t *testing.T) {
  651. expect := []struct {
  652. Name string
  653. Age int64
  654. }{
  655. {
  656. Name: "first",
  657. Age: 2,
  658. },
  659. {
  660. Name: "second",
  661. Age: 3,
  662. },
  663. }
  664. var value []struct {
  665. Age int64 `db:"age"`
  666. Name string `db:"name"`
  667. }
  668. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  669. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
  670. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  671. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  672. return unmarshalRows(&value, rows, true)
  673. }, "select name, age from users where user=?", "anyone"))
  674. for i, each := range expect {
  675. assert.Equal(t, each.Name, value[i].Name)
  676. assert.Equal(t, each.Age, value[i].Age)
  677. }
  678. })
  679. }
  680. func TestUnmarshalRowsStructAndEmbeddedAnonymousStructWithTags(t *testing.T) {
  681. type Embed struct {
  682. Value int64 `db:"value"`
  683. }
  684. expect := []struct {
  685. Name string
  686. Age int64
  687. Value int64
  688. }{
  689. {
  690. Name: "first",
  691. Age: 2,
  692. Value: 3,
  693. },
  694. {
  695. Name: "second",
  696. Age: 3,
  697. Value: 4,
  698. },
  699. }
  700. var value []struct {
  701. Name string `db:"name"`
  702. Age int64 `db:"age"`
  703. Embed
  704. }
  705. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  706. rs := sqlmock.NewRows([]string{"name", "age", "value"}).FromCSVString("first,2,3\nsecond,3,4")
  707. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  708. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  709. return unmarshalRows(&value, rows, true)
  710. }, "select name, age, value from users where user=?", "anyone"))
  711. for i, each := range expect {
  712. assert.Equal(t, each.Name, value[i].Name)
  713. assert.Equal(t, each.Age, value[i].Age)
  714. assert.Equal(t, each.Value, value[i].Value)
  715. }
  716. })
  717. }
  718. func TestUnmarshalRowsStructAndEmbeddedStructPtrAnonymousWithTags(t *testing.T) {
  719. type Embed struct {
  720. Value int64 `db:"value"`
  721. }
  722. expect := []struct {
  723. Name string
  724. Age int64
  725. Value int64
  726. }{
  727. {
  728. Name: "first",
  729. Age: 2,
  730. Value: 3,
  731. },
  732. {
  733. Name: "second",
  734. Age: 3,
  735. Value: 4,
  736. },
  737. }
  738. var value []struct {
  739. Name string `db:"name"`
  740. Age int64 `db:"age"`
  741. *Embed
  742. }
  743. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  744. rs := sqlmock.NewRows([]string{"name", "age", "value"}).FromCSVString("first,2,3\nsecond,3,4")
  745. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  746. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  747. return unmarshalRows(&value, rows, true)
  748. }, "select name, age, value from users where user=?", "anyone"))
  749. for i, each := range expect {
  750. assert.Equal(t, each.Name, value[i].Name)
  751. assert.Equal(t, each.Age, value[i].Age)
  752. assert.Equal(t, each.Value, value[i].Value)
  753. }
  754. })
  755. }
  756. func TestUnmarshalRowsStructPtr(t *testing.T) {
  757. expect := []*struct {
  758. Name string
  759. Age int64
  760. }{
  761. {
  762. Name: "first",
  763. Age: 2,
  764. },
  765. {
  766. Name: "second",
  767. Age: 3,
  768. },
  769. }
  770. var value []*struct {
  771. Name string
  772. Age int64
  773. }
  774. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  775. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
  776. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  777. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  778. return unmarshalRows(&value, rows, true)
  779. }, "select name, age from users where user=?", "anyone"))
  780. for i, each := range expect {
  781. assert.Equal(t, each.Name, value[i].Name)
  782. assert.Equal(t, each.Age, value[i].Age)
  783. }
  784. })
  785. }
  786. func TestUnmarshalRowsStructWithTagsPtr(t *testing.T) {
  787. expect := []*struct {
  788. Name string
  789. Age int64
  790. }{
  791. {
  792. Name: "first",
  793. Age: 2,
  794. },
  795. {
  796. Name: "second",
  797. Age: 3,
  798. },
  799. }
  800. var value []*struct {
  801. Age int64 `db:"age"`
  802. Name string `db:"name"`
  803. }
  804. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  805. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
  806. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  807. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  808. return unmarshalRows(&value, rows, true)
  809. }, "select name, age from users where user=?", "anyone"))
  810. for i, each := range expect {
  811. assert.Equal(t, each.Name, value[i].Name)
  812. assert.Equal(t, each.Age, value[i].Age)
  813. }
  814. })
  815. }
  816. func TestUnmarshalRowsStructWithTagsPtrWithInnerPtr(t *testing.T) {
  817. expect := []*struct {
  818. Name string
  819. Age int64
  820. }{
  821. {
  822. Name: "first",
  823. Age: 2,
  824. },
  825. {
  826. Name: "second",
  827. Age: 3,
  828. },
  829. }
  830. var value []*struct {
  831. Age *int64 `db:"age"`
  832. Name string `db:"name"`
  833. }
  834. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  835. rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
  836. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  837. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  838. return unmarshalRows(&value, rows, true)
  839. }, "select name, age from users where user=?", "anyone"))
  840. for i, each := range expect {
  841. assert.Equal(t, each.Name, value[i].Name)
  842. assert.Equal(t, each.Age, *value[i].Age)
  843. }
  844. })
  845. }
  846. func TestCommonSqlConn_QueryRowOptional(t *testing.T) {
  847. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  848. rs := sqlmock.NewRows([]string{"age"}).FromCSVString("5")
  849. mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
  850. var r struct {
  851. User string `db:"user"`
  852. Age int `db:"age"`
  853. }
  854. assert.Nil(t, query(db, func(rows *sql.Rows) error {
  855. return unmarshalRow(&r, rows, false)
  856. }, "select age from users where user=?", "anyone"))
  857. assert.Empty(t, r.User)
  858. assert.Equal(t, 5, r.Age)
  859. })
  860. }
  861. func TestUnmarshalRowError(t *testing.T) {
  862. tests := []struct {
  863. name string
  864. colErr error
  865. scanErr error
  866. err error
  867. next int
  868. validate func(err error)
  869. }{
  870. {
  871. name: "with error",
  872. err: errors.New("foo"),
  873. validate: func(err error) {
  874. assert.NotNil(t, err)
  875. },
  876. },
  877. {
  878. name: "without next",
  879. validate: func(err error) {
  880. assert.Equal(t, ErrNotFound, err)
  881. },
  882. },
  883. {
  884. name: "with error",
  885. scanErr: errors.New("foo"),
  886. next: 1,
  887. validate: func(err error) {
  888. assert.Equal(t, ErrNotFound, err)
  889. },
  890. },
  891. }
  892. for _, test := range tests {
  893. t.Run(test.name, func(t *testing.T) {
  894. runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
  895. rs := sqlmock.NewRows([]string{"age"}).FromCSVString("5")
  896. mock.ExpectQuery("select (.+) from users where user=?").WithArgs(
  897. "anyone").WillReturnRows(rs)
  898. var r struct {
  899. User string `db:"user"`
  900. Age int `db:"age"`
  901. }
  902. test.validate(query(db, func(rows *sql.Rows) error {
  903. scanner := mockedScanner{
  904. colErr: test.colErr,
  905. scanErr: test.scanErr,
  906. err: test.err,
  907. }
  908. return unmarshalRow(&r, &scanner, false)
  909. }, "select age from users where user=?", "anyone"))
  910. })
  911. })
  912. }
  913. }
  914. func runOrmTest(t *testing.T, fn func(db *sql.DB, mock sqlmock.Sqlmock)) {
  915. logx.Disable()
  916. db, mock, err := sqlmock.New()
  917. if err != nil {
  918. t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
  919. }
  920. defer db.Close()
  921. fn(db, mock)
  922. if err := mock.ExpectationsWereMet(); err != nil {
  923. t.Errorf("there were unfulfilled expectations: %s", err)
  924. }
  925. }
  926. type mockedScanner struct {
  927. colErr error
  928. scanErr error
  929. err error
  930. next int
  931. }
  932. func (m *mockedScanner) Columns() ([]string, error) {
  933. return nil, m.colErr
  934. }
  935. func (m *mockedScanner) Err() error {
  936. return m.err
  937. }
  938. func (m *mockedScanner) Next() bool {
  939. if m.next > 0 {
  940. m.next--
  941. return true
  942. }
  943. return false
  944. }
  945. func (m *mockedScanner) Scan(v ...interface{}) error {
  946. return m.scanErr
  947. }