Skip to content

Commit 7f2bcf9

Browse files
committed
backend tests
1 parent af58887 commit 7f2bcf9

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

models/git/protected_branch_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
12+
"code.gitea.io/gitea/models/db"
13+
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/models/unittest"
1115
)
1216

1317
func TestBranchRuleMatch(t *testing.T) {
@@ -76,3 +80,77 @@ func TestBranchRuleMatch(t *testing.T) {
7680
)
7781
}
7882
}
83+
84+
func TestUpdateProtectBranchPriorities(t *testing.T) {
85+
assert.NoError(t, unittest.PrepareTestDatabase())
86+
87+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
88+
89+
// Create some test protected branches with initial priorities
90+
protectedBranches := []*ProtectedBranch{
91+
{
92+
RepoID: repo.ID,
93+
RuleName: "master",
94+
Priority: 1,
95+
},
96+
{
97+
RepoID: repo.ID,
98+
RuleName: "develop",
99+
Priority: 2,
100+
},
101+
{
102+
RepoID: repo.ID,
103+
RuleName: "feature/*",
104+
Priority: 3,
105+
},
106+
}
107+
108+
for _, pb := range protectedBranches {
109+
_, err := db.GetEngine(db.DefaultContext).Insert(pb)
110+
assert.NoError(t, err)
111+
}
112+
113+
// Test updating priorities
114+
newPriorities := []int64{protectedBranches[2].ID, protectedBranches[0].ID, protectedBranches[1].ID}
115+
err := UpdateProtectBranchPriorities(db.DefaultContext, repo, newPriorities)
116+
assert.NoError(t, err)
117+
118+
// Verify new priorities
119+
pbs, err := FindRepoProtectedBranchRules(db.DefaultContext, repo.ID)
120+
assert.NoError(t, err)
121+
122+
expectedPriorities := map[string]int64{
123+
"feature/*": 1,
124+
"master": 2,
125+
"develop": 3,
126+
}
127+
128+
for _, pb := range pbs {
129+
assert.Equal(t, expectedPriorities[pb.RuleName], pb.Priority)
130+
}
131+
}
132+
133+
func TestNewProtectBranchPriority(t *testing.T) {
134+
assert.NoError(t, unittest.PrepareTestDatabase())
135+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
136+
137+
err := UpdateProtectBranch(db.DefaultContext, repo, &ProtectedBranch{
138+
RepoID: repo.ID,
139+
RuleName: "branch-1",
140+
Priority: 1,
141+
}, WhitelistOptions{})
142+
assert.NoError(t, err)
143+
144+
newPB := &ProtectedBranch{
145+
RepoID: repo.ID,
146+
RuleName: "branch-2",
147+
// Priority intentionally omitted
148+
}
149+
150+
err = UpdateProtectBranch(db.DefaultContext, repo, newPB, WhitelistOptions{})
151+
assert.NoError(t, err)
152+
153+
savedPB2, err := GetFirstMatchProtectedBranchRule(db.DefaultContext, repo.ID, "branch-2")
154+
assert.NoError(t, err)
155+
assert.Equal(t, int64(2), savedPB2.Priority)
156+
}

0 commit comments

Comments
 (0)