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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataLinq.Instances;
using DataLinq.Tests.Models.Employees;
using DataLinq.Testing;

Expand Down Expand Up @@ -53,6 +54,22 @@ public async Task Relations_DepartmentManagersLazyLoad_ResolvesCollection(TestPr

await Assert.That(department.Managers).IsNotEmpty();
await Assert.That(department.Managers.All(x => x.Department.DeptNo == department.Department.DeptNo)).IsTrue();

var relation = department.Department.Managers;
IEnumerable<Manager> rows = relation.AsEnumerable();
var expectedByKey = rows.ToDictionary(manager => manager.PrimaryKeys());

// Manager keys are composite; keyed enumeration must not use the parent department key.
relation.Clear();
IEnumerable<KeyValuePair<DataLinqKey, Manager>> keyedRows = relation.AsKeyValuePairs();
var pairs = keyedRows.ToArray();
await Assert.That(pairs.Length).IsEqualTo(expectedByKey.Count);
foreach (var pair in pairs)
{
await Assert.That(pair.Key).IsEqualTo(DataLinqKey.FromValues([pair.Value.dept_fk, pair.Value.emp_no]));
await Assert.That(ReferenceEquals(pair.Value, expectedByKey[pair.Key])).IsTrue();
await Assert.That(ReferenceEquals(relation.Get(pair.Key), pair.Value)).IsTrue();
}
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataLinq.Attributes;
using DataLinq.Diagnostics;
using DataLinq.Instances;
using DataLinq.Interfaces;
using DataLinq.Metadata;
Expand All @@ -12,6 +14,7 @@ namespace DataLinq.Tests.Compliance;
public class MultipleForeignKeyRelationTests
{
[Test]
[NotInParallel]
[Property(TestProviderAffinity.PropertyName, TestProviderAffinity.EveryProvider)]
[MethodDataSource(typeof(TestProviderDataSources), nameof(TestProviderDataSources.ActiveProviders))]
public async Task Transaction_MultipleForeignKeysToSameTable_LazyLoadsDistinctRelations(TestProviderDescriptor provider)
Expand Down Expand Up @@ -40,18 +43,46 @@ public async Task Transaction_MultipleForeignKeysToSameTable_LazyLoadsDistinctRe
await Assert.That(ReferenceEquals(createdBy, creator)).IsTrue();
await Assert.That(ReferenceEquals(approvedBy, approver)).IsTrue();

var createdInvoices = creator.CreatedInvoices.ToArray();
var relation = creator.CreatedInvoices;
var concreteRelation = (ImmutableRelation<RuntimeInvoice, int>)relation;
var loadsBeforeRowViews = GetCollectionLoads(databaseScope.Database);

// Both receiver types must bind to the standard row extension without loading.
IEnumerable<RuntimeInvoice> rows = relation.AsEnumerable();
IEnumerable<RuntimeInvoice> concreteRows = concreteRelation.AsEnumerable();

await Assert.That(ReferenceEquals(rows, relation)).IsTrue();
await Assert.That(ReferenceEquals(concreteRows, relation)).IsTrue();
await Assert.That(GetCollectionLoads(databaseScope.Database)).IsEqualTo(loadsBeforeRowViews);

var createdInvoices = rows.ToArray();
await Assert.That(GetCollectionLoads(databaseScope.Database)).IsEqualTo(loadsBeforeRowViews + 1);
var approvedInvoices = approver.ApprovedInvoices.ToArray();

await Assert.That(createdInvoices.Length).IsEqualTo(1);
await Assert.That(approvedInvoices.Length).IsEqualTo(1);
await Assert.That(ReferenceEquals(invoice, createdInvoices.Single())).IsTrue();
await Assert.That(ReferenceEquals(invoice, approvedInvoices.Single())).IsTrue();
await Assert.That(creator.ApprovedInvoices).IsEmpty();
await Assert.That(approver.CreatedInvoices).IsEmpty();

IEnumerable<KeyValuePair<DataLinqKey, RuntimeInvoice>> keyedRows = relation.AsKeyValuePairs();
var pair = keyedRows.Single();
await Assert.That(pair.Key).IsEqualTo(DataLinqKey.FromValue(100));
await Assert.That(ReferenceEquals(pair.Value, createdInvoices.Single())).IsTrue();
await Assert.That(ReferenceEquals(relation.Get(pair.Key), pair.Value)).IsTrue();
await Assert.That(ReferenceEquals(concreteRelation.AsKeyValuePairs().Single().Value, pair.Value)).IsTrue();

await Assert.That(creator.ApprovedInvoices.AsEnumerable()).IsEmpty();
await Assert.That(creator.ApprovedInvoices.AsKeyValuePairs()).IsEmpty();
await Assert.That(approver.CreatedInvoices.AsEnumerable()).IsEmpty();
await Assert.That(approver.CreatedInvoices.AsKeyValuePairs()).IsEmpty();

transaction.Commit();
}

private static long GetCollectionLoads(Database<MultipleForeignKeyRelationDb> database)
=> DataLinqMetrics.Snapshot().Providers
.Single(provider => provider.ProviderInstanceId == database.Provider.TelemetryInstanceId)
.Tables.Sum(table => table.Relations.CollectionLoads);
}

[Database("multiple_fk_relation")]
Expand Down
11 changes: 8 additions & 3 deletions src/DataLinq/Instances/ImmutableRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public interface IImmutableRelation<T> : IEnumerable<T> where T : IModelInstance
ImmutableArray<DataLinqKey> Keys { get; }
ImmutableArray<T> Values { get; }

IEnumerable<KeyValuePair<DataLinqKey, T>> AsEnumerable();
/// <summary>
/// Returns relation rows paired with their primary keys.
/// Resolving the keyed collection may synchronously load the relation.
/// </summary>
IEnumerable<KeyValuePair<DataLinqKey, T>> AsKeyValuePairs();
void Clear();
bool Any() => Count != 0;

Expand Down Expand Up @@ -100,7 +104,7 @@ public ImmutableRelationMock(IEnumerable<T> list)

public ImmutableArray<T> Values => throw new System.NotImplementedException();

public IEnumerable<KeyValuePair<DataLinqKey, T>> AsEnumerable()
public IEnumerable<KeyValuePair<DataLinqKey, T>> AsKeyValuePairs()
{
throw new System.NotImplementedException();
}
Expand Down Expand Up @@ -172,7 +176,8 @@ public class ImmutableRelation<T, TKey>(TKey foreignKey, IDataSourceAccess dataS
public ImmutableArray<DataLinqKey> Keys => GetInstances().Keys;
public int Count => GetValues().Length;
public bool ContainsKey(DataLinqKey key) => GetInstances().ContainsKey(key);
public IEnumerable<KeyValuePair<DataLinqKey, T>> AsEnumerable() => GetInstances().AsEnumerable();
/// <inheritdoc />
public IEnumerable<KeyValuePair<DataLinqKey, T>> AsKeyValuePairs() => GetInstances().AsEnumerable();
public FrozenDictionary<DataLinqKey, T> ToFrozenDictionary() => GetInstances();

protected TableCache GetTableCache() => GetTableCache(GetDataSource());
Expand Down
Loading