implement relationship integration tests

This commit is contained in:
2025-04-23 19:34:19 +02:00
parent 92c1d29ace
commit b5342a19ca
12 changed files with 1206 additions and 60 deletions

View File

@@ -38,7 +38,7 @@ var cypherKeywords = []string{
"WHERE",
}
var cypherOperators = []string{ //nolint: unused // this could be used in the future
var cypherOperators = []string{ //nolint:unused // this could be used in the future
"+",
"-",
"*",

View File

@@ -1,3 +1,3 @@
MATCH (n)-[r]->(o)
MATCH (n)-[r:Sibling|:Spouse|:Child|:Parent]->(o)
WHERE id(n) = $id1 AND id(o) = $id2
RETURN r as relationship

View File

@@ -2,6 +2,7 @@ package memgraph
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
@@ -23,7 +24,12 @@ func GetRelationship(ctx context.Context, id1, id2 int) neo4j.ManagedTransaction
return nil, err
}
return record.AsMap(), nil
relationshipsRaw, ok := record.Get("relationship")
if !ok {
return nil, fmt.Errorf("no relationships found")
}
return relationshipsRaw, nil
}
}
@@ -60,7 +66,12 @@ func UpdateRelationship(
return nil, err
}
return record.AsMap(), nil
relationshipRaw, ok := record.Get("relationship")
if !ok {
return nil, fmt.Errorf("no relationships found")
}
return relationshipRaw, nil
}
}
@@ -104,11 +115,12 @@ func CreateChildParentRelationship(
siblingRecord, err := siblingResult.Single(ctx)
if err != nil {
siblingRelationship, ok := siblingRecord.Get("relationships")
siblings, ok := siblingRelationship.([]dbtype.Relationship)
if ok {
relationships = append(relationships, siblings...)
siblings, ok := siblingRelationship.([]dbtype.Relationship)
if ok {
relationships = append(relationships, siblings...)
}
}
}
@@ -131,7 +143,17 @@ func CreateSiblingRelationship(
return nil, err
}
return result.Collect(ctx)
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
relationshipsRaw, ok := record.Get("relationships")
if !ok {
return nil, fmt.Errorf("no relationships found")
}
return relationshipsRaw, nil
}
}
@@ -150,6 +172,16 @@ func CreateSpouseRelationship(
return nil, err
}
return result.Collect(ctx)
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
relationshipsRaw, ok := record.Get("relationships")
if !ok {
return nil, fmt.Errorf("no relationships found")
}
return relationshipsRaw, nil
}
}

View File

@@ -11,7 +11,7 @@ import (
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
)
func TestGetRelationship(t *testing.T) {
func TestGetRelationships(t *testing.T) {
testCases := []struct {
mockTxSetup func() *mock.Transaction
expectedResult map[string]any
@@ -24,8 +24,8 @@ func TestGetRelationship(t *testing.T) {
mockTx := new(mock.Transaction)
mockResult := new(mock.Result)
mockRecord := &neo4j.Record{
Values: []any{"value"},
Keys: []string{"key"},
Values: []any{map[string]any{"key": "value"}},
Keys: []string{"relationship"},
}
mockResult.On("Single", context.Background()).Return(mockRecord, nil)
mockTx.On("Run", context.Background(), GetRelationshipCypherQuery, map[string]any{"id1": 1, "id2": 2}).Return(mockResult, nil)
@@ -142,14 +142,14 @@ func TestUpdateRelationship(t *testing.T) {
mockTx := new(mock.Transaction)
mockResult := new(mock.Result)
mockRecord := &neo4j.Record{
Values: []any{"value"},
Keys: []string{"key"},
Values: []any{map[string]any{"key": "value"}},
Keys: []string{"relationship"},
}
mockResult.On("Single", context.Background()).Return(mockRecord, nil)
mockTx.On("Run", context.Background(), UpdateRelationshipCypherQuery, map[string]any{
"id1": 1,
"id2": 2,
"relationship": api.FamilyRelationship{},
"relationship": map[string]any{},
}).Return(mockResult, nil)
return mockTx
},
@@ -163,7 +163,7 @@ func TestUpdateRelationship(t *testing.T) {
mockTx.On("Run", context.Background(), UpdateRelationshipCypherQuery, map[string]any{
"id1": 1,
"id2": 2,
"relationship": api.FamilyRelationship{},
"relationship": map[string]any{},
}).Return(nil, errors.New("run error"))
return mockTx
},

View File

@@ -120,11 +120,11 @@ func processSlice(val reflect.Value) []any {
if isPreservedType(item) {
slice[i] = item
} else if reflect.ValueOf(item).Kind() == reflect.Struct {
switch item.(type) {
switch typedItem := item.(type) {
case openapi_types.Date:
slice[i] = item.(openapi_types.Date).String()
slice[i] = typedItem.String()
default:
slice[i] = StructToMap(item)
slice[i] = StructToMap(typedItem)
}
} else {
slice[i] = item