Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public static CypherExtensionContext DefaultExtensionContext = new CypherExtensionContext();
private static readonly Dictionary<Type, string> EntityLabelCache = new Dictionary<Type, string>();

public static ICypherFluentQuery MatchEntity<T>(this ICypherFluentQuery query, T entity, string identifier = null, string preCql = "", string postCql = "", List<CypherProperty> propertyOverride = null) where T : class

Check warning on line 15 in src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs

View workflow job for this annotation

GitHub Actions / Build, Test, Pack & Publish

Cannot convert null literal to non-nullable reference type.

Check warning on line 15 in src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs

View workflow job for this annotation

GitHub Actions / Build, Test, Pack & Publish

Cannot convert null literal to non-nullable reference type.
{
var options = new MatchOptions
{
Expand All @@ -30,7 +30,7 @@
return MatchWorker(query, entity, options, (q, s) => q.Match(s));
}

public static ICypherFluentQuery OptionalMatchEntity<T>(this ICypherFluentQuery query, T entity, MatchOptions options= null)

Check warning on line 33 in src/Neo4jClient.Extension/Cypher/Extension/CypherExtension.Main.cs

View workflow job for this annotation

GitHub Actions / Build, Test, Pack & Publish

Cannot convert null literal to non-nullable reference type.
where T : class
{
if (options == null)
Expand Down Expand Up @@ -193,7 +193,11 @@
, relationship.ToCypherString<T, CypherMatchAttribute>(CypherExtensionContext.Create(query), relationship.Key, matchProperties)
, relationship.ToKey);

return matchFunction(query,cql);
dynamic cutdown = relationship.CreateDynamic(options.MatchOverride ?? CypherTypeItemHelper.PropertiesForPurpose<T, CypherMatchAttribute>(relationship));
var matchKey = GetMatchParamName(relationship.Key);

return matchFunction(query, cql)
.WithParam(matchKey, cutdown);
}

public static ICypherFluentQuery MatchRelationship<T>(this ICypherFluentQuery query, T relationship, List<CypherProperty> matchOverride = null) where T : BaseRelationship
Expand Down
44 changes: 44 additions & 0 deletions test/Neo4jClient.Extension.IntegrationTest/Tests/MatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Organisation>());

Console.WriteLine(q.GetFormattedDebugText());
var r = (await q.ResultsAsync).ToList();

r.Count.Should().Be(1);

//Not working??
Console.WriteLine($" Org={r[0].Name}");
}
}
}
13 changes: 13 additions & 0 deletions test/Neo4jClient.Extension.Test.Common/Domain/Company.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
14 changes: 14 additions & 0 deletions test/Neo4jClient.Extension.Test.Common/Neo/NeoConfig.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -44,12 +45,25 @@ public static void ConfigureModel()
.MergeOnMatchOrCreate(w => w.BlastRadius)
.Set();


FluentConfig.Config()
.With<Organisation>()
.Merge(x => x.Name)
.MergeOnMatchOrCreate(w => w.Name)
.Set();

FluentConfig.Config()
.With<HomeAddressRelationship>()
.Match(ha => ha.DateEffective)
.MergeOnMatchOrCreate(hr => hr.DateEffective)
.Set();

FluentConfig.Config()
.With<WorksForRelationship>()
.Match(wf => wf.Role)
.MergeOnMatchOrCreate(wf => wf.Role)
.Set();

FluentConfig.Config()
.With<WorkAddressRelationship>()
.Set();
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
7 changes: 7 additions & 0 deletions test/Neo4jClient.Extension.Test.Common/SampleDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using FluentAssertions;
using FluentAssertions.Common;
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;
Expand Down Expand Up @@ -72,14 +76,40 @@ public void MatchRelationshipSimple()
[Test]
public void MatchRelationshipWithProperty()
{
var addressRelationship = new HomeAddressRelationship(DateTimeOffset.Parse("2015-08-05 12:00"), "agent", "homeAddress");
var now = DateTimeOffset.Now;
var addressRelationship = new HomeAddressRelationship(now, "agent", "homeAddress");
var q = GetFluentQuery()
.MatchRelationship(addressRelationship);
var text = q.GetFormattedDebugText();

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:{{\n dateEffective: \"{now:O}\"\n}}.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)");
}
}
}
Loading