Refactor Queries Validation
This commit is contained in:
parent
9881c4cc73
commit
66ff297103
|
@ -38,7 +38,11 @@ use Appwrite\Network\Validator\IP;
|
|||
use Appwrite\Network\Validator\URL;
|
||||
use Appwrite\Utopia\Database\Validator\CustomId;
|
||||
use Appwrite\Utopia\Database\Validator\IndexedQueries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorFilterOrderQuery;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Cursor as CursorQueryValidator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Filter as FilterQueryValidator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit as LimitQueryValidator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset as OffsetQueryValidator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Order as OrderQueryValidator;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Detector\Detector;
|
||||
use Appwrite\Event\Database as EventDatabase;
|
||||
|
@ -2048,7 +2052,15 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
|
|||
]),
|
||||
]
|
||||
);
|
||||
$validator = new IndexedQueries(new LimitOffsetCursorFilterOrderQuery(attributes: $attributes), $attributes, $collection->getAttribute('indexes', []));
|
||||
$validator = new IndexedQueries(
|
||||
$attributes,
|
||||
$collection->getAttribute('indexes', []),
|
||||
new CursorQueryValidator(),
|
||||
new FilterQueryValidator($attributes),
|
||||
new LimitQueryValidator(),
|
||||
new OffsetQueryValidator(),
|
||||
new OrderQueryValidator(),
|
||||
);
|
||||
if (!$validator->isValid($queries)) {
|
||||
throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription());
|
||||
}
|
||||
|
|
|
@ -11,7 +11,8 @@ use Appwrite\Stats\Stats;
|
|||
use Appwrite\Utopia\Database\Validator\CustomId;
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Users;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetQuery;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\App;
|
||||
use Utopia\Audit\Audit;
|
||||
|
@ -574,7 +575,7 @@ App::get('/v1/users/:userId/logs')
|
|||
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||
->label('sdk.response.model', Response::MODEL_LOG_LIST)
|
||||
->param('userId', '', new UID(), 'User ID.')
|
||||
->param('queries', [], new Queries(new LimitOffsetQuery()), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Only supported methods are limit and offset', true)
|
||||
->param('queries', [], new Queries(new Limit(), new Offset()), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Only supported methods are limit and offset', true)
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->inject('locale')
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Query as QueryValidator;
|
||||
|
||||
class IndexedQueries extends Queries
|
||||
{
|
||||
|
@ -25,18 +24,13 @@ class IndexedQueries extends Queries
|
|||
*
|
||||
* This Queries Validator filters indexes for only available indexes
|
||||
*
|
||||
* @param QueryValidator $validator
|
||||
* @param Document[] $attributes
|
||||
* @param Document[] $indexes
|
||||
* @param Base ...$validators
|
||||
* @param bool $strict
|
||||
*/
|
||||
public function __construct($validator, $attributes = [], $indexes = [])
|
||||
public function __construct($attributes = [], $indexes = [], Base ...$validators)
|
||||
{
|
||||
// Remove failed/stuck/processing indexes
|
||||
$availableIndexes = \array_filter($indexes, function ($index) {
|
||||
return $index->getAttribute('status') === 'available';
|
||||
});
|
||||
|
||||
$this->attributes = $attributes;
|
||||
|
||||
$this->indexes[] = new Document([
|
||||
|
@ -58,7 +52,7 @@ class IndexedQueries extends Queries
|
|||
$this->indexes[] = $index;
|
||||
}
|
||||
|
||||
parent::__construct($validator);
|
||||
parent::__construct(...$validators);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Validator;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Query as QueryValidator;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
class Queries extends Validator
|
||||
|
@ -15,22 +14,18 @@ class Queries extends Validator
|
|||
protected $message = 'Invalid queries';
|
||||
|
||||
/**
|
||||
* @var QueryValidator
|
||||
* @var Base[]
|
||||
*/
|
||||
protected $validator;
|
||||
protected $validators;
|
||||
|
||||
/**
|
||||
* Queries constructor
|
||||
*
|
||||
* @param $validators - a list of validators
|
||||
* @param Base ...$validators a list of validators
|
||||
*/
|
||||
public function __construct(Limit $limit = null, Offset $offset = null, Order $order = null, Cursor $cursor = null, Filter $filter = null)
|
||||
public function __construct(Base ...$validators)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
$this->offset = $offset;
|
||||
$this->order = $order;
|
||||
$this->filter = $filter;
|
||||
$this->cursor = $cursor;
|
||||
$this->validators = $validators;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +53,6 @@ class Queries extends Validator
|
|||
*/
|
||||
public function isValid($value): bool
|
||||
{
|
||||
$queries = [];
|
||||
foreach ($value as $query) {
|
||||
if (!$query instanceof Query) {
|
||||
try {
|
||||
|
@ -70,23 +64,52 @@ class Queries extends Validator
|
|||
}
|
||||
|
||||
$method = $query->getMethod();
|
||||
$methodType = '';
|
||||
switch ($method) {
|
||||
case Query::TYPE_LIMIT:
|
||||
$validator = $this->limit;
|
||||
$methodType = Base::METHOD_TYPE_LIMIT;
|
||||
break;
|
||||
case Query::TYPE_OFFSET:
|
||||
$validator = $this->offset;
|
||||
case Query::TYPE_ORDER:
|
||||
$validator = $this->order;
|
||||
$methodType = Base::METHOD_TYPE_OFFSET;
|
||||
break;
|
||||
case Query::TYPE_CURSORAFTER:
|
||||
case Query::TYPE_CURSORBEFORE:
|
||||
$methodType = Base::METHOD_TYPE_CURSOR;
|
||||
break;
|
||||
case Query::TYPE_ORDERASC:
|
||||
case Query::TYPE_ORDERDESC:
|
||||
$methodType = Base::METHOD_TYPE_ORDER;
|
||||
break;
|
||||
case Query::TYPE_EQUAL:
|
||||
case Query::TYPE_NOTEQUAL:
|
||||
case Query::TYPE_LESSER:
|
||||
case Query::TYPE_LESSEREQUAL:
|
||||
case Query::TYPE_GREATER:
|
||||
case Query::TYPE_GREATEREQUAL:
|
||||
case Query::TYPE_SEARCH:
|
||||
$methodType = Base::METHOD_TYPE_FILTER;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($validator && !$validator->isValid($query)) {
|
||||
$this->message = 'Query not valid: ' . $this->validator->getDescription();
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
$queries[] = $query;
|
||||
$methodIsValid = false;
|
||||
foreach ($this->validators as $validator) {
|
||||
if ($validator->getMethodType() !== $methodType) {
|
||||
continue;
|
||||
}
|
||||
if (!$validator->isValid($query)) {
|
||||
$this->message = 'Query not valid: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
||||
$methodIsValid = true;
|
||||
}
|
||||
|
||||
if (!$methodIsValid) {
|
||||
$this->message = 'Query method not valid: ' . $method;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -2,13 +2,17 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\IndexedQueries;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Cursor;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Filter;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Order;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Queries as QueriesValidator;
|
||||
use Utopia\Database\Validator\Query as QueryValidator;
|
||||
|
||||
class Collection extends QueriesValidator
|
||||
class Collection extends IndexedQueries
|
||||
{
|
||||
/**
|
||||
* Expression constructor
|
||||
|
@ -39,6 +43,22 @@ class Collection extends QueriesValidator
|
|||
]);
|
||||
}
|
||||
|
||||
$attributes[] = new Document([
|
||||
'key' => '$id',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]);
|
||||
$attributes[] = new Document([
|
||||
'$id' => '$createdAt',
|
||||
'type' => Database::VAR_DATETIME,
|
||||
'array' => false,
|
||||
]);
|
||||
$attributes[] = new Document([
|
||||
'$id' => '$updatedAt',
|
||||
'type' => Database::VAR_DATETIME,
|
||||
'array' => false,
|
||||
]);
|
||||
|
||||
$indexes = [];
|
||||
foreach ($allowedAttributes as $attribute) {
|
||||
$indexes[] = new Document([
|
||||
|
@ -53,6 +73,14 @@ class Collection extends QueriesValidator
|
|||
'attributes' => ['search']
|
||||
]);
|
||||
|
||||
parent::__construct(new QueryValidator($attributes), $attributes, $indexes, true);
|
||||
$validators = [
|
||||
new Limit(),
|
||||
new Offset(),
|
||||
new Cursor(),
|
||||
new Filter($attributes),
|
||||
new Order($attributes),
|
||||
];
|
||||
|
||||
parent::__construct($attributes, $indexes, ...$validators);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,6 @@ use Appwrite\Utopia\Database\Validator\Queries\Collection;
|
|||
class Users extends Collection
|
||||
{
|
||||
public const ALLOWED_ATTRIBUTES = [
|
||||
'$id',
|
||||
'$createdAt',
|
||||
'$updatedAt',
|
||||
'name',
|
||||
'email',
|
||||
'phone',
|
||||
|
@ -18,6 +15,7 @@ class Users extends Collection
|
|||
'registration',
|
||||
'emailVerification',
|
||||
'phoneVerification',
|
||||
'search',
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -28,4 +26,4 @@ class Users extends Collection
|
|||
{
|
||||
parent::__construct('users', self::ALLOWED_ATTRIBUTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
70
src/Appwrite/Utopia/Database/Validator/Query/Base.php
Normal file
70
src/Appwrite/Utopia/Database/Validator/Query/Base.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Validator;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
abstract class Base extends Validator
|
||||
{
|
||||
public const METHOD_TYPE_LIMIT = 'limit';
|
||||
public const METHOD_TYPE_OFFSET = 'offset';
|
||||
public const METHOD_TYPE_CURSOR = 'cursor';
|
||||
public const METHOD_TYPE_ORDER = 'order';
|
||||
public const METHOD_TYPE_FILTER = 'filter';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
/**
|
||||
* Get Description.
|
||||
*
|
||||
* Returns validator description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is array
|
||||
*
|
||||
* Function will return true if object is array.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isArray(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Type
|
||||
*
|
||||
* Returns validator type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return self::TYPE_OBJECT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* @param Query $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isValid($query): bool;
|
||||
|
||||
/**
|
||||
* Returns what type of query this Validator is for
|
||||
*/
|
||||
abstract public function getMethodType(): string;
|
||||
}
|
|
@ -2,17 +2,12 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Validator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\UID;
|
||||
|
||||
class Cursor extends Validator
|
||||
class Cursor extends Base
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
|
@ -41,4 +36,9 @@ class Cursor extends Validator
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getMethodType(): string
|
||||
{
|
||||
return self::METHOD_TYPE_CURSOR;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Validator;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
class Filter extends Validator
|
||||
class Filter extends Base
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -92,11 +92,6 @@ class Filter extends Validator
|
|||
$attribute = $query->getAttribute();
|
||||
|
||||
switch ($method) {
|
||||
// Do we support contains ?
|
||||
// case Query::TYPE_CONTAINS:
|
||||
// $values = $query->getValues();
|
||||
// return $this->isValidContains($attribute, $values);
|
||||
|
||||
case Query::TYPE_EQUAL:
|
||||
case Query::TYPE_NOTEQUAL:
|
||||
case Query::TYPE_LESSER:
|
||||
|
@ -111,4 +106,9 @@ class Filter extends Validator
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMethodType(): string
|
||||
{
|
||||
return self::METHOD_TYPE_FILTER;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,12 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator;
|
||||
|
||||
class Limit extends Validator
|
||||
class Limit extends Base
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
protected int $maxLimit;
|
||||
|
||||
/**
|
||||
|
@ -25,18 +20,6 @@ class Limit extends Validator
|
|||
$this->maxLimit = $maxLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Description.
|
||||
*
|
||||
* Returns validator description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
protected function isValidLimit($limit): bool
|
||||
{
|
||||
$validator = new Range(0, $this->maxLimit);
|
||||
|
@ -62,7 +45,7 @@ class Limit extends Validator
|
|||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
|
||||
if ($method !== Query::LIMIT) {
|
||||
if ($method !== Query::TYPE_LIMIT) {
|
||||
$this->message = 'Query method invalid: ' . $method;
|
||||
return false;
|
||||
}
|
||||
|
@ -71,27 +54,8 @@ class Limit extends Validator
|
|||
return $this->isValidLimit($limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is array
|
||||
*
|
||||
* Function will return true if object is array.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isArray(): bool
|
||||
public function getMethodType(): string
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Type
|
||||
*
|
||||
* Returns validator type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return self::TYPE_OBJECT;
|
||||
return self::METHOD_TYPE_LIMIT;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,12 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator;
|
||||
|
||||
class Offset extends Validator
|
||||
class Offset extends Base
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
protected int $maxOffset;
|
||||
|
||||
/**
|
||||
|
@ -25,18 +20,6 @@ class Offset extends Validator
|
|||
$this->maxOffset = $maxOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Description.
|
||||
*
|
||||
* Returns validator description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
protected function isValidOffset($offset): bool
|
||||
{
|
||||
$validator = new Range(0, $this->maxOffset);
|
||||
|
@ -61,7 +44,7 @@ class Offset extends Validator
|
|||
{
|
||||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
|
||||
|
||||
if ($method !== Query::TYPE_OFFSET) {
|
||||
$this->message = 'Query method invalid: ' . $method;
|
||||
return false;
|
||||
|
@ -71,27 +54,8 @@ class Offset extends Validator
|
|||
return $this->isValidOffset($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is array
|
||||
*
|
||||
* Function will return true if object is array.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isArray(): bool
|
||||
public function getMethodType(): string
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Type
|
||||
*
|
||||
* Returns validator type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return self::TYPE_OBJECT;
|
||||
return self::METHOD_TYPE_OFFSET;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,12 @@
|
|||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
|
||||
class Order extends Validator
|
||||
class Order extends Base
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
@ -62,6 +58,11 @@ class Order extends Validator
|
|||
return $this->isValidAttribute($attribute);
|
||||
}
|
||||
|
||||
return parent::isValid($query);
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getMethodType(): string
|
||||
{
|
||||
return self::METHOD_TYPE_ORDER;
|
||||
}
|
||||
}
|
||||
|
|
121
tests/unit/Utopia/Database/Validator/IndexedQueriesTest.php
Normal file
121
tests/unit/Utopia/Database/Validator/IndexedQueriesTest.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\IndexedQueries;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Cursor;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Filter;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Order;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
|
||||
class IndexedQueriesTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testEmptyQueries(): void
|
||||
{
|
||||
$validator = new IndexedQueries();
|
||||
|
||||
$this->assertEquals(true, $validator->isValid([]));
|
||||
}
|
||||
|
||||
public function testInvalidQuery(): void
|
||||
{
|
||||
$validator = new IndexedQueries();
|
||||
|
||||
$this->assertEquals(false, $validator->isValid(["this.is.invalid"]));
|
||||
}
|
||||
|
||||
public function testInvalidMethod(): void
|
||||
{
|
||||
$validator = new IndexedQueries();
|
||||
$this->assertEquals(false, $validator->isValid(['equal("attr", "value")']));
|
||||
|
||||
$validator = new IndexedQueries([], [], new Limit());
|
||||
$this->assertEquals(false, $validator->isValid(['equal("attr", "value")']));
|
||||
}
|
||||
|
||||
public function testInvalidValue(): void
|
||||
{
|
||||
$validator = new IndexedQueries([], [], new Limit());
|
||||
$this->assertEquals(false, $validator->isValid(['limit(-1)']));
|
||||
}
|
||||
|
||||
public function testValid(): void
|
||||
{
|
||||
$attributes = [
|
||||
new Document([
|
||||
'key' => 'name',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
];
|
||||
$indexes = [
|
||||
new Document([
|
||||
'status' => 'available',
|
||||
'type' => Database::INDEX_KEY,
|
||||
'attributes' => ['name'],
|
||||
]),
|
||||
new Document([
|
||||
'status' => 'available',
|
||||
'type' => Database::INDEX_FULLTEXT,
|
||||
'attributes' => ['name'],
|
||||
]),
|
||||
];
|
||||
$validator = new IndexedQueries(
|
||||
$attributes,
|
||||
$indexes,
|
||||
new Cursor(),
|
||||
new Filter($attributes),
|
||||
new Limit(),
|
||||
new Offset(),
|
||||
new Order($attributes),
|
||||
);
|
||||
$this->assertEquals(true, $validator->isValid(['cursorAfter("asdf")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("name", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['limit(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['offset(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['orderAsc("name")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['search("name", "value")']), $validator->getDescription());
|
||||
}
|
||||
|
||||
public function testMissingIndex(): void
|
||||
{
|
||||
$attributes = [
|
||||
new Document([
|
||||
'key' => 'name',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
];
|
||||
$indexes = [
|
||||
new Document([
|
||||
'status' => 'available',
|
||||
'type' => Database::INDEX_KEY,
|
||||
'attributes' => ['name'],
|
||||
]),
|
||||
];
|
||||
$validator = new IndexedQueries(
|
||||
$attributes,
|
||||
$indexes,
|
||||
new Cursor(),
|
||||
new Filter($attributes),
|
||||
new Limit(),
|
||||
new Offset(),
|
||||
new Order($attributes),
|
||||
);
|
||||
$this->assertEquals(false, $validator->isValid(['equal("dne", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(false, $validator->isValid(['orderAsc("dne")']), $validator->getDescription());
|
||||
$this->assertEquals(false, $validator->isValid(['search("name", "value")']), $validator->getDescription());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Collection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CollectionTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testEmptyQueries(): void
|
||||
{
|
||||
$validator = new Collection('users', []);
|
||||
|
||||
$this->assertEquals($validator->isValid([]), true);
|
||||
}
|
||||
|
||||
public function testValid(): void
|
||||
{
|
||||
$validator = new Collection('users', ['name', 'search']);
|
||||
$this->assertEquals(true, $validator->isValid(['cursorAfter("asdf")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("name", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['limit(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['offset(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['orderAsc("name")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['search("search", "value")']), $validator->getDescription());
|
||||
}
|
||||
|
||||
public function testMissingIndex(): void
|
||||
{
|
||||
$validator = new Collection('users', ['name']);
|
||||
$this->assertEquals(false, $validator->isValid(['equal("dne", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(false, $validator->isValid(['orderAsc("dne")']), $validator->getDescription());
|
||||
$this->assertEquals(false, $validator->isValid(['search("search", "value")']), $validator->getDescription());
|
||||
}
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorFilterOrderQuery;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LimitOffsetCursorFilterOrderQueryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Validator
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new LimitOffsetCursorFilterOrderQuery(
|
||||
attributes: [
|
||||
new Document([
|
||||
'key' => 'attr',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('')), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('dne', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('dne')), false, $this->validator->getDescription());
|
||||
}
|
||||
|
||||
public function testValues(): void
|
||||
{
|
||||
|
||||
$validator = new Queries($this->validator);
|
||||
|
||||
// Test for Success
|
||||
$this->assertEquals($validator->isValid(['limit(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(100)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5000)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(25)', 'offset(25)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorAfter("asdf")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorBefore("asdf")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("attr", "v")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderAsc("attr")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderAsc("")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("attr")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("")']), true, $validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($validator->isValid(['limit(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(101)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5001)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("dne", "v")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("", "v")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("dne")']), false, $validator->getDescription());
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorFilterQuery;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LimitOffsetCursorFilterQueryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Validator
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new LimitOffsetCursorFilterQuery(
|
||||
attributes: [
|
||||
new Document([
|
||||
'key' => 'attr',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('dne', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('', ['v'])), false, $this->validator->getDescription());
|
||||
}
|
||||
|
||||
public function testValues(): void
|
||||
{
|
||||
|
||||
$validator = new Queries($this->validator);
|
||||
|
||||
// Test for Success
|
||||
$this->assertEquals($validator->isValid(['limit(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(100)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5000)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(25)', 'offset(25)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorAfter("asdf")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorBefore("asdf")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("attr", "v")']), true, $validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($validator->isValid(['limit(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(101)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5001)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderAsc("attr")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("attr")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("dne", "v")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("", "v")']), false, $validator->getDescription());
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorQuery;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LimitOffsetCursorQueryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Validator
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new LimitOffsetCursorQuery();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
}
|
||||
|
||||
public function testValues(): void
|
||||
{
|
||||
|
||||
$validator = new Queries($this->validator);
|
||||
|
||||
// Test for Success
|
||||
$this->assertEquals($validator->isValid(['limit(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(100)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5000)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(25)', 'offset(25)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorAfter("asdf")']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorBefore("asdf")']), true, $validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($validator->isValid(['limit(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(101)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5001)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("attr", "v")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderAsc("attr")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("attr")']), false, $validator->getDescription());
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetQuery;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LimitOffsetQueryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Validator
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new LimitOffsetQuery();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
}
|
||||
|
||||
public function testValues(): void
|
||||
{
|
||||
|
||||
$validator = new Queries($this->validator);
|
||||
|
||||
// Test for Success
|
||||
$this->assertEquals($validator->isValid(['limit(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(100)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(1)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(0)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5000)']), true, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(25)', 'offset(25)']), true, $validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($validator->isValid(['limit(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['limit(101)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(-1)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['offset(5001)']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorAfter("asdf")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['cursorBefore("asdf")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['equal("attr", "v")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderAsc("attr")']), false, $validator->getDescription());
|
||||
$this->assertEquals($validator->isValid(['orderDesc("attr")']), false, $validator->getDescription());
|
||||
}
|
||||
}
|
40
tests/unit/Utopia/Database/Validator/Queries/UsersTest.php
Normal file
40
tests/unit/Utopia/Database/Validator/Queries/UsersTest.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Users;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UsersTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testIsValid(): void
|
||||
{
|
||||
$validator = new Users();
|
||||
|
||||
/**
|
||||
* Test for Success
|
||||
*/
|
||||
$this->assertEquals(true, $validator->isValid([]), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("name", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("email", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("phone", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['greaterThan("passwordUpdate", "2020-10-15 06:38")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['greaterThan("registration", "2020-10-15 06:38")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("emailVerification", true)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("phoneVerification", true)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['search("search", "value")']), $validator->getDescription());
|
||||
|
||||
/**
|
||||
* Test for Failure
|
||||
*/
|
||||
$this->assertEquals(false, $validator->isValid(['equal("password", "value")']), $validator->getDescription());
|
||||
}
|
||||
}
|
76
tests/unit/Utopia/Database/Validator/QueriesTest.php
Normal file
76
tests/unit/Utopia/Database/Validator/QueriesTest.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Cursor;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Filter;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Order;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
|
||||
class QueriesTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testEmptyQueries(): void
|
||||
{
|
||||
$validator = new Queries();
|
||||
|
||||
$this->assertEquals(true, $validator->isValid([]));
|
||||
}
|
||||
|
||||
public function testInvalidQuery(): void
|
||||
{
|
||||
$validator = new Queries();
|
||||
|
||||
$this->assertEquals(false, $validator->isValid(["this.is.invalid"]));
|
||||
}
|
||||
|
||||
public function testInvalidMethod(): void
|
||||
{
|
||||
$validator = new Queries();
|
||||
$this->assertEquals(false, $validator->isValid(['equal("attr", "value")']));
|
||||
|
||||
$validator = new Queries(new Limit());
|
||||
$this->assertEquals(false, $validator->isValid(['equal("attr", "value")']));
|
||||
}
|
||||
|
||||
public function testInvalidValue(): void
|
||||
{
|
||||
$validator = new Queries(new Limit());
|
||||
$this->assertEquals(false, $validator->isValid(['limit(-1)']));
|
||||
}
|
||||
|
||||
public function testValid(): void
|
||||
{
|
||||
$attributes = [
|
||||
new Document([
|
||||
'key' => 'name',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
])
|
||||
];
|
||||
$validator = new Queries(
|
||||
new Cursor(),
|
||||
new Filter($attributes),
|
||||
new Limit(),
|
||||
new Offset(),
|
||||
new Order($attributes),
|
||||
);
|
||||
$this->assertEquals(true, $validator->isValid(['cursorAfter("asdf")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['equal("name", "value")']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['limit(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['offset(10)']), $validator->getDescription());
|
||||
$this->assertEquals(true, $validator->isValid(['orderAsc("name")']), $validator->getDescription());
|
||||
}
|
||||
}
|
41
tests/unit/Utopia/Database/Validator/Query/CursorTest.php
Normal file
41
tests/unit/Utopia/Database/Validator/Query/CursorTest.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Cursor;
|
||||
use Utopia\Database\Query;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CursorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Base
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new Cursor();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
}
|
||||
}
|
59
tests/unit/Utopia/Database/Validator/Query/FilterTest.php
Normal file
59
tests/unit/Utopia/Database/Validator/Query/FilterTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Filter;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FilterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Base
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new Filter(
|
||||
attributes: [
|
||||
new Document([
|
||||
'key' => 'attr',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('dne', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])), false, $this->validator->getDescription());
|
||||
}
|
||||
}
|
37
tests/unit/Utopia/Database/Validator/Query/LimitTest.php
Normal file
37
tests/unit/Utopia/Database/Validator/Query/LimitTest.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Limit;
|
||||
use Utopia\Database\Query;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LimitTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Base
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new Limit();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
}
|
||||
}
|
41
tests/unit/Utopia/Database/Validator/Query/OffsetTest.php
Normal file
41
tests/unit/Utopia/Database/Validator/Query/OffsetTest.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Offset;
|
||||
use Utopia\Database\Query;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class OffsetTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Base
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new Offset();
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(1)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(0)), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5000)), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(100)), false, $this->validator->getDescription());
|
||||
}
|
||||
}
|
54
tests/unit/Utopia/Database/Validator/Query/OrderTest.php
Normal file
54
tests/unit/Utopia/Database/Validator/Query/OrderTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Query\Base;
|
||||
use Appwrite\Utopia\Database\Validator\Query\Order;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class OrderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Base
|
||||
*/
|
||||
protected $validator = null;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->validator = new Order(
|
||||
attributes: [
|
||||
new Document([
|
||||
'key' => 'attr',
|
||||
'type' => Database::VAR_STRING,
|
||||
'array' => false,
|
||||
]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function testValue(): void
|
||||
{
|
||||
// Test for Success
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('attr')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderAsc('')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('attr')), true, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('')), true, $this->validator->getDescription());
|
||||
|
||||
// Test for Failure
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::limit(101)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(-1)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::offset(5001)), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('attr', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('dne', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::equal('', ['v'])), false, $this->validator->getDescription());
|
||||
$this->assertEquals($this->validator->isValid(Query::orderDesc('dne')), false, $this->validator->getDescription());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue