2022-08-10 16:09:29 +00:00
|
|
|
<?php
|
2022-08-15 20:28:25 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
declare( strict_types=1 );
|
2022-08-10 16:09:29 +00:00
|
|
|
|
2022-08-15 20:28:25 +00:00
|
|
|
namespace Tests\Functions;
|
|
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
use Hosterra\FluxBuilder\Functions\Map;
|
|
|
|
|
use Hosterra\FluxBuilder\Expression\Map as MapExpression;
|
|
|
|
|
use Hosterra\FluxBuilder\Expression\Selection as SelectionExpression;
|
2022-08-10 16:09:29 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
final class MapFunctionTest extends TestCase {
|
|
|
|
|
public function testSimpleMap() {
|
|
|
|
|
$expression = new Map( 'r with name: r.user' );
|
2022-08-10 16:09:29 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
$query = '|> map(fn: (r) => ({ r with name: r.user })) ';
|
2022-08-10 16:09:29 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
$this->assertEquals( $query, $expression->__toString() );
|
|
|
|
|
}
|
2022-10-25 17:55:35 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
public function testWithMapObject() {
|
|
|
|
|
$expression = new Map( MapExpression::with( 'name', 'r.user' ) );
|
2022-10-25 17:55:35 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
$query = '|> map(fn: (r) => ({ r with name: r.user })) ';
|
2022-10-25 17:55:35 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
$this->assertEquals( $query, $expression->__toString() );
|
|
|
|
|
}
|
2022-10-25 17:55:35 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
public function testRecordMapObject() {
|
|
|
|
|
$expression = new Map( MapExpression::columns( [
|
|
|
|
|
'time' => 'r._time',
|
|
|
|
|
'source' => 'r.tag',
|
|
|
|
|
'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
|
|
|
|
2024-03-30 16:44:38 +01: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"} })) ';
|
2022-10-25 17:55:35 +00:00
|
|
|
|
2024-03-30 16:44:38 +01:00
|
|
|
$this->assertEquals( $query, $expression->__toString() );
|
|
|
|
|
}
|
2022-08-15 20:28:25 +00:00
|
|
|
}
|