utils_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package model_test
  2. import (
  3. "errors"
  4. "go/constant"
  5. "testing"
  6. "github.com/labring/aiproxy/core/model"
  7. "github.com/smartystreets/goconvey/convey"
  8. "gorm.io/gorm"
  9. )
  10. func TestString2Int(t *testing.T) {
  11. convey.Convey("String2Int", t, func() {
  12. convey.Convey("should convert valid string", func() {
  13. convey.So(model.String2Int("123"), convey.ShouldEqual, 123)
  14. })
  15. convey.Convey("should return 0 for empty string", func() {
  16. convey.So(model.String2Int(""), convey.ShouldEqual, 0)
  17. })
  18. convey.Convey("should return 0 for invalid string", func() {
  19. convey.So(model.String2Int("abc"), convey.ShouldEqual, 0)
  20. })
  21. })
  22. }
  23. func TestToLimitOffset(t *testing.T) {
  24. convey.Convey("toLimitOffset", t, func() {
  25. convey.Convey("should calculate correct offset", func() {
  26. limit, offset := model.ToLimitOffset(1, 10)
  27. convey.So(limit, convey.ShouldEqual, 10)
  28. convey.So(offset, convey.ShouldEqual, 0)
  29. limit, offset = model.ToLimitOffset(2, 10)
  30. convey.So(limit, convey.ShouldEqual, 10)
  31. convey.So(offset, convey.ShouldEqual, 10)
  32. })
  33. convey.Convey("should handle page < 1", func() {
  34. _, offset := model.ToLimitOffset(0, 10)
  35. convey.So(offset, convey.ShouldEqual, 0)
  36. })
  37. convey.Convey("should clamp perPage", func() {
  38. // Default 10 if <= 0
  39. limit, _ := model.ToLimitOffset(1, 0)
  40. convey.So(limit, convey.ShouldEqual, 10)
  41. // Max 100
  42. limit, _ = model.ToLimitOffset(1, 101)
  43. convey.So(limit, convey.ShouldEqual, 100)
  44. })
  45. })
  46. }
  47. func TestErrors(t *testing.T) {
  48. convey.Convey("Errors", t, func() {
  49. convey.Convey("NotFoundError", func() {
  50. err := model.NotFoundError("user")
  51. convey.So(errors.Is(err, gorm.ErrRecordNotFound), convey.ShouldBeTrue)
  52. convey.So(err.Error(), convey.ShouldContainSubstring, "user")
  53. })
  54. convey.Convey("HandleNotFound", func() {
  55. // Should wrap ErrRecordNotFound
  56. err := model.HandleNotFound(gorm.ErrRecordNotFound, "user")
  57. convey.So(errors.Is(err, gorm.ErrRecordNotFound), convey.ShouldBeTrue)
  58. convey.So(err.Error(), convey.ShouldContainSubstring, "user")
  59. // Should pass through other errors
  60. otherErr := errors.New("other error")
  61. err = model.HandleNotFound(otherErr, "user")
  62. convey.So(err, convey.ShouldEqual, otherErr)
  63. // Should return nil for nil
  64. err = model.HandleNotFound(nil, "user")
  65. convey.So(err, convey.ShouldBeNil)
  66. })
  67. convey.Convey("IgnoreNotFound", func() {
  68. // Should ignore ErrRecordNotFound
  69. err := model.IgnoreNotFound(gorm.ErrRecordNotFound)
  70. convey.So(err, convey.ShouldBeNil)
  71. // Should pass through other errors
  72. otherErr := errors.New("other error")
  73. err = model.IgnoreNotFound(otherErr)
  74. convey.So(err, convey.ShouldEqual, otherErr)
  75. // Should return nil for nil
  76. err = model.IgnoreNotFound(nil)
  77. convey.So(err, convey.ShouldBeNil)
  78. })
  79. convey.Convey("HandleUpdateResult", func() {
  80. // Error case
  81. res := &gorm.DB{Error: errors.New("db error")}
  82. err := model.HandleUpdateResult(res, "user")
  83. convey.So(err, convey.ShouldNotBeNil)
  84. convey.So(err.Error(), convey.ShouldEqual, "db error")
  85. // RowsAffected == 0 case (should be NotFoundError)
  86. res = &gorm.DB{Error: nil, RowsAffected: 0}
  87. err = model.HandleUpdateResult(res, "user")
  88. convey.So(errors.Is(err, gorm.ErrRecordNotFound), convey.ShouldBeTrue)
  89. convey.So(err.Error(), convey.ShouldContainSubstring, "user")
  90. // Success case
  91. res = &gorm.DB{Error: nil, RowsAffected: 1}
  92. err = model.HandleUpdateResult(res, "user")
  93. convey.So(err, convey.ShouldBeNil)
  94. })
  95. })
  96. }
  97. func TestZeroNullTypes(t *testing.T) {
  98. convey.Convey("ZeroNullInt64", t, func() {
  99. convey.Convey("Value", func() {
  100. var z model.ZeroNullInt64 = 0
  101. v, _ := z.Value()
  102. convey.So(v, convey.ShouldBeNil)
  103. z = 10
  104. v, _ = z.Value()
  105. convey.So(v, convey.ShouldEqual, 10)
  106. })
  107. convey.Convey("Scan", func() {
  108. var z model.ZeroNullInt64
  109. // Nil
  110. convey.So(z.Scan(nil), convey.ShouldBeNil)
  111. convey.So(z, convey.ShouldEqual, 0)
  112. // Int
  113. convey.So(z.Scan(int(123)), convey.ShouldBeNil)
  114. convey.So(z, convey.ShouldEqual, 123)
  115. // Int64
  116. convey.So(z.Scan(int64(456)), convey.ShouldBeNil)
  117. convey.So(z, convey.ShouldEqual, 456)
  118. // String
  119. convey.So(z.Scan("789"), convey.ShouldBeNil)
  120. convey.So(z, convey.ShouldEqual, 789)
  121. // Invalid type
  122. err := z.Scan(true)
  123. convey.So(err, convey.ShouldNotBeNil)
  124. convey.So(err.Error(), convey.ShouldContainSubstring, "unsupported type")
  125. // Invalid string
  126. err = z.Scan("abc")
  127. convey.So(err, convey.ShouldNotBeNil)
  128. })
  129. })
  130. convey.Convey("ZeroNullFloat64", t, func() {
  131. convey.Convey("Value", func() {
  132. var z model.ZeroNullFloat64 = 0
  133. v, _ := z.Value()
  134. convey.So(v, convey.ShouldBeNil)
  135. z = 10.5
  136. v, _ = z.Value()
  137. convey.So(v, convey.ShouldEqual, 10.5)
  138. })
  139. convey.Convey("Scan", func() {
  140. var z model.ZeroNullFloat64
  141. // Nil
  142. convey.So(z.Scan(nil), convey.ShouldBeNil)
  143. convey.So(z, convey.ShouldEqual, 0)
  144. // Float64
  145. convey.So(z.Scan(10.5), convey.ShouldBeNil)
  146. convey.So(z, convey.ShouldEqual, 10.5)
  147. // String
  148. convey.So(z.Scan("20.5"), convey.ShouldBeNil)
  149. convey.So(z, convey.ShouldEqual, 20.5)
  150. // Int (implicit conversion in switch)
  151. convey.So(z.Scan(int(30)), convey.ShouldBeNil)
  152. convey.So(z, convey.ShouldEqual, 30.0)
  153. // Invalid type
  154. err := z.Scan(true)
  155. convey.So(err, convey.ShouldNotBeNil)
  156. convey.So(err.Error(), convey.ShouldContainSubstring, "unsupported type")
  157. // Invalid string
  158. err = z.Scan("abc")
  159. convey.So(err, convey.ShouldNotBeNil)
  160. })
  161. })
  162. convey.Convey("EmptyNullString", t, func() {
  163. convey.Convey("Value", func() {
  164. var s model.EmptyNullString = ""
  165. v, _ := s.Value()
  166. convey.So(v, convey.ShouldBeNil)
  167. s = "test"
  168. v, _ = s.Value()
  169. convey.So(v, convey.ShouldEqual, "test")
  170. })
  171. convey.Convey("Scan", func() {
  172. var s model.EmptyNullString
  173. convey.So(s.Scan(nil), convey.ShouldBeNil)
  174. convey.So(string(s), convey.ShouldEqual, "")
  175. convey.So(s.Scan("test"), convey.ShouldBeNil)
  176. convey.So(string(s), convey.ShouldEqual, "test")
  177. convey.So(s.Scan([]byte("bytes")), convey.ShouldBeNil)
  178. convey.So(string(s), convey.ShouldEqual, "bytes")
  179. // Invalid type
  180. err := s.Scan(123)
  181. convey.So(err, convey.ShouldNotBeNil)
  182. convey.So(err.Error(), convey.ShouldContainSubstring, "unsupported type")
  183. })
  184. convey.Convey(constant.String.String(), func() {
  185. var s model.EmptyNullString = "test"
  186. convey.So(s.String(), convey.ShouldEqual, "test")
  187. })
  188. })
  189. }