mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-27 18:19:17 +00:00
dd30d9d5c0
The function `GetByBean` has an obvious defect that when the fields are empty values, it will be ignored. Then users will get a wrong result which is possibly used to make a security problem. To avoid the possibility, this PR removed function `GetByBean` and all references. And some new generic functions have been introduced to be used. The recommand usage like below. ```go // if query an object according id obj, err := db.GetByID[Object](ctx, id) // query with other conditions obj, err := db.Get[Object](ctx, builder.Eq{"a": a, "b":b}) ```
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
// 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{}))
|
|
|
|
cnt, err := db.GetEngine(db.DefaultContext).Count(&repo_model.RepoUnit{})
|
|
assert.NoError(t, err)
|
|
|
|
var repoUnitCnt int
|
|
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
|
|
repoUnitCnt++
|
|
return nil
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, cnt, repoUnitCnt)
|
|
|
|
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
|
|
has, err := db.ExistByID[repo_model.RepoUnit](ctx, repoUnit.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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)
|
|
}
|