Skip to content

Commit

Permalink
InserReservation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 9, 2023
1 parent 3644ace commit 67a4bec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Binary file modified bookings
Binary file not shown.
25 changes: 25 additions & 0 deletions internal/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ func TestRepository_PostReservation(t *testing.T) {
if rr.Code != http.StatusTemporaryRedirect {
t.Errorf("PostReservation handler returned wrong response code for invalid Start date: got %d, wanted %d", rr.Code, http.StatusTemporaryRedirect)
}

// test for failure to insert reservation into DB
postedData = url.Values{}
postedData.Add("start_date", "invalid")
postedData.Add("end_date", "2050-01-02")
postedData.Add("first_name", "John")
postedData.Add("last_name", "Smith")
postedData.Add("email", "john@smith.com")
postedData.Add("phone", "555-555-5555")
postedData.Add("room_id", "2")
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode()))
ctx, _ = getCtx(req)
req = req.WithContext(ctx)

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

rr = httptest.NewRecorder()

handler = http.HandlerFunc(Repo.PostReservation)

handler.ServeHTTP(rr, req)

if rr.Code != http.StatusTemporaryRedirect {
t.Errorf("PostReservation handler faild whrt trying to fail insertingf reservation: got %d, wanted %d", rr.Code, http.StatusTemporaryRedirect)
}
}

func getCtx(req *http.Request) (context.Context, error) {
Expand Down
6 changes: 5 additions & 1 deletion internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func (m *testDBRepo) AllUsers() bool {

// InserReservation inserts a reservation into the database
func (m *testDBRepo) InserReservation(res models.Reservation) (int, error) {
return 1, nil
// if the room id is 2, then fail; otherwise, pass
if res.RoomID == 2 {
return 0, errors.New("InserReservation error")
}
return 1, nil
}

// InsertRoomRestriction inserts a room restriction into the database
Expand Down

0 comments on commit 67a4bec

Please sign in to comment.