Skip to content

Commit

Permalink
Test for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kafkiansky committed Jun 30, 2024
1 parent 1ae89cc commit 754930a
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/click.dump

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions bin/click_memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

require_once __DIR__.'/vendor/autoload.php';

use Kafkiansky\PHPClick;

PHPClick\rowsToFile(
__DIR__.'/click.log',
PHPClick\Row::columns(
PHPClick\Column::string('users'),
PHPClick\Column::dateTime64(new \DateTimeImmutable()),
PHPClick\Column::map(
[PHPClick\Column::string('x'), PHPClick\Column::string('y')],
),
PHPClick\Column::nullable(
PHPClick\Column::int64(13),
),
),
PHPClick\Row::columns(
PHPClick\Column::string('users'),
PHPClick\Column::dateTime64(new \DateTimeImmutable()),
PHPClick\Column::map(
[PHPClick\Column::string('x'), PHPClick\Column::string('y')],
),
PHPClick\Column::nullable(),
),
);

$client = new PHPClick\Client('http://127.0.0.1:8124/');

$logHandle = fopen(__DIR__.'/click.log', 'r');

try {
$client->insert(
PHPClick\Query::builder('test.logs')->columns(
'LogName',
'LogTime',
'LogAttributes',
'LogUserId',
),
PHPClick\Batch::fromStream($logHandle),
);
} finally {
if (\is_resource($logHandle)) {
\fclose($logHandle);
}
}
81 changes: 81 additions & 0 deletions bin/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Kafkiansky\PHPClick\Column;
use Kafkiansky\PHPClick\Row;

require_once __DIR__.'/../vendor/autoload.php';

$row = Row::columns(
Column::string('KEK'),
Column::string('3e9f74da-7d69-48c9-bc1a-4b2af2d3f487'),
Column::string('payment_succeeded'),
Column::dateTime(new \DateTimeImmutable('NOW')),
Column::map(
[Column::string('x'), Column::string('y')]
),
);

$rows = [];

for ($i = 0; $i < 1_000_000; $i++) {
$rows[] = Row::columns(
Column::string('KEK'),
Column::string('3e9f74da-7d69-48c9-bc1a-4b2af2d3f487'),
Column::string('payment_succeeded'),
Column::dateTime(new \DateTimeImmutable('NOW')),
Column::map(
[Column::string('x'), Column::string('y')]
),
);
}

$client = new \Kafkiansky\PHPClick\Client('http://127.0.0.1:8124/');

$client->insert(
\Kafkiansky\PHPClick\Query::string('INSERT INTO proto.logs (LogName, LogTime, LogAttributes, LogUserId)'),
\Kafkiansky\PHPClick\Batch::fromRows(
Row::columns(
Column::string('users'),
Column::dateTime64(new \DateTimeImmutable()),
Column::map(
[Column::string('x'), Column::string('y')],
),
Column::nullable(
Column::int64(13),
),
),
Row::columns(
Column::string('users'),
Column::dateTime64(new \DateTimeImmutable()),
Column::map(
[Column::string('x'), Column::string('y')],
),
Column::nullable(),
),
),
);
dd(1);

$buffer = \Kafkiansky\Binary\Buffer::empty(\Kafkiansky\Binary\Endianness::little());
$row->writeToBuffer($buffer);
$row->writeToBuffer($buffer);
$row->writeToBuffer($buffer);

$client = (new HttpClientBuilder())
// ->intercept(new \Amp\Http\Client\Interceptor\ModifyRequest(function (Request $request): Request {
// $request->setUri('http://127.0.0.1:8124');
// return $request;
// }))
->build()
;

$uri = \sprintf("http://127.0.0.1:8124/?input_format_values_interpret_expressions=0&query=%s", rawurlencode('INSERT INTO proto.events (AppId, SessionId, EventName, EventTime, EventAttributes) FORMAT RowBinary'));

$fp = \fopen(__DIR__.'/click.dump', 'a+');

$req = new Request($uri, 'POST', $buffer->reset());

