Skip to content

Commit

Permalink
chore: add test on queryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv committed Dec 19, 2024
1 parent fe33e63 commit f6da0cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/Agent/src/Utils/Traits/QueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

trait QueryHandler
{
private function executeQuery(string $query, string $connectionName, Permissions $permissions, Caller $caller, ?array $requestContextVariables = []): array
protected function executeQuery(string $query, string $connectionName, Permissions $permissions, Caller $caller, ?array $requestContextVariables = []): array
{
$query = preg_replace('/\s+/', ' ', trim($query));
[$query, $contextVariables] = $this->injectContextVariables($query, $caller, $permissions, $requestContextVariables);
Expand All @@ -29,7 +29,7 @@ private function executeQuery(string $query, string $connectionName, Permissions
return $rootDatasource->executeNativeQuery($connectionName, $query, $contextVariables);
}

private function injectContextVariables(string $query, Caller $caller, Permissions $permissions, ?array $requestContextVariables = []): array
protected function injectContextVariables(string $query, Caller $caller, Permissions $permissions, ?array $requestContextVariables = []): array
{
$user = $permissions->getUserData($caller->getId());
$team = $permissions->getTeam($caller->getRenderingId());
Expand All @@ -41,7 +41,7 @@ private function injectContextVariables(string $query, Caller $caller, Permissio
);
}

private function parseQuerySegment(CollectionContract $collection, Permissions $permissions, Caller $caller): ?ConditionTree
protected function parseQuerySegment(CollectionContract $collection, Permissions $permissions, Caller $caller): ?ConditionTree
{
if (! $this->request->get('segmentQuery')) {
return null;
Expand Down
64 changes: 63 additions & 1 deletion tests/Agent/Routes/Resources/ListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ForestAdmin\AgentPHP\Agent\Utils\ForestSchema\SchemaEmitter;
use ForestAdmin\AgentPHP\DatasourceToolkit\Collection;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Caller;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Nodes\ConditionTreeLeaf;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\Filters\PaginatedFilter;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\Projection\Projection;
Expand All @@ -20,7 +21,6 @@
use function ForestAdmin\config;

use GuzzleHttp\Psr7\Response;

use Prophecy\Argument;
use Prophecy\Prophet;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -109,13 +109,19 @@
'id' => 44,
'name' => 'Operations',
],
'segments' => [
'User' => [
'34dbebc45335faf1d984967d9a4fc672dbd7fc7d',
],
],
]
),
config('permissionExpiration')
);

$listing = \Mockery::mock(Listing::class)
->makePartial()
->shouldAllowMockingProtectedMethods()
->shouldReceive('checkIp')
->getMock();

Expand Down Expand Up @@ -380,3 +386,59 @@
->toThrow(ForestException::class, "🌳🌳🌳 The given operator 'Shorter_Than' is not supported by the column: id. The allowed operators are: [Equal, Blank, In]");

});

test('when request has segment - should throw if there is no connectionName', function () use ($before) {
$_GET['filename'] = 'export-users';
$_GET['header'] = 'id,first_name,last_name,birthday,active';
$_POST['segmentName'] = 'User segment';
$_POST['segmentQuery'] = 'SELECT id FROM users WHERE id = 1;';
$data = [];

$listing = $before($this, ['listing' => $data]);

expect(fn () => $listing->handleRequest(['collectionName' => 'User']))
->toThrow(ForestException::class, "🌳🌳🌳 'connectionName' parameter is mandatory");
});

test('when request has segment - return a response 200', function () use ($before) {
$_GET['filename'] = 'export-users';
$_GET['header'] = 'id,first_name,last_name,birthday,active';
$_POST['connectionName'] = 'EloquentDatasource';
$_POST['segmentName'] = 'User segment';
$_POST['segmentQuery'] = 'SELECT id FROM users WHERE id = 1;';
$data = [
[
'id' => 1,
'first_name' => 'John',
'last_name' => 'Doe',
'birthday' => '1980-01-01',
'active' => true,
],
];

$listing = $before($this, ['listing' => $data]);
$listing->shouldReceive('parseQuerySegment')
->andReturn(new ConditionTreeLeaf('id', Operators::IN, [1]));

expect($listing->handleRequest(['collectionName' => 'User']))
->toBeArray()
->toEqual(
[
'name' => 'User',
'content' => [
'data' => [
[
'type' => 'User',
'id' => '1',
'attributes' => [
'first_name' => 'John',
'last_name' => 'Doe',
'birthday' => '1980-01-01',
'active' => true,
],
],
],
],
]
);
});

0 comments on commit f6da0cc

Please sign in to comment.