88
99 "code.gitea.io/gitea/models/unit"
1010 "code.gitea.io/gitea/models/unittest"
11+ "code.gitea.io/gitea/modules/util"
1112
1213 "github.com/stretchr/testify/assert"
1314)
@@ -30,3 +31,82 @@ func TestDefaultTargetBranchSelection(t *testing.T) {
3031 repo .Units = nil
3132 assert .Equal (t , "branch2" , repo .GetPullRequestTargetBranch (ctx ))
3233}
34+
35+ func TestPullRequestConfigFromDB (t * testing.T ) {
36+ cases := []struct {
37+ // name describes the row shape under test; the comments capture why each row matters.
38+ name string
39+ json string
40+ wantMergeUpdate bool
41+ wantRebaseUpdate bool
42+ wantDefaultStyle UpdateStyle
43+ wantValidatesPass bool
44+ }{
45+ {
46+ // Empty object exercises the all-defaults path (e.g. fresh repos created via low-level paths).
47+ name : "defaults" , json : "{}" ,
48+ wantMergeUpdate : true , wantRebaseUpdate : true ,
49+ wantDefaultStyle : UpdateStyleMerge , wantValidatesPass : true ,
50+ },
51+ {
52+ // Realistic upgrade case: pre-PR JSON lacks the new fields and has AllowRebaseUpdate=false.
53+ // Historical setting must be preserved while new fields take safe defaults.
54+ name : "legacy without new fields" ,
55+ json : `{"AllowMerge":true,"AllowRebase":true,"AllowRebaseMerge":true,"AllowSquash":true,"AllowRebaseUpdate":false}` ,
56+ wantMergeUpdate : true , wantRebaseUpdate : false ,
57+ wantDefaultStyle : UpdateStyleMerge , wantValidatesPass : true ,
58+ },
59+ {
60+ // Partially-migrated row with explicit empty string must normalize so ValidateUpdateSettings passes.
61+ name : "empty default style" , json : `{"DefaultUpdateStyle":""}` ,
62+ wantMergeUpdate : true , wantRebaseUpdate : true ,
63+ wantDefaultStyle : UpdateStyleMerge , wantValidatesPass : true ,
64+ },
65+ }
66+ for _ , tc := range cases {
67+ t .Run (tc .name , func (t * testing.T ) {
68+ cfg := new (PullRequestsConfig )
69+ assert .NoError (t , cfg .FromDB ([]byte (tc .json )))
70+ assert .Equal (t , tc .wantMergeUpdate , cfg .AllowMergeUpdate )
71+ assert .Equal (t , tc .wantRebaseUpdate , cfg .AllowRebaseUpdate )
72+ assert .Equal (t , tc .wantDefaultStyle , cfg .DefaultUpdateStyle )
73+ if tc .wantValidatesPass {
74+ assert .NoError (t , cfg .ValidateUpdateSettings ())
75+ }
76+ })
77+ }
78+ }
79+
80+ func TestPullRequestConfigValidateUpdateSettingsInvalidArgument (t * testing.T ) {
81+ cases := []struct {
82+ name string
83+ cfg PullRequestsConfig
84+ }{
85+ {
86+ name : "invalid default style" ,
87+ cfg : PullRequestsConfig {
88+ AllowMergeUpdate : true ,
89+ AllowRebaseUpdate : true ,
90+ DefaultUpdateStyle : "invalid" ,
91+ },
92+ },
93+ {
94+ name : "no update style enabled" ,
95+ cfg : PullRequestsConfig {
96+ DefaultUpdateStyle : UpdateStyleMerge ,
97+ },
98+ },
99+ {
100+ name : "default update style disabled" ,
101+ cfg : PullRequestsConfig {
102+ AllowRebaseUpdate : true ,
103+ DefaultUpdateStyle : UpdateStyleMerge ,
104+ },
105+ },
106+ }
107+ for _ , tc := range cases {
108+ t .Run (tc .name , func (t * testing.T ) {
109+ assert .ErrorIs (t , tc .cfg .ValidateUpdateSettings (), util .ErrInvalidArgument )
110+ })
111+ }
112+ }
0 commit comments