code.gitea.io/gitea@v1.21.7/models/db/list_test.go (about)

     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  	var repoUnitCount int
    35  	_, err := db.GetEngine(db.DefaultContext).SQL("SELECT COUNT(*) FROM repo_unit").Get(&repoUnitCount)
    36  	assert.NoError(t, err)
    37  	assert.NotEmpty(t, repoUnitCount)
    38  
    39  	opts := mockListOptions{}
    40  	var repoUnits []repo_model.RepoUnit
    41  	err = db.Find(db.DefaultContext, &opts, &repoUnits)
    42  	assert.NoError(t, err)
    43  	assert.Len(t, repoUnits, repoUnitCount)
    44  
    45  	cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
    46  	assert.NoError(t, err)
    47  	assert.EqualValues(t, repoUnitCount, cnt)
    48  
    49  	repoUnits = make([]repo_model.RepoUnit, 0, 10)
    50  	newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)
    51  	assert.NoError(t, err)
    52  	assert.EqualValues(t, cnt, newCnt)
    53  }