THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1KB

  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. }