Пример #1
0
 public ReplaceRelationshipsCommandTests()
 {
     ReplaceRelationshipsCommand = new ReplaceRelationshipsCommand
     {
         SourceIdPropertyName  = $"{nameof(ReplaceRelationshipsCommand.SourceIdPropertyName)}",
         SourceIdPropertyValue = $"{nameof(ReplaceRelationshipsCommand.SourceIdPropertyValue)}",
         SourceNodeLabels      = { "SourceNodeLabel" }
     };
 }
        public async Task ReplaceRelationships_CreateSingleNewRelationship_NoExistingRelationships_Test()
        {
            const string sourceNodeLabel       = "sourceNodeLabel";
            const string sourceIdPropertyName  = "sourceId";
            string       sourceIdPropertyValue = Guid.NewGuid().ToString();

            const string destNodeLabel       = "destNodeLabel";
            const string destIdPropertyName  = "destId";
            string       destIdPropertyValue = Guid.NewGuid().ToString();

            const string relationshipType     = "relationshipType";
            const string relationshipVariable = "r";

            //todo: arrange without any of the cut?
            // create source node to create relationship from
            long sourceNodeId = await MergeNode(sourceNodeLabel, sourceIdPropertyName,
                                                new Dictionary <string, object> {
                { sourceIdPropertyName, sourceIdPropertyValue }
            });

            // create destination node to create relationship to
            long destNodeId = await MergeNode(destNodeLabel, destIdPropertyName,
                                              new Dictionary <string, object> {
                { destIdPropertyName, destIdPropertyValue }
            });

            // act
            var command = new ReplaceRelationshipsCommand
            {
                SourceNodeLabels = new HashSet <string> {
                    sourceNodeLabel
                },
                SourceIdPropertyName  = sourceIdPropertyName,
                SourceIdPropertyValue = sourceIdPropertyValue
            };

            command.AddRelationshipsTo(
                relationshipType, null,
                new[] { destNodeLabel },
                destIdPropertyName,
                destIdPropertyValue);
            await _graphDatabase.RunWriteQueries(command);

            AssertResult(relationshipVariable, new[]
            {
                new ExpectedRelationship
                {
                    Type        = relationshipType,
                    StartNodeId = sourceNodeId,
                    EndNodeId   = destNodeId,
                    Properties  = new Dictionary <string, object>()
                }
            }, await AllRelationships(sourceNodeLabel, sourceIdPropertyName, sourceIdPropertyValue,
                                      relationshipType, destNodeLabel, relationshipVariable));
        }
        public async Task ReplaceRelationships_CreateSingleNewRelationship_ExistingRelationship_Test()
        {
            const string sourceNodeLabel       = "sourceNodeLabel";
            const string sourceIdPropertyName  = "sourceId";
            string       sourceIdPropertyValue = Guid.NewGuid().ToString();

            const string destNodeLabel       = "destNodeLabel";
            const string destIdPropertyName  = "destId";
            string       destIdPropertyValue = Guid.NewGuid().ToString();

            string preexistingDestIdPropertyValue = Guid.NewGuid().ToString();

            const string relationshipType     = "relationshipType";
            const string relationshipVariable = "r";

            // create source node to create relationship from
            long sourceNodeId = await MergeNode(sourceNodeLabel, sourceIdPropertyName,
                                                new Dictionary <string, object> {
                { sourceIdPropertyName, sourceIdPropertyValue }
            });

            // create destination node for preexisting relationship
            await MergeNode(destNodeLabel, destIdPropertyName,
                            new Dictionary <string, object> {
                { destIdPropertyName, preexistingDestIdPropertyValue }
            });

            // create destination node to create new relationship to
            long destNodeId = await MergeNode(destNodeLabel, destIdPropertyName,
                                              new Dictionary <string, object> {
                { destIdPropertyName, destIdPropertyValue }
            });

            // create pre-existing relationships
            var preexistingQuery = new ReplaceRelationshipsCommand
            {
                SourceNodeLabels = new HashSet <string> {
                    sourceNodeLabel
                },
                SourceIdPropertyName  = sourceIdPropertyName,
                SourceIdPropertyValue = sourceIdPropertyValue
            };

            preexistingQuery.AddRelationshipsTo(relationshipType, null, new [] { destNodeLabel }, destIdPropertyName, destIdPropertyValue);

            await _graphDatabase.RunWriteQueries(preexistingQuery);

            // act
            //todo: change dest node?
            var query = new ReplaceRelationshipsCommand
            {
                SourceNodeLabels = new HashSet <string> {
                    sourceNodeLabel
                },
                SourceIdPropertyName  = sourceIdPropertyName,
                SourceIdPropertyValue = sourceIdPropertyValue
            };

            query.AddRelationshipsTo(relationshipType, null, new [] { destNodeLabel }, destIdPropertyName, destIdPropertyValue);

            await _graphDatabase.RunWriteQueries(query);

            AssertResult(relationshipVariable, new[]
            {
                new ExpectedRelationship
                {
                    Type        = relationshipType,
                    StartNodeId = sourceNodeId,
                    EndNodeId   = destNodeId,
                    Properties  = new Dictionary <string, object>()
                }
            }, await AllRelationships(sourceNodeLabel, sourceIdPropertyName, sourceIdPropertyValue,
                                      relationshipType, destNodeLabel, relationshipVariable));
        }