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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "testing"
  7. "github.com/go-xorm/core"
  8. "github.com/go-xorm/xorm"
  9. "github.com/stretchr/testify/assert"
  10. "gopkg.in/testfixtures.v2"
  11. )
  12. // NonexistentID an ID that will never exist
  13. const NonexistentID = 9223372036854775807
  14. // CreateTestEngine create in-memory sqlite database for unit tests
  15. // Any package that calls this must import github.com/mattn/go-sqlite3
  16. func CreateTestEngine(fixturesDir string) error {
  17. var err error
  18. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  19. if err != nil {
  20. return err
  21. }
  22. x.SetMapper(core.GonicMapper{})
  23. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  24. return err
  25. }
  26. x.ShowSQL(true)
  27. return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
  28. }
  29. // PrepareTestDatabase load test fixtures into test database
  30. func PrepareTestDatabase() error {
  31. return LoadFixtures()
  32. }
  33. type testCond struct {
  34. query interface{}
  35. args []interface{}
  36. }
  37. // Cond create a condition with arguments for a test
  38. func Cond(query interface{}, args ...interface{}) interface{} {
  39. return &testCond{query: query, args: args}
  40. }
  41. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  42. for _, condition := range conditions {
  43. switch cond := condition.(type) {
  44. case *testCond:
  45. sess.Where(cond.query, cond.args...)
  46. default:
  47. sess.Where(cond)
  48. }
  49. }
  50. }
  51. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  52. sess := x.NewSession()
  53. defer sess.Close()
  54. whereConditions(sess, conditions)
  55. return sess.Get(bean)
  56. }
  57. // BeanExists for testing, check if a bean exists
  58. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  59. exists, err := loadBeanIfExists(bean, conditions...)
  60. assert.NoError(t, err)
  61. return exists
  62. }
  63. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  64. // database
  65. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  66. exists, err := loadBeanIfExists(bean, conditions...)
  67. assert.NoError(t, err)
  68. assert.True(t, exists,
  69. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  70. bean, bean, conditions)
  71. return bean
  72. }
  73. // GetCount get the count of a bean
  74. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  75. sess := x.NewSession()
  76. defer sess.Close()
  77. whereConditions(sess, conditions)
  78. count, err := sess.Count(bean)
  79. assert.NoError(t, err)
  80. return int(count)
  81. }
  82. // AssertNotExistsBean assert that a bean does not exist in the test database
  83. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  84. exists, err := loadBeanIfExists(bean, conditions...)
  85. assert.NoError(t, err)
  86. assert.False(t, exists)
  87. }
  88. // AssertSuccessfulInsert assert that beans is successfully inserted
  89. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  90. _, err := x.Insert(beans...)
  91. assert.NoError(t, err)
  92. }
  93. // AssertCount assert the count of a bean
  94. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  95. assert.EqualValues(t, expected, GetCount(t, bean))
  96. }
  97. // AssertInt64InRange assert value is in range [low, high]
  98. func AssertInt64InRange(t *testing.T, low, high, value int64) {
  99. assert.True(t, value >= low && value <= high,
  100. "Expected value in range [%d, %d], found %d", low, high, value)
  101. }