Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Find all related orders of a customer returns empty #1651

Closed
kfirpeled opened this issue Apr 22, 2020 · 2 comments
Closed

[BUG] Find all related orders of a customer returns empty #1651

kfirpeled opened this issue Apr 22, 2020 · 2 comments
Labels

Comments

@kfirpeled
Copy link

kfirpeled commented Apr 22, 2020

Version
LiteDb 5.0.7
OSX Catalina 10.15.4
dotnet 3.1.102

Describe the bug
Finding customer's order returns zero results when using lambda expression.
Digging into this deep I could see that the BsonMapper is not resolving the expression correctly

Code to Reproduce


    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 : IDisposable
    {
        private LiteDatabase _database;
        private ILiteCollection<Customer> _customerCollection;
        private ILiteCollection<Order> _orderCollection;

        public CustomerRelationshipsTests()
        {
            BsonMapper.Global.Entity<Order>().DbRef(order => order.Customer);

            _database = new LiteDatabase("customer1.db");

            _orderCollection = _database.GetCollection<Order>();
            _customerCollection = _database.GetCollection<Customer>();
        }


        [Fact]
        public void Find_ByRelationId_Success()
        {
            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)
                // NOT WORKING (BsonExpression generated from the lambda is $.Customer._id instead of $.Customer.$id)
                .Find(orderEntity => orderEntity.Customer.Id == customer.Id)
                // .Find(Query.EQ("$.Customer.$id", customer.Id)) WORKS - returning results correctly
                // .Find(Query.EQ("$.Customer._id", customer.Id)) NOT WORKING
                .ToList();

            Assert.Equal(2, actualOrders.Count); // Fails here, actual result is zero
            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);
        }

        public void Dispose()
        {
            _database.DropCollection(_orderCollection.Name);
            _database.DropCollection(_customerCollection.Name);
            _database.Dispose();
        }
    }


**Is it related to BsonMapper ? **

var expr = BsonMapper.Global.GetExpression<Order, bool>(orderEntity => orderEntity.Customer.Id == customer.Id);

Returns ($.Customer._id=@p0)
Should it return ($.Customer.$id=@p0) ? Or the issue is somewhere else?

Expected behavior
I expect to use find with lambda to select customer's order by the customer's id.
A workaround for me is to build the query myself (which is not ideal)

@kfirpeled kfirpeled added the bug label Apr 22, 2020
@kfirpeled kfirpeled changed the title [BUG] BsomMapper fails to resolve lambda expression of linked entity correctly [BUG] BsonMapper fails to resolve lambda expression of linked entity correctly Apr 22, 2020
@kfirpeled kfirpeled changed the title [BUG] BsonMapper fails to resolve lambda expression of linked entity correctly [BUG] Find all related orders of a customer returns empty Apr 22, 2020
@kfirpeled
Copy link
Author

Update:
It seems that since 5.0.7 this is reproduced constantly,
In previous versions, it reproduced sporadically 🤷🏼‍♀️

@lbnascimento
Copy link
Contributor

@kfirp-bs This issue has been fixed in the latest master and its fix will be present in the next incremental release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants