Unit tests

This commit is contained in:
Trevi Awater
2024-03-02 22:53:19 +01:00
parent b009222333
commit 55d6d6060e
5 changed files with 11430 additions and 4105 deletions

View File

@@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Neo4j.Android", "Xa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Neo4j.iOS", "Xamarin.Neo4j\Xamarin.Neo4j.iOS\Xamarin.Neo4j.iOS.csproj", "{544E1297-4171-4B91-8AF4-4CD5CFD18A04}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Neo4j.Tests", "Xamarin.Neo4j\Xamarin.Neo4j.Tests\Xamarin.Neo4j.Tests.csproj", "{A9874913-14B2-4BED-A838-89697258405A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|iPhone = Ad-Hoc|iPhone
@@ -82,5 +84,21 @@ Global
{544E1297-4171-4B91-8AF4-4CD5CFD18A04}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{544E1297-4171-4B91-8AF4-4CD5CFD18A04}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
{544E1297-4171-4B91-8AF4-4CD5CFD18A04}.Release|iPhoneSimulator.Deploy.0 = Release|iPhoneSimulator
{A9874913-14B2-4BED-A838-89697258405A}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.AppStore|iPhone.Build.0 = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|iPhone.Build.0 = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|Any CPU.Build.0 = Release|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|iPhone.ActiveCfg = Release|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|iPhone.Build.0 = Release|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{A9874913-14B2-4BED-A838-89697258405A}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,99 @@
using NUnit.Framework;
using Xamarin.Neo4j.Utilities;
namespace Xamarin.Neo4j.Tests
{
public class QueryHelperTests
{
[Test]
public void TestWithUnlabeledRelationship()
{
const string query = "MATCH (t:Tender)-[]->(z) RETURN t, z";
const string expected = "MATCH (t:Tender)-[display_variable_1]->(z) RETURN display_variable_1, t, z";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithLabeledWithoutAliasRelationship()
{
const string query = "MATCH (t:Tender)-[:LAST]->(z) RETURN t, z";
const string expected = "MATCH (t:Tender)-[display_variable_1:LAST]->(z) RETURN display_variable_1, t, z";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithLabeledAndAliasedRelationship()
{
const string query = "MATCH (t:Tender)-[y:LAST]->(z) RETURN t, z, y";
var expected = query;
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithReturnAll()
{
const string query = "MATCH (t:Tender)-[:LAST]->(z) RETURN *";
const string expected = "MATCH (t:Tender)-[display_variable_1:LAST]->(z) RETURN *";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithNoReturn()
{
const string query = "MATCH (t:Tender)-[:LAST]->(z)";
var expected = query;
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithMultipleUnlabeledRelationships()
{
const string query = "MATCH (t:Tender)-[]->(z)-[]->(x) RETURN t, z, x";
const string expected =
"MATCH (t:Tender)-[display_variable_1]->(z)-[display_variable_2]->(x) RETURN display_variable_2, display_variable_1, t, z, x";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestBackwardsDirection()
{
const string query = "MATCH (tv:TenderVersion)<-[:HAS_VERSION]-(t:Tender) RETURN tv, t";
const string expected =
"MATCH (tv:TenderVersion)<-[display_variable_1:HAS_VERSION]-(t:Tender) RETURN display_variable_1, tv, t";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
[Test]
public void TestWithMultipleLabeledRelationships()
{
const string query = "MATCH (t:Tender)-[:LAST]->(z)-[:NEXT]->(x) RETURN t, z, x";
const string expected =
"MATCH (t:Tender)-[display_variable_1:LAST]->(z)-[display_variable_2:NEXT]->(x) RETURN display_variable_2, display_variable_1, t, z, x";
var actual = QueryHelper.ToDisplayQuery(query);
Assert.AreEqual(expected, actual);
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.Neo4j\Xamarin.Neo4j.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,12 +1,3 @@
//
// QueryHelper.cs
//
// Trevi Awater
// 02-03-2024
//
// © Xamarin.Neo4j
//
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
@@ -17,12 +8,10 @@ namespace Xamarin.Neo4j.Utilities
{
public static string ToDisplayQuery(string query)
{
// Check if there is a return statement, if not return the original query
if (query.IndexOf("RETURN ", StringComparison.OrdinalIgnoreCase) <= 0)
if (query.IndexOf("RETURN ", StringComparison.OrdinalIgnoreCase) < 0)
return query;
// Adjusted pattern to match relationships with and without labels/types.
const string pattern = @"-\[(\w*:?(\w+)?)\]->";
const string pattern = @"(-\[(\w*:?(\w+)?)\]->)|(<-\[(\w*:?(\w+)?)\]-)";
var matches = Regex.Matches(query, pattern);
var relationships = new List<string>();
@@ -32,32 +21,58 @@ namespace Xamarin.Neo4j.Utilities
foreach (Match match in matches)
{
var newLabel = $"display_variable_{relationshipCounter++}"; // Generate a unique label for the relationship
// Determine the correct new pattern based on whether the original had a type/label
var oldPattern = match.Value;
var newPattern = !string.IsNullOrEmpty(match.Groups[1].Value) ?
oldPattern.Replace("[:", $"[{newLabel}:").Replace("]->", "]->") : // For labeled/types
oldPattern.Replace("[]", $"[{newLabel}]"); // For unlabeled
var idx = transformedQuery.IndexOf(oldPattern);
if (idx != -1)
if (string.IsNullOrEmpty(match.Groups[2].Value) || string.IsNullOrEmpty(match.Groups[2].Value.Split(':')[0]))
{
transformedQuery = transformedQuery.Substring(0, idx) + newPattern + transformedQuery.Substring(idx + oldPattern.Length);
}
var newLabel = $"display_variable_{relationshipCounter++}";
var oldPattern = match.Value;
var newPattern = "";
relationships.Add(newLabel);
if (match.Groups[1].Success)
{
newPattern = $"-[{newLabel + match.Groups[2]}]->";
}
else if (match.Groups[5].Success)
{
newPattern = $"<-[{newLabel + match.Groups[5]}]-";
}
var idx = transformedQuery.IndexOf(oldPattern);
if (idx != -1)
{
transformedQuery = transformedQuery.Substring(0, idx) + newPattern + transformedQuery.Substring(idx + oldPattern.Length);
}
relationships.Add(newLabel);
}
else if (match.Groups[2].Value.Contains(":"))
{
var label = match.Groups[2].Value.Split(':')[0];
if (!relationships.Contains(label))
{
relationships.Add(label);
}
}
}
// Construct the new RETURN clause with the added relationships.
if (relationships.Count > 0)
{
var returnIndex = transformedQuery.LastIndexOf("RETURN ", StringComparison.OrdinalIgnoreCase);
var returnClause = transformedQuery.Substring(returnIndex + "RETURN ".Length);
var newReturnClause = string.Join(", ", relationships) + ", " + returnClause;
transformedQuery = transformedQuery.Substring(0, returnIndex) + "RETURN " + newReturnClause;
if (returnClause.Trim() != "*")
{
var newReturnClause = returnClause;
foreach (var rel in relationships)
{
if (!returnClause.Contains(rel))
{
newReturnClause = rel + ", " + newReturnClause;
}
}
transformedQuery = transformedQuery.Substring(0, returnIndex) + "RETURN " + newReturnClause;
}
}
return transformedQuery;