From 863d25b85f4d0c63b3b44b4b8c5ecb61d9cf73a4 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Thu, 26 Dec 2024 19:20:54 +0300 Subject: [PATCH] tests: add taskservice_create test --- coverage-badge.svg | 2 +- internal/services/taskservice/create.go | 2 +- tests/integration/taskservice_create_test.go | 25 ++++++++++++++++++ tests/integration/taskservice_upload_test.go | 17 +----------- tests/integration/utils.go | 27 ++++++++++++++++++++ 5 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 tests/integration/taskservice_create_test.go create mode 100644 tests/integration/utils.go diff --git a/coverage-badge.svg b/coverage-badge.svg index c897da2..8d2c133 100644 --- a/coverage-badge.svg +++ b/coverage-badge.svg @@ -2,5 +2,5 @@ coverage - 22.3% + 25.5% diff --git a/internal/services/taskservice/create.go b/internal/services/taskservice/create.go index 208e67d..d12c651 100644 --- a/internal/services/taskservice/create.go +++ b/internal/services/taskservice/create.go @@ -20,7 +20,7 @@ func (s *Service) Create(repo string) (*models.Task, error) { FirstOrCreate(&altRepo) task := models.Task{ - RepoID: taskRepo.ID, + Repo: &taskRepo, ALTRepo: altRepo, Type: models.TypeUpsert, Status: models.StatusPending, diff --git a/tests/integration/taskservice_create_test.go b/tests/integration/taskservice_create_test.go new file mode 100644 index 0000000..dffbc85 --- /dev/null +++ b/tests/integration/taskservice_create_test.go @@ -0,0 +1,25 @@ +package integration + +import ( + "testing" + + "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) { + db := prepareDb(t) + config := prepareConfig() + + service := taskservice.New(db, config) + + repoName := "example-foo" + + task, err := service.Create(repoName) + + assert.NoError(t, err) + assert.Equal(t, uint(1), task.ID) + assert.Equal(t, repoName, task.Repo.Name) + assert.Equal(t, models.StatusPending, task.Status) +} diff --git a/tests/integration/taskservice_upload_test.go b/tests/integration/taskservice_upload_test.go index 9714f9b..5b29047 100644 --- a/tests/integration/taskservice_upload_test.go +++ b/tests/integration/taskservice_upload_test.go @@ -11,10 +11,7 @@ import ( "testing" "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" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" ) @@ -71,18 +68,9 @@ func createMultipartFileHeader(filePath string) *multipart.FileHeader { return files[0] } -func prepareDb(t *testing.T) *gorm.DB { - db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) - assert.NoError(t, err) - - err = db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{}) - assert.NoError(t, err) - - return db -} - func TestTaskService_Upload(t *testing.T) { db := prepareDb(t) + config := prepareConfig() task := &models.Task{ ALTRepoID: 1, @@ -93,9 +81,6 @@ func TestTaskService_Upload(t *testing.T) { db.Create(task) - config := &config.Config{ - UploadDir: "./test_uploads", - } os.MkdirAll(config.GetUploadDir(), os.ModePerm) os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm) diff --git a/tests/integration/utils.go b/tests/integration/utils.go new file mode 100644 index 0000000..7773ccd --- /dev/null +++ b/tests/integration/utils.go @@ -0,0 +1,27 @@ +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" +) + +func prepareDb(t *testing.T) *gorm.DB { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + assert.NoError(t, err) + + err = db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{}) + assert.NoError(t, err) + + return db +} + +func prepareConfig() *config.Config { + return &config.Config{ + UploadDir: "./test_uploads", + } +}