test admin operations

This commit is contained in:
2025-04-10 15:58:46 +02:00
parent 31bc1c0bb9
commit 7bf31c0975
2 changed files with 102 additions and 1 deletions

View File

@@ -72,6 +72,29 @@ func GetProfileAdmins(ctx context.Context, userId int) neo4j.ManagedTransactionW
return nil, err
}
return result.Collect(ctx)
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}
func GetManagedProfiles(ctx context.Context, userId int) neo4j.ManagedTransactionWork {
return func(tx neo4j.ManagedTransaction) (any, error) {
result, err := tx.Run(ctx, GetManagedProfilesCypherQuery, map[string]any{
"id": userId,
})
if err != nil {
return nil, err
}
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.AsMap(), nil
}
}

View File

@@ -0,0 +1,78 @@
package memgraph
import (
"context"
"errors"
"testing"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/stretchr/testify/assert"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph/mock"
)
func TestGetManagedProfiles(t *testing.T) {
testCases := []struct {
name string
mockTxSetup func() *mock.Transaction
expectedResult map[string]any
expectedError error
}{
{
name: "Successful case",
mockTxSetup: func() *mock.Transaction {
mockTx := new(mock.Transaction)
mockResult := new(mock.Result)
mockRecord := &neo4j.Record{
Values: []any{"value"},
Keys: []string{"key"},
}
mockResult.On("Single", context.Background()).Return(mockRecord, nil)
mockTx.On("Run", context.Background(), GetManagedProfilesCypherQuery, map[string]any{"id": 123}).Return(mockResult, nil)
return mockTx
},
expectedResult: map[string]any{"key": "value"},
expectedError: nil,
},
{
name: "Error during Run",
mockTxSetup: func() *mock.Transaction {
mockTx := new(mock.Transaction)
mockTx.On("Run", context.Background(), GetManagedProfilesCypherQuery, map[string]any{"id": 123}).Return(nil, errors.New("run error"))
return mockTx
},
expectedResult: nil,
expectedError: errors.New("run error"),
},
{
name: "Error during Single",
mockTxSetup: func() *mock.Transaction {
mockTx := new(mock.Transaction)
mockResult := new(mock.Result)
mockResult.On("Single", context.Background()).Return(nil, errors.New("single error"))
mockTx.On("Run", context.Background(), GetManagedProfilesCypherQuery, map[string]any{"id": 123}).Return(mockResult, nil)
return mockTx
},
expectedResult: nil,
expectedError: errors.New("single error"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
userId := 123
mockTx := tc.mockTxSetup()
work := GetManagedProfiles(ctx, userId)
result, err := work(mockTx)
if tc.expectedError != nil {
assert.Error(t, err)
assert.Nil(t, result)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}