2022-08-05 15:41:38 +00:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Arendsen\FluxQueryBuilder\QueryBuilder;
|
|
|
|
|
|
|
|
|
|
final class QueryBuilderTest extends TestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-08 14:42:10 +00:00
|
|
|
* @dataProvider simpleQueryProvider
|
2022-08-05 15:41:38 +00:00
|
|
|
*/
|
2022-08-08 14:42:10 +00:00
|
|
|
public function testSimpleQuery($bucket, $measurement, $range, $expectedQuery)
|
2022-08-05 15:41:38 +00:00
|
|
|
{
|
|
|
|
|
$queryBuilder = new QueryBuilder();
|
|
|
|
|
$queryBuilder->from($bucket)
|
|
|
|
|
->fromMeasurement($measurement)
|
|
|
|
|
->addRangeStart($range);
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
2022-08-05 15:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
public function simpleQueryProvider(): array
|
2022-08-05 15:41:38 +00:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'case 1' => [
|
|
|
|
|
[
|
2022-08-08 14:42:10 +00:00
|
|
|
'bucket' => 'example_bucket',
|
2022-08-05 15:41:38 +00:00
|
|
|
],
|
|
|
|
|
'test_measurement',
|
|
|
|
|
'-360h',
|
2022-08-08 14:42:10 +00:00
|
|
|
'from(bucket: "example_bucket") |> range(start: "-360h") |> filter(fn: (r) => r._measurement == "test_measurement") '
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider throwsExceptionWithoutRequiredDataProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testThrowsExceptionWithoutRequiredData($from, $measurement, $range)
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
|
|
|
|
|
$queryBuilder = new QueryBuilder();
|
|
|
|
|
|
|
|
|
|
if($from) {
|
|
|
|
|
$queryBuilder->from($from);
|
|
|
|
|
}
|
|
|
|
|
if($measurement) {
|
|
|
|
|
$queryBuilder->fromMeasurement($measurement);
|
|
|
|
|
}
|
|
|
|
|
if($range) {
|
|
|
|
|
$queryBuilder->addRange($range);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$queryBuilder->build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function throwsExceptionWithoutRequiredDataProvider(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'without from data' => [
|
|
|
|
|
null, 'test_measurement', ['start' => '-360h'],
|
|
|
|
|
],
|
|
|
|
|
'without measurement data' => [
|
|
|
|
|
['from' => 'test_bucket'], null, ['start' => '-360h'],
|
|
|
|
|
],
|
|
|
|
|
'without range data' => [
|
|
|
|
|
['from' => 'test_bucket'], 'test_measurement', null,
|
2022-08-05 15:41:38 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|