Skip to content

Commit

Permalink
feat: index manager
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 7, 2024
1 parent fc36745 commit 2d12ee1
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 8 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ $query = (new Query())
$results = $client->search($query);
```

## Managing your index

```php
use OramaCloud\Manager\CloudManager;
use OramaCloud\Manager\IndexManager;

$apiKey = 'HDks8Hkao82-ha9daj';
$manager = new CloudManager($apiKey);

$indexId = 'h7asdjk9d12kdlofabsha123';
$index = new IndexManager($indexId, $manager);

// Empty data
$index->empty();

// Insert records
$index->insert([ 'id' => 1, 'name' => 'John Doe', 'age' => 20 ]);

// Update record
$index->update([ 'id' => 1, 'name' => 'Jane Doe', 'age' => 30 ]);

// Delete record
$index->delete([ 'id' => 1 ]);

// Trigger deployment
$index->deploy();
```

## Run Tests

```sh
Expand Down
13 changes: 6 additions & 7 deletions src/Manager/CloudManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ class CloudManager
private $apiKey;
private $http;

public function __construct($apiKey)
public function __construct($apiKey, $http = null)
{
$this->apiKey = $apiKey;
$this->http = new HttpClient();
}

public function index(string $indexId): IndexManager
{
return new IndexManager($indexId, $this);
$this->http = is_null($http) ? new HttpClient() : $http;
}

public function setIndexId(string $id): void
Expand All @@ -29,6 +24,10 @@ public function setIndexId(string $id): void

public function callIndexWebhook(string $endpoint, $payload = null)
{
if (!$this->indexId) {
throw new \Exception('Index ID is not set');
}

$config = [
'headers' => [
'Content-Type' => 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function deploy()
public function hasPendingOperations()
{
$response = $this->callIndexWebhook(Endpoints::HAS_DATA);
return $response->hasData;
return property_exists($response, 'hasData') ? $response->hasData : false;
}

private function checkIndexID()
Expand Down
114 changes: 114 additions & 0 deletions tests/Feature/IndexManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use OramaCloud\Manager\CloudManager;
use OramaCloud\Manager\IndexManager;

beforeEach(function () {
// Reset capturedRequests before each test
$this->capturedRequests = [];

// Middleware to capture requests
$captureMiddleware = function (callable $handler) {
return function ($request, array $options) use ($handler) {
$this->capturedRequests[] = $request;
return $handler($request, $options);
};
};

$mockResponse = new MockHandler([
new Response(200, [], json_encode(['success' => true]))
]);

$handlerStack = HandlerStack::create($mockResponse);
$handlerStack->push($captureMiddleware, 'capture_middleware');
$mockClient = new GuzzleClient(['handler' => $handlerStack]);

$this->manager = new CloudManager('mock-api-key', $mockClient);
$this->index = new IndexManager('mock-index', $this->manager);
});

describe('Index manager', function () {
it('should empty the index', function () {
// Empty index
$result = $this->index->empty();
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/snapshot');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});

it('should insert a document', function () {
$data = ['id' => 1, 'name' => 'John Doe'];
// Insert document
$result = $this->index->insert($data);
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['upsert' => $data]);
});

it('should update a document', function () {
$data = ['id' => 1, 'name' => 'Jane Doe'];
// Update document
$result = $this->index->update($data);
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['upsert' => $data]);
});

it('should delete a document', function () {
// Delete index
$result = $this->index->delete(['id' => 1]);
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/notify');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(['remove' => ['id' => 1]]);
});

it('should snapshot the index', function () {
$data = ['id' => 1, 'name' => 'John Doe'];
// Snapshot index
$result = $this->index->snapshot($data);
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/snapshot');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe($data);
});

it('should deploy the index', function () {
// Deploy index
$result = $this->index->deploy();
expect($result)->toHaveKey('success');

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/deploy');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});

it('should check for pending operations', function () {
// Check for pending operations
$result = $this->index->hasPendingOperations();
expect($result)->toBe(false);

$lastRequest = end($this->capturedRequests);
expect($lastRequest->getMethod())->toBe('POST');
expect($lastRequest->getUri()->getPath())->toBe('/api/v1/webhooks/mock-index/has-data');
expect(json_decode($lastRequest->getBody()->getContents(), true))->toBe(null);
});
});

0 comments on commit 2d12ee1

Please sign in to comment.