-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_integration_test.go
105 lines (86 loc) · 2.6 KB
/
e2e_integration_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
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
package main
import (
"context"
"encoding/json"
"function-first-composition-example-go/review-server/configuration"
"function-first-composition-example-go/review-server/server"
"function-first-composition-example-go/review-server/testutil"
"github.com/magiconair/properties/assert"
"io"
"math/rand"
"net/http"
"strconv"
"testing"
)
func TestRestaurantEndPointRanksRestaurant(t *testing.T) {
ctx := context.Background()
database := testutil.StartDB(ctx, "db/init.sql")
users := []testutil.User{
{"user1", "User 1", true},
{"user2", "User 2", false},
{"user3", "User 3", false},
}
restaurants := []testutil.Restaurant{
{"cafegloucesterid", "Cafe Gloucester"},
{"burgerkingid", "Burger King"},
}
ratings := []testutil.RatingForRestaurant{
{"rating1", users[0], restaurants[0], "EXCELLENT"},
{"rating2", users[1], restaurants[0], "TERRIBLE"},
{"rating3", users[2], restaurants[0], "AVERAGE"},
{"rating4", users[2], restaurants[1], "ABOVE_AVERAGE"},
}
for _, user := range users {
testutil.CreateUser(database, user)
}
for _, r := range restaurants {
testutil.CreateRestaurant(database, r)
}
for _, r := range ratings {
testutil.CreateRatingByUserForRestaurant(database, r)
}
t.Cleanup(func() {
if err := database.Stop(ctx); err != nil {
t.Fatalf("failed to terminate db: %s", err.Error())
}
})
randomPort := rand.Intn(65535-1024) + 1024
reviewServer := server.NewServer(func() *configuration.Configuration {
return &configuration.Configuration{
DataSource: *database.DataSource(),
ServerPort: randomPort,
}
})
if err := reviewServer.Start(); err != nil {
t.Fatalf("failed to start server: %s", err.Error())
}
t.Cleanup(func() {
if err := reviewServer.Stop(); err != nil {
t.Fatalf("failed to stop server: %s", err.Error())
}
})
response, err := http.Get("http://localhost:" + strconv.Itoa(randomPort) + "/vancouverbc/restaurants/recommended")
if err != nil {
t.Fatalf("Failed to make request: %s", err.Error())
}
assert.Equal(t, response.StatusCode, 200)
var body Response
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
t.Fatalf("Failed to read body: %v", err)
}
if err := json.Unmarshal(bodyBytes, &body); err != nil {
t.Fatalf("failed to unmarshall request body: %s", err.Error())
}
_ = json.Unmarshal(bodyBytes, &body)
assert.Equal(t, len(body.Restaurants), 2)
assert.Equal(t, body.Restaurants[0].Id, "cafegloucesterid")
assert.Equal(t, body.Restaurants[1].Id, "burgerkingid")
}
type Restaurant struct {
Id string `json:"id"`
Name string `json:"name"`
}
type Response struct {
Restaurants []Restaurant `json:"restaurants"`
}