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 @@ -15,7 +15,6 @@

using System;
using FluentAssertions;
using Neo4j.Driver.Internal.IO;
using Neo4j.Driver.Internal.Util;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.

using FluentAssertions;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Tests.TestUtil;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Internal.Types;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Tests.TestUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void ShouldFailIfDelegateThrowsException()
{
var record = TestRecord.Create(("x", 69));

Action act = () => record.AsObject(
var act = () => record.AsObject(
(int x) =>
{
if (x == 69)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
using System.Collections;
using System.Collections.Generic;
using FluentAssertions;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Internal.Types;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Tests.TestUtil;
using Xunit;
using InvalidOperationException = System.InvalidOperationException;
Expand Down
7 changes: 1 addition & 6 deletions Neo4j.Driver/Neo4j.Driver.Tests/Mapping/LabelCaptureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@

namespace Neo4j.Driver.Tests.Mapping;

public class LabelCaptureTests
public class LabelCaptureTests : MappingTestWithGlobalState
{
public LabelCaptureTests()
{
RecordObjectMapping.Reset();
}

[Fact]
public void ShouldCaptureSingleNodeLabel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using FluentAssertions;
using Moq;
using Moq.AutoMock;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Internal.Mapping.TypeConversion;
using Neo4j.Driver.Internal.Types;
using Neo4j.Driver.Mapping;
using Xunit;
Expand Down Expand Up @@ -128,6 +131,32 @@ public void GetConvertedValueShouldReturnNullWhenFieldNotFound()
result.Should().BeNull();
}

[Fact]
public void GetConvertedValueShouldUseTypeConversion()
{
var expected = new Guid("b19f766c-4f74-41a9-a73d-6298697d81a7");
var entityMappingInfo = new EntityMappingInfo("field-name", EntityMappingSource.Property);
_mocker.GetMock<IMappingSourceDelegateBuilder>()
.Setup(x => x.GetMappingDelegate(entityMappingInfo))
.Returns(GetMockMappingDelegate(expected.ToString(), true));

object expectedObj = expected;
_mocker.GetMock<IMappingTypeConversionManager>()
.Setup(x => x.TryConvert(typeof(string), typeof(Guid), expected.ToString(), out expectedObj))
.Returns(true);

var subject = _mocker.CreateInstance<MappableValueProvider>(true);

var result = subject.GetConvertedValue(
Mock.Of<IRecord>(),
entityMappingInfo,
typeof(Guid));

result.Should().Be(expected);
}



[Fact]
public void ShouldReturnNullWhenValueIsNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using FluentAssertions;
using Moq;
using Moq.AutoMock;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Tests.TestUtil;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using System;
using FluentAssertions;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Internal.Mapping;
using Xunit;

namespace Neo4j.Driver.Tests.Mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@

namespace Neo4j.Driver.Tests.Mapping;

public class MappingProviderTests
public class MappingProviderTests : MappingTestWithGlobalState
{
public MappingProviderTests()
{
RecordObjectMapping.Reset();
}

[Fact]
public void ShouldOverrideDefaultMapping()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System.Collections.Generic;
using FluentAssertions;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Internal.Types;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Tests.TestUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [https://neo4j.com]
//
// 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 Neo4j.Driver.Mapping;
using Xunit;

namespace Neo4j.Driver.Tests.Mapping;

[CollectionDefinition("UsesMappingGlobalState", DisableParallelization = true)]
public class UsesMappingGlobalState
{
}

[Collection("UsesMappingGlobalState")]
public class MappingTestWithGlobalState
{
public MappingTestWithGlobalState()
{
RecordObjectMapping.Reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,5 @@ private class ClassWithDefaultConstructorWithAttributes([MappingSource("forename
public int Age => age;
}

private record TestXY(int X, string Y)
{
}
private record TestXY(int X, string Y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using System.Collections.Generic;
using FluentAssertions;
using Neo4j.Driver.Mapping;
using Neo4j.Driver.Internal.Mapping;
using Neo4j.Driver.Tests.TestUtil;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [https://neo4j.com]
//
// 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 FluentAssertions;
using Neo4j.Driver.Internal.Mapping.TypeConversion;
using Xunit;

namespace Neo4j.Driver.Tests.Mapping.TypeConversion;

public class TypeConversionManagerTests
{
[Fact]
public void ShouldConvertTypes()
{
var manager = new MappingTypeConversionManager();
manager.RegisterConverter<int, string>(i => i.ToString());
manager.RegisterConverter<string, int>(int.Parse);

manager.TryConvert(42, out string str).Should().BeTrue();
str.Should().Be("42");

manager.TryConvert("42", out int i).Should().BeTrue();
i.Should().Be(42);
}

[Fact]
public void ShouldNotConvertTypes()
{
var manager = new MappingTypeConversionManager();
manager.RegisterConverter<int, string>(i => i.ToString());
manager.RegisterConverter<string, int>(int.Parse);

manager.TryConvert(42, out int _).Should().BeFalse();

manager.TryConvert("42", out string _).Should().BeFalse();
}

[Fact]
public void ShouldNotConvertTypesWhenNoConverterRegistered()
{
var manager = new MappingTypeConversionManager();
manager.TryConvert(42, out string _).Should().BeFalse();
manager.TryConvert("42", out int _).Should().BeFalse();
}

[Fact]
public void ShouldNotConvertTypesWhenConverterIsNotRegistered()
{
var manager = new MappingTypeConversionManager();
manager.RegisterConverter<int, string>(i => i.ToString());

manager.TryConvert(42, out int _).Should().BeFalse();
}

[Fact]
public void ShouldConvertTypesCalledWithDifferentTypes()
{
var manager = new MappingTypeConversionManager();
manager.RegisterConverter<int, string>(i => i.ToString());
manager.RegisterConverter<string, int>(int.Parse);

manager.TryConvert(typeof(int), typeof(string), 42, out var str).Should().BeTrue();
str.Should().Be("42");

manager.TryConvert(typeof(string), typeof(int), "42", out var i).Should().BeTrue();
i.Should().Be(42);
}
}
Loading