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.

unit_tests.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 db
  5. import (
  6. "context"
  7. "math"
  8. "code.gitea.io/gitea/modules/unittestbridge"
  9. "xorm.io/xorm"
  10. )
  11. // Code in this file is mainly used by models.CheckConsistencyFor, which is not in the unit test for various reasons.
  12. // In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too.
  13. // NonexistentID an ID that will never exist
  14. const NonexistentID = int64(math.MaxInt64)
  15. //SetUnitTestEngine is used by unit test code
  16. func SetUnitTestEngine(eng *xorm.Engine) {
  17. x = eng
  18. DefaultContext = &Context{
  19. Context: context.Background(),
  20. e: x,
  21. }
  22. }
  23. type testCond struct {
  24. query interface{}
  25. args []interface{}
  26. }
  27. // Cond create a condition with arguments for a test
  28. func Cond(query interface{}, args ...interface{}) interface{} {
  29. return &testCond{query: query, args: args}
  30. }
  31. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  32. for _, condition := range conditions {
  33. switch cond := condition.(type) {
  34. case *testCond:
  35. sess.Where(cond.query, cond.args...)
  36. default:
  37. sess.Where(cond)
  38. }
  39. }
  40. }
  41. // LoadBeanIfExists loads beans from fixture database if exist
  42. func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  43. sess := x.NewSession()
  44. defer sess.Close()
  45. whereConditions(sess, conditions)
  46. return sess.Get(bean)
  47. }
  48. // BeanExists for testing, check if a bean exists
  49. func BeanExists(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) bool {
  50. ta := unittestbridge.NewAsserter(t)
  51. exists, err := LoadBeanIfExists(bean, conditions...)
  52. ta.NoError(err)
  53. return exists
  54. }
  55. // AssertExistsAndLoadBean assert that a bean exists and load it from the test database
  56. func AssertExistsAndLoadBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) interface{} {
  57. ta := unittestbridge.NewAsserter(t)
  58. exists, err := LoadBeanIfExists(bean, conditions...)
  59. ta.NoError(err)
  60. ta.True(exists,
  61. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  62. bean, bean, conditions)
  63. return bean
  64. }
  65. // GetCount get the count of a bean
  66. func GetCount(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) int {
  67. ta := unittestbridge.NewAsserter(t)
  68. sess := x.NewSession()
  69. defer sess.Close()
  70. whereConditions(sess, conditions)
  71. count, err := sess.Count(bean)
  72. ta.NoError(err)
  73. return int(count)
  74. }
  75. // AssertNotExistsBean assert that a bean does not exist in the test database
  76. func AssertNotExistsBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) {
  77. ta := unittestbridge.NewAsserter(t)
  78. exists, err := LoadBeanIfExists(bean, conditions...)
  79. ta.NoError(err)
  80. ta.False(exists)
  81. }
  82. // AssertExistsIf asserts that a bean exists or does not exist, depending on
  83. // what is expected.
  84. func AssertExistsIf(t unittestbridge.Tester, expected bool, bean interface{}, conditions ...interface{}) {
  85. ta := unittestbridge.NewAsserter(t)
  86. exists, err := LoadBeanIfExists(bean, conditions...)
  87. ta.NoError(err)
  88. ta.Equal(expected, exists)
  89. }
  90. // AssertSuccessfulInsert assert that beans is successfully inserted
  91. func AssertSuccessfulInsert(t unittestbridge.Tester, beans ...interface{}) {
  92. ta := unittestbridge.NewAsserter(t)
  93. _, err := x.Insert(beans...)
  94. ta.NoError(err)
  95. }
  96. // AssertCount assert the count of a bean
  97. func AssertCount(t unittestbridge.Tester, bean, expected interface{}) {
  98. ta := unittestbridge.NewAsserter(t)
  99. ta.EqualValues(expected, GetCount(ta, bean))
  100. }
  101. // AssertInt64InRange assert value is in range [low, high]
  102. func AssertInt64InRange(t unittestbridge.Tester, low, high, value int64) {
  103. ta := unittestbridge.NewAsserter(t)
  104. ta.True(value >= low && value <= high,
  105. "Expected value in range [%d, %d], found %d", low, high, value)
  106. }