code.gitea.io/gitea@v1.21.7/models/packages/package_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package packages_test
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	packages_model "code.gitea.io/gitea/models/packages"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  
    15  	_ "code.gitea.io/gitea/models"
    16  	_ "code.gitea.io/gitea/models/actions"
    17  	_ "code.gitea.io/gitea/models/activities"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestMain(m *testing.M) {
    23  	unittest.MainTest(m, &unittest.TestOptions{
    24  		GiteaRootPath: filepath.Join("..", ".."),
    25  	})
    26  }
    27  
    28  func TestHasOwnerPackages(t *testing.T) {
    29  	assert.NoError(t, unittest.PrepareTestDatabase())
    30  
    31  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    32  
    33  	p, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{
    34  		OwnerID:   owner.ID,
    35  		LowerName: "package",
    36  	})
    37  	assert.NotNil(t, p)
    38  	assert.NoError(t, err)
    39  
    40  	// A package without package versions gets automatically cleaned up and should return false
    41  	has, err := packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
    42  	assert.False(t, has)
    43  	assert.NoError(t, err)
    44  
    45  	pv, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
    46  		PackageID:    p.ID,
    47  		LowerVersion: "internal",
    48  		IsInternal:   true,
    49  	})
    50  	assert.NotNil(t, pv)
    51  	assert.NoError(t, err)
    52  
    53  	// A package with an internal package version gets automatically cleaned up and should return false
    54  	has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
    55  	assert.False(t, has)
    56  	assert.NoError(t, err)
    57  
    58  	pv, err = packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{
    59  		PackageID:    p.ID,
    60  		LowerVersion: "normal",
    61  		IsInternal:   false,
    62  	})
    63  	assert.NotNil(t, pv)
    64  	assert.NoError(t, err)
    65  
    66  	// A package with a normal package version should return true
    67  	has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID)
    68  	assert.True(t, has)
    69  	assert.NoError(t, err)
    70  }