code.gitea.io/gitea@v1.21.7/services/repository/repository_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	"code.gitea.io/gitea/models/unit"
    12  	"code.gitea.io/gitea/models/unittest"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestLinkedRepository(t *testing.T) {
    18  	assert.NoError(t, unittest.PrepareTestDatabase())
    19  	testCases := []struct {
    20  		name             string
    21  		attachID         int64
    22  		expectedRepo     *repo_model.Repository
    23  		expectedUnitType unit.Type
    24  	}{
    25  		{"LinkedIssue", 1, &repo_model.Repository{ID: 1}, unit.TypeIssues},
    26  		{"LinkedComment", 3, &repo_model.Repository{ID: 1}, unit.TypePullRequests},
    27  		{"LinkedRelease", 9, &repo_model.Repository{ID: 1}, unit.TypeReleases},
    28  		{"Notlinked", 10, nil, -1},
    29  	}
    30  	for _, tc := range testCases {
    31  		t.Run(tc.name, func(t *testing.T) {
    32  			attach, err := repo_model.GetAttachmentByID(db.DefaultContext, tc.attachID)
    33  			assert.NoError(t, err)
    34  			repo, unitType, err := LinkedRepository(db.DefaultContext, attach)
    35  			assert.NoError(t, err)
    36  			if tc.expectedRepo != nil {
    37  				assert.Equal(t, tc.expectedRepo.ID, repo.ID)
    38  			}
    39  			assert.Equal(t, tc.expectedUnitType, unitType)
    40  		})
    41  	}
    42  }