$response = $client->request($req, new \Amp\TimeoutCancellation(1));
dd($response->getStatus(), $response->getHeader('X-ClickHouse-Exception-Code'), $response->getBody()->buffer(limit: 1024));
8 changes: 8 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
php:
build:
context: ./docker/php
dockerfile: Dockerfile
volumes:
- ./:/var/www
command: tail -f /dev/null
34 changes: 34 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM php:8.3-cli-alpine

RUN set -ex && \
apk --no-cache --allow-untrusted add --update \
g++ \
gcc \
zlib \
linux-headers \
make \
icu-dev \
git \
bash \
zip \
autoconf \
libzip-dev \
zlib-dev \
libsodium \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
libsodium-dev && \
docker-php-source extract && \
docker-php-ext-install bcmath intl && \
docker-php-ext-configure sodium && \
docker-php-ext-configure pcntl --enable-pcntl && \
docker-php-ext-configure zip && \
rm -rf /var/cache/apk/* && \
docker-php-source delete

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=2.7.6 \
&& chmod +x /usr/local/bin/composer \
&& composer clear-cache

WORKDIR /var/www
Empty file removed tests/.gitignore
Empty file.
69 changes: 69 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Kafkiansky\PHPClick\Tests;

use Amp\Http\Client\DelegateHttpClient;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Kafkiansky\Binary\Buffer;
use Kafkiansky\Binary\Endianness;
use Kafkiansky\PHPClick\Batch;
use Kafkiansky\PHPClick\Client;
use Kafkiansky\PHPClick\Column;
use Kafkiansky\PHPClick\Exception\QueryCannotBeExecuted;
use Kafkiansky\PHPClick\Query;
use Kafkiansky\PHPClick\Row;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\TestCase;

#[CoversClass(Client::class)]
final class ClientTest extends TestCase
{
public function testQueryExecuted(): void
{
$httpClient = $this->createMock(DelegateHttpClient::class);
$httpClient
->expects(self::once())
->method('request')
->with(new Callback(function (Request $request): bool {
$buf = Buffer::empty(Endianness::little());
Column::string('test')->write($buf);

self::assertSame(sprintf('http://127.0.0.1:8123/?input_format_values_interpret_expressions=0&query=%s', \rawurlencode('INSERT INTO test.logs(LogName) Format RowBinary')), (string)$request->getUri());
self::assertSame($buf->reset(), $request->getBody()->getContent()->read());

return true;
}))
->willReturn(new Response(protocolVersion: '1.1', status: 200, reason: null, headers: [], body: null, request: new Request('')))
;

$client = new Client(
'http://127.0.0.1:8123',
$httpClient,
);

$client->insert(Query::string('INSERT INTO test.logs(LogName)'), Batch::fromRows(Row::columns(Column::string('test'))));
}

public function testQueryFailed(): void
{
$httpClient = $this->createMock(DelegateHttpClient::class);
$httpClient
->expects(self::once())
->method('request')
->willReturn(new Response(protocolVersion: '1.1', status: 500, reason: null, headers: ['X-ClickHouse-Exception-Code' => '16'], body: 'Code: 16. DB::Exception: No such column LogName in table test.logs (af235e5b-cada-47b6-a0ac-7872b09843cf). (NO_SUCH_COLUMN_IN_TABLE) (version 24.5.1.1763 (official build))', request: new Request('')))
;

$client = new Client(
'http://127.0.0.1:8123',
$httpClient,
);

self::expectException(QueryCannotBeExecuted::class);
self::expectExceptionMessage('INSERT INTO test.logs(LogName)" cannot be executed due to exception (16): "Code: 16. DB::Exception: No such column LogName in table test.logs (af235e5b-cada-47b6-a0ac-7872b09843cf). (NO_SUCH_COLUMN_IN_TABLE) (version 24.5.1.1763 (official build))".');
$client->insert(Query::string('INSERT INTO test.logs(LogName)'), Batch::fromRows(Row::columns(Column::string('test'))));
}
}

0 comments on commit 754930a

Please sign in to comment.