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.

setup_for_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. var fixtures *testfixtures.Context
  32. // CreateTestEngine create an xorm engine for testing
  33. func CreateTestEngine() error {
  34. testfixtures.SkipDatabaseNameCheck(true)
  35. var err error
  36. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  37. if err != nil {
  38. return err
  39. }
  40. x.SetMapper(core.GonicMapper{})
  41. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  42. return err
  43. }
  44. fixtures, err = testfixtures.NewFolder(x.DB().DB, &testfixtures.SQLite{}, "fixtures/")
  45. return err
  46. }
  47. func TestFixturesAreConsistent(t *testing.T) {
  48. assert.NoError(t, PrepareTestDatabase())
  49. CheckConsistencyForAll(t)
  50. }
  51. // PrepareTestDatabase load test fixtures into test database
  52. func PrepareTestDatabase() error {
  53. return fixtures.Load()
  54. }
  55. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  56. sess := x.NewSession()
  57. defer sess.Close()
  58. for _, cond := range conditions {
  59. sess = sess.Where(cond)
  60. }
  61. return sess.Get(bean)
  62. }
  63. // BeanExists for testing, check if a bean exists
  64. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  65. exists, err := loadBeanIfExists(bean, conditions...)
  66. assert.NoError(t, err)
  67. return exists
  68. }
  69. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  70. // database
  71. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  72. exists, err := loadBeanIfExists(bean, conditions...)
  73. assert.NoError(t, err)
  74. assert.True(t, exists,
  75. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  76. bean, bean, conditions)
  77. return bean
  78. }
  79. // AssertNotExistsBean assert that a bean does not exist in the test database
  80. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  81. exists, err := loadBeanIfExists(bean, conditions...)
  82. assert.NoError(t, err)
  83. assert.False(t, exists)
  84. }
  85. // AssertSuccessfulInsert assert that beans is successfully inserted
  86. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  87. _, err := x.Insert(beans...)
  88. assert.NoError(t, err)
  89. }
  90. // AssertSuccessfulUpdate assert that bean is successfully updated
  91. func AssertSuccessfulUpdate(t *testing.T, bean interface{}, conditions ...interface{}) {
  92. _, err := x.Update(bean, conditions...)
  93. assert.NoError(t, err)
  94. }