Skip to content

Commit 5b66a32

Browse files
committed
add some tests to books repository
1 parent ac8a162 commit 5b66a32

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

pkg/api/books_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,97 @@
11
package api
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"golang-rest-api-template/pkg/cache"
7+
"golang-rest-api-template/pkg/database"
8+
"golang-rest-api-template/pkg/models"
9+
"net/http"
10+
"net/http/httptest"
11+
"testing"
12+
13+
"gorm.io/gorm"
14+
15+
"github.com/go-redis/redis/v8"
16+
17+
"github.com/gin-gonic/gin"
18+
"github.com/golang/mock/gomock"
19+
"github.com/stretchr/testify/assert"
20+
)
21+
22+
func TestNewBookRepository(t *testing.T) {
23+
ctrl := gomock.NewController(t)
24+
defer ctrl.Finish()
25+
26+
mockDB := database.NewMockDatabase(ctrl)
27+
mockCache := cache.NewMockCache(ctrl)
28+
mockCtx := context.Background()
29+
30+
repo := NewBookRepository(mockDB, mockCache, &mockCtx)
31+
32+
assert.NotNil(t, repo, "NewBookRepository should return a non-nil instance of bookRepository")
33+
assert.Equal(t, mockDB, repo.DB, "DB should be set to the mock database instance")
34+
assert.Equal(t, mockCache, repo.RedisClient, "RedisClient should be set to the mock cache instance")
35+
}
36+
37+
func TestHealthcheck(t *testing.T) {
38+
// Set up the mock controller and the mocked dependencies
39+
ctrl := gomock.NewController(t)
40+
defer ctrl.Finish()
41+
42+
// Set up the Gin context with a response recorder
43+
gin.SetMode(gin.TestMode)
44+
recorder := httptest.NewRecorder()
45+
_, router := gin.CreateTestContext(recorder)
46+
47+
// Create a mock repository and expect the Healthcheck method to be called
48+
mockRepo := NewMockBookRepository(ctrl)
49+
mockRepo.EXPECT().Healthcheck(gomock.Any()).Do(func(c *gin.Context) {
50+
c.JSON(http.StatusOK, "ok") // Explicitly setting the response here
51+
})
52+
53+
// Setting up a basic GET route to test Healthcheck
54+
router.GET("/healthcheck", mockRepo.Healthcheck)
55+
56+
// Perform the GET request
57+
req, _ := http.NewRequest(http.MethodGet, "/healthcheck", nil)
58+
router.ServeHTTP(recorder, req)
59+
60+
// Check the response
61+
assert.Equal(t, http.StatusOK, recorder.Code)
62+
assert.Equal(t, "\"ok\"", recorder.Body.String())
63+
}
64+
65+
func TestFindBooks(t *testing.T) {
66+
ctrl := gomock.NewController(t)
67+
defer ctrl.Finish()
68+
69+
mockDB := database.NewMockDatabase(ctrl)
70+
mockCache := cache.NewMockCache(ctrl)
71+
mockGormDB := database.NewMockDatabase(ctrl) // Correct type for GORM DB operations
72+
ctx := context.Background()
73+
74+
repo := NewBookRepository(mockDB, mockCache, &ctx)
75+
76+
// Set up Gin
77+
gin.SetMode(gin.TestMode)
78+
r := gin.Default()
79+
r.GET("/books", repo.FindBooks)
80+
81+
// Set up common mock expectations
82+
mockGormDB.EXPECT().Find(gomock.Any()).DoAndReturn(func(books *[]models.Book) *gorm.DB {
83+
*books = append(*books, models.Book{Title: "New Book", Author: "New Author"})
84+
return &gorm.DB{Error: nil} // Assume this is the struct provided by the actual Gorm package
85+
}).AnyTimes()
86+
87+
books := []models.Book{{Title: "Book One", Author: "Author One"}}
88+
cachedData, _ := json.Marshal(books)
89+
mockCache.EXPECT().Get(ctx, "books_offset_0_limit_10").Return(redis.NewStringResult(string(cachedData), nil))
90+
91+
w := httptest.NewRecorder()
92+
req, _ := http.NewRequest("GET", "/books?offset=0&limit=10", nil)
93+
r.ServeHTTP(w, req)
94+
95+
assert.Equal(t, http.StatusOK, w.Code)
96+
assert.Contains(t, w.Body.String(), "Book One")
97+
}

0 commit comments

Comments
 (0)