Skip to content

Commit 7b5a7af

Browse files
committed
add tests for batch operations
1 parent b835fb0 commit 7b5a7af

File tree

2 files changed

+264
-1
lines changed

2 files changed

+264
-1
lines changed

tests/Query/BuilderTest.php

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,221 @@ public function it_can_process_update_item()
721721
$this->assertEquals($processor, $query['processor']);
722722
}
723723

724+
/** @test */
725+
public function it_can_process_batch_get_item()
726+
{
727+
$method = 'batchGetItem';
728+
$params = [
729+
'TableName' => 'Thread',
730+
'RequestItems' => [
731+
'Thread' => [
732+
'Keys' => [
733+
[
734+
'ForumName' => [
735+
'S' => 'Amazon DynamoDB'
736+
],
737+
'Subject' => [
738+
'S' => 'DynamoDB Thread 1'
739+
]
740+
],
741+
[
742+
'ForumName' => [
743+
'S' => 'Amazon DynamoDB'
744+
],
745+
'Subject' => [
746+
'S' => 'DynamoDB Thread 2'
747+
]
748+
]
749+
]
750+
]
751+
]
752+
];
753+
$processor = 'processBatchGetItems';
754+
755+
$query = $this->newQuery('Thread')
756+
->batchGetItem([
757+
[
758+
'ForumName' => 'Amazon DynamoDB',
759+
'Subject' => 'DynamoDB Thread 1'
760+
],
761+
[
762+
'ForumName' => 'Amazon DynamoDB',
763+
'Subject' => 'DynamoDB Thread 2'
764+
]
765+
]);
766+
767+
$this->assertEquals($method, $query['method']);
768+
$this->assertEquals($params, $query['params']);
769+
$this->assertEquals($processor, $query['processor']);
770+
}
771+
772+
/** @test */
773+
public function it_can_process_batch_put_item()
774+
{
775+
$method = 'batchWriteItem';
776+
$params = [
777+
'TableName' => 'Thread',
778+
'RequestItems' => [
779+
'Thread' => [
780+
[
781+
'PutRequest' => [
782+
'Item' => [
783+
'ForumName' => [
784+
'S' => 'Amazon DynamoDB'
785+
],
786+
'Subject' => [
787+
'S' => 'DynamoDB Thread 3'
788+
]
789+
]
790+
]
791+
],
792+
[
793+
'PutRequest' => [
794+
'Item' => [
795+
'ForumName' => [
796+
'S' => 'Amazon DynamoDB'
797+
],
798+
'Subject' => [
799+
'S' => 'DynamoDB Thread 4'
800+
]
801+
]
802+
]
803+
]
804+
]
805+
]
806+
];
807+
808+
$query = $this->newQuery('Thread')
809+
->batchPutItem([
810+
[
811+
'ForumName' => 'Amazon DynamoDB',
812+
'Subject' => 'DynamoDB Thread 3'
813+
],
814+
[
815+
'ForumName' => 'Amazon DynamoDB',
816+
'Subject' => 'DynamoDB Thread 4'
817+
]
818+
]);
819+
820+
$this->assertEquals($method, $query['method']);
821+
$this->assertEquals($params, $query['params']);
822+
$this->assertNull($query['processor']);
823+
}
824+
825+
/** @test */
826+
public function it_can_process_batch_delete_item()
827+
{
828+
$method = 'batchWriteItem';
829+
$params = [
830+
'TableName' => 'Thread',
831+
'RequestItems' => [
832+
'Thread' => [
833+
[
834+
'DeleteRequest' => [
835+
'Key' => [
836+
'ForumName' => [
837+
'S' => 'Amazon DynamoDB'
838+
],
839+
'Subject' => [
840+
'S' => 'DynamoDB Thread 1'
841+
]
842+
]
843+
]
844+
],
845+
[
846+
'DeleteRequest' => [
847+
'Key' => [
848+
'ForumName' => [
849+
'S' => 'Amazon DynamoDB'
850+
],
851+
'Subject' => [
852+
'S' => 'DynamoDB Thread 2'
853+
]
854+
]
855+
]
856+
]
857+
]
858+
]
859+
];
860+
861+
$query = $this->newQuery('Thread')
862+
->batchDeleteItem([
863+
[
864+
'ForumName' => 'Amazon DynamoDB',
865+
'Subject' => 'DynamoDB Thread 1'
866+
],
867+
[
868+
'ForumName' => 'Amazon DynamoDB',
869+
'Subject' => 'DynamoDB Thread 2'
870+
]
871+
]);
872+
873+
$this->assertEquals($method, $query['method']);
874+
$this->assertEquals($params, $query['params']);
875+
$this->assertNull($query['processor']);
876+
}
877+
878+
/** @test */
879+
public function it_can_process_batch_write_item()
880+
{
881+
$method = 'batchWriteItem';
882+
$params = [
883+
'TableName' => 'Thread',
884+
'RequestItems' => [
885+
'Thread' => [
886+
[
887+
'PutRequest' => [
888+
'Item' => [
889+
'ForumName' => [
890+
'S' => 'Amazon DynamoDB'
891+
],
892+
'Subject' => [
893+
'S' => 'DynamoDB Thread 3'
894+
]
895+
]
896+
]
897+
],
898+
[
899+
'DeleteRequest' => [
900+
'Key' => [
901+
'ForumName' => [
902+
'S' => 'Amazon DynamoDB'
903+
],
904+
'Subject' => [
905+
'S' => 'DynamoDB Thread 1'
906+
]
907+
]
908+
]
909+
]
910+
]
911+
]
912+
];
913+
914+
$query = $this->newQuery('Thread')
915+
->batchWriteItem([
916+
[
917+
'PutRequest' => [
918+
'Item' => [
919+
'ForumName' => 'Amazon DynamoDB',
920+
'Subject' => 'DynamoDB Thread 3'
921+
]
922+
]
923+
],
924+
[
925+
'DeleteRequest' => [
926+
'Key' => [
927+
'ForumName' => 'Amazon DynamoDB',
928+
'Subject' => 'DynamoDB Thread 1'
929+
]
930+
]
931+
]
932+
]);
933+
934+
$this->assertEquals($method, $query['method']);
935+
$this->assertEquals($params, $query['params']);
936+
$this->assertNull($query['processor']);
937+
}
938+
724939
/** @test */
725940
public function it_can_increment_value_of_attribute()
726941
{

tests/Query/ProcessorTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ class ProcessorTest extends TestCase
2323
'multiple_items_result' => '{"Items":[{"Category":{"S":"Amazon Web Services"},"Name":{"S":"Amazon S3"}},{"Threads":{"N":"2"},"Category":{"S":"Amazon Web Services"},"Messages":{"N":"4"},"Views":{"N":"1000"},"Name":{"S":"Amazon DynamoDB"}}],"Count":2,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
2424
'multiple_items_processed' => '{"Items":[{"Category":"Amazon Web Services","Name":"Amazon S3"},{"Threads":2,"Category":"Amazon Web Services","Messages":4,"Views":1000,"Name":"Amazon DynamoDB"}],"Count":2,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
2525
'multiple_items_empty_result' => '{"Items":[],"Count":0,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
26-
'multiple_items_empty_processed' => '{"Items":[],"Count":0,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}'
26+
'multiple_items_empty_processed' => '{"Items":[],"Count":0,"ScannedCount":2,"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
27+
'batch_get_items_result' => '{"Responses":{"Thread":[{"Replies":{"N":"0"},"Answered":{"N":"0"},"Views":{"N":"0"},"ForumName":{"S":"Amazon DynamoDB"},"Subject":{"S":"DynamoDB Thread 1"}},{"Replies":{"N":"0"},"Answered":{"N":"0"},"Views":{"N":"0"},"ForumName":{"S":"Amazon DynamoDB"},"Subject":{"S":"DynamoDB Thread 2"}}]},"UnprocessedKeys":[],"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
28+
'batch_get_items_processed' => '{"Responses":{"Thread":[{"Replies":0,"Answered":0,"Views":0,"ForumName":"Amazon DynamoDB","Subject":"DynamoDB Thread 1"},{"Replies":0,"Answered":0,"Views":0,"ForumName":"Amazon DynamoDB","Subject":"DynamoDB Thread 2"}]},"UnprocessedKeys":[],"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
29+
'batch_get_items_empty_result' => '{"Responses":{"Thread":[]},"UnprocessedKeys":[],"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
30+
'batch_get_items_empty_processed' => '{"Responses":{"Thread":[]},"UnprocessedKeys":[],"@metadata":{"statusCode":200,"effectiveUri":"https:\/\/dynamodb.ap-northeast-1.amazonaws.com","transferStats":{"http":[[]]}}}',
2731
];
2832

