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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // PrepareTestDatabase load test fixtures into test database
  29. func PrepareTestDatabase() error {
  30. return LoadFixtures()
  31. }
  32. type testCond struct {
  33. query interface{}
  34. args []interface{}
  35. }
  36. // Cond create a condition with arguments for a test
  37. func Cond(query interface{}, args ...interface{}) interface{} {
  38. return &testCond{query: query, args: args}
  39. }
  40. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  41. for _, condition := range conditions {
  42. switch cond := condition.(type) {
  43. case *testCond:
  44. sess.Where(cond.query, cond.args...)
  45. default:
  46. sess.Where(cond)
  47. }
  48. }
  49. }
  50. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  51. sess := x.NewSession()
  52. defer sess.Close()
  53. whereConditions(sess, conditions)
  54. return sess.Get(bean)
  55. }
  56. // BeanExists for testing, check if a bean exists
  57. func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
  58. exists, err := loadBeanIfExists(bean, conditions...)
  59. assert.NoError(t, err)
  60. return exists
  61. }
  62. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  63. // database
  64. func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
  65. exists, err := loadBeanIfExists(bean, conditions...)
  66. assert.NoError(t, err)
  67. assert.True(t, exists,
  68. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  69. bean, bean, conditions)
  70. return bean
  71. }
  72. // GetCount get the count of a bean
  73. func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
  74. sess := x.NewSession()
  75. defer sess.Close()
  76. whereConditions(sess, conditions)
  77. count, err := sess.Count(bean)
  78. assert.NoError(t, err)
  79. return int(count)
  80. }
  81. // AssertNotExistsBean assert that a bean does not exist in the test database
  82. func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
  83. exists, err := loadBeanIfExists(bean, conditions...)
  84. assert.NoError(t, err)
  85. assert.False(t, exists)
  86. }
  87. // AssertSuccessfulInsert assert that beans is successfully inserted
  88. func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
  89. _, err := x.Insert(beans...)
  90. assert.NoError(t, err)
  91. }
  92. // AssertCount assert the count of a bean
  93. func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
  94. assert.EqualValues(t, expected, GetCount(t, bean))
  95. }