-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
25 changed files
with
2,230 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Neo4j.Driver/Neo4j.Driver.Tests/Mapping/DictAsRecordTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) "Neo4j" | ||
// Neo4j Sweden AB [http://neo4j.com] | ||
// | ||
// This file is part of Neo4j. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Neo4j.Driver.Preview.Mapping; | ||
using Xunit; | ||
using Record = Neo4j.Driver.Internal.Result.Record; | ||
|
||
namespace Neo4j.Driver.Tests.Mapping | ||
{ | ||
public class DictAsRecordTests | ||
{ | ||
[Fact] | ||
public void ShouldReturnCorrectValues() | ||
{ | ||
var originalRecord = new Record(new[] {"name", "age"}, new object[] {"Bob", 42}); | ||
var dict = new Dictionary<string, object> | ||
{ | ||
{"key1", "value1"}, | ||
{"key2", "value2"} | ||
}; | ||
|
||
var subject = new DictAsRecord(dict, originalRecord); | ||
|
||
subject.Record.Should().BeSameAs(originalRecord); | ||
subject.Keys.Should().BeEquivalentTo(dict.Keys); | ||
subject.Values.Should().BeEquivalentTo(dict); | ||
subject[0].Should().Be("value1"); | ||
subject[1].Should().Be("value2"); | ||
subject["key1"].Should().Be("value1"); | ||
subject["key2"].Should().Be("value2"); | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
Neo4j.Driver/Neo4j.Driver.Tests/Mapping/LabelCaptureTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) "Neo4j" | ||
// Neo4j Sweden AB [http://neo4j.com] | ||
// | ||
// This file is part of Neo4j. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Neo4j.Driver.Internal.Types; | ||
using Neo4j.Driver.Preview.Mapping; | ||
using Xunit; | ||
using Record = Neo4j.Driver.Internal.Result.Record; | ||
|
||
namespace Neo4j.Driver.Tests.Mapping | ||
{ | ||
public class LabelCaptureTests | ||
{ | ||
public class TestMappedClass | ||
{ | ||
[MappingSource("Person", EntityMappingSource.NodeLabel)] | ||
public string Label { get; set; } | ||
|
||
[MappingSource("Person", EntityMappingSource.NodeLabel)] | ||
public List<string> Labels { get; set; } | ||
|
||
[MappingSource("Relationship", EntityMappingSource.RelationshipType)] | ||
public string RelationshipType { get; set; } | ||
} | ||
|
||
public LabelCaptureTests() | ||
{ | ||
RecordObjectMapping.Reset(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCaptureSingleNodeLabel() | ||
{ | ||
var node = new Node(1, new[] { "Test" }, new Dictionary<string, object>()); | ||
var record = new Record(new[] { "Person" }, new object[] { node }); | ||
|
||
var mapped = record.AsObject<TestMappedClass>(); | ||
|
||
mapped.Label.Should().Be("Test"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCaptureMultipleNodeLabelsIntoString() | ||
{ | ||
var node = new Node(1, new[] { "Alpha", "Bravo", "Charlie" }, new Dictionary<string, object>()); | ||
var record = new Record(new[] { "Person" }, new object[] { node }); | ||
|
||
var mapped = record.AsObject<TestMappedClass>(); | ||
|
||
mapped.Label.Should().Be("Alpha,Bravo,Charlie"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCaptureRelationshipType() | ||
{ | ||
var node = new Relationship(1, 2, 3, "ACTED_IN", new Dictionary<string, object>()); | ||
var record = new Record(new[] { "Relationship" }, new object[] { node }); | ||
|
||
var mapped = record.AsObject<TestMappedClass>(); | ||
|
||
mapped.RelationshipType.Should().Be("ACTED_IN"); | ||
} | ||
|
||
class CustomMapper : IMappingProvider | ||
{ | ||
/// <inheritdoc /> | ||
public void CreateMappers(IMappingRegistry registry) | ||
{ | ||
registry.RegisterMapping<TestMappedClass>( | ||
b => b | ||
.Map( | ||
x => x.Label, | ||
"Person", | ||
EntityMappingSource.NodeLabel, | ||
x => string.Join("|", ((string[])x).Select(y => y.ToUpper()))) | ||
.Map( | ||
x => x.Labels, | ||
"Person", | ||
EntityMappingSource.NodeLabel, | ||
x => ((string[])x).Select(y => y.Replace("a", "x")).ToList()) | ||
.Map( | ||
x => x.RelationshipType, | ||
"Relationship", | ||
EntityMappingSource.RelationshipType, | ||
x => x?.ToString()?.ToLower())); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldCaptureAndConvertLabels() | ||
{ | ||
RecordObjectMapping.RegisterProvider<CustomMapper>(); | ||
var node = new Node(1, new[] { "Alpha", "Bravo", "Charlie" }, new Dictionary<string, object>()); | ||
var record = new Record(new[] { "Person" }, new object[] { node }); | ||
|
||
var mapped = record.AsObject<TestMappedClass>(); | ||
|
||
mapped.Label.Should().Be("ALPHA|BRAVO|CHARLIE"); | ||
mapped.Labels.Should().BeEquivalentTo(new[] { "Alphx", "Brxvo", "Chxrlie" }); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCaptureAndConvertRelationshipType() | ||
{ | ||
RecordObjectMapping.RegisterProvider<CustomMapper>(); | ||
var node = new Relationship(1, 2, 3, "ACTED_IN", new Dictionary<string, object>()); | ||
var record = new Record(new[] { "Relationship" }, new object[] { node }); | ||
|
||
var mapped = record.AsObject<TestMappedClass>(); | ||
|
||
mapped.RelationshipType.Should().Be("acted_in"); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Neo4j.Driver/Neo4j.Driver.Tests/Mapping/MappingBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) "Neo4j" | ||
// Neo4j Sweden AB [http://neo4j.com] | ||
// | ||
// This file is part of Neo4j. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using FluentAssertions; | ||
using Neo4j.Driver.Preview.Mapping; | ||
using Xunit; | ||
|
||
namespace Neo4j.Driver.Tests.Mapping | ||
{ | ||
public class MappingBuilderTests | ||
{ | ||
private class TestClass | ||
{ | ||
public int Settable { get; set; } | ||
public int NotSettable { get; } = -1; | ||
public int NotAProperty = 0x6060B017; | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowIfNotAMemberExpression() | ||
{ | ||
var subject = new MappingBuilder<TestClass>(); | ||
var act = () => subject.Map(x => "something", "foo"); | ||
act.Should().Throw<ArgumentException>(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowIfNotAPropertyExpression() | ||
{ | ||
var subject = new MappingBuilder<TestClass>(); | ||
var act = () => subject.Map(x => x.NotAProperty, "foo"); | ||
act.Should().Throw<ArgumentException>(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowIfPropertyDoesNotHaveASetter() | ||
{ | ||
var subject = new MappingBuilder<TestClass>(); | ||
var act = () => subject.Map(x => x.NotSettable, "foo"); | ||
act.Should().Throw<ArgumentException>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.