Improve FnType to add a body or block

Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
David Arendsen 2023-03-08 21:20:20 +01:00
commit ec73c70706
4 changed files with 56 additions and 26 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Type\FnType;
use PHPUnit\Framework\TestCase;
final class AggregateWindowFunctionTest extends TestCase
@ -20,18 +21,22 @@ final class AggregateWindowFunctionTest extends TestCase
public function testAllParameters()
{
$expression = new AggregateWindow('20s', 'mean', [
'period' => 'every',
'offset' => '0s',
'location' => 'location',
'column' => '_value',
'timeSrc' => '_stop',
'timeDst' => '_time',
'createEmpty' => false
]);
$expression = new AggregateWindow(
'20s',
FnType::params(['r'])->withBody('r._field == "test"'),
[
'period' => 'every',
'offset' => '0s',
'location' => 'location',
'column' => '_value',
'timeSrc' => '_stop',
'timeDst' => '_time',
'createEmpty' => false
]
);
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: mean, location: "location", ' .
'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: (r) => r._field == "test",
location: "location", ' . 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
$this->assertEquals($query, $expression->__toString());
}

View file

@ -81,12 +81,16 @@ final class FactoryTypeTest extends TestCase
'this can be anything'
],
'FnType with only content' => [
new FnType('(r) => ({ r with _value: r._value * r._value })'),
'(r) => ({ r with _value: r._value * r._value })'
FnType::params([])->withBlock('r with _value: r._value * r._value'),
'() => { r with _value: r._value * r._value }'
],
'FnType with params and content' => [
new FnType(['r', 'a'], '({ r with _value: r._value * r._value })'),
'(r, a) => ({ r with _value: r._value * r._value })'
'FnType with params and a body' => [
FnType::params(['r'])->withBody('r with _value: r._value * r._value'),
'(r) => r with _value: r._value * r._value'
],
'FnType with params and a block' => [
FnType::params(['r', 'a'])->withBlock('return r with _value: r._value * r._value'),
'(r, a) => { return r with _value: r._value * r._value }'
],
];
}