-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test.go
More file actions
160 lines (135 loc) · 4.5 KB
/
Copy pathe2e_test.go
File metadata and controls
160 lines (135 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go-backend/db"
"go-backend/mistral"
"go-backend/otel"
"go-backend/transcribe"
)
var (
DB *db.Queries
baseURL string
)
func TestMain(m *testing.M) {
ctx := context.Background()
shutdown, err := otel.Setup()
if err != nil {
log.Fatal("setup otel: ", err)
}
defer shutdown()
postgres, err := testcontainers.Run(
ctx, "postgres:17",
testcontainers.WithEnv(map[string]string{"POSTGRES_PASSWORD": "postgres"}),
testcontainers.WithExposedPorts("5432/tcp"),
testcontainers.WithWaitStrategy(
wait.ForListeningPort("5432/tcp"),
wait.ForLog("database system is ready to accept connections"),
),
)
if err != nil {
log.Fatal("start postgres: ", err)
}
host, _ := postgres.Host(ctx)
port, _ := postgres.MappedPort(ctx, "5432/tcp")
os.Setenv("DATABASE_URL", fmt.Sprintf("postgres://postgres:postgres@%s:%s/postgres?sslmode=disable", host, port.Port()))
database, err := db.Connect(ctx)
if err != nil {
log.Fatal("connect db: ", err)
}
DB = db.New(database)
mistralStub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(mistral.TranscribeResponse{
Text: "Hello from stub",
Usage: mistral.Usage{TotalTokens: 42},
})
}))
controller := transcribe.NewController(DB, mistral.NewMistral(mistralStub.URL))
appServer := httptest.NewServer(NewServer(controller).Handler())
baseURL = appServer.URL
code := m.Run()
appServer.Close()
mistralStub.Close()
database.Close()
postgres.Terminate(ctx)
os.Exit(code)
}
func createUser(t *testing.T) uuid.UUID {
t.Helper()
userID := uuid.New()
_, err := DB.CreateUser(t.Context(), userID)
require.NoError(t, err)
return userID
}
func tokenUsage(t *testing.T, userID uuid.UUID) int {
t.Helper()
req, _ := http.NewRequest(http.MethodGet, baseURL+"/tokenUsage", nil)
req.Header.Set("X-User-ID", userID.String())
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var count int
fmt.Sscanf(string(body), "%d", &count)
return count
}
func TestTranscribe(t *testing.T) {
userID := createUser(t)
req, _ := http.NewRequest(http.MethodPost, baseURL+"/transcribe", strings.NewReader(`{"uri":"https://example.com/audio.mp3"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User-ID", userID.String())
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "Hello from stub", string(body))
}
func TestTokenUsage(t *testing.T) {
userID := createUser(t)
req, _ := http.NewRequest(http.MethodPost, baseURL+"/transcribe", strings.NewReader(`{"uri":"https://example.com/audio.mp3"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User-ID", userID.String())
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, 42, tokenUsage(t, userID))
}
func TestMetrics(t *testing.T) {
userID := createUser(t)
req, _ := http.NewRequest(http.MethodPost, baseURL+"/transcribe", strings.NewReader(`{"uri":"https://example.com/audio.mp3"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User-ID", userID.String())
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
resp.Body.Close()
metricsResp, err := http.Get(baseURL + "/metrics")
require.NoError(t, err)
defer metricsResp.Body.Close()
require.Equal(t, http.StatusOK, metricsResp.StatusCode)
body, err := io.ReadAll(metricsResp.Body)
require.NoError(t, err)
metrics := string(body)
assert.Contains(t, metrics, "http_server_request_duration_seconds", "HTTP server duration metric missing")
assert.Contains(t, metrics, "http_client_request_duration_seconds", "HTTP client (Mistral) duration metric missing")
assert.Contains(t, metrics, "db_sql_latency_milliseconds", "DB query duration metric missing")
assert.Contains(t, metrics, "db_sql_connection_open", "DB connection pool metric missing")
}