Skip to content

Commit

Permalink
chore(client): improve raw request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imdhemy committed Mar 2, 2024
1 parent aafb793 commit 798308f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 47 deletions.
51 changes: 7 additions & 44 deletions tests/ClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testIndexCannotBeArrayOfNullsForDelete()
}

/** @test */
public function sendRawGetRequest(): void
public function sendRawRequest(): void
{
$client = $this->getClient();

Expand All @@ -165,56 +165,19 @@ public function sendRawGetRequest(): void
}

/** @test */
public function sendRawPostRequest(): void
public function insertDocumentUsingRawRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);
$randomIndex = 'test_index_' .time();

$response = $client->request('POST', "/$index/_doc", ['body' => ['foo' => 'bar']]);
$response = $client->request('POST', "/$randomIndex/_doc", ['body' => ['field' => 'value']]);

$this->assertIsArray($response);
$this->assertArrayHasKey('_index', $response);
$this->assertEquals($index, $response['_index']);
$this->assertSame($randomIndex, $response['_index']);
$this->assertArrayHasKey('_id', $response);
$this->assertArrayHasKey('result', $response);
$this->assertEquals('created', $response['result']);

$client->indices()->delete(['index' => $index]);
}

/** @test */
public function sendRawPutRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);

$response = $client->request('PUT', "/$index/_settings", ['body' => ['index' => ['number_of_replicas' => 2]]]);

$this->assertIsArray($response);
$this->assertArrayHasKey('acknowledged', $response);
$this->assertTrue($response['acknowledged']);

$client->indices()->delete(['index' => $index]);
}

/** @test */
public function sendRawDeleteRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);
$client->index(['index' => $index, 'id' => 1, 'body' => ['foo' => 'bar']]);

$response = $client->request('DELETE', "/$index/_doc/1");

$this->assertIsArray($response);
$this->assertArrayHasKey('_index', $response);
$this->assertEquals($index, $response['_index']);
$this->assertArrayHasKey('result', $response);
$this->assertEquals('deleted', $response['result']);

$client->indices()->delete(['index' => $index]);
$this->assertSame('created', $response['result']);
}

private function getLevelOutput(string $level, array $output): string
Expand Down
57 changes: 54 additions & 3 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

namespace OpenSearch\Tests;

use GuzzleHttp\Ring\Client\MockHandler;
use GuzzleHttp\Ring\Future\FutureArray;
use Mockery as m;
use OpenSearch;
use OpenSearch\Client;
use OpenSearch\ClientBuilder;
use OpenSearch\Common\Exceptions\MaxRetriesException;
use GuzzleHttp\Ring\Client\MockHandler;
use GuzzleHttp\Ring\Future\FutureArray;
use Mockery as m;

/**
* Class ClientTest
Expand Down Expand Up @@ -409,4 +409,55 @@ public function testExtractArgumentIterable()
$this->assertCount(0, $params);
$this->assertInstanceOf(\IteratorIterator::class, $argument);
}

/** @test */
public function sendRawRequest(): void
{
$callable = function () {};
$transport = $this->createMock(OpenSearch\Transport::class);
$client = new OpenSearch\Client($transport, $callable, []);

$transport->expects($this->once())->method('performRequest')->with('GET', '/', [], null, []);

$client->request('GET', '/');
}

/** @test */
public function sendRawRequestWithBody(): void
{
$callable = function () {};
$transport = $this->createMock(OpenSearch\Transport::class);
$client = new OpenSearch\Client($transport, $callable, []);
$body = ['query' => ['match' => ['text_entry' => 'long live king']]];

$transport->expects($this->once())->method('performRequest')->with('GET', '/shakespeare/_search', [], $body, []);

$client->request('GET', '/shakespeare/_search', compact('body'));
}

/** @test */
public function sendRawRequestWithParams(): void
{
$callable = function () {};
$transport = $this->createMock(OpenSearch\Transport::class);
$client = new OpenSearch\Client($transport, $callable, []);
$params = ['foo' => 'bar'];

$transport->expects($this->once())->method('performRequest')->with('GET', '/_search', $params, null, []);

$client->request('GET', '/_search', compact('params'));
}

/** @test */
public function sendRawRequestWithOptions(): void
{
$callable = function () {};
$transport = $this->createMock(OpenSearch\Transport::class);
$client = new OpenSearch\Client($transport, $callable, []);
$options = ['client' => ['future' => 'lazy']];

$transport->expects($this->once())->method('performRequest')->with('GET', '/', [], null, $options);

$client->request('GET', '/', compact('options'));
}
}

0 comments on commit 798308f

Please sign in to comment.