Skip to content

Commit

Permalink
Fix Linq-to-BsonExpression with BsonRef property when it inherits fro…
Browse files Browse the repository at this point in the history
…m another class
  • Loading branch information
lbnascimento committed Apr 22, 2020
1 parent 883a1dc commit 653c8f7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions LiteDB.Tests/Issue1651_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;

namespace LiteDB.Issue1651
{
public class Order : BaseEntity
{
public Customer Customer { get; set; }
}

public class Customer : BaseEntity
{
public string Name { get; set; }
}

public class BaseEntity
{
public Guid Id { get; set; }
}
public class CustomerRelationshipsTests
{
[Fact]
public void Find_ByRelationId_Success()
{
BsonMapper.Global.Entity<Order>().DbRef(order => order.Customer);

using var _database = new LiteDatabase(":memory:");
var _orderCollection = _database.GetCollection<Order>("Order");
var _customerCollection = _database.GetCollection<Customer>("Customer");

var customer = new Customer() { Name = "John", };

Assert.True(_customerCollection.Upsert(customer));
Assert.True(_customerCollection.Upsert(new Customer() { Name = "Anonymous" }));

Assert.NotEqual(Guid.Empty, customer.Id);

var order = new Order()
{
Customer = customer,
};
var order2 = new Order()
{
Customer = new Customer() { Id = customer.Id },
};
var orphanOrder = new Order();

Assert.True(_orderCollection.Upsert(orphanOrder));
Assert.True(_orderCollection.Upsert(order));
Assert.True(_orderCollection.Upsert(order2));

customer.Name = "Josh";
Assert.True(_customerCollection.Update(customer));

var actualOrders = _orderCollection
.Include(orderEntity => orderEntity.Customer)
.Find(orderEntity => orderEntity.Customer.Id == customer.Id)
.ToList();

Assert.Equal(2, actualOrders.Count);
Assert.Equal(new[] { customer.Name, customer.Name },
actualOrders.Select(actualOrder => actualOrder.Customer.Name));
Assert.Equal(2, (_customerCollection.FindAll().ToList()).Count);
Assert.Equal(3, (_orderCollection.FindAll().ToList()).Count);
}
}
}
2 changes: 1 addition & 1 deletion LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private string ResolveMember(MemberInfo member)
var name = member.Name;

// checks if parent field are not DbRef (checks for same dataType)
var isParentDbRef = _dbRefType != null && _dbRefType == member.DeclaringType;
var isParentDbRef = _dbRefType != null && member.DeclaringType.IsAssignableFrom(_dbRefType);

// get class entity from mapper
var entity = _mapper.GetEntityMapper(member.DeclaringType);
Expand Down

1 comment on commit 653c8f7

@lbnascimento
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for #1651

Please sign in to comment.