Skip to content

Commit

Permalink
Add tests for restaurant review creation and listing
Browse files Browse the repository at this point in the history
Introduce comprehensive tests for creating and listing reviews for restaurants. The tests cover user authentication, restaurant creation, and verify the expected HTTP status codes for review operations.
  • Loading branch information
mukulmantosh committed Aug 14, 2024
1 parent 8fb2e3a commit 4fe0b22
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions pkg/tests/restaurant/review_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package restaurant

import (
"Go_Food_Delivery/cmd/api/middleware"
restroModel "Go_Food_Delivery/pkg/database/models/restaurant"
"Go_Food_Delivery/pkg/database/models/review"
userModel "Go_Food_Delivery/pkg/database/models/user"
"Go_Food_Delivery/pkg/handler"
revw "Go_Food_Delivery/pkg/handler/review"
restro "Go_Food_Delivery/pkg/service/restaurant"
reviewSrv "Go_Food_Delivery/pkg/service/review"
usr "Go_Food_Delivery/pkg/service/user"
"Go_Food_Delivery/pkg/tests"
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-faker/faker/v4"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

func TestReview(t *testing.T) {
t.Setenv("APP_ENV", "TEST")
t.Setenv("STORAGE_TYPE", "local")
t.Setenv("STORAGE_DIRECTORY", "uploads")
t.Setenv("LOCAL_STORAGE_PATH", "./tmp")
testDB := tests.Setup()
validate := validator.New()
AppEnv := os.Getenv("APP_ENV")
testServer := handler.NewServer(testDB)
middlewares := []gin.HandlerFunc{middleware.AuthMiddleware()}

userService := usr.NewUserService(testDB, AppEnv)
restaurantService := restro.NewRestaurantService(testDB, AppEnv)
reviewService := reviewSrv.NewReviewService(testDB, AppEnv)
revw.NewReviewProtectedHandler(testServer, "/review", reviewService, middlewares, validate)

type FakeUser struct {
User string `json:"user" faker:"name"`
Email string `json:"email" faker:"email"`
Password string `json:"password" faker:"password"`
}

var customUser FakeUser
var user userModel.User
var restrro restroModel.Restaurant
_ = faker.FakeData(&customUser)
user.Email = customUser.Email
user.Password = customUser.Password

ctx := context.Background()
_, err := userService.Add(ctx, &user)
if err != nil {
t.Error(err)
}

loginToken, err := userService.Login(ctx, user.ID)
if err != nil {
t.Fatal(err)
}

Token := fmt.Sprintf("Bearer %s", loginToken)

// Restaurant
name := faker.Name()
description := faker.Paragraph()
address := faker.Word()
city := faker.Word()
state := faker.Word()

restrro.Name = name
restrro.Description = description
restrro.Address = address
restrro.City = city
restrro.State = state

_, err = restaurantService.Add(ctx, &restrro)
if err != nil {
t.Fatal(err)
}

restaurants, err := restaurantService.ListRestaurants(ctx)
if err != nil {
t.Fatal(err)
}
restaurantId := restaurants[0].RestaurantID

t.Run("Review::Create", func(t *testing.T) {
var reviewParam review.ReviewParams
reviewParam.Comment = faker.Word()
reviewParam.Rating = 4
payload, err := json.Marshal(&reviewParam)
if err != nil {
t.Fatal(err)
}

req, _ := http.NewRequest(http.MethodPost,
fmt.Sprintf("/review/%d", restaurantId),
strings.NewReader(string(payload)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", Token)
w := httptest.NewRecorder()
testServer.Gin.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)

})

t.Run("Review::List", func(t *testing.T) {
var reviewParam review.ReviewParams
reviewParam.Comment = faker.Word()
reviewParam.Rating = 4
payload, err := json.Marshal(&reviewParam)
if err != nil {
t.Fatal(err)
}

req, _ := http.NewRequest(http.MethodGet,
fmt.Sprintf("/review/%d", restaurantId),
strings.NewReader(string(payload)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", Token)
w := httptest.NewRecorder()
testServer.Gin.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)

})

//tests.Teardown(testDB)

}

0 comments on commit 4fe0b22

Please sign in to comment.