code.gitea.io/gitea@v1.21.7/routers/web/repo/issue_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"testing"
     8  
     9  	issues_model "code.gitea.io/gitea/models/issues"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCombineLabelComments(t *testing.T) {
    15  	kases := []struct {
    16  		name           string
    17  		beforeCombined []*issues_model.Comment
    18  		afterCombined  []*issues_model.Comment
    19  	}{
    20  		{
    21  			name: "kase 1",
    22  			beforeCombined: []*issues_model.Comment{
    23  				{
    24  					Type:     issues_model.CommentTypeLabel,
    25  					PosterID: 1,
    26  					Content:  "1",
    27  					Label: &issues_model.Label{
    28  						Name: "kind/bug",
    29  					},
    30  					CreatedUnix: 0,
    31  				},
    32  				{
    33  					Type:     issues_model.CommentTypeLabel,
    34  					PosterID: 1,
    35  					Content:  "",
    36  					Label: &issues_model.Label{
    37  						Name: "kind/bug",
    38  					},
    39  					CreatedUnix: 0,
    40  				},
    41  				{
    42  					Type:        issues_model.CommentTypeComment,
    43  					PosterID:    1,
    44  					Content:     "test",
    45  					CreatedUnix: 0,
    46  				},
    47  			},
    48  			afterCombined: []*issues_model.Comment{
    49  				{
    50  					Type:        issues_model.CommentTypeLabel,
    51  					PosterID:    1,
    52  					Content:     "1",
    53  					CreatedUnix: 0,
    54  					AddedLabels: []*issues_model.Label{},
    55  					Label: &issues_model.Label{
    56  						Name: "kind/bug",
    57  					},
    58  				},
    59  				{
    60  					Type:        issues_model.CommentTypeComment,
    61  					PosterID:    1,
    62  					Content:     "test",
    63  					CreatedUnix: 0,
    64  				},
    65  			},
    66  		},
    67  		{
    68  			name: "kase 2",
    69  			beforeCombined: []*issues_model.Comment{
    70  				{
    71  					Type:     issues_model.CommentTypeLabel,
    72  					PosterID: 1,
    73  					Content:  "1",
    74  					Label: &issues_model.Label{
    75  						Name: "kind/bug",
    76  					},
    77  					CreatedUnix: 0,
    78  				},
    79  				{
    80  					Type:     issues_model.CommentTypeLabel,
    81  					PosterID: 1,
    82  					Content:  "",
    83  					Label: &issues_model.Label{
    84  						Name: "kind/bug",
    85  					},
    86  					CreatedUnix: 70,
    87  				},
    88  				{
    89  					Type:        issues_model.CommentTypeComment,
    90  					PosterID:    1,
    91  					Content:     "test",
    92  					CreatedUnix: 0,
    93  				},
    94  			},
    95  			afterCombined: []*issues_model.Comment{
    96  				{
    97  					Type:        issues_model.CommentTypeLabel,
    98  					PosterID:    1,
    99  					Content:     "1",
   100  					CreatedUnix: 0,
   101  					AddedLabels: []*issues_model.Label{
   102  						{
   103  							Name: "kind/bug",
   104  						},
   105  					},
   106  					Label: &issues_model.Label{
   107  						Name: "kind/bug",
   108  					},
   109  				},
   110  				{
   111  					Type:        issues_model.CommentTypeLabel,
   112  					PosterID:    1,
   113  					Content:     "",
   114  					CreatedUnix: 70,
   115  					RemovedLabels: []*issues_model.Label{
   116  						{
   117  							Name: "kind/bug",
   118  						},
   119  					},
   120  					Label: &issues_model.Label{
   121  						Name: "kind/bug",
   122  					},
   123  				},
   124  				{
   125  					Type:        issues_model.CommentTypeComment,
   126  					PosterID:    1,
   127  					Content:     "test",
   128  					CreatedUnix: 0,
   129  				},
   130  			},
   131  		},
   132  		{
   133  			name: "kase 3",
   134  			beforeCombined: []*issues_model.Comment{
   135  				{
   136  					Type:     issues_model.CommentTypeLabel,
   137  					PosterID: 1,
   138  					Content:  "1",
   139  					Label: &issues_model.Label{
   140  						Name: "kind/bug",
   141  					},
   142  					CreatedUnix: 0,
   143  				},
   144  				{
   145  					Type:     issues_model.CommentTypeLabel,
   146  					PosterID: 2,
   147  					Content:  "",
   148  					Label: &issues_model.Label{
   149  						Name: "kind/bug",
   150  					},
   151  					CreatedUnix: 0,
   152  				},
   153  				{
   154  					Type:        issues_model.CommentTypeComment,
   155  					PosterID:    1,
   156  					Content:     "test",
   157  					CreatedUnix: 0,
   158  				},
   159  			},
   160  			afterCombined: []*issues_model.Comment{
   161  				{
   162  					Type:        issues_model.CommentTypeLabel,
   163  					PosterID:    1,
   164  					Content:     "1",
   165  					CreatedUnix: 0,
   166  					AddedLabels: []*issues_model.Label{
   167  						{
   168  							Name: "kind/bug",
   169  						},
   170  					},
   171  					Label: &issues_model.Label{
   172  						Name: "kind/bug",
   173  					},
   174  				},
   175  				{
   176  					Type:        issues_model.CommentTypeLabel,
   177  					PosterID:    2,
   178  					Content:     "",
   179  					CreatedUnix: 0,
   180  					RemovedLabels: []*issues_model.Label{
   181  						{
   182  							Name: "kind/bug",
   183  						},
   184  					},
   185  					Label: &issues_model.Label{
   186  						Name: "kind/bug",
   187  					},
   188  				},
   189  				{
   190  					Type:        issues_model.CommentTypeComment,
   191  					PosterID:    1,
   192  					Content:     "test",
   193  					CreatedUnix: 0,
   194  				},
   195  			},
   196  		},
   197  		{
   198  			name: "kase 4",
   199  			beforeCombined: []*issues_model.Comment{
   200  				{
   201  					Type:     issues_model.CommentTypeLabel,
   202  					PosterID: 1,
   203  					Content:  "1",
   204  					Label: &issues_model.Label{
   205  						Name: "kind/bug",
   206  					},
   207  					CreatedUnix: 0,
   208  				},
   209  				{
   210  					Type:     issues_model.CommentTypeLabel,
   211  					PosterID: 1,
   212  					Content:  "1",
   213  					Label: &issues_model.Label{
   214  						Name: "kind/backport",
   215  					},
   216  					CreatedUnix: 10,
   217  				},
   218  			},
   219  			afterCombined: []*issues_model.Comment{
   220  				{
   221  					Type:        issues_model.CommentTypeLabel,
   222  					PosterID:    1,
   223  					Content:     "1",
   224  					CreatedUnix: 10,
   225  					AddedLabels: []*issues_model.Label{
   226  						{
   227  							Name: "kind/bug",
   228  						},
   229  						{
   230  							Name: "kind/backport",
   231  						},
   232  					},
   233  					Label: &issues_model.Label{
   234  						Name: "kind/bug",
   235  					},
   236  				},
   237  			},
   238  		},
   239  		{
   240  			name: "kase 5",
   241  			beforeCombined: []*issues_model.Comment{
   242  				{
   243  					Type:     issues_model.CommentTypeLabel,
   244  					PosterID: 1,
   245  					Content:  "1",
   246  					Label: &issues_model.Label{
   247  						Name: "kind/bug",
   248  					},
   249  					CreatedUnix: 0,
   250  				},
   251  				{
   252  					Type:        issues_model.CommentTypeComment,
   253  					PosterID:    2,
   254  					Content:     "testtest",
   255  					CreatedUnix: 0,
   256  				},
   257  				{
   258  					Type:     issues_model.CommentTypeLabel,
   259  					PosterID: 1,
   260  					Content:  "",
   261  					Label: &issues_model.Label{
   262  						Name: "kind/bug",
   263  					},
   264  					CreatedUnix: 0,
   265  				},
   266  			},
   267  			afterCombined: []*issues_model.Comment{
   268  				{
   269  					Type:     issues_model.CommentTypeLabel,
   270  					PosterID: 1,
   271  					Content:  "1",
   272  					Label: &issues_model.Label{
   273  						Name: "kind/bug",
   274  					},
   275  					AddedLabels: []*issues_model.Label{
   276  						{
   277  							Name: "kind/bug",
   278  						},
   279  					},
   280  					CreatedUnix: 0,
   281  				},
   282  				{
   283  					Type:        issues_model.CommentTypeComment,
   284  					PosterID:    2,
   285  					Content:     "testtest",
   286  					CreatedUnix: 0,
   287  				},
   288  				{
   289  					Type:     issues_model.CommentTypeLabel,
   290  					PosterID: 1,
   291  					Content:  "",
   292  					RemovedLabels: []*issues_model.Label{
   293  						{
   294  							Name: "kind/bug",
   295  						},
   296  					},
   297  					Label: &issues_model.Label{
   298  						Name: "kind/bug",
   299  					},
   300  					CreatedUnix: 0,
   301  				},
   302  			},
   303  		},
   304  		{
   305  			name: "kase 6",
   306  			beforeCombined: []*issues_model.Comment{
   307  				{
   308  					Type:     issues_model.CommentTypeLabel,
   309  					PosterID: 1,
   310  					Content:  "1",
   311  					Label: &issues_model.Label{
   312  						Name: "kind/bug",
   313  					},
   314  					CreatedUnix: 0,
   315  				},
   316  				{
   317  					Type:     issues_model.CommentTypeLabel,
   318  					PosterID: 1,
   319  					Content:  "1",
   320  					Label: &issues_model.Label{
   321  						Name: "reviewed/confirmed",
   322  					},
   323  					CreatedUnix: 0,
   324  				},
   325  				{
   326  					Type:     issues_model.CommentTypeLabel,
   327  					PosterID: 1,
   328  					Content:  "",
   329  					Label: &issues_model.Label{
   330  						Name: "kind/bug",
   331  					},
   332  					CreatedUnix: 0,
   333  				},
   334  				{
   335  					Type:     issues_model.CommentTypeLabel,
   336  					PosterID: 1,
   337  					Content:  "1",
   338  					Label: &issues_model.Label{
   339  						Name: "kind/feature",
   340  					},
   341  					CreatedUnix: 0,
   342  				},
   343  			},
   344  			afterCombined: []*issues_model.Comment{
   345  				{
   346  					Type:     issues_model.CommentTypeLabel,
   347  					PosterID: 1,
   348  					Content:  "1",
   349  					Label: &issues_model.Label{
   350  						Name: "kind/bug",
   351  					},
   352  					AddedLabels: []*issues_model.Label{
   353  						{
   354  							Name: "reviewed/confirmed",
   355  						},
   356  						{
   357  							Name: "kind/feature",
   358  						},
   359  					},
   360  					CreatedUnix: 0,
   361  				},
   362  			},
   363  		},
   364  	}
   365  
   366  	for _, kase := range kases {
   367  		t.Run(kase.name, func(t *testing.T) {
   368  			issue := issues_model.Issue{
   369  				Comments: kase.beforeCombined,
   370  			}
   371  			combineLabelComments(&issue)
   372  			assert.EqualValues(t, kase.afterCombined, issue.Comments)
   373  		})
   374  	}
   375  }