Skip to content

Commit 39a5606

Browse files
committed
Add more tests for Query Builder and Grammar
1 parent cbfeb6f commit 39a5606

File tree

3 files changed

+158
-11
lines changed

3 files changed

+158
-11
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
},
2424
"require-dev": {
2525
"symfony/var-dumper": "^5.0",
26-
"vlucas/phpdotenv": "^4.1"
26+
"vlucas/phpdotenv": "^4.1",
27+
"mockery/mockery": "^1.3"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,6 @@ protected function compileContainsCondition($where)
298298
return "contains({$where['column']}, {$where['value']})";
299299
}
300300

301-
/**
302-
* Compile a size condition.
303-
* @param array $where
304-
* @return string
305-
*/
306-
protected function compileSizeCondition($where)
307-
{
308-
return "size({$where['column']}, {$where['value']})";
309-
}
310-
311301
/**
312302
* @inheritdoc
313303
*/

tests/Query/BuilderTest.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,36 @@
22

33
namespace Kitar\Dynamodb\Tests\Query;
44

5+
use Aws\Result;
6+
use Mockery as m;
57
use Kitar\Dynamodb\Connection;
68
use Kitar\Dynamodb\Model\Model;
9+
use Kitar\Dynamodb\Query\Builder;
10+
use Kitar\Dynamodb\Query\Grammar;
11+
use Kitar\Dynamodb\Query\Processor;
712
use PHPUnit\Framework\TestCase;
13+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
814

915
class Product extends Model
1016
{
1117
}
1218

1319
class BuilderTest extends TestCase
1420
{
21+
use MockeryPHPUnitIntegration;
22+
1523
protected $connection;
1624

1725
protected function setUp() :void
1826
{
1927
$this->connection = new Connection([]);
2028
}
2129

30+
protected function tearDown() :void
31+
{
32+
m::close();
33+
}
34+
2235
protected function newQuery($table_name)
2336
{
2437
return $this->connection->table($table_name)->dryRun();
@@ -351,6 +364,111 @@ public function it_can_process_filter_between()
351364
$this->assertEquals($params, $query['params']);
352365
}
353366

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+
354472
/** @test */
355473
public function it_can_process_get_item()
356474
{
@@ -624,4 +742,42 @@ public function it_can_process_scan()
624742
$this->assertEquals($params, $query['params']);
625743
$this->assertEquals($processor, $query['processor']);
626744
}
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+
}
627783
}

0 commit comments

Comments
 (0)