implement person_google_tests + close session

This commit is contained in:
2025-04-18 09:51:06 +02:00
parent de9c38032c
commit 443db56df4
2 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package api
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/mock"
memgraphMock "github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph/mock"
"go.uber.org/zap/zaptest"
)
func TestCloseSession(t *testing.T) {
logger := zaptest.NewLogger(t)
mockSession := new(memgraphMock.SessionWithContext)
ctx := context.Background()
timeOut := 2 * time.Second
t.Run("successful session close", func(t *testing.T) {
mockSession.On("Close", mock.Anything).Return(nil).Once()
closeSession(ctx, logger, mockSession, timeOut)
mockSession.AssertCalled(t, "Close", mock.Anything)
mockSession.AssertExpectations(t)
})
t.Run("error during session close", func(t *testing.T) {
mockSession.On("Close", mock.Anything).Return(errors.New("close error")).Once()
closeSession(ctx, logger, mockSession, timeOut)
mockSession.AssertCalled(t, "Close", mock.Anything)
mockSession.AssertExpectations(t)
})
}

View File

@@ -0,0 +1,156 @@
package api
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
memgraphMock "github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph/mock"
)
func TestGetPersonByGoogleId(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("Successful case", func(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(map[string]any{"person": "test-person"}, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
db: mockDriver,
dbOpTimeout: 5 * time.Second,
}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/person/google-id", nil)
srv.GetPersonByGoogleId(c, "test-google-id")
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "test-person")
})
t.Run("Internal server error case", func(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("db error"))
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
db: mockDriver,
dbOpTimeout: 5 * time.Second,
}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/person/google-id", nil)
srv.GetPersonByGoogleId(c, "test-google-id")
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, w.Body.String(), "db error")
})
}
func TestCreatePersonByGoogleIdAndInviteCode(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("Successful case", func(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("ExecuteWrite", mock.Anything, mock.Anything).Return(map[string]any{"result": "success"}, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
db: mockDriver,
dbOpTimeout: 5 * time.Second,
}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"invite_code": "test-code", "person": {"name": "test-person"}}`
c.Request = httptest.NewRequest(http.MethodPost, "/person/google-id/invite-code", nil)
c.Request.Body = io.NopCloser(strings.NewReader(body))
srv.CreatePersonByGoogleIdAndInviteCode(c, "test-google-id")
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "success")
})
t.Run("Bad request case", func(t *testing.T) {
srv := &server{}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"invalid_json":`
c.Request = httptest.NewRequest(http.MethodPost, "/person/google-id/invite-code", nil)
c.Request.Body = io.NopCloser(strings.NewReader(body))
srv.CreatePersonByGoogleIdAndInviteCode(c, "test-google-id")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "invalid character")
})
}
func TestCreatePersonByGoogleId(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Run("Successful case", func(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("ExecuteWrite", mock.Anything, mock.Anything, mock.Anything).Return(map[string]any{"result": "success"}, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
db: mockDriver,
dbOpTimeout: 5 * time.Second,
}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"name": "test-person"}`
c.Request = httptest.NewRequest(http.MethodPost, "/person/google-id", nil)
c.Request.Body = io.NopCloser(strings.NewReader(body))
srv.CreatePersonByGoogleId(c, "test-google-id")
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "success")
})
t.Run("Bad request case", func(t *testing.T) {
srv := &server{}
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"invalid_json":`
c.Request = httptest.NewRequest(http.MethodPost, "/person/google-id", nil)
c.Request.Body = io.NopCloser(strings.NewReader(body))
srv.CreatePersonByGoogleId(c, "test-google-id")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "{\"msg\":\"unexpected EOF\"}")
})
}