-
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.
- Loading branch information
Showing
9 changed files
with
254 additions
and
272 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 |
---|---|---|
@@ -1,55 +1,62 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use OramaCloud\Client; | ||
use OramaCloud\Client\Query; | ||
|
||
const API_ENDPOINT = 'mock-endpoint'; | ||
const PUBLIC_API_KEY = 'mock-api-key'; | ||
|
||
test('basic fulltext search', function () { | ||
// Create a mock handler and queue a response. | ||
$mock = new MockHandler([ | ||
// initial request to get the collect URL | ||
new Response(200, [], json_encode([ | ||
'collectUrl' => 'mock-url', | ||
'deploymentID' => 'mock-deployment-id', | ||
'index' => 'mock-index', | ||
])), | ||
// search request | ||
new Response(200, [], json_encode([ | ||
'hits' => [['id' => 2]], | ||
'elapsed' => 0.2, | ||
'count' => 1, | ||
])), | ||
// telemetry data collection | ||
new Response(200, [], json_encode([ | ||
'message' => 'Telemetry data collected successfully', | ||
])), | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$mockClient = new GuzzleClient(['handler' => $handlerStack]); | ||
|
||
$client = new Client([ | ||
'api_key' => PUBLIC_API_KEY, | ||
'endpoint' => API_ENDPOINT, | ||
], $mockClient); | ||
|
||
$result = $client->search( | ||
(new Query()) | ||
->term('red shoes') | ||
->mode('fulltext') | ||
->limit(10) | ||
); | ||
|
||
expect($result)->toHaveKey('hits'); | ||
expect($result)->toHaveKey('elapsed'); | ||
expect($result)->toHaveKey('count'); | ||
|
||
expect($result['count'])->toBeGreaterThan(0); | ||
expect(count($result['hits']))->toBeLessThanOrEqual(10); | ||
}); | ||
use Tests\TestCase; | ||
|
||
class ClientTest extends TestCase | ||
{ | ||
const API_ENDPOINT = 'mock-endpoint'; | ||
const PUBLIC_API_KEY = 'mock-api-key'; | ||
|
||
public function testBasicFulltextSearch() | ||
{ | ||
// Create a mock handler and queue a response. | ||
$mock = new MockHandler([ | ||
// initial request to get the collect URL | ||
new Response(200, [], json_encode([ | ||
'collectUrl' => 'mock-url', | ||
'deploymentID' => 'mock-deployment-id', | ||
'index' => 'mock-index', | ||
])), | ||
// search request | ||
new Response(200, [], json_encode([ | ||
'hits' => [['id' => 2]], | ||
'elapsed' => 0.2, | ||
'count' => 1, | ||
])), | ||
// telemetry data collection | ||
new Response(200, [], json_encode([ | ||
'message' => 'Telemetry data collected successfully', | ||
])), | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$mockClient = new GuzzleClient(['handler' => $handlerStack]); | ||
|
||
$client = new Client([ | ||
'api_key' => self::PUBLIC_API_KEY, | ||
'endpoint' => self::API_ENDPOINT, | ||
], $mockClient); | ||
|
||
$result = $client->search( | ||
(new Query()) | ||
->term('red shoes') | ||
->mode('fulltext') | ||
->limit(10) | ||
); | ||
|
||
$this->assertArrayHasKey('hits', $result); | ||
$this->assertArrayHasKey('elapsed', $result); | ||
$this->assertArrayHasKey('count', $result); | ||
|
||
$this->assertGreaterThan(0, $result['count']); | ||
$this->assertLessThanOrEqual(10, count($result['hits'])); | ||
} | ||
} |
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,114 +1,116 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
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); | ||
use Tests\TestCase; | ||
|
||
class IndexManagerTest extends TestCase | ||
{ | ||
protected $manager; | ||
protected $index; | ||
protected $capturedRequests; | ||
|
||
protected function setUp(): void | ||
{ | ||
// Reset capturedRequests before each test | ||
$this->capturedRequests = []; | ||
|
||
// Middleware to capture requests | ||
$captureMiddleware = function ($handler) { | ||
return function ($request, $options) use ($handler) { | ||
$this->capturedRequests[] = $request; | ||
return $handler($request, $options); | ||
}; | ||
}; | ||
}; | ||
|
||
$mockResponse = new MockHandler([ | ||
new Response(200, [], json_encode(['success' => true])) | ||
]); | ||
$mockResponse = new MockHandler([ | ||
new Response(200, [], json_encode(['success' => true])) | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mockResponse); | ||
$handlerStack->push($captureMiddleware, 'capture_middleware'); | ||
$mockClient = new GuzzleClient(['handler' => $handlerStack]); | ||
$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); | ||
}); | ||
$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 () { | ||
public function testShouldEmptyTheIndex() | ||
{ | ||
// Empty index | ||
$result = $this->index->empty(); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->empty(); | ||
$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); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/snapshot', $lastRequest->getUri()->getPath()); | ||
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should insert a document', function () { | ||
public function testShouldInsertADocument() | ||
{ | ||
$data = ['id' => 1, 'name' => 'John Doe']; | ||
// Insert document | ||
$result = $this->index->insert($data); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->insert($data); | ||
$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]); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath()); | ||
$this->assertEquals(['upsert' => $data], json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should update a document', function () { | ||
public function testShouldUpdateADocument() | ||
{ | ||
$data = ['id' => 1, 'name' => 'Jane Doe']; | ||
// Update document | ||
$result = $this->index->update($data); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->update($data); | ||
$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]); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath()); | ||
$this->assertEquals(['upsert' => $data], json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should delete a document', function () { | ||
public function testShouldDeleteADocument() | ||
{ | ||
// Delete index | ||
$result = $this->index->delete(['id' => 1]); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->delete(['id' => 1]); | ||
$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]]); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/notify', $lastRequest->getUri()->getPath()); | ||
$this->assertEquals(['remove' => ['id' => 1]], json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should snapshot the index', function () { | ||
public function testShouldSnapshotTheIndex() | ||
{ | ||
$data = ['id' => 1, 'name' => 'John Doe']; | ||
// Snapshot index | ||
$result = $this->index->snapshot($data); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->snapshot($data); | ||
$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); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/snapshot', $lastRequest->getUri()->getPath()); | ||
$this->assertEquals($data, json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should deploy the index', function () { | ||
public function testShouldDeployTheIndex() | ||
{ | ||
// Deploy index | ||
$result = $this->index->deploy(); | ||
expect($result)->toHaveKey('success'); | ||
|
||
$this->index->deploy(); | ||
$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); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/deploy', $lastRequest->getUri()->getPath()); | ||
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
|
||
it('should check for pending operations', function () { | ||
public function testShouldCheckForPendingOperations() | ||
{ | ||
// Check for pending operations | ||
$result = $this->index->hasPendingOperations(); | ||
expect($result)->toBe(false); | ||
|
||
$this->index->hasPendingOperations(); | ||
$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); | ||
}); | ||
}); | ||
$this->assertEquals('POST', $lastRequest->getMethod()); | ||
$this->assertEquals('/api/v1/webhooks/mock-index/has-data', $lastRequest->getUri()->getPath()); | ||
$this->assertNull(json_decode($lastRequest->getBody()->getContents(), true)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.