session_iterate_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestIterate(t *testing.T) {
  10. assert.NoError(t, prepareEngine())
  11. type UserIterate struct {
  12. Id int64
  13. IsMan bool
  14. }
  15. assert.NoError(t, testEngine.Sync2(new(UserIterate)))
  16. cnt, err := testEngine.Insert(&UserIterate{
  17. IsMan: true,
  18. })
  19. assert.NoError(t, err)
  20. assert.EqualValues(t, 1, cnt)
  21. cnt = 0
  22. err = testEngine.Iterate(new(UserIterate), func(i int, bean interface{}) error {
  23. user := bean.(*UserIterate)
  24. assert.EqualValues(t, 1, user.Id)
  25. assert.EqualValues(t, true, user.IsMan)
  26. cnt++
  27. return nil
  28. })
  29. assert.NoError(t, err)
  30. assert.EqualValues(t, 1, cnt)
  31. }
  32. func TestBufferIterate(t *testing.T) {
  33. assert.NoError(t, prepareEngine())
  34. type UserBufferIterate struct {
  35. Id int64
  36. IsMan bool
  37. }
  38. assert.NoError(t, testEngine.Sync2(new(UserBufferIterate)))
  39. var size = 20
  40. for i := 0; i < size; i++ {
  41. cnt, err := testEngine.Insert(&UserBufferIterate{
  42. IsMan: true,
  43. })
  44. assert.NoError(t, err)
  45. assert.EqualValues(t, 1, cnt)
  46. }
  47. var cnt = 0
  48. err := testEngine.BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
  49. user := bean.(*UserBufferIterate)
  50. assert.EqualValues(t, cnt+1, user.Id)
  51. assert.EqualValues(t, true, user.IsMan)
  52. cnt++
  53. return nil
  54. })
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, size, cnt)
  57. cnt = 0
  58. err = testEngine.Limit(20).BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
  59. user := bean.(*UserBufferIterate)
  60. assert.EqualValues(t, cnt+1, user.Id)
  61. assert.EqualValues(t, true, user.IsMan)
  62. cnt++
  63. return nil
  64. })
  65. assert.NoError(t, err)
  66. assert.EqualValues(t, size, cnt)
  67. cnt = 0
  68. err = testEngine.Limit(7).BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
  69. user := bean.(*UserBufferIterate)
  70. assert.EqualValues(t, cnt+1, user.Id)
  71. assert.EqualValues(t, true, user.IsMan)
  72. cnt++
  73. return nil
  74. })
  75. assert.NoError(t, err)
  76. assert.EqualValues(t, 7, cnt)
  77. }