code.gitea.io/gitea@v1.22.3/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  	repoUnits, err := db.Find[repo_model.RepoUnit](db.DefaultContext, opts)
    41  	assert.NoError(t, err)
    42  	assert.Len(t, repoUnits, repoUnitCount)
    43  
    44  	cnt, err := db.Count[repo_model.RepoUnit](db.DefaultContext, opts)
    45  	assert.NoError(t, err)
    46  	assert.EqualValues(t, repoUnitCount, cnt)
    47  
    48  	repoUnits, newCnt, err := db.FindAndCount[repo_model.RepoUnit](db.DefaultContext, opts)
    49  	assert.NoError(t, err)
    50  	assert.EqualValues(t, cnt, newCnt)
    51  	assert.Len(t, repoUnits, repoUnitCount)
    52  }