|
2 | 2 |
|
3 | 3 | namespace Kitar\Dynamodb\Tests\Query;
|
4 | 4 |
|
| 5 | +use Aws\Result; |
| 6 | +use Mockery as m; |
5 | 7 | use Kitar\Dynamodb\Connection;
|
6 | 8 | use Kitar\Dynamodb\Model\Model;
|
| 9 | +use Kitar\Dynamodb\Query\Builder; |
| 10 | +use Kitar\Dynamodb\Query\Grammar; |
| 11 | +use Kitar\Dynamodb\Query\Processor; |
7 | 12 | use PHPUnit\Framework\TestCase;
|
| 13 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
8 | 14 |
|
9 | 15 | class Product extends Model
|
10 | 16 | {
|
11 | 17 | }
|
12 | 18 |
|
13 | 19 | class BuilderTest extends TestCase
|
14 | 20 | {
|
| 21 | + use MockeryPHPUnitIntegration; |
| 22 | + |
15 | 23 | protected $connection;
|
16 | 24 |
|
17 | 25 | protected function setUp() :void
|
18 | 26 | {
|
19 | 27 | $this->connection = new Connection([]);
|
20 | 28 | }
|
21 | 29 |
|
| 30 | + protected function tearDown() :void |
| 31 | + { |
| 32 | + m::close(); |
| 33 | + } |
| 34 | + |
22 | 35 | protected function newQuery($table_name)
|
23 | 36 | {
|
24 | 37 | return $this->connection->table($table_name)->dryRun();
|
@@ -351,6 +364,111 @@ public function it_can_process_filter_between()
|
351 | 364 | $this->assertEquals($params, $query['params']);
|
352 | 365 | }
|
353 | 366 |
|
| 367 | + /** @test */ |
| 368 | + public function it_can_process_attribute_exists_function() |
| 369 | + { |
| 370 | + $params = [ |
| 371 | + 'TableName' => 'Forum', |
| 372 | + 'FilterExpression' => 'attribute_exists(#1)', |
| 373 | + 'ExpressionAttributeNames' => [ |
| 374 | + '#1' => 'Messages' |
| 375 | + ] |
| 376 | + ]; |
| 377 | + |
| 378 | + $query = $this->newQuery('Forum') |
| 379 | + ->filter('Messages', 'attribute_exists') |
| 380 | + ->scan(); |
| 381 | + |
| 382 | + $this->assertEquals($params, $query['params']); |
| 383 | + } |
| 384 | + |
| 385 | + /** @test */ |
| 386 | + public function it_can_process_attribute_not_exists_function() |
| 387 | + { |
| 388 | + $params = [ |
| 389 | + 'TableName' => 'Forum', |
| 390 | + 'FilterExpression' => 'attribute_not_exists(#1)', |
| 391 | + 'ExpressionAttributeNames' => [ |
| 392 | + '#1' => 'Messages' |
| 393 | + ] |
| 394 | + ]; |
| 395 | + |
| 396 | + $query = $this->newQuery('Forum') |
| 397 | + ->filter('Messages', 'attribute_not_exists') |
| 398 | + ->scan(); |
| 399 | + |
| 400 | + $this->assertEquals($params, $query['params']); |
| 401 | + } |
| 402 | + |
| 403 | + /** @test */ |
| 404 | + public function it_can_process_attribute_type_function() |
| 405 | + { |
| 406 | + $params = [ |
| 407 | + 'TableName' => 'Forum', |
| 408 | + 'FilterExpression' => 'attribute_type(#1, :1)', |
| 409 | + 'ExpressionAttributeNames' => [ |
| 410 | + '#1' => 'Messages' |
| 411 | + ], |
| 412 | + 'ExpressionAttributeValues' => [ |
| 413 | + ':1' => [ |
| 414 | + 'S' => 'N' |
| 415 | + ] |
| 416 | + ] |
| 417 | + ]; |
| 418 | + |
| 419 | + $query = $this->newQuery('Forum') |
| 420 | + ->filter('Messages', 'attribute_type', 'N') |
| 421 | + ->scan(); |
| 422 | + |
| 423 | + $this->assertEquals($params, $query['params']); |
| 424 | + } |
| 425 | + |
| 426 | + /** @test */ |
| 427 | + public function it_can_process_begins_with_function() |
| 428 | + { |
| 429 | + $params = [ |
| 430 | + 'TableName' => 'ProductCatalog', |
| 431 | + 'FilterExpression' => 'begins_with(#1, :1)', |
| 432 | + 'ExpressionAttributeNames' => [ |
| 433 | + '#1' => 'Title' |
| 434 | + ], |
| 435 | + 'ExpressionAttributeValues' => [ |
| 436 | + ':1' => [ |
| 437 | + 'S' => 'Book' |
| 438 | + ] |
| 439 | + ] |
| 440 | + ]; |
| 441 | + |
| 442 | + $query = $this->newQuery('ProductCatalog') |
| 443 | + ->filter('Title', 'begins_with', 'Book') |
| 444 | + ->scan(); |
| 445 | + |
| 446 | + $this->assertEquals($params, $query['params']); |
| 447 | + } |
| 448 | + |
| 449 | + /** @test */ |
| 450 | + public function it_can_process_contains_function() |
| 451 | + { |
| 452 | + $params = [ |
| 453 | + 'TableName' => 'ProductCatalog', |
| 454 | + 'FilterExpression' => 'contains(#1, :1)', |
| 455 | + 'ExpressionAttributeNames' => [ |
| 456 | + '#1' => 'Title' |
| 457 | + ], |
| 458 | + 'ExpressionAttributeValues' => [ |
| 459 | + ':1' => [ |
| 460 | + 'S' => 'Bike' |
| 461 | + ] |
| 462 | + ] |
| 463 | + ]; |
| 464 | + |
| 465 | + $query = $this->newQuery('ProductCatalog') |
| 466 | + ->filter('Title', 'contains', 'Bike') |
| 467 | + ->scan(); |
| 468 | + |
| 469 | + $this->assertEquals($params, $query['params']); |
| 470 | + } |
| 471 | + |
354 | 472 | /** @test */
|
355 | 473 | public function it_can_process_get_item()
|
356 | 474 | {
|
@@ -624,4 +742,42 @@ public function it_can_process_scan()
|
624 | 742 | $this->assertEquals($params, $query['params']);
|
625 | 743 | $this->assertEquals($processor, $query['processor']);
|
626 | 744 | }
|
| 745 | + |
| 746 | + /** @test */ |
| 747 | + public function it_can_process_process() |
| 748 | + { |
| 749 | + $connection = m::mock(Connection::class); |
| 750 | + $connection->shouldReceive('scan') |
| 751 | + ->with(['TableName' => 'Forum']) |
| 752 | + ->andReturn(new Result(['Items' => []])); |
| 753 | + |
| 754 | + $query = new Builder($connection, new Grammar, new Processor); |
| 755 | + |
| 756 | + $query->from('Forum')->scan(); |
| 757 | + } |
| 758 | + |
| 759 | + /** @test */ |
| 760 | + public function it_can_process_process_with_no_processor() |
| 761 | + { |
| 762 | + $connection = m::mock(Connection::class); |
| 763 | + $connection->shouldReceive('putItem') |
| 764 | + ->with([ |
| 765 | + 'TableName' => 'Thread', |
| 766 | + 'Item' => [ |
| 767 | + 'ForumName' => [ |
| 768 | + 'S' => 'Laravel' |
| 769 | + ], |
| 770 | + 'Subject' => [ |
| 771 | + 'S' => 'Laravel Thread 1' |
| 772 | + ] |
| 773 | + ] |
| 774 | + ])->andReturn(new Result(['Items' => []])); |
| 775 | + |
| 776 | + $query = new Builder($connection, new Grammar, new Processor); |
| 777 | + |
| 778 | + $query->from('Thread')->putItem([ |
| 779 | + 'ForumName' => 'Laravel', |
| 780 | + 'Subject' => 'Laravel Thread 1' |
| 781 | + ]); |
| 782 | + } |
627 | 783 | }
|
0 commit comments