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.

92 lines
2.6 KiB

  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/mattn/go-sqlite3" // for the test engine
  10. "github.com/stretchr/testify/assert"
  11. "gopkg.in/testfixtures.v2"
  12. )
  13. // NonexistentID an ID that will never exist
  14. const NonexistentID = 9223372036854775807
  15. // CreateTestEngine create an xorm engine for testing
  16. func CreateTestEngine() 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. return InitFixtures(&testfixtures.SQLite{}, "fixtures/")
  27. }
  28. // TestFixturesAreConsistent assert that test fixtures are consistent
  29. func TestFixturesAreConsistent(t *testing.T) {
  30. assert.NoError(t, PrepareTestDatabase())
  31. CheckConsistencyForAll(t)
  32. }
  33. // PrepareTestDatabase load test fixtures into test database
  34. func PrepareTestDatabase() error {
  35. return LoadFixtures()
  36. }
  37. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  38. sess := x.NewSession()
  39. defer sess.Close()
  40. for _, cond := range conditions {
  41. sess = sess.Where(cond)
  42. }
  43. return sess.Get(bean)
  44. }
  45. // BeanExists for testing, check if a bean exists
  46. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  47. exists, err := loadBeanIfExists(bean, conditions...)
  48. assert.NoError(t, err)
  49. return exists
  50. }
  51. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  52. // database
  53. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  54. exists, err := loadBeanIfExists(bean, conditions...)
  55. assert.NoError(t, err)
  56. assert.True(t, exists,
  57. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  58. bean, bean, conditions)
  59. return bean
  60. }
  61. // AssertNotExistsBean assert that a bean does not exist in the test database
  62. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  63. exists, err := loadBeanIfExists(bean, conditions...)
  64. assert.NoError(t, err)
  65. assert.False(t, exists)
  66. }
  67. // AssertSuccessfulInsert assert that beans is successfully inserted
  68. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  69. _, err := x.Insert(beans...)
  70. assert.NoError(t, err)
  71. }
  72. // AssertCount assert the count of a bean
  73. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  74. actual, err := x.Count(bean)
  75. assert.NoError(t, err)
  76. assert.EqualValues(t, expected, actual)
  77. }