Skip to content

Commit

Permalink
- removed duplicate !array check in for clause
Browse files Browse the repository at this point in the history
- fixed array of predicates falsely being recognized as a single predicate instead of a group.
- SupportCommandsTest correctly covers bindArrayValues
- added tests for unbounded and time based aggregation window queries.
  • Loading branch information
LaravelFreelancerNL committed Oct 19, 2021
1 parent af9ee29 commit 5e0a37a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/AQL/HasQueryClauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause;
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
use phpDocumentor\Reflection\Types\ArrayKey;

/**
* Trait hasQueryClauses
Expand Down Expand Up @@ -192,7 +191,6 @@ public function aggregate(
* @link https://www.arangodb.com/docs/stable/aql/operations-sort.html
*
* @param mixed ...$references
* @return QueryBuilder
*/
public function sort(...$references): self
{
Expand Down
7 changes: 2 additions & 5 deletions src/Clauses/ForClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ class ForClause extends Clause
protected array|Expression|ExpressionInterface|QueryBuilder|string|null $in;

/**
* @param array<string|Expression>|string|Expression $variables
* @param array<string|Expression> $variables
* @param array<mixed>|string|QueryBuilder|Expression|null $in
*/
public function __construct(
array|string|Expression $variables,
array $variables,
array|string|QueryBuilder|Expression $in = null
) {
parent::__construct();

if (!is_array($variables)) {
$variables = [$variables];
}
$this->variables = $variables;

$this->in = $in;
Expand Down
7 changes: 6 additions & 1 deletion src/Traits/ValidatesPredicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public function isPredicate(mixed $value): bool
return true;
}

if (is_array($value) && isset($value[0]) && ! is_array($value[0])) {
if (
is_array($value)
&& isset($value[0])
&& ! is_array($value[0])
&& ! $value[0] instanceof PredicateExpression
) {
return true;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/AQL/QueryClausesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,29 @@ public function testWindowClauseWithRange()
$result->query
);
}

public function testWindowClauseUnbounded()
{
$result = (new QueryBuilder())
->for('t', 'observations')
->window(['preceding' => 'unbounded', 'following' => 10], 't.time')
->get();

self::assertEquals(
'FOR t IN observations WINDOW t.time WITH {"preceding":"unbounded","following":10}',
$result->query
);
}
public function testWindowClauseDurationBasedAggregation()
{
$qb = new QueryBuilder();
$qb->for('t', 'observations')
->window(['preceding' => 'PT30M'], $qb->dateTimestamp('t.time'))
->get();

self::assertEquals(
'FOR t IN observations WINDOW DATE_TIMESTAMP(t.time) WITH {"preceding":"PT30M"}',
$qb->get()->query
);
}
}
1 change: 1 addition & 0 deletions tests/Unit/AQL/SupportCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LaravelFreelancerNL\FluentAQL\Tests\TestCase;

/**
* @covers \LaravelFreelancerNL\FluentAQL\QueryBuilder
* @covers \LaravelFreelancerNL\FluentAQL\AQL\HasSupportCommands
* @covers \LaravelFreelancerNL\FluentAQL\Clauses\RawClause
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Expressions/PredicateExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,28 @@ public function testPredicateWithoutRightOperand()

self::assertEquals('x == null', $result);
}

public function testGroupOfPredicateExpressions()
{
$qb = new QueryBuilder();
$predicate1 = new PredicateExpression(
(new LiteralExpression('u.name')),
'==',
'Cookie Monster'
);
$predicate2 = new PredicateExpression(
(new LiteralExpression('u.age')),
'==',
27
);

$result = $qb->for('u', 'users')
->filter([$predicate1, $predicate2])
->return('u');

self::assertEquals(
'FOR u IN users FILTER u.name == @' . $qb->getQueryId() . '_1 AND u.age == 27 RETURN u',
$result->get()->query
);
}
}

0 comments on commit 5e0a37a

Please sign in to comment.