From 8b673b56f0568e9e07a1426a3c5ff112ba582c12 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Mon, 6 Feb 2023 12:30:57 +0200 Subject: [PATCH] Extended support by implementing TDIGEST.MAX command (#1099) * Added support for TDIGEST.MAX command * Codestyle fixes --------- Co-authored-by: Vladyslav Vildanov --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/TDigest/TDIGESTMAX.php | 28 ++++++ .../Command/Redis/TDigest/TDIGESTMAX_Test.php | 92 +++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/Command/Redis/TDigest/TDIGESTMAX.php create mode 100644 tests/Predis/Command/Redis/TDigest/TDIGESTMAX_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 57089101..f489ddc2 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -179,6 +179,7 @@ use Predis\Command\CommandInterface; * @method $this tdigestcdf(string $key, int ...$value) * @method $this tdigestcreate(string $key, int $compression = 0) * @method $this tdigestinfo(string $key) + * @method $this tdigestmax(string $key) * @method $this zadd($key, array $membersAndScoresDictionary) * @method $this zcard($key) * @method $this zcount($key, $min, $max) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 134d3284..edc5fa1f 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -189,6 +189,7 @@ use Predis\Response\Status; * @method array tdigestcdf(string $key, int ...$value) * @method Status tdigestcreate(string $key, int $compression = 0) * @method array tdigestinfo(string $key) + * @method string tdigestmax(string $key) * @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null) * @method int xdel(string $key, string ...$id) * @method int xlen(string $key) diff --git a/src/Command/Redis/TDigest/TDIGESTMAX.php b/src/Command/Redis/TDigest/TDIGESTMAX.php new file mode 100644 index 00000000..5dc86410 --- /dev/null +++ b/src/Command/Redis/TDigest/TDIGESTMAX.php @@ -0,0 +1,28 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion >= 2.4.0 + */ + public function testReturnsMaxValueFromGivenSketch(): void + { + $redis = $this->getClient(); + + $redis->tdigestcreate('key'); + $redis->tdigestcreate('empty_key'); + + $redis->tdigestadd('key', 3, 2, 4, 5, 1); + + $actualResponse = $redis->tdigestmax('key'); + + $this->assertSame('5', $actualResponse); + $this->assertSame('nan', $redis->tdigestmax('empty_key')); + } + + /** + * @group connected + * @return void + * @requiresRedisBfVersion >= 2.4.0 + */ + public function testThrowsExceptionOnNonExistingTDigestSketch(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('ERR T-Digest: key does not exist'); + + $redis->tdigestmax('key'); + } +}