tests: add incorrect upload test
All checks were successful
Format and Lint / format-check (push) Successful in 3m48s
Format and Lint / test (push) Successful in 3m53s

This commit is contained in:
Максим Слипенко 2024-12-26 20:12:55 +03:00
parent 863d25b85f
commit 0fb274261f
4 changed files with 101 additions and 42 deletions

View file

@ -2,5 +2,5 @@
<rect width="1100" height="20" fill="#555"/> <rect width="1100" height="20" fill="#555"/>
<rect x="60" width="50" height="20" fill="#4c1"/> <rect x="60" width="50" height="20" fill="#4c1"/>
<text x="5" y="14" fill="#fff" font-family="Verdana" font-size="11">coverage</text> <text x="5" y="14" fill="#fff" font-family="Verdana" font-size="11">coverage</text>
<text x="70" y="14" fill="#fff" font-family="Verdana" font-size="11">25.5%</text> <text x="70" y="14" fill="#fff" font-family="Verdana" font-size="11">29.0%</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 341 B

After

Width:  |  Height:  |  Size: 341 B

View file

@ -3,9 +3,10 @@ package integration
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
"github.com/stretchr/testify/assert"
) )
func TestTaskService_Create(t *testing.T) { func TestTaskService_Create(t *testing.T) {

View file

@ -8,6 +8,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strconv"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -68,9 +69,24 @@ func createMultipartFileHeader(filePath string) *multipart.FileHeader {
return files[0] return files[0]
} }
func createTaskUploadInput(task *models.Task, fileName string) *taskservice.TaskUploadInput {
filePath := path.Join("./test_rpms/", fileName)
fileHeader := createMultipartFileHeader(filePath)
return &taskservice.TaskUploadInput{
TaskID: strconv.FormatUint(uint64(task.ID), 10),
Files: []*multipart.FileHeader{fileHeader},
}
}
func TestTaskService_Upload(t *testing.T) { func TestTaskService_Upload(t *testing.T) {
t.Run("Correct upload (new)", func(t *testing.T) {
db := prepareDb(t) db := prepareDb(t)
config := prepareConfig() config := prepareConfig()
service := taskservice.New(db, config)
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
defer os.RemoveAll(config.GetUploadDir())
task := &models.Task{ task := &models.Task{
ALTRepoID: 1, ALTRepoID: 1,
@ -78,36 +94,77 @@ func TestTaskService_Upload(t *testing.T) {
Status: models.StatusPending, Status: models.StatusPending,
} }
task.ID = uint(1) task.ID = uint(1)
db.Create(task) db.Create(task)
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
service := taskservice.New(db, config)
fileName := "example-foo-1.0.0-alt1.x86_64.rpm" fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
filePath := path.Join("./test_rpms/", fileName)
fileHeader := createMultipartFileHeader(filePath) err := service.Upload(createTaskUploadInput(
task,
input := &taskservice.TaskUploadInput{ fileName,
TaskID: "1", ))
Files: []*multipart.FileHeader{fileHeader},
}
// Вызываем метод Upload
err := service.Upload(input)
assert.NoError(t, err) assert.NoError(t, err)
// Проверяем, что задача обновлена
var updatedTask models.Task var updatedTask models.Task
db.First(&updatedTask, 1) db.First(&updatedTask, 1)
assert.Equal(t, models.StatusCompleted, updatedTask.Status) assert.Equal(t, models.StatusCompleted, updatedTask.Status)
// Проверяем, что файл добавлен в базу
var rpmFiles []models.RPMFile var rpmFiles []models.RPMFile
db.Find(&rpmFiles) db.Find(&rpmFiles)
assert.Len(t, rpmFiles, 1) assert.Len(t, rpmFiles, 1)
assert.Equal(t, fileName, rpmFiles[0].Filename) assert.Equal(t, fileName, rpmFiles[0].Filename)
})
t.Run("Incorrect upload (too old version)", func(t *testing.T) {
db := prepareDb(t)
config := prepareConfig()
service := taskservice.New(db, config)
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
defer os.RemoveAll(config.GetUploadDir())
// ===
gitRepo := &models.GitRepo{
Name: "example-foo",
}
altRepo := &models.ALTRepo{
Name: "Sisyphus",
}
db.Debug().Create(&models.GitRepoAltRepoTask{
Repo: gitRepo,
ALTRepo: altRepo,
CurrentTask: &models.Task{
Status: models.StatusCompleted,
Files: []models.RPMFile{
{
Name: "example-foo",
Version: "1.0.0",
Release: "2",
Epoch: 0,
},
},
},
})
task := &models.Task{
Repo: gitRepo,
ALTRepo: *altRepo,
Status: models.StatusPending,
}
db.Create(task)
// ===
err := service.Upload(createTaskUploadInput(
task,
"example-foo-1.0.0-alt1.x86_64.rpm",
))
var tooOldErr *taskservice.TooOldError
assert.ErrorAs(t, err, &tooOldErr)
assert.Equal(t, "example-foo is too old!", tooOldErr.Error())
})
} }

View file

@ -3,11 +3,12 @@ package integration
import ( import (
"testing" "testing"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
) )
func prepareDb(t *testing.T) *gorm.DB { func prepareDb(t *testing.T) *gorm.DB {