-
-
Notifications
You must be signed in to change notification settings - Fork 56
chore: test functions against a real PostgreSQL server #379
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonbInsertTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Doctrine\DBAL\Exception; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbInsert; | ||
|
|
||
| class JsonbInsertTest extends JsonTestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSONB_INSERT' => JsonbInsert::class, | ||
| ]; | ||
| } | ||
|
|
||
| public function test_jsonb_insert_new_value(): void | ||
| { | ||
| $dql = 'SELECT JSONB_INSERT(t.object1, :path, :value) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'path' => '{email}', | ||
| 'value' => '"john@example.com"', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertArrayHasKey('email', $decoded); | ||
| $this->assertSame('john@example.com', $decoded['email']); | ||
| } | ||
|
|
||
| public function test_jsonb_insert_nested_path(): void | ||
| { | ||
| $dql = 'SELECT JSONB_INSERT(t.object1, :path, :value) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'path' => '{address,zip}', | ||
| 'value' => '"10001"', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertArrayHasKey('address', $decoded); | ||
| $this->assertIsArray($decoded['address']); | ||
| $this->assertArrayHasKey('zip', $decoded['address']); | ||
| $this->assertSame('10001', $decoded['address']['zip']); | ||
| } | ||
|
|
||
| public function test_throws_exception_when_inserting_at_existing_object_key(): void | ||
| { | ||
| $this->expectException(Exception::class); | ||
| $dql = 'SELECT JSONB_INSERT(t.object1, :path, :value) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $this->executeDqlQuery($dql, [ | ||
| 'path' => '{name}', | ||
| 'value' => '"John Doe"', | ||
| ]); | ||
| } | ||
|
|
||
| public function test_throws_exception_when_inserting_at_existing_nested_path(): void | ||
| { | ||
| $this->expectException(Exception::class); | ||
|
|
||
| $dql = 'SELECT JSONB_INSERT(t.object1, :path, :value) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 5'; | ||
|
|
||
| $this->executeDqlQuery($dql, [ | ||
| 'path' => '{address,zip}', | ||
| 'value' => '"10001"', | ||
| ]); | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonbObjectAggTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbObjectAgg; | ||
|
|
||
| class JsonbObjectAggTest extends JsonTestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSONB_OBJECT_AGG' => JsonbObjectAgg::class, | ||
| ]; | ||
| } | ||
|
|
||
| public function test_jsonb_object_agg(): void | ||
| { | ||
| $dql = "SELECT JSONB_OBJECT_AGG('key', 'value') as result | ||
| FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsJsons t | ||
| WHERE t.id = 1"; | ||
| $result = $this->executeDqlQuery($dql); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertSame(['key' => 'value'], $decoded); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
tests/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonbObjectKeysTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbObjectKeys; | ||
|
|
||
| class JsonbObjectKeysTest extends JsonTestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSONB_OBJECT_KEYS' => JsonbObjectKeys::class, | ||
| ]; | ||
| } | ||
|
|
||
| public function test_jsonb_object_keys(): void | ||
| { | ||
| $dql = 'SELECT JSONB_OBJECT_KEYS(t.object1) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql); | ||
| $this->assertIsString($result[0]['result']); | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
...s/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonbPathQueryArrayTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbPathQueryArray; | ||
|
|
||
| class JsonbPathQueryArrayTest extends JsonTestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSONB_PATH_QUERY_ARRAY' => JsonbPathQueryArray::class, | ||
| ]; | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_array_simple(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_ARRAY(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"a": 1, "b": 2}', | ||
| 'path' => '$.b', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertCount(1, $decoded); | ||
| $this->assertSame(2, $decoded[0]); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_array_multiple_values(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_ARRAY(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"items": [1, 2, 3]}', | ||
| 'path' => '$.items[*]', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertCount(3, $decoded); | ||
| $this->assertSame([1, 2, 3], $decoded); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_array_with_filter(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_ARRAY(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"items": [{"id": 1}, {"id": 2}, {"id": 3}]}', | ||
| 'path' => '$.items[*] ? (@.id > 1)', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertCount(2, $decoded); | ||
| $this->assertSame(['id' => 2], $decoded[0]); | ||
| $this->assertSame(['id' => 3], $decoded[1]); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_array_with_column_reference(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_ARRAY(t.object1, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, ['path' => '$.tags[*]']); | ||
| $this->assertIsString($result[0]['result']); | ||
| $decoded = \json_decode($result[0]['result'], true); | ||
| $this->assertIsArray($decoded); | ||
| $this->assertCount(2, $decoded); | ||
| $this->assertSame(['developer', 'manager'], $decoded); | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
...s/Integration/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonbPathQueryFirstTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbPathQueryFirst; | ||
|
|
||
| class JsonbPathQueryFirstTest extends JsonTestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSONB_PATH_QUERY_FIRST' => JsonbPathQueryFirst::class, | ||
| ]; | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_first_simple(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_FIRST(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"a": 1, "b": 2}', | ||
| 'path' => '$.b', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $this->assertSame('2', $result[0]['result']); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_first_array(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_FIRST(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"items": [1, 2, 3]}', | ||
| 'path' => '$.items[*]', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $this->assertSame('1', $result[0]['result']); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_first_with_filter(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_FIRST(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"items": [{"id": 1}, {"id": 2}, {"id": 3}]}', | ||
| 'path' => '$.items[*] ? (@.id > 1)', | ||
| ]); | ||
| $this->assertIsString($result[0]['result']); | ||
| $this->assertSame('{"id": 2}', $result[0]['result']); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_first_with_no_match(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_FIRST(:json, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, [ | ||
| 'json' => '{"items": [{"id": 1}]}', | ||
| 'path' => '$.items[*] ? (@.id > 1)', | ||
| ]); | ||
| $this->assertNull($result[0]['result']); | ||
| } | ||
|
|
||
| public function test_jsonb_path_query_first_with_column_reference(): void | ||
| { | ||
| $dql = 'SELECT JSONB_PATH_QUERY_FIRST(t.object1, :path) as result | ||
| FROM Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons t | ||
| WHERE t.id = 1'; | ||
| $result = $this->executeDqlQuery($dql, ['path' => '$.tags[*]']); | ||
| $this->assertIsString($result[0]['result']); | ||
| $this->assertSame('"developer"', $result[0]['result']); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.