tests: add incorrect upload test
This commit is contained in:
parent
863d25b85f
commit
0fb274261f
4 changed files with 101 additions and 42 deletions
|
@ -2,5 +2,5 @@
|
|||
<rect width="1100" height="20" fill="#555"/>
|
||||
<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="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>
|
||||
|
|
Before Width: | Height: | Size: 341 B After Width: | Height: | Size: 341 B |
|
@ -3,9 +3,10 @@ package integration
|
|||
import (
|
||||
"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/services/taskservice"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTaskService_Create(t *testing.T) {
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -68,9 +69,24 @@ func createMultipartFileHeader(filePath string) *multipart.FileHeader {
|
|||
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) {
|
||||
t.Run("Correct upload (new)", 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())
|
||||
|
||||
task := &models.Task{
|
||||
ALTRepoID: 1,
|
||||
|
@ -78,36 +94,77 @@ func TestTaskService_Upload(t *testing.T) {
|
|||
Status: models.StatusPending,
|
||||
}
|
||||
task.ID = uint(1)
|
||||
|
||||
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"
|
||||
filePath := path.Join("./test_rpms/", fileName)
|
||||
|
||||
fileHeader := createMultipartFileHeader(filePath)
|
||||
|
||||
input := &taskservice.TaskUploadInput{
|
||||
TaskID: "1",
|
||||
Files: []*multipart.FileHeader{fileHeader},
|
||||
}
|
||||
|
||||
// Вызываем метод Upload
|
||||
err := service.Upload(input)
|
||||
err := service.Upload(createTaskUploadInput(
|
||||
task,
|
||||
fileName,
|
||||
))
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Проверяем, что задача обновлена
|
||||
var updatedTask models.Task
|
||||
db.First(&updatedTask, 1)
|
||||
assert.Equal(t, models.StatusCompleted, updatedTask.Status)
|
||||
|
||||
// Проверяем, что файл добавлен в базу
|
||||
var rpmFiles []models.RPMFile
|
||||
db.Find(&rpmFiles)
|
||||
assert.Len(t, rpmFiles, 1)
|
||||
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())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,11 +3,12 @@ package integration
|
|||
import (
|
||||
"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"
|
||||
"gorm.io/driver/sqlite"
|
||||
"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 {
|
||||
|
|
Loading…
Reference in a new issue