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

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"testing"
     8  
     9  	repo_model "code.gitea.io/gitea/models/repo"
    10  	"code.gitea.io/gitea/models/unittest"
    11  	"code.gitea.io/gitea/modules/contexttest"
    12  	"code.gitea.io/gitea/modules/web"
    13  	"code.gitea.io/gitea/services/forms"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestNewReleasePost(t *testing.T) {
    19  	for _, testCase := range []struct {
    20  		RepoID  int64
    21  		UserID  int64
    22  		TagName string
    23  		Form    forms.NewReleaseForm
    24  	}{
    25  		{
    26  			RepoID:  1,
    27  			UserID:  2,
    28  			TagName: "v1.1", // pre-existing tag
    29  			Form: forms.NewReleaseForm{
    30  				TagName: "newtag",
    31  				Target:  "master",
    32  				Title:   "title",
    33  				Content: "content",
    34  			},
    35  		},
    36  		{
    37  			RepoID:  1,
    38  			UserID:  2,
    39  			TagName: "newtag",
    40  			Form: forms.NewReleaseForm{
    41  				TagName: "newtag",
    42  				Target:  "master",
    43  				Title:   "title",
    44  				Content: "content",
    45  			},
    46  		},
    47  	} {
    48  		unittest.PrepareTestEnv(t)
    49  
    50  		ctx, _ := contexttest.MockContext(t, "user2/repo1/releases/new")
    51  		contexttest.LoadUser(t, ctx, 2)
    52  		contexttest.LoadRepo(t, ctx, 1)
    53  		contexttest.LoadGitRepo(t, ctx)
    54  		web.SetForm(ctx, &testCase.Form)
    55  		NewReleasePost(ctx)
    56  		unittest.AssertExistsAndLoadBean(t, &repo_model.Release{
    57  			RepoID:      1,
    58  			PublisherID: 2,
    59  			TagName:     testCase.Form.TagName,
    60  			Target:      testCase.Form.Target,
    61  			Title:       testCase.Form.Title,
    62  			Note:        testCase.Form.Content,
    63  		}, unittest.Cond("is_draft=?", len(testCase.Form.Draft) > 0))
    64  		ctx.Repo.GitRepo.Close()
    65  	}
    66  }
    67  
    68  func TestNewReleasesList(t *testing.T) {
    69  	unittest.PrepareTestEnv(t)
    70  	ctx, _ := contexttest.MockContext(t, "user2/repo-release/releases")
    71  	contexttest.LoadUser(t, ctx, 2)
    72  	contexttest.LoadRepo(t, ctx, 57)
    73  	contexttest.LoadGitRepo(t, ctx)
    74  	t.Cleanup(func() { ctx.Repo.GitRepo.Close() })
    75  
    76  	Releases(ctx)
    77  	releases := ctx.Data["Releases"].([]*repo_model.Release)
    78  	type computedFields struct {
    79  		NumCommitsBehind int64
    80  		TargetBehind     string
    81  	}
    82  	expectedComputation := map[string]computedFields{
    83  		"v1.0": {
    84  			NumCommitsBehind: 3,
    85  			TargetBehind:     "main",
    86  		},
    87  		"v1.1": {
    88  			NumCommitsBehind: 1,
    89  			TargetBehind:     "main",
    90  		},
    91  		"v2.0": {
    92  			NumCommitsBehind: 0,
    93  			TargetBehind:     "main",
    94  		},
    95  		"non-existing-target-branch": {
    96  			NumCommitsBehind: 1,
    97  			TargetBehind:     "main",
    98  		},
    99  		"empty-target-branch": {
   100  			NumCommitsBehind: 1,
   101  			TargetBehind:     "main",
   102  		},
   103  	}
   104  	for _, r := range releases {
   105  		actual := computedFields{
   106  			NumCommitsBehind: r.NumCommitsBehind,
   107  			TargetBehind:     r.TargetBehind,
   108  		}
   109  		assert.Equal(t, expectedComputation[r.TagName], actual, "wrong computed fields for %s: %#v", r.TagName, r)
   110  	}
   111  }