Skip to content

Commit

Permalink
Fixed Response::isOk() to work better with bulk update api
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed Oct 10, 2014
1 parent c9f41fa commit cad5ec4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-10-10
- Fixed Response::isOk() to work better with bulk update api

2014-10-05
- ResultSet creation moved to static ResultSet::create() method #690
- Accept an options array at Type::updateDocument() #686
Expand Down
10 changes: 9 additions & 1 deletion lib/Elastica/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,17 @@ public function isOk()
}
return false;
}

if (isset($data['items'])) {
if (isset($data['errors']) && true === $data['errors']) {
return false;
}

foreach ($data['items'] as $item) {
if (false == $item['index']['ok']) {
if (isset($item['index']['ok']) && false == $item['index']['ok']) {
return false;

} elseif (isset($item['index']['status']) && ($item['index']['status'] < 200 || $item['index']['status'] >= 300)) {
return false;
}
}
Expand Down
81 changes: 81 additions & 0 deletions test/lib/Elastica/Test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\Query;
use Elastica\Query\MatchAll;
use Elastica\Request;
use Elastica\Response;
use Elastica\Type\Mapping;
use Elastica\Test\Base as BaseTest;

Expand Down Expand Up @@ -79,6 +80,86 @@ public function testIsOkMultiple()
$this->assertTrue($response->isOk());
}

public function testIsOkBulkWithErrorsField()
{
$response = new Response(json_encode(array(
'took' => 213,
'errors' => false,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkWithErrorsField()
{
$response = new Response(json_encode(array(
'took' => 213,
'errors' => true,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertFalse($response->isOk());
}

public function testIsOkBulkItemsWithOkField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => true)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkItemsWithOkField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => false)),
)
)));

$this->assertFalse($response->isOk());
}

public function testIsOkBulkItemsWithStatusField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkItemsWithStatusField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 301)),
)
)));

$this->assertFalse($response->isOk());
}

public function testGetDataEmpty()
{
$index = $this->_createIndex();
Expand Down

0 comments on commit cad5ec4

Please sign in to comment.