user postData instead

This commit is contained in:
vinchent 2024-07-14 11:59:43 +02:00
parent d76070c21d
commit 413bfc1685

View File

@ -58,7 +58,7 @@ func TestHandlers(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if resp.StatusCode != e.expectedStatusCode { if resp.StatusCode != e.expectedStatusCode {
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode) t.Errorf("for %s, expected %d but got %d\n", e.name, e.expectedStatusCode, resp.StatusCode)
} }
} }
} }
@ -98,7 +98,7 @@ func TestRepository_MakeReservation(t *testing.T) {
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode { if rr.Code != test.expectedStatusCode {
t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d", t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
test.name, rr.Code, http.StatusOK) test.name, rr.Code, http.StatusOK)
} }
} }
@ -109,103 +109,103 @@ func TestRepository_MakeReservation(t *testing.T) {
var postMakeReservationTests = []struct { var postMakeReservationTests = []struct {
name string name string
reservationInfo []string reservationInfo []postData
roomID int roomID int
expectedStatusCode int expectedStatusCode int
}{ }{
{ {
"ok", "ok",
[]string{ []postData{
"first_name=John", {key: "first_name", value: "John"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=1", {key: "room_id", value: "1"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
1, http.StatusSeeOther, 1, http.StatusSeeOther,
}, },
{ {
"no_session", "no_session",
[]string{ []postData{
"first_name=John", {key: "first_name", value: "John"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=0", {key: "room_id", value: "0"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
0, http.StatusTemporaryRedirect, 0, http.StatusTemporaryRedirect,
}, },
{ {
"no_post_data", "no_post_data",
[]string{}, []postData{},
0, http.StatusTemporaryRedirect, 0, http.StatusTemporaryRedirect,
}, },
{ {
"missing first name", "missing first name",
[]string{ []postData{
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=1", {key: "room_id", value: "0"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
1, http.StatusOK, 1, http.StatusOK,
}, },
{ {
"wrong first name", "wrong first name",
[]string{ []postData{
"first_name=J", {key: "first_name", value: "J"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=1", {key: "room_id", value: "0"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
1, http.StatusOK, 1, http.StatusOK,
}, },
{ {
"wrong email", "wrong email",
[]string{ []postData{
"first_name=John", {key: "first_name", value: "John"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith", {key: "email", value: "john@smith"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=1", {key: "room_id", value: "0"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
1, http.StatusOK, 1, http.StatusOK,
}, },
{ {
"insert reservation error", "insert reservation error",
[]string{ []postData{
"first_name=John", {key: "first_name", value: "John"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=2", {key: "room_id", value: "2"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
2, http.StatusTemporaryRedirect, 2, http.StatusTemporaryRedirect,
}, },
{ {
"insert room restriction error", "insert room restriction error",
[]string{ []postData{
"first_name=John", {key: "first_name", value: "John"},
"last_name=Smith", {key: "last_name", value: "Smith"},
"email=john@smith.com", {key: "email", value: "john@smith.com"},
"phone=1234", {key: "phone", value: "1234"},
"room_id=100", {key: "room_id", value: "100"},
"start_date=2050-01-01", {key: "start_date", value: "2050-01-01"},
"end_date=2050-01-02", {key: "end_date", value: "2050-01-02"},
}, },
100, http.StatusTemporaryRedirect, 100, http.StatusTemporaryRedirect,
}, },
@ -215,9 +215,11 @@ func TestRepository_PostMakeReservation(t *testing.T) {
for _, test := range postMakeReservationTests { for _, test := range postMakeReservationTests {
var reqBody string var reqBody string
if len(test.reservationInfo) > 0 { if len(test.reservationInfo) > 0 {
reqBody = test.reservationInfo[0] reqBody = fmt.Sprintf("%s=%s", test.reservationInfo[0].key,
test.reservationInfo[0].value)
for _, element := range test.reservationInfo[1:] { for _, element := range test.reservationInfo[1:] {
reqBody = fmt.Sprintf("%s&%s", reqBody, element) reqBody = fmt.Sprintf("%s&%s=%s", reqBody, element.key,
element.value)
} }
} }
layout := "2006-01-02" layout := "2006-01-02"
@ -245,7 +247,7 @@ func TestRepository_PostMakeReservation(t *testing.T) {
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode { if rr.Code != test.expectedStatusCode {
fmt.Printf("for %s, reservation handler returned response code: got %d, wanted %d", fmt.Printf("for %s, reservation handler returned response code: got %d, wanted %d\n",
test.name, rr.Code, test.expectedStatusCode) test.name, rr.Code, test.expectedStatusCode)
} }
} }