tests: add taskcontroller tests
All checks were successful
Format and Lint / format-check (push) Successful in 4m20s
Format and Lint / test (push) Successful in 3m47s

This commit is contained in:
Максим Слипенко 2024-12-26 21:16:05 +03:00
parent a93bd71e01
commit a9fb388c45
5 changed files with 228 additions and 4 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">29.0%</text> <text x="70" y="14" fill="#fff" font-family="Verdana" font-size="11">38.6%</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 341 B

After

Width:  |  Height:  |  Size: 341 B

View file

@ -1,14 +1,20 @@
package taskcontroller package taskcontroller
import ( import (
"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"
) )
type TaskController struct { type TaskService interface {
taskService *taskservice.Service Create(repo string) (*models.Task, error)
Upload(input *taskservice.TaskUploadInput) error
} }
func New(taskService *taskservice.Service) *TaskController { type TaskController struct {
taskService TaskService
}
func New(taskService TaskService) *TaskController {
return &TaskController{ return &TaskController{
taskService: taskService, taskService: taskService,
} }

View file

@ -0,0 +1,27 @@
package taskcontroller_test
import (
"errors"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
)
type TaskServiceMock struct {
CreateFunc func(string) (*models.Task, error)
UploadFunc func(*taskservice.TaskUploadInput) error
}
func (s *TaskServiceMock) Create(repo string) (*models.Task, error) {
if s.CreateFunc != nil {
return s.CreateFunc(repo)
}
return nil, errors.New("not implemented")
}
func (s *TaskServiceMock) Upload(input *taskservice.TaskUploadInput) error {
if s.UploadFunc != nil {
return s.UploadFunc(input)
}
return errors.New("not implemented")
}

View file

@ -0,0 +1,84 @@
package taskcontroller_test
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"gorm.io/gorm"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
)
func TestCreate(t *testing.T) {
tests := []struct {
name string
inputBody interface{}
mockService *TaskServiceMock
expectedStatus int
}{
{
name: "Valid request",
inputBody: map[string]string{
"repo": "example-foo",
},
mockService: &TaskServiceMock{
CreateFunc: func(repo string) (*models.Task, error) {
return &models.Task{
Model: gorm.Model{
ID: 1,
},
Status: models.StatusInProgress,
}, nil
},
},
expectedStatus: http.StatusOK,
},
{
name: "Invalid JSON",
inputBody: "invalid-json",
mockService: &TaskServiceMock{},
expectedStatus: http.StatusBadRequest,
},
{
name: "Service error",
inputBody: map[string]string{
"repo": "example-foo",
},
mockService: &TaskServiceMock{
CreateFunc: func(repo string) (*models.Task, error) {
return nil, errors.New("internal error")
},
},
expectedStatus: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
controller := taskcontroller.New(tt.mockService)
var body []byte
if tt.inputBody != nil {
body, _ = json.Marshal(tt.inputBody)
}
req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(controller.Create)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != tt.expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.expectedStatus)
}
})
}
}

View file

@ -0,0 +1,107 @@
package taskcontroller_test
import (
"bytes"
"errors"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
)
func TestUpload(t *testing.T) {
tests := []struct {
name string
taskID string
files []byte
mockService *TaskServiceMock
expectedStatus int
}{
{
name: "Valid upload",
taskID: "1",
files: []byte("test file content"), // можно заменить на реальный файл, если нужно
mockService: &TaskServiceMock{
UploadFunc: func(input *taskservice.TaskUploadInput) error {
return nil
},
},
expectedStatus: http.StatusOK,
},
{
name: "Invalid taskID",
taskID: "",
files: []byte("test file content"),
mockService: &TaskServiceMock{},
expectedStatus: http.StatusBadRequest,
},
/*
TODO:
{
name: "File too large",
taskID: "1",
files: []byte("test file content"),
mockService: &TaskServiceMock{
UploadFunc: func(input *taskservice.TaskUploadInput) error {
return nil
},
},
expectedStatus: http.StatusBadRequest,
},
*/
{
name: "Error during file upload",
taskID: "1",
files: []byte("test file content"),
mockService: &TaskServiceMock{
UploadFunc: func(input *taskservice.TaskUploadInput) error {
return errors.New("internal upload error")
},
},
expectedStatus: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
controller := taskcontroller.New(tt.mockService)
r := chi.NewRouter()
r.Post("/tasks/{taskID}/upload", controller.Upload)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("files", "file.txt")
if err != nil {
t.Fatalf("Failed to create form file: %v", err)
}
part.Write(tt.files)
writer.Close()
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest(http.MethodPost, ts.URL+"/tasks/"+tt.taskID+"/upload", body)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("Failed to execute request: %v", err)
}
defer resp.Body.Close()
if status := resp.StatusCode; status != tt.expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.expectedStatus)
}
})
}
}