1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the tarantool/client package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Tests\Integration\MessagePack; |
15
|
|
|
|
16
|
|
|
use Decimal\Decimal; |
17
|
|
|
use Tarantool\Client\Client; |
18
|
|
|
use Tarantool\Client\Packer\Extension\DecimalExtension; |
19
|
|
|
use Tarantool\Client\Packer\PurePacker; |
20
|
|
|
use Tarantool\Client\Schema\Criteria; |
21
|
|
|
use Tarantool\Client\Tests\Integration\ClientBuilder; |
22
|
|
|
use Tarantool\Client\Tests\Integration\TestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @requires Tarantool >=2.3 |
26
|
|
|
* @requires extension decimal |
27
|
|
|
* |
28
|
|
|
* @lua dec = require('decimal').new('18446744073709551615') |
29
|
|
|
* @lua space = create_space('decimal_primary') |
30
|
|
|
* @lua space:format({{name = 'id', type = 'decimal'}}) |
31
|
|
|
* @lua space:create_index("primary", {parts = {1, 'decimal'}}) |
32
|
|
|
* @lua space:insert({dec}) |
33
|
|
|
*/ |
34
|
|
|
final class DecimalExtensionTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
public const DECIMAL_BIG_INT = '18446744073709551615'; |
37
|
|
|
private const TARANTOOL_DECIMAL_PRECISION = 38; |
38
|
|
|
|
39
|
|
|
public function testBinarySelectByDecimalKeySucceeds() : void |
40
|
|
|
{ |
41
|
|
|
$client = self::createClientWithDecimalSupport(); |
42
|
|
|
|
43
|
|
|
$decimal = new Decimal(self::DECIMAL_BIG_INT, self::TARANTOOL_DECIMAL_PRECISION); |
44
|
|
|
$space = $client->getSpace('decimal_primary'); |
45
|
|
|
$result = $space->select(Criteria::key([$decimal])); |
46
|
|
|
|
47
|
|
|
self::assertTrue(isset($result[0][0])); |
48
|
|
|
self::assertTrue($decimal->equals($result[0][0])); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @requires Tarantool >=2.10-stable |
53
|
|
|
*/ |
54
|
|
|
public function testSqlSelectByDecimalKeySucceeds() : void |
55
|
|
|
{ |
56
|
|
|
$client = self::createClientWithDecimalSupport(); |
57
|
|
|
|
58
|
|
|
$decimal = new Decimal(self::DECIMAL_BIG_INT, self::TARANTOOL_DECIMAL_PRECISION); |
59
|
|
|
$result = $client->executeQuery('SELECT * FROM "decimal_primary" WHERE "id" = ?', $decimal); |
60
|
|
|
|
61
|
|
|
self::assertFalse($result->isEmpty()); |
62
|
|
|
self::assertTrue($decimal->equals($result->getFirst()['id'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @dataProvider provideDecimalStrings |
67
|
|
|
*/ |
68
|
|
|
public function testLuaPackingAndUnpacking(string $decimalString) : void |
69
|
|
|
{ |
70
|
|
|
$client = self::createClientWithDecimalSupport(); |
71
|
|
|
|
72
|
|
|
[$decimal] = $client->evaluate('return require("decimal").new(...)', $decimalString); |
73
|
|
|
self::assertTrue($decimal->equals($decimalString)); |
74
|
|
|
|
75
|
|
|
[$isEqual] = $client->evaluate( |
76
|
|
|
sprintf("return require('decimal').new('%s') == ...", $decimalString), |
77
|
|
|
new Decimal($decimalString, self::TARANTOOL_DECIMAL_PRECISION) |
78
|
|
|
); |
79
|
|
|
self::assertTrue($isEqual); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function provideDecimalStrings() : iterable |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
['0'], |
86
|
|
|
['-0'], |
87
|
|
|
['42'], |
88
|
|
|
['-127'], |
89
|
|
|
['0.0'], |
90
|
|
|
['00000.0000000'], |
91
|
|
|
['00009.9000000'], |
92
|
|
|
['1.000000099'], |
93
|
|
|
['4.2'], |
94
|
|
|
['1E-10'], |
95
|
|
|
['-2E-15'], |
96
|
|
|
['0.0000234'], |
97
|
|
|
[str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION)], |
98
|
|
|
['-'.str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION)], |
99
|
|
|
['0.'.str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION)], |
100
|
|
|
[str_repeat('1', self::TARANTOOL_DECIMAL_PRECISION).'.0'], |
101
|
|
|
['9.'.str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1)], |
102
|
|
|
[str_repeat('9', self::TARANTOOL_DECIMAL_PRECISION - 1).'.9'], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testBigIntegerUnpacksToDecimal() : void |
107
|
|
|
{ |
108
|
|
|
$client = self::createClientWithDecimalSupport(); |
109
|
|
|
[$number] = $client->evaluate('return 18446744073709551615ULL'); |
110
|
|
|
|
111
|
|
|
self::assertInstanceOf(Decimal::class, $number); |
112
|
|
|
self::assertTrue((new Decimal('18446744073709551615'))->equals($number)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @dataProvider \Tarantool\Client\Tests\PackerDataProvider::providePurePackerWithDefaultSettings() |
117
|
|
|
*/ |
118
|
|
|
public function testPurePackerUnpacksBigIntToDecimal(PurePacker $packer) : void |
119
|
|
|
{ |
120
|
|
|
$client = ClientBuilder::createFromEnv() |
121
|
|
|
->setPackerFactory(static function () use ($packer) { return $packer; }) |
122
|
|
|
->build(); |
123
|
|
|
|
124
|
|
|
[$number] = $client->evaluate(sprintf('return %sULL', self::DECIMAL_BIG_INT)); |
125
|
|
|
|
126
|
|
|
self::assertInstanceOf(Decimal::class, $number); |
127
|
|
|
self::assertTrue((new Decimal(self::DECIMAL_BIG_INT))->equals($number)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private static function createClientWithDecimalSupport() : Client |
131
|
|
|
{ |
132
|
|
|
return ClientBuilder::createFromEnv() |
133
|
|
|
->setPackerFactory(static function () { |
134
|
|
|
return PurePacker::fromExtensions(new DecimalExtension()); |
135
|
|
|
}) |
136
|
|
|
->build(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|