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.

setup_for_test.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/go-xorm/core"
  12. "github.com/go-xorm/xorm"
  13. _ "github.com/mattn/go-sqlite3" // for the test engine
  14. "github.com/stretchr/testify/assert"
  15. "gopkg.in/testfixtures.v2"
  16. )
  17. const NonexistentID = 9223372036854775807
  18. func TestMain(m *testing.M) {
  19. if err := CreateTestEngine(); err != nil {
  20. fmt.Printf("Error creating test engine: %v\n", err)
  21. os.Exit(1)
  22. }
  23. setting.AppURL = "https://try.gitea.io/"
  24. setting.RunUser = "runuser"
  25. setting.SSH.Port = 3000
  26. setting.SSH.Domain = "try.gitea.io"
  27. setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
  28. setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
  29. os.Exit(m.Run())
  30. }
  31. // CreateTestEngine create an xorm engine for testing
  32. func CreateTestEngine() error {
  33. var err error
  34. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  35. if err != nil {
  36. return err
  37. }
  38. x.SetMapper(core.GonicMapper{})
  39. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  40. return err
  41. }
  42. return InitFixtures(&testfixtures.SQLite{}, "fixtures/")
  43. }
  44. func TestFixturesAreConsistent(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. CheckConsistencyForAll(t)
  47. }
  48. // PrepareTestDatabase load test fixtures into test database
  49. func PrepareTestDatabase() error {
  50. return LoadFixtures()
  51. }
  52. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  53. sess := x.NewSession()
  54. defer sess.Close()
  55. for _, cond := range conditions {
  56. sess = sess.Where(cond)
  57. }
  58. return sess.Get(bean)
  59. }
  60. // BeanExists for testing, check if a bean exists
  61. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  62. exists, err := loadBeanIfExists(bean, conditions...)
  63. assert.NoError(t, err)
  64. return exists
  65. }
  66. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  67. // database
  68. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  69. exists, err := loadBeanIfExists(bean, conditions...)
  70. assert.NoError(t, err)
  71. assert.True(t, exists,
  72. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  73. bean, bean, conditions)
  74. return bean
  75. }
  76. // AssertNotExistsBean assert that a bean does not exist in the test database
  77. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  78. exists, err := loadBeanIfExists(bean, conditions...)
  79. assert.NoError(t, err)
  80. assert.False(t, exists)
  81. }
  82. // AssertSuccessfulInsert assert that beans is successfully inserted
  83. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  84. _, err := x.Insert(beans...)
  85. assert.NoError(t, err)
  86. }
  87. // AssertCount assert the count of a bean
  88. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  89. actual, err := x.Count(bean)
  90. assert.NoError(t, err)
  91. assert.EqualValues(t, expected, actual)
  92. }