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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. x.ShowSQL(true)
  30. return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
  31. }
  32. // PrepareTestDatabase load test fixtures into test database
  33. func PrepareTestDatabase() error {
  34. return LoadFixtures()
  35. }
  36. func prepareTestEnv(t testing.TB) {
  37. assert.NoError(t, PrepareTestDatabase())
  38. assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
  39. assert.NoError(t, com.CopyDir("../integrations/gitea-repositories-meta", setting.RepoRootPath))
  40. }
  41. type testCond struct {
  42. query interface{}
  43. args []interface{}
  44. }
  45. // Cond create a condition with arguments for a test
  46. func Cond(query interface{}, args ...interface{}) interface{} {
  47. return &testCond{query: query, args: args}
  48. }
  49. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  50. for _, condition := range conditions {
  51. switch cond := condition.(type) {
  52. case *testCond:
  53. sess.Where(cond.query, cond.args...)
  54. default:
  55. sess.Where(cond)
  56. }
  57. }
  58. }
  59. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  60. sess := x.NewSession()
  61. defer sess.Close()
  62. whereConditions(sess, conditions)
  63. return sess.Get(bean)
  64. }
  65. // BeanExists for testing, check if a bean exists
  66. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  67. exists, err := loadBeanIfExists(bean, conditions...)
  68. assert.NoError(t, err)
  69. return exists
  70. }
  71. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  72. // database
  73. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  74. exists, err := loadBeanIfExists(bean, conditions...)
  75. assert.NoError(t, err)
  76. assert.True(t, exists,
  77. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  78. bean, bean, conditions)
  79. return bean
  80. }
  81. // GetCount get the count of a bean
  82. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  83. sess := x.NewSession()
  84. defer sess.Close()
  85. whereConditions(sess, conditions)
  86. count, err := sess.Count(bean)
  87. assert.NoError(t, err)
  88. return int(count)
  89. }
  90. // AssertNotExistsBean assert that a bean does not exist in the test database
  91. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  92. exists, err := loadBeanIfExists(bean, conditions...)
  93. assert.NoError(t, err)
  94. assert.False(t, exists)
  95. }
  96. // AssertSuccessfulInsert assert that beans is successfully inserted
  97. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  98. _, err := x.Insert(beans...)
  99. assert.NoError(t, err)
  100. }
  101. // AssertCount assert the count of a bean
  102. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  103. assert.EqualValues(t, expected, GetCount(t, bean))
  104. }
  105. // AssertInt64InRange assert value is in range [low, high]
  106. func AssertInt64InRange(t *testing.T, low, high, value int64) {
  107. assert.True(t, value >= low && value <= high,
  108. "Expected value in range [%d, %d], found %d", low, high, value)
  109. }