Skip to content

Commit

Permalink
adding some tests for percolate exisiting docs with paramters
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel hartmann committed Mar 16, 2014
1 parent 6ee78f1 commit 86ca940
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-03-15
- percolate existing documents and add percolate options (#570)

2014-03-11
- Fixed request body reuse in http transport #567

Expand Down
6 changes: 3 additions & 3 deletions lib/Elastica/Percolator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function matchDoc(Document $doc, $query = null, $type = 'type', $params =
$path = $this->_index->getName() . '/' . $type . '/_percolate';
$data = array('doc' => $doc->getData());

return $this->percolate($path, $query, $data, $params);
return $this->_percolate($path, $query, $data, $params);
}

/**
Expand All @@ -93,7 +93,7 @@ public function matchExistingDoc($id, $type, $query = null, $params = array())
$id = urlencode($id);
$path = $this->_index->getName() . '/' . $type . '/'. $id . '/_percolate';

return $this->percolate($path, $query, array(), $params);
return $this->_percolate($path, $query, array(), $params);
}

/**
Expand All @@ -103,7 +103,7 @@ public function matchExistingDoc($id, $type, $query = null, $params = array())
* @param array $params
* @return array
*/
protected function percolate($path, $query, $data = array(), $params = array())
protected function _percolate($path, $query, $data = array(), $params = array())
{
// Add query to filter the percolator queries which are executed.
if ($query) {
Expand Down
66 changes: 66 additions & 0 deletions test/lib/Elastica/Test/PercolatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,70 @@ public function testRegisterAndUnregisterPercolator()

$index->delete();
}

protected function _getDefaultPercolator($percolatorName = 'existingDoc')
{
$index = $this->_createIndex();
$percolator = new Percolator($index);

$query = new Term(array('name' => 'foobar'));
$percolator->registerQuery($percolatorName, $query);
return $percolator;
}

protected function _addDefaultDocuments($index, $type='testing')
{
$type = $index->getType('testing');
$doc1 = new Document(1, array('name' => 'foobar'));
$doc2 = new Document(2, array('name' => 'barbaz'));
$type->addDocument($doc1);
$type->addDocument($doc2);
$index->refresh();
return $type;
}

public function testPercolateExistingDocWithoutAnyParameter()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

$matches = $percolator->matchExistingDoc(1, $type->getName());

$this->assertCount(1, $matches);
$this->assertEquals('existingDoc', $matches[0]['_id']);
$index->delete();
}

public function testPercolateExistingDocWithPercolateFormatIds()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

$parameter = array('percolate_format' => 'ids');
$matches = $percolator->matchExistingDoc(1, $type->getName(), null, $parameter);

$this->assertCount(1, $matches);
$this->assertEquals('existingDoc', $matches[0]);
$index->delete();
}

public function testPercolateExistingDocWithIdThatShouldBeUrlEncoded()
{
$percolator = $this->_getDefaultPercolator();
$index = $percolator->getIndex();
$type = $this->_addDefaultDocuments($index);

// id with whitespace, should be urlencoded
$id = "foo bar 1";

$type->addDocument(new Document($id, array('name' => 'foobar')));
$index->refresh();

$matches = $percolator->matchExistingDoc($id, $type->getName());

$this->assertCount(1, $matches);
$index->delete();
}
}

0 comments on commit 86ca940

Please sign in to comment.