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