-
Notifications
You must be signed in to change notification settings - Fork 458
/
component_test.go
80 lines (59 loc) · 1.93 KB
/
component_test.go
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
package service
import (
"context"
"log"
"net/http"
"os"
"testing"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/server"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/tests"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainings/ports"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestCreateTraining(t *testing.T) {
t.Parallel()
token := tests.FakeAttendeeJWT(t, uuid.New().String())
client := tests.NewTrainingsHTTPClient(t, token)
hour := tests.RelativeDate(10, 12)
trainingUUID := client.CreateTraining(t, "some note", hour)
trainingsResponse := client.GetTrainings(t)
var trainingsUUIDs []string
for _, t := range trainingsResponse.Trainings {
trainingsUUIDs = append(trainingsUUIDs, t.Uuid)
}
require.Contains(t, trainingsUUIDs, trainingUUID)
}
func TestCancelTraining(t *testing.T) {
t.Parallel()
token := tests.FakeAttendeeJWT(t, uuid.New().String())
client := tests.NewTrainingsHTTPClient(t, token)
hour := tests.RelativeDate(10, 13)
trainingUUID := client.CreateTraining(t, "some note", hour)
client.CancelTraining(t, trainingUUID, http.StatusOK)
trainingsResponse := client.GetTrainings(t)
var trainingsUUIDs []string
for _, t := range trainingsResponse.Trainings {
trainingsUUIDs = append(trainingsUUIDs, t.Uuid)
}
require.NotContains(t, trainingsUUIDs, trainingUUID)
}
func startService() bool {
app := NewComponentTestApplication(context.Background())
trainingsHTTPAddr := os.Getenv("TRAININGS_HTTP_ADDR")
go server.RunHTTPServerOnAddr(trainingsHTTPAddr, func(router chi.Router) http.Handler {
return ports.HandlerFromMux(ports.NewHttpServer(app), router)
})
ok := tests.WaitForPort(trainingsHTTPAddr)
if !ok {
log.Println("Timed out waiting for trainings HTTP to come up")
}
return ok
}
func TestMain(m *testing.M) {
if !startService() {
os.Exit(1)
}
os.Exit(m.Run())
}