kirschbaum-development / eloquent-power-joins

The Laravel magic you know, now applied to joins.
MIT License
1.39k stars 87 forks source link

Failing tests #149

Closed amshehzad closed 1 year ago

amshehzad commented 1 year ago

Hi, I was writing a unit test for one my method, but strangely it does fails sometimes. here is my method:

public function onLeave(Carbon $date = null, $paid = null): bool
{
    //todo::question: if leave is not approved yet, should we return true?
    $date = $date ?? today();

    return LeaveDay::joinRelationship('leave.reason', [
        'leave' => fn($leave) => $leave->whereNotNull('approved_by'),
        'reason' => fn($reason) => $reason->when($paid !== null,
            fn($reason) => $reason->where('paid', $paid)
        ),
    ])
        ->whereDate('date', $date->toDateString())
        ->where('leave_days.user_id', $this->id)
        ->count();

    /*return LeaveDay::query()
        ->whereHas('leave', fn(Builder $query) => $query->approved())
        ->when($paid !== null, fn(Builder $query) => $query->whereRelation('reason', 'paid', $paid))
        ->whereDate('date', $date->toDateString())
        ->where('user_id', $this->id)
        ->count();*/
}

If I do it using the Eloquoent approach, which you can see is commented, it works and doesn't fail. I've repeated the tests 50 times and it does fail occasionally if I use powerJoins

image

Here I repeated 100 times and it fails 6 times ( I'm using same test data )

and here is when I tried using Eleqount approach: image

luisdalmolin commented 1 year ago

I'm sorry, but your two queries do two things very differently. Your first example use joins, while the second doesn't, so they are not a direct comparison.

amshehzad commented 1 year ago

But why do the tests fail sometimes? isn't that strange? I mean it should not fail. if there is some issue on data if should fail everytime