diff --git a/src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs b/src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs index 2eae68a..a70f0ff 100644 --- a/src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs +++ b/src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs @@ -193,7 +193,11 @@ this ICypherFluentQuery query , relationship.ToCypherString(CypherExtensionContext.Create(query), relationship.Key, matchProperties) , relationship.ToKey); - return matchFunction(query,cql); + dynamic cutdown = relationship.CreateDynamic(options.MatchOverride ?? CypherTypeItemHelper.PropertiesForPurpose(relationship)); + var matchKey = GetMatchParamName(relationship.Key); + + return matchFunction(query, cql) + .WithParam(matchKey, cutdown); } public static ICypherFluentQuery MatchRelationship(this ICypherFluentQuery query, T relationship, List matchOverride = null) where T : BaseRelationship diff --git a/test/Neo4jClient.Extension.IntegrationTest/Tests/MatchTests.cs b/test/Neo4jClient.Extension.IntegrationTest/Tests/MatchTests.cs index 4f56bfa..b55926a 100644 --- a/test/Neo4jClient.Extension.IntegrationTest/Tests/MatchTests.cs +++ b/test/Neo4jClient.Extension.IntegrationTest/Tests/MatchTests.cs @@ -4,6 +4,7 @@ using FluentAssertions; using Neo4jClient.Extension.Cypher; using Neo4jClient.Extension.Test.Cypher; +using Neo4jClient.Extension.Test.Data.Neo.Relationships; using Neo4jClient.Extension.Test.TestData.Entities; using Neo4jClient.Extension.Test.TestEntities.Relationships; using NUnit.Framework; @@ -209,5 +210,48 @@ await CypherQuery retrieved.BlastRadius.Should().NotBeNull(); retrieved.BlastRadius.Value.SquareKilometers.Should().BeApproximately(12.4, 0.01); } + + public Task ArrangeTestData() + { + var archer = SampleDataFactory.GetWellKnownPerson(1); + var isis = new Organisation {Name="ISIS"}; + var kgb = new Organisation { Name = "KGB" }; + + var archerVariable = "a"; + var kgbVariable = "k"; + var isisVariable = "i"; + + var agentRelationship = new WorksForRelationship("special agent", archerVariable, isisVariable); + var doubleAgentRelationship = new WorksForRelationship("double agent", archerVariable, kgbVariable); + + var q = RealQueryFactory(); + + return q + .CreateEntity(archer, archerVariable) + .CreateEntity(isis, isisVariable) + .CreateEntity(kgb, kgbVariable) + .CreateRelationship(agentRelationship) + .CreateRelationship(doubleAgentRelationship) + .ExecuteWithoutResultsAsync(); + } + + [Test] + public async Task Match() + { + ArrangeTestData(); + + // Act + var q = RealQueryFactory() + .MatchRelationship(new WorksForRelationship("special agent", "p", "o")) + .Return(o => o.As()); + + Console.WriteLine(q.GetFormattedDebugText()); + var r = (await q.ResultsAsync).ToList(); + + r.Count.Should().Be(1); + + //Not working?? + Console.WriteLine($" Org={r[0].Name}"); + } } } diff --git a/test/Neo4jClient.Extension.Test.Common/Domain/Company.cs b/test/Neo4jClient.Extension.Test.Common/Domain/Company.cs new file mode 100644 index 0000000..9778cc3 --- /dev/null +++ b/test/Neo4jClient.Extension.Test.Common/Domain/Company.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Neo4jClient.Extension.Test.Cypher +{ + public class Organisation + { + public string Name { get; set; } + } +} diff --git a/test/Neo4jClient.Extension.Test.Common/Neo/NeoConfig.cs b/test/Neo4jClient.Extension.Test.Common/Neo/NeoConfig.cs index 7d4a10b..a8f229e 100644 --- a/test/Neo4jClient.Extension.Test.Common/Neo/NeoConfig.cs +++ b/test/Neo4jClient.Extension.Test.Common/Neo/NeoConfig.cs @@ -1,5 +1,6 @@ using Neo4jClient.Extension.Cypher; using Neo4jClient.Extension.Test.Cypher; +using Neo4jClient.Extension.Test.Data.Neo.Relationships; using Neo4jClient.Extension.Test.TestData.Entities; using Neo4jClient.Extension.Test.TestEntities.Relationships; @@ -44,12 +45,25 @@ public static void ConfigureModel() .MergeOnMatchOrCreate(w => w.BlastRadius) .Set(); + + FluentConfig.Config() + .With() + .Merge(x => x.Name) + .MergeOnMatchOrCreate(w => w.Name) + .Set(); + FluentConfig.Config() .With() .Match(ha => ha.DateEffective) .MergeOnMatchOrCreate(hr => hr.DateEffective) .Set(); + FluentConfig.Config() + .With() + .Match(wf => wf.Role) + .MergeOnMatchOrCreate(wf => wf.Role) + .Set(); + FluentConfig.Config() .With() .Set(); diff --git a/test/Neo4jClient.Extension.Test.Common/Neo/Relationships/WorksForRelationship.cs b/test/Neo4jClient.Extension.Test.Common/Neo/Relationships/WorksForRelationship.cs new file mode 100644 index 0000000..1631072 --- /dev/null +++ b/test/Neo4jClient.Extension.Test.Common/Neo/Relationships/WorksForRelationship.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Neo4jClient.Extension.Cypher; +using Neo4jClient.Extension.Cypher.Attributes; + +namespace Neo4jClient.Extension.Test.Data.Neo.Relationships +{ + [CypherLabel(Name = LabelName)] + public class WorksForRelationship : BaseRelationship + { + public const string LabelName = "WORKS_FOR"; + + public WorksForRelationship(string role, string from = "person", string to = "organisation") + : base(from, to) + { + Role = role; + } + + public WorksForRelationship(string from = "person", string to = "address") + : base(from, to) + { + } + + public string Role { get; set; } + } +} diff --git a/test/Neo4jClient.Extension.Test.Common/SampleDataFactory.cs b/test/Neo4jClient.Extension.Test.Common/SampleDataFactory.cs index 54acc59..e20760e 100644 --- a/test/Neo4jClient.Extension.Test.Common/SampleDataFactory.cs +++ b/test/Neo4jClient.Extension.Test.Common/SampleDataFactory.cs @@ -38,5 +38,12 @@ public static Weapon GetWellKnownWeapon(int n) weapon.BlastRadius = Area.FromSquareMeters(20); return weapon; } + + public static Organisation GetWellKnownOrganisation() + { + var org = new Organisation(); + org.Name = "ISIS"; + return org; + } } } \ No newline at end of file diff --git a/test/Neo4jClient.Extension.UnitTest/Cypher/FluentConfigMatchTests.cs b/test/Neo4jClient.Extension.UnitTest/Cypher/FluentConfigMatchTests.cs index f3a9774..0f43a2b 100644 --- a/test/Neo4jClient.Extension.UnitTest/Cypher/FluentConfigMatchTests.cs +++ b/test/Neo4jClient.Extension.UnitTest/Cypher/FluentConfigMatchTests.cs @@ -1,5 +1,8 @@ using System; +using FluentAssertions; +using Neo4jClient.Cypher; using Neo4jClient.Extension.Cypher; +using Neo4jClient.Extension.Test.Data.Neo.Relationships; using Neo4jClient.Extension.Test.TestData.Relationships; using Neo4jClient.Extension.Test.TestEntities.Relationships; using NUnit.Framework; @@ -79,7 +82,34 @@ public void MatchRelationshipWithProperty() Console.WriteLine(text); - Assert.That(text, Is.EqualTo(@"MATCH (agent)-[agenthomeAddress:HOME_ADDRESS {dateEffective:$agenthomeAddressMatchKey.dateEffective}]->(homeAddress)")); + Assert.That(text, Is.EqualTo(@"MATCH (agent)-[agenthomeAddress:HOME_ADDRESS {dateEffective:{ + dateEffective: ""2015-08-05T12:00:00+10:00"" +}.dateEffective}]->(homeAddress)")); + } + + public ICypherFluentQuery MatchRelationshipWithProperty2Act() + { + var archer = SampleDataFactory.GetWellKnownPerson(1); + + var personVariable = "p"; + var orgVariable = "o"; + + var roleRelationship = new WorksForRelationship("special agent", personVariable, orgVariable); + + var q = GetFluentQuery() + .MatchRelationship(roleRelationship); + + return q; + } + + [Test] + public void MatchRelationshipWithProperty2() + { + var q = MatchRelationshipWithProperty2Act(); + var cypher = q.GetFormattedDebugText(); + cypher.Should().Be(@"MATCH (p)-[po:WORKS_FOR {role:{ + role: ""special agent"" +}.role}]->(o)"); } } }