fix tests

This commit is contained in:
2025-04-18 13:50:03 +02:00
parent 8472085dba
commit c1ae4b8960
8 changed files with 63 additions and 19 deletions

View File

@@ -15,6 +15,8 @@ jobs:
- name: Setup Go 1.24.x'
uses: actions/setup-go@v5
with:
cache-dependency-path: |
${{ inputs.working-directory }}/go.sum
go-version: '1.24.1'
- name: Display Go version

View File

@@ -73,7 +73,7 @@ func TestCouldSeePersonsProfile(t *testing.T) {
mockSession := &memgraphMock.SessionWithContext{
ReturnOnce: &sync.Once{},
}
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("invalid"), "invalid", nil).Once()
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("invalid"), "invalid", nil)
err := CouldSeePersonsProfile(ctx, mockSession, 1, 3)
require.Error(t, err)

View File

@@ -121,14 +121,14 @@ func (srv *server) SoftDeletePerson(c *gin.Context, id int, params api.SoftDelet
qctx, qCancel := context.WithTimeout(c.Request.Context(), srv.dbOpTimeout)
defer qCancel()
res, err := session.ExecuteWrite(qctx, memgraph.SoftDeletePerson(qctx, id))
_, err := session.ExecuteWrite(qctx, memgraph.SoftDeletePerson(qctx, id))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
return
}
c.JSON(http.StatusOK, res)
c.JSON(http.StatusOK, map[string]string{"description": "Person soft deleted"})
}
func (srv *server) UpdatePerson(c *gin.Context, id int, params api.UpdatePersonParams) {

View File

@@ -76,7 +76,7 @@ func TestCreatePersonByGoogleIdAndInviteCode(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("ExecuteWrite", mock.Anything, mock.Anything, mock.Anything).Return(map[string]any{"result": "success"}, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
@@ -112,7 +112,7 @@ func TestCreatePersonByGoogleIdAndInviteCode(t *testing.T) {
srv.CreatePersonByGoogleIdAndInviteCode(c, "test-google-id")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "invalid character")
assert.Contains(t, w.Body.String(), "{\"msg\":\"unexpected EOF\"}")
})
}

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph"
@@ -23,15 +24,20 @@ func TestCreatePerson(t *testing.T) {
t.Run("Successful case", func(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockTransaction := new(memgraphMock.ExplicitTransaction)
mockResult := new(memgraphMock.Result)
mockResult.On("Single", mock.Anything).Return(map[string]any{"id": 1}, nil)
mockResult.On("Single", mock.Anything).Return(&neo4j.Record{
Values: []any{1},
Keys: []string{"id"},
}, nil)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("BeginTransaction", mock.Anything).Return(mockSession, nil)
mockSession.On("Run", mock.Anything, memgraph.CreatePersonCypherQuery, mock.Anything).Return(mockResult, nil)
mockSession.On("Run", mock.Anything, memgraph.CreateAdminRelationshipCypherQuery, mock.Anything).Return(nil, nil)
mockSession.On("Commit", mock.Anything).Return(nil)
mockSession.On("BeginTransaction", mock.Anything, mock.Anything).Return(mockTransaction, nil)
mockTransaction.On("Run", mock.Anything, memgraph.CreatePersonCypherQuery, mock.Anything).Return(mockResult, nil).Once()
mockTransaction.On("Run", mock.Anything, memgraph.CreateAdminRelationshipCypherQuery, mock.Anything).Return(nil, nil).Once()
mockTransaction.On("Commit", mock.Anything).Return(nil)
mockSession.On("Close", mock.Anything).Return(nil)
mockTransaction.On("Close", mock.Anything).Return(nil)
srv := &server{
db: mockDriver,
@@ -72,7 +78,7 @@ func TestCreatePerson(t *testing.T) {
mockSession := new(memgraphMock.SessionWithContext)
mockDriver := new(memgraphMock.DriverWithContext)
mockDriver.On("NewSession", mock.Anything, mock.Anything).Return(mockSession)
mockSession.On("BeginTransaction", mock.Anything).Return(nil, errors.New("transaction error"))
mockSession.On("BeginTransaction", mock.Anything, mock.Anything).Return(nil, errors.New("transaction error"))
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
@@ -104,7 +110,7 @@ func TestGetPersonById(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).Return(map[string]any{"id": 1}, nil)
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(map[string]any{"id": 1}, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
@@ -128,7 +134,7 @@ func TestGetPersonById(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).Return(nil, fmt.Errorf("unauthorized"))
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("unauthorized"))
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
@@ -156,7 +162,7 @@ func TestSoftDeletePerson(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(nil, nil)
mockSession.On("ExecuteWrite", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil)
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{
@@ -180,7 +186,7 @@ func TestSoftDeletePerson(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(nil, fmt.Errorf("unauthorized"))
mockSession.On("ExecuteRead", mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("unauthorized"))
mockSession.On("Close", mock.Anything).Return(nil)
srv := &server{

View File

@@ -24,3 +24,36 @@ func (m *Transaction) Run(ctx context.Context, cypher string, params map[string]
func (m *Transaction) legacy() neo4j.Transaction {
return m.Called().Get(0).(neo4j.Transaction)
}
type ExplicitTransaction struct {
neo4j.ExplicitTransaction
mock.Mock
}
func (m *ExplicitTransaction) Run(ctx context.Context, cypher string, params map[string]any) (neo4j.ResultWithContext, error) {
args := m.Called(ctx, cypher, params)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(neo4j.ResultWithContext), args.Error(1)
}
func (m *ExplicitTransaction) Commit(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *ExplicitTransaction) Rollback(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *ExplicitTransaction) Close(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *ExplicitTransaction) legacy() neo4j.Transaction {
return m.Called().Get(0).(neo4j.Transaction)
}

View File

@@ -107,8 +107,12 @@ func TestUpdatePersonByGoogleID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
mockTx := new(mock.Transaction)
mockTx.On("Run", ctx, UpdatePersonCypherQuery).Return(nil, tt.mockRunError)
mockResult := new(mock.Result)
mockResult.On("Single", ctx).Return(&neo4j.Record{
Values: []any{"value"},
Keys: []string{"key"},
}, nil)
mockTx.On("Run", ctx, UpdatePersonByInviteCodeCypherQuery, tmock.Anything).Return(mockResult, tt.mockRunError)
personProps := &api.PersonProperties{}
work := UpdatePersonByInviteCode(ctx, "test-person-id", personProps)

View File

@@ -6,7 +6,6 @@ import (
"testing"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph/mock"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
@@ -142,7 +141,7 @@ func TestHardDeletePerson(t *testing.T) {
require.Nil(t, result)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
require.Nil(t, result)
}
})
}