-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added feature tests * Added filter to test configuration * Explicitly added configuration file
- Loading branch information
1 parent
8e0f9b5
commit 16137d5
Showing
11 changed files
with
318 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Vladvildanov\PredisVl\Feature; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Predis\Client; | ||
|
||
abstract class FeatureTestCase extends TestCase | ||
{ | ||
/** | ||
* Returns a named array with default values for connection parameters. | ||
* | ||
* @return array Default connection parameters | ||
*/ | ||
protected function getDefaultParametersArray(): array | ||
{ | ||
return [ | ||
'scheme' => 'tcp', | ||
'host' => constant('REDIS_SERVER_HOST'), | ||
'port' => constant('REDIS_SERVER_PORT'), | ||
'database' => constant('REDIS_SERVER_DBNUM'), | ||
]; | ||
} | ||
|
||
/** | ||
* Creates Redis client with default or given configuration. | ||
* | ||
* @param array|null $parameters | ||
* @param array|null $options | ||
* @param bool|null $flushDB | ||
* @return Client | ||
*/ | ||
protected function getClient(?array $parameters = null, ?array $options = null, ?bool $flushDB = true): Client | ||
{ | ||
$parameters = array_merge( | ||
$this->getDefaultParametersArray(), | ||
$parameters ?: [] | ||
); | ||
|
||
$client = new Client($parameters, $options); | ||
|
||
if ($flushDB) { | ||
$client->flushdb(); | ||
} | ||
|
||
return $client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
<?php | ||
|
||
namespace Vladvildanov\PredisVl\Feature\Index; | ||
|
||
use Vladvildanov\PredisVl\Feature\FeatureTestCase; | ||
use Predis\Client; | ||
use Vladvildanov\PredisVl\Index\SearchIndex; | ||
use Vladvildanov\PredisVl\VectorHelper; | ||
|
||
class SearchIndexTest extends FeatureTestCase | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private Client $client; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private array $hashSchema; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private array $jsonSchema; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = $this->getClient(); | ||
$this->hashSchema = [ | ||
'index' => [ | ||
'name' => 'foobar', | ||
'prefix' => 'foo:', | ||
], | ||
'fields' => [ | ||
'id' => [ | ||
'type' => 'text', | ||
'alias' => 'foo', | ||
], | ||
'count' => [ | ||
'type' => 'numeric', | ||
], | ||
'id_embeddings' => [ | ||
'type' => 'vector', | ||
'algorithm' => 'flat', | ||
'dims' => 3, | ||
'datatype' => 'float32', | ||
'distance_metric' => 'cosine', | ||
] | ||
] | ||
]; | ||
$this->jsonSchema = [ | ||
'index' => [ | ||
'name' => 'foobar', | ||
'prefix' => 'foo:', | ||
'storage_type' => 'json' | ||
], | ||
'fields' => [ | ||
'$.id' => [ | ||
'type' => 'text', | ||
'alias' => 'foo', | ||
], | ||
'$.count' => [ | ||
'type' => 'numeric', | ||
], | ||
'$.id_embeddings' => [ | ||
'type' => 'vector', | ||
'algorithm' => 'flat', | ||
'dims' => 3, | ||
'datatype' => 'float32', | ||
'distance_metric' => 'cosine', | ||
] | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testCreatesHashIndexWithMultipleFields(): void | ||
{ | ||
$index = new SearchIndex($this->client, $this->hashSchema); | ||
$this->assertEquals('OK', $index->create()); | ||
|
||
$indexInfo = $this->client->ftinfo($this->hashSchema['index']['name']); | ||
|
||
$this->assertEquals('foobar', $indexInfo[1]); | ||
$this->assertEquals('HASH', $indexInfo[5][1]); | ||
$this->assertEquals('foo:', $indexInfo[5][3][0]); | ||
$this->assertEquals('foo', $indexInfo[7][0][3]); | ||
$this->assertEquals('TEXT', $indexInfo[7][0][5]); | ||
$this->assertEquals('NUMERIC', $indexInfo[7][1][5]); | ||
$this->assertEquals('VECTOR', $indexInfo[7][2][5]); | ||
|
||
$this->assertEquals( | ||
'OK', | ||
$index->load('foo:1', ['id' => '1', 'count' => 10, 'id_embeddings' => VectorHelper::toBytes([0.000001, 0.000002, 0.000003])]) | ||
); | ||
|
||
$searchResult = $this->client->ftsearch($this->hashSchema['index']['name'], '*'); | ||
|
||
$this->assertSame('foo:1', $searchResult[1]); | ||
$this->assertSame('1', $searchResult[2][1]); | ||
$this->assertSame('10', $searchResult[2][3]); | ||
$this->assertSame(VectorHelper::toBytes([0.000001, 0.000002, 0.000003]), $searchResult[2][5]); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testCreatesJsonIndexWithMultipleFields(): void | ||
{ | ||
$index = new SearchIndex($this->client, $this->jsonSchema); | ||
$this->assertEquals('OK', $index->create()); | ||
|
||
$indexInfo = $this->client->ftinfo($this->jsonSchema['index']['name']); | ||
|
||
$this->assertEquals('foobar', $indexInfo[1]); | ||
$this->assertEquals('JSON', $indexInfo[5][1]); | ||
$this->assertEquals('foo:', $indexInfo[5][3][0]); | ||
$this->assertEquals('foo', $indexInfo[7][0][3]); | ||
$this->assertEquals('TEXT', $indexInfo[7][0][5]); | ||
$this->assertEquals('NUMERIC', $indexInfo[7][1][5]); | ||
$this->assertEquals('VECTOR', $indexInfo[7][2][5]); | ||
|
||
$this->assertEquals( | ||
'OK', | ||
$index->load('foo:1', '{"id":"1","count":10,"id_embeddings":[0.000001, 0.000002, 0.000003]}') | ||
); | ||
|
||
$searchResult = $this->client->ftsearch($this->jsonSchema['index']['name'], '*'); | ||
|
||
$this->assertSame('foo:1', $searchResult[1]); | ||
$this->assertSame('{"id":"1","count":10,"id_embeddings":[1e-6,2e-6,3e-6]}', $searchResult[2][1]); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testCreatesHashIndexWithOverride(): void | ||
{ | ||
$index = new SearchIndex($this->client, $this->hashSchema); | ||
$this->assertEquals('OK', $index->create()); | ||
|
||
$indexInfo = $this->client->ftinfo($this->hashSchema['index']['name']); | ||
|
||
$this->assertEquals('foobar', $indexInfo[1]); | ||
$this->assertEquals('HASH', $indexInfo[5][1]); | ||
$this->assertEquals('foo:', $indexInfo[5][3][0]); | ||
$this->assertEquals('foo', $indexInfo[7][0][3]); | ||
$this->assertEquals('TEXT', $indexInfo[7][0][5]); | ||
$this->assertEquals('NUMERIC', $indexInfo[7][1][5]); | ||
$this->assertEquals('VECTOR', $indexInfo[7][2][5]); | ||
|
||
$this->assertEquals('OK', $index->create(true)); | ||
|
||
$this->assertEquals( | ||
'OK', | ||
$index->load('foo:1', ['id' => '1', 'count' => 10, 'id_embeddings' => VectorHelper::toBytes([0.000001, 0.000002, 0.000003])]) | ||
); | ||
|
||
$searchResult = $this->client->ftsearch($this->hashSchema['index']['name'], '*'); | ||
|
||
$this->assertSame('foo:1', $searchResult[1]); | ||
$this->assertSame('1', $searchResult[2][1]); | ||
$this->assertSame('10', $searchResult[2][3]); | ||
$this->assertSame(VectorHelper::toBytes([0.000001, 0.000002, 0.000003]), $searchResult[2][5]); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testFetchHashData(): void | ||
{ | ||
$index = new SearchIndex($this->client, $this->hashSchema); | ||
$this->assertEquals('OK', $index->create()); | ||
|
||
$this->assertEquals( | ||
'OK', | ||
$index->load('foo:1', ['id' => '1', 'count' => 10, 'id_embeddings' => VectorHelper::toBytes([0.000001, 0.000002, 0.000003])]) | ||
); | ||
|
||
$this->assertEquals( | ||
['id' => '1', 'count' => 10, 'id_embeddings' => VectorHelper::toBytes([0.000001, 0.000002, 0.000003])], | ||
$index->fetch('1')); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testFetchJsonData(): void | ||
{ | ||
$index = new SearchIndex($this->client, $this->jsonSchema); | ||
$this->assertEquals('OK', $index->create()); | ||
|
||
$this->assertEquals( | ||
'OK', | ||
$index->load('foo:1', '{"id":"1","count":10,"id_embeddings":[0.000001, 0.000002, 0.000003]}') | ||
); | ||
|
||
$this->assertEquals( | ||
'{"id":"1","count":10,"id_embeddings":[1e-6,2e-6,3e-6]}', | ||
$index->fetch('1')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace Unit\Index; | ||
namespace Vladvildanov\PredisVl\Unit\Index; | ||
|
||
use Exception; | ||
use Mockery; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.