diff --git a/apps/db-adapter/internal/memgraph/admin.go b/apps/db-adapter/internal/memgraph/admin.go index 300767f..a2d6720 100644 --- a/apps/db-adapter/internal/memgraph/admin.go +++ b/apps/db-adapter/internal/memgraph/admin.go @@ -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 } } diff --git a/apps/db-adapter/internal/memgraph/admin_test.go b/apps/db-adapter/internal/memgraph/admin_test.go new file mode 100644 index 0000000..2aecae0 --- /dev/null +++ b/apps/db-adapter/internal/memgraph/admin_test.go @@ -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) + } + }) + } +}