2022-08-10 16:09:29 +00:00
|
|
|
<?php
|
2022-08-15 20:28:25 +00:00
|
|
|
|
2022-08-10 16:09:29 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-08-15 20:28:25 +00:00
|
|
|
namespace Tests\Functions;
|
|
|
|
|
|
2022-08-12 12:37:30 +00:00
|
|
|
use Arendsen\FluxQueryBuilder\Functions\Map;
|
2022-10-25 17:55:35 +00:00
|
|
|
use Arendsen\FluxQueryBuilder\Expression\Map as MapExpression;
|
|
|
|
|
use Arendsen\FluxQueryBuilder\Expression\Selection as SelectionExpression;
|
2022-08-10 16:09:29 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
2022-08-15 20:28:25 +00:00
|
|
|
final class MapFunctionTest extends TestCase
|
|
|
|
|
{
|
2022-08-10 16:09:29 +00:00
|
|
|
public function testSimpleMap()
|
|
|
|
|
{
|
|
|
|
|
$expression = new Map('r with name: r.user');
|
|
|
|
|
|
|
|
|
|
$query = '|> map(fn: (r) => ({ r with name: r.user })) ';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($query, $expression->__toString());
|
|
|
|
|
}
|
2022-10-25 17:55:35 +00:00
|
|
|
|
|
|
|
|
public function testWithMapObject()
|
|
|
|
|
{
|
|
|
|
|
$expression = new Map(MapExpression::with('name', 'r.user'));
|
|
|
|
|
|
|
|
|
|
$query = '|> map(fn: (r) => ({ r with name: r.user })) ';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($query, $expression->__toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRecordMapObject()
|
|
|
|
|
{
|
|
|
|
|
$expression = new Map(MapExpression::columns([
|
|
|
|
|
'time' => 'r._time',
|
|
|
|
|
'source' => 'r.tag',
|
2023-01-12 12:11:28 +00:00
|
|
|
'alert' => SelectionExpression::if('r._value > 10')->then(true)->else(false)->__toString(),
|
|
|
|
|
'test' => SelectionExpression::if('r._value > 10')->then('yes')->else('no')->__toString()
|
|
|
|
|
])->__toString());
|
2022-10-25 17:55:35 +00:00
|
|
|
|
|
|
|
|
$query = '|> map(fn: (r) => ({ {time: r._time, source: r.tag, ' .
|
|
|
|
|
'alert: if r._value > 10 then true else false, ' .
|
|
|
|
|
'test: if r._value > 10 then "yes" else "no"} })) ';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($query, $expression->__toString());
|
|
|
|
|
}
|
2022-08-15 20:28:25 +00:00
|
|
|
}
|