From b2fa62ec5aa83917040c94303a9a5234ba7c40a3 Mon Sep 17 00:00:00 2001 From: Yurii Myronchuk Date: Fri, 23 Oct 2026 18:25:57 +0200 Subject: [PATCH] HQD-588: add a unit tree for request rate (RPS) prices Add the rps tree: rps (base unit, factor 1) and krps (factor 1000), following the same shape as every other tree (bps, power, ...). There is no generator for units-tree.php, so it is hand-edited to match units-tree.yml. Add a float assertion to UnitTest covering 1500 rps = 1.5 krps - the existing assertConvert() helper only compares integer factors. Add UnitsTreeConsistencyTest to catch the yml/php trees drifting apart in the future, since nothing else enforces that they agree. --- res/units-tree.php | 10 ++++++ res/units-tree.yml | 3 ++ tests/unit/UnitTest.php | 11 +++++++ tests/unit/UnitsTreeConsistencyTest.php | 44 +++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 tests/unit/UnitsTreeConsistencyTest.php diff --git a/res/units-tree.php b/res/units-tree.php index 78c6c39..2a5d232 100644 --- a/res/units-tree.php +++ b/res/units-tree.php @@ -346,4 +346,14 @@ 'parent' => 'power', 'factor' => 1000, ), + 'rps' => + array ( + 'parent' => '', + 'factor' => 1, + ), + 'krps' => + array ( + 'parent' => 'rps', + 'factor' => 1000, + ), ); diff --git a/res/units-tree.yml b/res/units-tree.yml index 0944dea..88a4cd9 100644 --- a/res/units-tree.yml +++ b/res/units-tree.yml @@ -74,3 +74,6 @@ power: W: 1 kw: 1000 kW: 1000 + +rps: + krps: 1000 diff --git a/tests/unit/UnitTest.php b/tests/unit/UnitTest.php index 0801fe0..88aed96 100644 --- a/tests/unit/UnitTest.php +++ b/tests/unit/UnitTest.php @@ -41,6 +41,9 @@ protected function setUp(): void $this->usd = Unit::usd(); $this->eur = Unit::eur(); + + $this->rps = Unit::rps(); + $this->krps = Unit::krps(); } public function testSame() @@ -106,6 +109,14 @@ public function testConvertThroughRoot() $this->assertConvert(1000, $this->mb, $this->gb); } + public function testConvertFractional() + { + // assertConvert() only compares integer factors; a request rate price is + // often fractional once converted to its billing unit (1500 rps = 1.5 krps). + $this->assertSame(1.5, $this->rps->convert($this->krps, 1500)); + $this->assertSame(1500.0, $this->krps->convert($this->rps, 1.5)); + } + public function testUnknownUnit() { $unknown = Unit::create('unknown'); diff --git a/tests/unit/UnitsTreeConsistencyTest.php b/tests/unit/UnitsTreeConsistencyTest.php new file mode 100644 index 0000000..cf9a927 --- /dev/null +++ b/tests/unit/UnitsTreeConsistencyTest.php @@ -0,0 +1,44 @@ + $children) { + // Every yaml root is also a directly usable unit, with itself as parent. + $expected[$root] = ['parent' => '', 'factor' => 1]; + foreach ($children as $name => $factor) { + $expected[$name] = ['parent' => $root, 'factor' => $factor]; + } + } + + ksort($expected); + $actual = $php; + ksort($actual); + + $this->assertSame($expected, $actual, 'units-tree.yml and units-tree.php are out of sync; there is no generator, so both must be edited by hand.'); + } +}