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

unit_tests.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "fmt"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/Unknwon/com"
  12. "github.com/go-xorm/core"
  13. "github.com/go-xorm/xorm"
  14. _ "github.com/mattn/go-sqlite3" // for the test engine
  15. "github.com/stretchr/testify/assert"
  16. "gopkg.in/testfixtures.v2"
  17. )
  18. // NonexistentID an ID that will never exist
  19. const NonexistentID = 9223372036854775807
  20. // giteaRoot a path to the gitea root
  21. var giteaRoot string
  22. // MainTest a reusable TestMain(..) function for unit tests that need to use a
  23. // test database. Creates the test database, and sets necessary settings.
  24. func MainTest(m *testing.M, pathToGiteaRoot string) {
  25. giteaRoot = pathToGiteaRoot
  26. fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures")
  27. if err := createTestEngine(fixturesDir); err != nil {
  28. fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
  29. os.Exit(1)
  30. }
  31. setting.AppURL = "https://try.gitea.io/"
  32. setting.RunUser = "runuser"
  33. setting.SSH.Port = 3000
  34. setting.SSH.Domain = "try.gitea.io"
  35. setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
  36. setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
  37. os.Exit(m.Run())
  38. }
  39. func createTestEngine(fixturesDir string) error {
  40. var err error
  41. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  42. if err != nil {
  43. return err
  44. }
  45. x.SetMapper(core.GonicMapper{})
  46. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  47. return err
  48. }
  49. switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") {
  50. case "true", "1":
  51. x.ShowSQL(true)
  52. }
  53. return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
  54. }
  55. // PrepareTestDatabase load test fixtures into test database
  56. func PrepareTestDatabase() error {
  57. return LoadFixtures()
  58. }
  59. // PrepareTestEnv prepares the environment for unit tests. Can only be called
  60. // by tests that use the above MainTest(..) function.
  61. func PrepareTestEnv(t testing.TB) {
  62. assert.NoError(t, PrepareTestDatabase())
  63. assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
  64. metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
  65. assert.NoError(t, com.CopyDir(metaPath, setting.RepoRootPath))
  66. }
  67. type testCond struct {
  68. query interface{}
  69. args []interface{}
  70. }
  71. // Cond create a condition with arguments for a test
  72. func Cond(query interface{}, args ...interface{}) interface{} {
  73. return &testCond{query: query, args: args}
  74. }
  75. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  76. for _, condition := range conditions {
  77. switch cond := condition.(type) {
  78. case *testCond:
  79. sess.Where(cond.query, cond.args...)
  80. default:
  81. sess.Where(cond)
  82. }
  83. }
  84. }
  85. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  86. sess := x.NewSession()
  87. defer sess.Close()
  88. whereConditions(sess, conditions)
  89. return sess.Get(bean)
  90. }
  91. // BeanExists for testing, check if a bean exists
  92. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  93. exists, err := loadBeanIfExists(bean, conditions...)
  94. assert.NoError(t, err)
  95. return exists
  96. }
  97. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  98. // database
  99. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  100. exists, err := loadBeanIfExists(bean, conditions...)
  101. assert.NoError(t, err)
  102. assert.True(t, exists,
  103. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  104. bean, bean, conditions)
  105. return bean
  106. }
  107. // GetCount get the count of a bean
  108. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  109. sess := x.NewSession()
  110. defer sess.Close()
  111. whereConditions(sess, conditions)
  112. count, err := sess.Count(bean)
  113. assert.NoError(t, err)
  114. return int(count)
  115. }
  116. // AssertNotExistsBean assert that a bean does not exist in the test database
  117. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  118. exists, err := loadBeanIfExists(bean, conditions...)
  119. assert.NoError(t, err)
  120. assert.False(t, exists)
  121. }
  122. // AssertSuccessfulInsert assert that beans is successfully inserted
  123. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  124. _, err := x.Insert(beans...)
  125. assert.NoError(t, err)
  126. }
  127. // AssertCount assert the count of a bean
  128. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  129. assert.EqualValues(t, expected, GetCount(t, bean))
  130. }
  131. // AssertInt64InRange assert value is in range [low, high]
  132. func AssertInt64InRange(t *testing.T, low, high, value int64) {
  133. assert.True(t, value >= low && value <= high,
  134. "Expected value in range [%d, %d], found %d", low, high, value)
  135. }