add tests for postavailability and reservation summary
This commit is contained in:
		@ -2,11 +2,9 @@ package handlers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"go-udemy-web-1/internal/config"
 | 
			
		||||
	"go-udemy-web-1/internal/driver"
 | 
			
		||||
	"go-udemy-web-1/internal/forms"
 | 
			
		||||
	"go-udemy-web-1/internal/helpers"
 | 
			
		||||
	"go-udemy-web-1/internal/models"
 | 
			
		||||
	"go-udemy-web-1/internal/render"
 | 
			
		||||
	"go-udemy-web-1/internal/repository"
 | 
			
		||||
@ -225,18 +223,21 @@ func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	layout := "2006-01-02"
 | 
			
		||||
	startDate, err := time.Parse(layout, start)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't parse start date")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	endDate, err := time.Parse(layout, end)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't parse end date")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't connect to database")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -248,7 +249,8 @@ func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		// No availability
 | 
			
		||||
		m.App.InfoLog.Println("No availability")
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "No availability")
 | 
			
		||||
		http.Redirect(w, r, "/search-availability", http.StatusSeeOther)
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusSeeOther)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	data := make(map[string]interface{})
 | 
			
		||||
@ -358,14 +360,16 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	roomID, err := strconv.Atoi(chi.URLParam(r, "id"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't parse roomID")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	m.App.Session.Get(r.Context(), "reservation")
 | 
			
		||||
 | 
			
		||||
	res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		helpers.ServerError(w, errors.New("cannot get reservation from session"))
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -386,17 +390,20 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	layout := "2006-01-02"
 | 
			
		||||
	startDate, err := time.Parse(layout, sd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	endDate, err := time.Parse(layout, ed)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't parse start date")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	room, err := m.DB.GetRoomById(roomID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		helpers.ServerError(w, err)
 | 
			
		||||
		m.App.Session.Put(r.Context(), "error", "Can't parse end date")
 | 
			
		||||
		http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	res.RoomID = roomID
 | 
			
		||||
 | 
			
		||||
@ -351,6 +351,106 @@ func Test_AvailabilityJSON(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
// {{{ Test ReservationSummary
 | 
			
		||||
 | 
			
		||||
var reservationSummaryTests = []struct {
 | 
			
		||||
	name               string
 | 
			
		||||
	haveSession        bool
 | 
			
		||||
	expectedStatusCode int
 | 
			
		||||
}{
 | 
			
		||||
	{"ok", true, http.StatusOK},
 | 
			
		||||
	{"nok", false, http.StatusTemporaryRedirect},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test_ReservationSummary(t *testing.T) {
 | 
			
		||||
	for _, test := range reservationSummaryTests {
 | 
			
		||||
		req, _ := http.NewRequest("GET", "/reservation-summary", nil)
 | 
			
		||||
		ctx := getCtx(req)
 | 
			
		||||
		req = req.WithContext(ctx)
 | 
			
		||||
 | 
			
		||||
		rr := httptest.NewRecorder()
 | 
			
		||||
 | 
			
		||||
		layout := "2006-01-02"
 | 
			
		||||
		sd, _ := time.Parse(layout, "2050-01-01")
 | 
			
		||||
		ed, _ := time.Parse(layout, "2050-01-02")
 | 
			
		||||
		reservation := models.Reservation{
 | 
			
		||||
			StartDate: sd,
 | 
			
		||||
			EndDate:   ed,
 | 
			
		||||
			FirstName: "John",
 | 
			
		||||
			LastName:  "Smith",
 | 
			
		||||
			Email:     "john@smith.com",
 | 
			
		||||
			RoomID:    1,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if test.haveSession {
 | 
			
		||||
			session.Put(req.Context(), "reservation", reservation)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		handler := http.HandlerFunc(Repo.ReservationSummary)
 | 
			
		||||
		handler.ServeHTTP(rr, req)
 | 
			
		||||
 | 
			
		||||
		if rr.Code != test.expectedStatusCode {
 | 
			
		||||
			t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
 | 
			
		||||
				test.name, rr.Code, test.expectedStatusCode)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
// {{{ Test PostAvailability
 | 
			
		||||
 | 
			
		||||
var postAvailabilityTests = []struct {
 | 
			
		||||
	name               string
 | 
			
		||||
	queryInfo          []postData
 | 
			
		||||
	expectedStatusCode int
 | 
			
		||||
}{
 | 
			
		||||
	{"ok", []postData{
 | 
			
		||||
		{key: "start", value: "2050-01-01"},
 | 
			
		||||
		{key: "end", value: "2050-01-02"},
 | 
			
		||||
	}, http.StatusOK},
 | 
			
		||||
	{"database error", []postData{
 | 
			
		||||
		{key: "start", value: "2050-01-03"},
 | 
			
		||||
		{key: "end", value: "2050-01-04"},
 | 
			
		||||
	}, http.StatusTemporaryRedirect},
 | 
			
		||||
	{"no availability", []postData{
 | 
			
		||||
		{key: "start", value: "2050-01-05"},
 | 
			
		||||
		{key: "end", value: "2050-01-06"},
 | 
			
		||||
	}, http.StatusSeeOther},
 | 
			
		||||
	{"wrong start date", []postData{
 | 
			
		||||
		{key: "start", value: "2050-05"},
 | 
			
		||||
		{key: "end", value: "2050-01-06"},
 | 
			
		||||
	}, http.StatusTemporaryRedirect},
 | 
			
		||||
	{"wrong end date", []postData{
 | 
			
		||||
		{key: "start", value: "2050-01-05"},
 | 
			
		||||
		{key: "end", value: "01-06"},
 | 
			
		||||
	}, http.StatusTemporaryRedirect},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test_PostAvailability(t *testing.T) {
 | 
			
		||||
	for _, test := range postAvailabilityTests {
 | 
			
		||||
		reqBody := url.Values{}
 | 
			
		||||
		for _, element := range test.queryInfo {
 | 
			
		||||
			reqBody.Add(element.key, element.value)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		req, _ := http.NewRequest("POST", "/availability", strings.NewReader(reqBody.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.PostAvailability)
 | 
			
		||||
		handler.ServeHTTP(rr, req)
 | 
			
		||||
 | 
			
		||||
		if rr.Code != test.expectedStatusCode {
 | 
			
		||||
			t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
 | 
			
		||||
				test.name, rr.Code, test.expectedStatusCode)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
// {{{ Test Helpers
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -41,6 +41,17 @@ func (m *testDBRepo) SearchAvailabilityByDatesByRoomID(start, end time.Time, roo
 | 
			
		||||
// SearchAvailabilityForAllRooms returns a slice of rooms, if any, for given date range
 | 
			
		||||
func (m *testDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error) {
 | 
			
		||||
	var rooms []models.Room
 | 
			
		||||
	if start.Format("2006-01-02") == "2050-01-01" && end.Format("2006-01-02") == "2050-01-02" {
 | 
			
		||||
		room := models.Room{
 | 
			
		||||
			RoomName: "room",
 | 
			
		||||
			ID:       1,
 | 
			
		||||
		}
 | 
			
		||||
		rooms = append(rooms, room)
 | 
			
		||||
		return rooms, nil
 | 
			
		||||
	}
 | 
			
		||||
	if start.Format("2006-01-02") == "2050-01-03" && end.Format("2006-01-02") == "2050-01-04" {
 | 
			
		||||
		return rooms, errors.New("deliberate error")
 | 
			
		||||
	}
 | 
			
		||||
	return rooms, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user