-
Notifications
You must be signed in to change notification settings - Fork 0
PHPORM-33 Add tests on Query\Builder methods #14
Conversation
tests/Query/BuilderTest.php
Outdated
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]], | ||
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']), | ||
]; | ||
|
||
yield 'whereIn associative array' => [ | ||
['find' => [['id' => ['$in' => [45582, 2, 3]]], []]], | ||
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we consider raising an exception if the value is not a list, or is that likely to ruffle some feathers among users? Passing an associative array this way seems incorrect, and MongoDB itself would rightfully complain if a BSON object was provided where an array is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception added. I agree that we need to be more strict than Laravel, with didactic error messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where was the exception added? I only error tests for "find with single string argument" and various "IntegerInRaw" cases, neither of which are related to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I reverted. I inadvertently made the change in laravel/framework
before realising it would be complex to do in laravel-mongodb. If you insist I can try to add this exception in an other PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's revisit this in a separate PR -- feel free to create a new issue to track it.
I'm very much in favor of raising exceptions for poorly formatted values. As annoying as it may be for some users, I think it will ultimately avoid unexpected edge cases and bugs down the line.
437d9cf
to
44baa9b
Compare
tests/Query/BuilderTest.php
Outdated
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]], | ||
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']), | ||
]; | ||
|
||
yield 'whereIn associative array' => [ | ||
['find' => [['id' => ['$in' => [45582, 2, 3]]], []]], | ||
fn (Builder $builder) => $builder->whereIn('id', ['issue' => 45582, 'id' => 2, 3]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where was the exception added? I only error tests for "find with single string argument" and various "IntegerInRaw" cases, neither of which are related to this.
tests/Query/BuilderTest.php
Outdated
|
||
yield 'offset limit negative' => [ | ||
['find' => [[], []]], | ||
fn (Builder $builder) => $builder->offset(-5)->limit(-10), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I missed this in a previous review or if this was recently added, but I'd propose throwing an exception if negative values are passed for offset and limit. That seems preferable to ignoring them outright.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a Laravel feature. I'm not sure we should care about changing this behavior.
https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Query/Builder.php#L2435-L2437
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/Query/BuilderTest.php
Outdated
|
||
yield 'offset 0 limit 0' => [ | ||
['find' => [[], []]], | ||
fn (Builder $builder) => $builder->offset(0)->limit(0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offhand, do you know if zero is handled exactly the same as "null (reset)"?
If so, I'd be in favor of the following changes:
- Rename this test to "offset limit zero (unset)"
- Create a new test below named "offset limit null (unset)", based on this test. The point of these tests would be to demonstrate that zero and null values both result in the options being unspecified.
- Create a new test here named "offset limit zero (reset)", based on the "offset limit null (reset)" test below. Like that, it would demonstrate that zero and null both reset the options back to an unset state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment might have been missed.
I'll defer to you if you'd like to make the suggested test changes. Otherwise, feel free to resolve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test updated. Thanks for the reminder.
7e0f82d
to
43dfe24
Compare
@@ -332,7 +332,7 @@ public function testTransaction(): void | |||
$count = User::count(); | |||
$this->assertEquals(2, $count); | |||
|
|||
$this->assertTrue(User::where('alcaeus')->exists()); | |||
$this->assertTrue(User::where('name', 'alcaeus')->exists()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being too permissive didn't allow to detect this incorrect query.
- { "alcaeus": null }
+ { "name": "alcaeus" }
The new exception made it surface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to stop using my name in tests. It makes blaming me for my mistakes too easy 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@@ -923,6 +923,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' | |||
} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't comment on the if
itself, but I'm wondering if the condition should read func_num_args() >= 3
- we wouldn't be removing the $
from an operator if $boolean
was defined as well 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me how $boolean
is used, but I see it's passed on to the parent method below. I think that's right, and this suggests another lingering bug.
I'll defer to you guys whether to fix here (if so, add a regression test that passes a fourth parameter to where()
(I suppose an explicit 'and'
string is fine) or report a new JIRA issue and address in a subsequent PR. A new issue probably makes more sense for visibility in the release notes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #20
@@ -1376,4 +1380,28 @@ public function havingBetween($column, iterable $values, $boolean = 'and', $not | |||
{ | |||
throw new \BadMethodCallException('This method is not supported by MongoDB'); | |||
} | |||
|
|||
/** @internal This method is not supported by MongoDB. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good strategy to mark these as internal to have the IDE notify users already 👍
@@ -332,7 +332,7 @@ public function testTransaction(): void | |||
$count = User::count(); | |||
$this->assertEquals(2, $count); | |||
|
|||
$this->assertTrue(User::where('alcaeus')->exists()); | |||
$this->assertTrue(User::where('name', 'alcaeus')->exists()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to stop using my name in tests. It makes blaming me for my mistakes too easy 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two follow-up comments.
LGTM to merge as-is if you'd rather not make the test changes. In that case, the issue @alcaeus mentioned in https://github.com/GromNaN/laravel-mongodb/pull/14/files#r1273137525 should be tracked in a separate ticket.
@@ -923,6 +923,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' | |||
} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me how $boolean
is used, but I see it's passed on to the parent method below. I think that's right, and this suggests another lingering bug.
I'll defer to you guys whether to fix here (if so, add a regression test that passes a fourth parameter to where()
(I suppose an explicit 'and'
string is fine) or report a new JIRA issue and address in a subsequent PR. A new issue probably makes more sense for visibility in the release notes.
tests/Query/BuilderTest.php
Outdated
|
||
yield 'offset 0 limit 0' => [ | ||
['find' => [[], []]], | ||
fn (Builder $builder) => $builder->offset(0)->limit(0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment might have been missed.
I'll defer to you if you'd like to make the suggested test changes. Otherwise, feel free to resolve.
- Add tests on query builder methods that don't need to be fixed. - Throw exception when calling unsupported methods: whereIntegerInRaw, orWhereIntegerInRaw, whereIntegerNotInRaw, orWhereIntegerNotInRaw - Throw an exception when Query\Builder::where is called with only a column name
Fix PHPORM-33
Replace #4
whereIntegerInRaw
,orWhereIntegerInRaw
,whereIntegerNotInRaw
,orWhereIntegerNotInRaw
Query\Builder::where
is called with only a column name