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

unit_tests.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "os"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/core"
  11. "github.com/go-xorm/xorm"
  12. "github.com/stretchr/testify/assert"
  13. "gopkg.in/testfixtures.v2"
  14. )
  15. // NonexistentID an ID that will never exist
  16. const NonexistentID = 9223372036854775807
  17. // CreateTestEngine create in-memory sqlite database for unit tests
  18. // Any package that calls this must import github.com/mattn/go-sqlite3
  19. func CreateTestEngine(fixturesDir string) error {
  20. var err error
  21. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  22. if err != nil {
  23. return err
  24. }
  25. x.SetMapper(core.GonicMapper{})
  26. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  27. return err
  28. }
  29. switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") {
  30. case "true", "1":
  31. x.ShowSQL(true)
  32. }
  33. return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
  34. }
  35. // PrepareTestDatabase load test fixtures into test database
  36. func PrepareTestDatabase() error {
  37. return LoadFixtures()
  38. }
  39. func prepareTestEnv(t testing.TB) {
  40. assert.NoError(t, PrepareTestDatabase())
  41. assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
  42. assert.NoError(t, com.CopyDir("../integrations/gitea-repositories-meta", setting.RepoRootPath))
  43. }
  44. type testCond struct {
  45. query interface{}
  46. args []interface{}
  47. }
  48. // Cond create a condition with arguments for a test
  49. func Cond(query interface{}, args ...interface{}) interface{} {
  50. return &testCond{query: query, args: args}
  51. }
  52. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  53. for _, condition := range conditions {
  54. switch cond := condition.(type) {
  55. case *testCond:
  56. sess.Where(cond.query, cond.args...)
  57. default:
  58. sess.Where(cond)
  59. }
  60. }
  61. }
  62. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  63. sess := x.NewSession()
  64. defer sess.Close()
  65. whereConditions(sess, conditions)
  66. return sess.Get(bean)
  67. }
  68. // BeanExists for testing, check if a bean exists
  69. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  70. exists, err := loadBeanIfExists(bean, conditions...)
  71. assert.NoError(t, err)
  72. return exists
  73. }
  74. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  75. // database
  76. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  77. exists, err := loadBeanIfExists(bean, conditions...)
  78. assert.NoError(t, err)
  79. assert.True(t, exists,
  80. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  81. bean, bean, conditions)
  82. return bean
  83. }
  84. // GetCount get the count of a bean
  85. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  86. sess := x.NewSession()
  87. defer sess.Close()
  88. whereConditions(sess, conditions)
  89. count, err := sess.Count(bean)
  90. assert.NoError(t, err)
  91. return int(count)
  92. }
  93. // AssertNotExistsBean assert that a bean does not exist in the test database
  94. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  95. exists, err := loadBeanIfExists(bean, conditions...)
  96. assert.NoError(t, err)
  97. assert.False(t, exists)
  98. }
  99. // AssertSuccessfulInsert assert that beans is successfully inserted
  100. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  101. _, err := x.Insert(beans...)
  102. assert.NoError(t, err)
  103. }
  104. // AssertCount assert the count of a bean
  105. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  106. assert.EqualValues(t, expected, GetCount(t, bean))
  107. }
  108. // AssertInt64InRange assert value is in range [low, high]
  109. func AssertInt64InRange(t *testing.T, low, high, value int64) {
  110. assert.True(t, value >= low && value <= high,
  111. "Expected value in range [%d, %d], found %d", low, high, value)
  112. }