@@ -15,6 +15,114 @@ import (
1515 "github.com/stretchr/testify/require"
1616)
1717
18+ func TestIssueNewIssueLabels (t * testing.T ) {
19+ require .NoError (t , unittest .PrepareTestDatabase ())
20+
21+ issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 2 })
22+ label1 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 1 })
23+ label2 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 4 })
24+ doer := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
25+
26+ label3 := & issues_model.Label {RepoID : 1 , Name : "label3" , Color : "#123" }
27+ require .NoError (t , issues_model .NewLabel (db .DefaultContext , label3 ))
28+
29+ // label1 is already set, do nothing
30+ // label3 is new, add it
31+ require .NoError (t , issues_model .NewIssueLabels (db .DefaultContext , issue , []* issues_model.Label {label1 , label3 }, doer ))
32+
33+ assert .Len (t , issue .Labels , 3 )
34+ // check that the pre-existing label1 is still present
35+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
36+ // check that new label3 was added
37+ assert .Equal (t , label3 .ID , issue .Labels [1 ].ID )
38+ // check that pre-existing label2 was not removed
39+ assert .Equal (t , label2 .ID , issue .Labels [2 ].ID )
40+ }
41+
42+ func TestIssueNewIssueLabel (t * testing.T ) {
43+ require .NoError (t , unittest .PrepareTestDatabase ())
44+
45+ issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 3 })
46+ doer := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
47+
48+ label := & issues_model.Label {RepoID : 1 , Name : "label3" , Color : "#123" }
49+ require .NoError (t , issues_model .NewLabel (db .DefaultContext , label ))
50+
51+ require .NoError (t , issues_model .NewIssueLabel (db .DefaultContext , issue , label , doer ))
52+
53+ assert .Len (t , issue .Labels , 1 )
54+ assert .Equal (t , label .ID , issue .Labels [0 ].ID )
55+ }
56+
57+ func TestIssueReplaceIssueLabels (t * testing.T ) {
58+ require .NoError (t , unittest .PrepareTestDatabase ())
59+
60+ issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 2 })
61+ label1 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 1 })
62+ label2 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 4 })
63+ doer := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
64+
65+ label3 := & issues_model.Label {RepoID : 1 , Name : "label3" , Color : "#123" }
66+ require .NoError (t , issues_model .NewLabel (db .DefaultContext , label3 ))
67+
68+ issue .LoadLabels (db .DefaultContext )
69+ assert .Len (t , issue .Labels , 2 )
70+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
71+ assert .Equal (t , label2 .ID , issue .Labels [1 ].ID )
72+
73+ // label1 is already set, do nothing
74+ // label3 is new, add it
75+ // label2 is not in the list but already set, remove it
76+ require .NoError (t , issues_model .ReplaceIssueLabels (db .DefaultContext , issue , []* issues_model.Label {label1 , label3 }, doer ))
77+
78+ assert .Len (t , issue .Labels , 2 )
79+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
80+ assert .Equal (t , label3 .ID , issue .Labels [1 ].ID )
81+ }
82+
83+ func TestIssueDeleteIssueLabel (t * testing.T ) {
84+ require .NoError (t , unittest .PrepareTestDatabase ())
85+
86+ issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 2 })
87+ label1 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 1 })
88+ label2 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 4 })
89+ doer := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
90+
91+ issue .LoadLabels (db .DefaultContext )
92+ assert .Len (t , issue .Labels , 2 )
93+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
94+ assert .Equal (t , label2 .ID , issue .Labels [1 ].ID )
95+
96+ require .NoError (t , issues_model .DeleteIssueLabel (db .DefaultContext , issue , label2 , doer ))
97+
98+ assert .Len (t , issue .Labels , 1 )
99+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
100+ }
101+
102+ func TestIssueLoadLabels (t * testing.T ) {
103+ require .NoError (t , unittest .PrepareTestDatabase ())
104+
105+ issue := unittest .AssertExistsAndLoadBean (t , & issues_model.Issue {ID : 2 })
106+ label1 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 1 })
107+ label2 := unittest .AssertExistsAndLoadBean (t , & issues_model.Label {ID : 4 })
108+
109+ assert .Empty (t , issue .Labels )
110+ issue .LoadLabels (db .DefaultContext )
111+ assert .Len (t , issue .Labels , 2 )
112+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
113+ assert .Equal (t , label2 .ID , issue .Labels [1 ].ID )
114+
115+ unittest .AssertSuccessfulDelete (t , & issues_model.IssueLabel {IssueID : issue .ID , LabelID : label2 .ID })
116+
117+ // the database change is not noticed because the labels are cached
118+ issue .LoadLabels (db .DefaultContext )
119+ assert .Len (t , issue .Labels , 2 )
120+
121+ issue .ReloadLabels (db .DefaultContext )
122+ assert .Len (t , issue .Labels , 1 )
123+ assert .Equal (t , label1 .ID , issue .Labels [0 ].ID )
124+ }
125+
18126func TestNewIssueLabelsScope (t * testing.T ) {
19127 require .NoError (t , unittest .PrepareTestDatabase ())
20128
0 commit comments