summaryrefslogtreecommitdiffstats
path: root/models/db/iterate_test.go
blob: 57b1bd3cd192f880255605f48a962757c64fd1b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package db_test

import (
	"context"
	"testing"

	"code.gitea.io/gitea/models/db"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"

	"github.com/stretchr/testify/assert"
)

func TestIterate(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())
	xe := unittest.GetXORMEngine()
	assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))

	var repoCnt int
	err := db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
		repoCnt++
		return nil
	})
	assert.NoError(t, err)
	assert.EqualValues(t, 80, repoCnt)

	err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
		reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}
		has, err := db.GetByBean(ctx, &reopUnit2)
		if err != nil {
			return err
		} else if !has {
			return db.ErrNotExist{Resource: "repo_unit", ID: repoUnit.ID}
		}
		assert.EqualValues(t, repoUnit.RepoID, repoUnit.RepoID)
		assert.EqualValues(t, repoUnit.CreatedUnix, repoUnit.CreatedUnix)
		return nil
	})
	assert.NoError(t, err)
}