2933
protected function setUp() :void
@@ -79,6 +83,30 @@ public function it_can_process_multiple_items_empty_result()
7983
$this->assertEquals($expected, $items);
8084
}
8185

86+
/** @test */
87+
public function it_can_process_batch_get_items_result()
88+
{
89+
$expected = json_decode($this->mocks['batch_get_items_processed'], true);
90+
91+
$awsResult = new Result(json_decode($this->mocks['batch_get_items_result'], true));
92+
93+
$items = $this->processor->processBatchGetItems($awsResult, null);
94+
95+
$this->assertEquals($expected, $items);
96+
}
97+
98+
/** @test */
99+
public function it_can_process_batch_get_items_empty_result()
100+
{
101+
$expected = json_decode($this->mocks['batch_get_items_empty_processed'], true);
102+
103+
$awsResult = new Result(json_decode($this->mocks['batch_get_items_empty_result'], true));
104+
105+
$items = $this->processor->processBatchGetItems($awsResult, null);
106+
107+
$this->assertEquals($expected, $items);
108+
}
109+
82110
/** @test */
83111
public function it_can_convert_single_result_to_model_instance()
84112
{
@@ -113,4 +141,24 @@ public function it_can_convert_multiple_results_to_model_instance()
113141
], $item->toArray());
114142
$this->assertEquals(200, $item->meta()['@metadata']['statusCode']);
115143
}
144+
145+
/** @test */
146+
public function it_can_convert_batch_get_results_to_model_instance()
147+
{
148+
$awsResult = new Result(json_decode($this->mocks['batch_get_items_result'], true));
149+
150+
$items = $this->processor->processBatchGetItems($awsResult, User::class);
151+
152+
$item = $items->first();
153+
154+
$this->assertEquals(User::class, get_class($item));
155+
$this->assertEquals([
156+
'Replies' => 0,
157+
'Answered' => 0,
158+
'Views' => 0,
159+
'ForumName' => 'Amazon DynamoDB',
160+
'Subject' => 'DynamoDB Thread 1',
161+
], $item->toArray());
162+
$this->assertEquals(200, $item->meta()['@metadata']['statusCode']);
163+
}
116164
}

0 commit comments

Comments
 (0)