Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

unit_tests.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/go-xorm/xorm"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // NonexistentID an ID that will never exist
  11. const NonexistentID = 9223372036854775807
  12. // PrepareTestDatabase load test fixtures into test database
  13. func PrepareTestDatabase() error {
  14. return LoadFixtures()
  15. }
  16. type testCond struct {
  17. query interface{}
  18. args []interface{}
  19. }
  20. // Cond create a condition with arguments for a test
  21. func Cond(query interface{}, args ...interface{}) interface{} {
  22. return &testCond{query: query, args: args}
  23. }
  24. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  25. for _, condition := range conditions {
  26. switch cond := condition.(type) {
  27. case *testCond:
  28. sess.Where(cond.query, cond.args...)
  29. default:
  30. sess.Where(cond)
  31. }
  32. }
  33. }
  34. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  35. sess := x.NewSession()
  36. defer sess.Close()
  37. whereConditions(sess, conditions)
  38. return sess.Get(bean)
  39. }
  40. // BeanExists for testing, check if a bean exists
  41. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  42. exists, err := loadBeanIfExists(bean, conditions...)
  43. assert.NoError(t, err)
  44. return exists
  45. }
  46. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  47. // database
  48. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  49. exists, err := loadBeanIfExists(bean, conditions...)
  50. assert.NoError(t, err)
  51. assert.True(t, exists,
  52. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  53. bean, bean, conditions)
  54. return bean
  55. }
  56. // GetCount get the count of a bean
  57. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  58. sess := x.NewSession()
  59. defer sess.Close()
  60. whereConditions(sess, conditions)
  61. count, err := sess.Count(bean)
  62. assert.NoError(t, err)
  63. return int(count)
  64. }
  65. // AssertNotExistsBean assert that a bean does not exist in the test database
  66. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  67. exists, err := loadBeanIfExists(bean, conditions...)
  68. assert.NoError(t, err)
  69. assert.False(t, exists)
  70. }
  71. // AssertSuccessfulInsert assert that beans is successfully inserted
  72. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  73. _, err := x.Insert(beans...)
  74. assert.NoError(t, err)
  75. }
  76. // AssertCount assert the count of a bean
  77. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  78. assert.EqualValues(t, expected, GetCount(t, bean))
  79. }
  80. // AssertInt64InRange assert value is in range [low, high]
  81. func AssertInt64InRange(t *testing.T, low, high, value int64) {
  82. assert.True(t, value >= low && value <= high,
  83. "Expected value in range [%d, %d], found %d", low, high, value)
  84. }