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 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // PrepareTestDatabase load test fixtures into test database
  48. func PrepareTestDatabase() error {
  49. return fixtures.Load()
  50. }
  51. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  52. sess := x.NewSession()
  53. defer sess.Close()
  54. for _, cond := range conditions {
  55. sess = sess.Where(cond)
  56. }
  57. return sess.Get(bean)
  58. }
  59. // BeanExists for testing, check if a bean exists
  60. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  61. exists, err := loadBeanIfExists(bean, conditions...)
  62. assert.NoError(t, err)
  63. return exists
  64. }
  65. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  66. // database
  67. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  68. exists, err := loadBeanIfExists(bean, conditions...)
  69. assert.NoError(t, err)
  70. assert.True(t, exists)
  71. return bean
  72. }
  73. // AssertNotExistsBean assert that a bean does not exist in the test database
  74. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  75. exists, err := loadBeanIfExists(bean, conditions...)
  76. assert.NoError(t, err)
  77. assert.False(t, exists)
  78. }