Skip to content
Open
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
76 changes: 76 additions & 0 deletions csharp/Platform.Data.Tests/ILinksExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Xunit;
using Platform.Data.Exceptions;
using Platform.Delegates;

namespace Platform.Data.Tests
{
/// <summary>
/// Tests for ILinksExtensions methods
/// </summary>
public static class ILinksExtensionsTests
{
private class MockLinks : ILinks<ulong, LinksConstants<ulong>>
{
private readonly LinksConstants<ulong> _constants;

public MockLinks()
{
_constants = new LinksConstants<ulong>(enableExternalReferencesSupport: true);
}

public LinksConstants<ulong> Constants => _constants;

public ulong Count(IList<ulong>? restriction) => 0;

public ulong Each(IList<ulong>? restriction, ReadHandler<ulong>? handler)
{
// Return Constants.Break to simulate no matching links found
return _constants.Break;
}

public ulong Create(IList<ulong>? substitution, WriteHandler<ulong>? handler) => 0;

public ulong Update(IList<ulong>? restriction, IList<ulong>? substitution, WriteHandler<ulong>? handler) => 0;

public ulong Delete(IList<ulong>? restriction, WriteHandler<ulong>? handler) => 0;
}

/// <summary>
/// Tests that GetLink throws ArgumentLinkDoesNotExistsException when link doesn't exist
/// </summary>
[Fact]
public static void GetLinkThrowsExceptionWhenLinkDoesNotExist()
{
var links = new MockLinks();
var nonExistentLinkId = 999UL;

// The mock implementation returns Constants.Break from Each, which means no link was found
// and the Setter.Result remains null, so GetLink should throw ArgumentLinkDoesNotExistsException
var exception = Assert.Throws<ArgumentLinkDoesNotExistsException<ulong>>(() =>
links.GetLink(nonExistentLinkId));

// Verify the exception contains the correct link id (checking if it's in the message)
Assert.Contains(nonExistentLinkId.ToString(), exception.Message);
}

/// <summary>
/// Tests that GetLink works correctly for external references
/// </summary>
[Fact]
public static void GetLinkWorksForExternalReferences()
{
var links = new MockLinks();
var externalReference = new Hybrid<ulong>(0, true); // External reference

// For external references, GetLink should return a Point without calling Each
var result = links.GetLink(externalReference);

Assert.NotNull(result);
// The Point should contain the external reference value
Assert.Equal(externalReference.Value, result[0]);
}
}
}
7 changes: 6 additions & 1 deletion csharp/Platform.Data/ILinksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public static TLinkAddress Each<TLinkAddress, TConstants>(this ILinks<TLinkAddre
}
var linkPartsSetter = new Setter<IList<TLinkAddress>?, TLinkAddress>(constants.Continue, constants.Break);
links.Each(linkPartsSetter.SetAndReturnTrue, link);
return linkPartsSetter.Result;
var result = linkPartsSetter.Result;
if (result == null)
{
throw new ArgumentLinkDoesNotExistsException<TLinkAddress>(link);
}
return result;
}

#region Points
Expand Down
Loading