rows_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 TestRows(t *testing.T) {
  10. assert.NoError(t, prepareEngine())
  11. type UserRows struct {
  12. Id int64
  13. IsMan bool
  14. }
  15. assert.NoError(t, testEngine.Sync2(new(UserRows)))
  16. cnt, err := testEngine.Insert(&UserRows{
  17. IsMan: true,
  18. })
  19. assert.NoError(t, err)
  20. assert.EqualValues(t, 1, cnt)
  21. rows, err := testEngine.Rows(new(UserRows))
  22. assert.NoError(t, err)
  23. defer rows.Close()
  24. cnt = 0
  25. user := new(UserRows)
  26. for rows.Next() {
  27. err = rows.Scan(user)
  28. assert.NoError(t, err)
  29. cnt++
  30. }
  31. assert.EqualValues(t, 1, cnt)
  32. assert.False(t, rows.Next())
  33. assert.NoError(t, rows.Close())
  34. rows0, err := testEngine.Where("1>1").Rows(new(UserRows))
  35. assert.NoError(t, err)
  36. defer rows0.Close()
  37. cnt = 0
  38. user0 := new(UserRows)
  39. for rows0.Next() {
  40. err = rows0.Scan(user0)
  41. assert.NoError(t, err)
  42. cnt++
  43. }
  44. assert.EqualValues(t, 0, cnt)
  45. assert.NoError(t, rows0.Close())
  46. sess := testEngine.NewSession()
  47. defer sess.Close()
  48. rows1, err := sess.Prepare().Rows(new(UserRows))
  49. assert.NoError(t, err)
  50. defer rows1.Close()
  51. cnt = 0
  52. for rows1.Next() {
  53. err = rows1.Scan(user)
  54. assert.NoError(t, err)
  55. cnt++
  56. }
  57. assert.EqualValues(t, 1, cnt)
  58. var tbName = testEngine.Quote(testEngine.TableName(user, true))
  59. rows2, err := testEngine.SQL("SELECT * FROM " + tbName).Rows(new(UserRows))
  60. assert.NoError(t, err)
  61. defer rows2.Close()
  62. cnt = 0
  63. for rows2.Next() {
  64. err = rows2.Scan(user)
  65. assert.NoError(t, err)
  66. cnt++
  67. }
  68. assert.EqualValues(t, 1, cnt)
  69. }
  70. func TestRowsMyTableName(t *testing.T) {
  71. assert.NoError(t, prepareEngine())
  72. type UserRowsMyTable struct {
  73. Id int64
  74. IsMan bool
  75. }
  76. var tableName = "user_rows_my_table_name"
  77. assert.NoError(t, testEngine.Table(tableName).Sync2(new(UserRowsMyTable)))
  78. cnt, err := testEngine.Table(tableName).Insert(&UserRowsMyTable{
  79. IsMan: true,
  80. })
  81. assert.NoError(t, err)
  82. assert.EqualValues(t, 1, cnt)
  83. rows, err := testEngine.Table(tableName).Rows(new(UserRowsMyTable))
  84. assert.NoError(t, err)
  85. defer rows.Close()
  86. cnt = 0
  87. user := new(UserRowsMyTable)
  88. for rows.Next() {
  89. err = rows.Scan(user)
  90. assert.NoError(t, err)
  91. cnt++
  92. }
  93. assert.EqualValues(t, 1, cnt)
  94. }
  95. type UserRowsSpecTable struct {
  96. Id int64
  97. IsMan bool
  98. }
  99. func (UserRowsSpecTable) TableName() string {
  100. return "user_rows_my_table_name"
  101. }
  102. func TestRowsSpecTableName(t *testing.T) {
  103. assert.NoError(t, prepareEngine())
  104. assert.NoError(t, testEngine.Sync2(new(UserRowsSpecTable)))
  105. cnt, err := testEngine.Insert(&UserRowsSpecTable{
  106. IsMan: true,
  107. })
  108. assert.NoError(t, err)
  109. assert.EqualValues(t, 1, cnt)
  110. rows, err := testEngine.Rows(new(UserRowsSpecTable))
  111. assert.NoError(t, err)
  112. defer rows.Close()
  113. cnt = 0
  114. user := new(UserRowsSpecTable)
  115. for rows.Next() {
  116. err = rows.Scan(user)
  117. assert.NoError(t, err)
  118. cnt++
  119. }
  120. assert.EqualValues(t, 1, cnt)
  121. }