Extract logic into transcribe service

This commit is contained in:
2025-08-12 10:59:51 +03:00
parent f625e21418
commit 2c9a5f4bfb
8 changed files with 354 additions and 356 deletions

View File

@@ -9,16 +9,20 @@ import (
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"time"
"git.vakhrushev.me/av/transcriber/internal/entity"
"git.vakhrushev.me/av/transcriber/internal/repo/sqlite"
"git.vakhrushev.me/av/transcriber/internal/service/transcribe"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/sqlite3"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"github.com/pressly/goose/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -30,32 +34,15 @@ func setupTestDB(t *testing.T) (*sql.DB, *goqu.Database) {
gq := goqu.New("sqlite3", db)
// Создаем таблицы
createFilesTable := `
CREATE TABLE files (
id TEXT PRIMARY KEY,
storage TEXT NOT NULL,
size INTEGER NOT NULL,
created_at DATETIME NOT NULL
);`
createJobsTable := `
CREATE TABLE transcribe_jobs (
id TEXT PRIMARY KEY,
state TEXT NOT NULL,
file_id TEXT,
is_error BOOLEAN NOT NULL DEFAULT 0,
error_text TEXT,
worker TEXT,
acquired_at DATETIME,
created_at DATETIME NOT NULL,
FOREIGN KEY (file_id) REFERENCES files(id)
);`
_, err = db.Exec(createFilesTable)
err = goose.SetDialect("sqlite3")
require.NoError(t, err)
_, err = db.Exec(createJobsTable)
_, b, _, _ := runtime.Caller(0)
migpath, err := filepath.Abs(path.Join(b, "../../../../migrations"))
require.NoError(t, err)
err = goose.Up(db, migpath)
require.NoError(t, err)
return db, gq
@@ -69,15 +56,17 @@ func setupTestRouter(t *testing.T) (*gin.Engine, *TranscribeHandler) {
fileRepo := sqlite.NewFileRepository(db, gq)
jobRepo := sqlite.NewTranscriptJobRepository(db, gq)
handler := NewTranscribeHandler(jobRepo, fileRepo)
trsService := transcribe.NewTranscribeService(jobRepo, fileRepo)
handler := NewTranscribeHandler(jobRepo, trsService)
router := gin.New()
router.MaxMultipartMemory = 32 << 20 // 32 MiB
api := router.Group("/api")
{
api.POST("/transcribe/audio", handler.CreateTranscribeJob)
api.GET("/transcribe/:id", handler.GetTranscribeJobStatus)
api.POST("/audio", handler.CreateTranscribeJob)
api.GET("/status/:id", handler.GetTranscribeJobStatus)
}
return router, handler
@@ -106,7 +95,7 @@ func createMultipartRequest(t *testing.T, audioFilePath string) (*http.Request,
require.NoError(t, err)
// Создаем HTTP запрос
req, err := http.NewRequest("POST", "/api/transcribe/audio", &buf)
req, err := http.NewRequest("POST", "/api/audio", &buf)
require.NoError(t, err)
req.Header.Set("Content-Type", writer.FormDataContentType())
@@ -182,7 +171,7 @@ func TestCreateTranscribeJob_NoFile(t *testing.T) {
router, _ := setupTestRouter(t)
// Создаем запрос без файла
req, err := http.NewRequest("POST", "/api/transcribe/audio", nil)
req, err := http.NewRequest("POST", "/api/audio", nil)
require.NoError(t, err)
// Выполняем запрос
@@ -333,7 +322,7 @@ func TestGetTranscribeJobStatus_Success(t *testing.T) {
require.NoError(t, err)
// Создаем запрос
req, err := http.NewRequest("GET", "/api/transcribe/test-job-id", nil)
req, err := http.NewRequest("GET", "/api/status/test-job-id", nil)
require.NoError(t, err)
// Выполняем запрос
@@ -356,7 +345,7 @@ func TestGetTranscribeJobStatus_NotFound(t *testing.T) {
router, _ := setupTestRouter(t)
// Создаем запрос с несуществующим ID
req, err := http.NewRequest("GET", "/api/transcribe/non-existent-id", nil)
req, err := http.NewRequest("GET", "/api/status/non-existent-id", nil)
require.NoError(t, err)
// Выполняем запрос