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.

list_test.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db_test
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/db"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/models/unittest"
  9. "github.com/stretchr/testify/assert"
  10. "xorm.io/builder"
  11. )
  12. type mockListOptions struct {
  13. db.ListOptions
  14. }
  15. func (opts *mockListOptions) IsListAll() bool {
  16. return true
  17. }
  18. func (opts *mockListOptions) ToConds() builder.Cond {
  19. return builder.NewCond()
  20. }
  21. func TestFind(t *testing.T) {
  22. assert.NoError(t, unittest.PrepareTestDatabase())
  23. xe := unittest.GetXORMEngine()
  24. assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
  25. var repoUnitCount int
  26. _, err := db.GetEngine(db.DefaultContext).SQL("SELECT COUNT(*) FROM repo_unit").Get(&repoUnitCount)
  27. assert.NoError(t, err)
  28. assert.NotEmpty(t, repoUnitCount)
  29. opts := mockListOptions{}
  30. var repoUnits []repo_model.RepoUnit
  31. err = db.Find(db.DefaultContext, &opts, &repoUnits)
  32. assert.NoError(t, err)
  33. assert.Len(t, repoUnits, repoUnitCount)
  34. cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
  35. assert.NoError(t, err)
  36. assert.EqualValues(t, repoUnitCount, cnt)
  37. repoUnits = make([]repo_model.RepoUnit, 0, 10)
  38. newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)
  39. assert.NoError(t, err)
  40. assert.EqualValues(t, cnt, newCnt)
  41. }