Skip to content

Commit

Permalink
Rename QueryValidator to Query
Browse files Browse the repository at this point in the history
Query matches the naming convention of the other validators. We can use aliasing
whenever any conflict arises with Utopia\Database\Query.
  • Loading branch information
stnguyen90 committed Aug 12, 2022
1 parent a165cd0 commit ab84f65
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Database/Validator/Queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Utopia\Validator;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Validator\QueryValidator;
use Utopia\Database\Validator\Query as QueryValidator;
use Utopia\Database\Query;

class Queries extends Validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Utopia\Database\Database;
use Utopia\Validator;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Query as DatabaseQuery;

class QueryValidator extends Validator
class Query extends Validator
{
/**
* @var string
Expand All @@ -24,9 +24,12 @@ class QueryValidator extends Validator
protected int $maxValuesCount;

/**
* Expression constructor
* Query constructor
*
* @param Document[] $attributes
* @param int $maxLimit
* @param int $maxOffset
* @param int $maxValuesCount
*/
public function __construct(array $attributes, int $maxLimit = 100, int $maxOffset = 5000, int $maxValuesCount = 100)
{
Expand Down Expand Up @@ -86,20 +89,20 @@ public function getDescription(): string
*
* Otherwise, returns true.
*
* @param Query $query
* @param DatabaseQuery $query
*
* @return bool
*/
public function isValid($query): bool
{
// Validate method
$method = $query->getMethod();
if (!Query::isMethod($method)) {
if (!DatabaseQuery::isMethod($method)) {
$this->message = 'Query method invalid: ' . $method;
return false;
}

if ($method === Query::TYPE_LIMIT) {
if ($method === DatabaseQuery::TYPE_LIMIT) {
$limit = $query->getValue();
if ($limit === null || $limit < 0 || $limit > $this->maxLimit) {
$this->message = 'Limit must be between 0 and ' . $this->maxLimit . '(inclusive)';
Expand All @@ -108,7 +111,7 @@ public function isValid($query): bool
return true;
}

if ($method === Query::TYPE_OFFSET) {
if ($method === DatabaseQuery::TYPE_OFFSET) {
$offset = $query->getValue();
if ($offset === null || $offset < 0 || $offset > $this->maxOffset) {
$this->message = 'Offset must be between 0 and ' . $this->maxOffset . '(inclusive)';
Expand All @@ -117,7 +120,7 @@ public function isValid($query): bool
return true;
}

if ($method === Query::TYPE_CURSORAFTER || $method === Query::TYPE_CURSORBEFORE) {
if ($method === DatabaseQuery::TYPE_CURSORAFTER || $method === DatabaseQuery::TYPE_CURSORBEFORE) {
$value = $query->getValue();
if ($value === null) {
$this->message = 'Cursor must not be null';
Expand All @@ -138,7 +141,7 @@ public function isValid($query): bool
return false;
}

if ($method === Query::TYPE_ORDERASC || $method === Query::TYPE_ORDERDESC) {
if ($method === DatabaseQuery::TYPE_ORDERASC || $method === DatabaseQuery::TYPE_ORDERDESC) {
return true;
}

Expand Down Expand Up @@ -166,7 +169,7 @@ public function isValid($query): bool
}

// Contains method only supports array attributes
if (!$attributeSchema['array'] && $query->getMethod() === Query::TYPE_CONTAINS) {
if (!$attributeSchema['array'] && $query->getMethod() === DatabaseQuery::TYPE_CONTAINS) {
$this->message = 'Query method only supported on array attributes: ' . $query->getMethod();
return false;
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Database/Validator/QueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Utopia\Tests\Validator;

use Utopia\Database\Validator\QueryValidator;
use Utopia\Database\Validator\Query;
use PHPUnit\Framework\TestCase;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Query as DatabaseQuery;
use Utopia\Database\Validator\Queries;

class QueriesTest extends TestCase
Expand All @@ -17,7 +17,7 @@ class QueriesTest extends TestCase
protected $collection = [];

/**
* @var Query[] $queries
* @var DatabaseQuery[] $queries
*/
protected $queries = [];

Expand Down Expand Up @@ -99,8 +99,8 @@ public function setUp(): void

$this->queryValidator = new Query($this->collection['attributes']);

$query1 = Query::parse('notEqual("title", ["Iron Man", "Ant Man"])');
$query2 = Query::parse('equal("description", "Best movie ever")');
$query1 = DatabaseQuery::parse('notEqual("title", ["Iron Man", "Ant Man"])');
$query2 = DatabaseQuery::parse('equal("description", "Best movie ever")');

array_push($this->queries, $query1, $query2);

Expand Down Expand Up @@ -164,28 +164,28 @@ public function testQueries()

$this->assertEquals(true, $validator->isValid($this->queries), $validator->getDescription());

$this->queries[] = Query::parse('lessThan("price", 6.50)');
$this->queries[] = DatabaseQuery::parse('lessThan("price", 6.50)');
$this->assertEquals(true, $validator->isValid($this->queries));


// test for FAILURE

$this->queries[] = Query::parse('greaterThan("rating", 4)');
$this->queries[] = DatabaseQuery::parse('greaterThan("rating", 4)');

$this->assertEquals(false, $validator->isValid($this->queries));
$this->assertEquals("Index not found: title,description,price,rating", $validator->getDescription());

// test for queued index
$query1 = Query::parse('lessThan("price", 6.50)');
$query2 = Query::parse('notEqual("title", ["Iron Man", "Ant Man"])');
$query1 = DatabaseQuery::parse('lessThan("price", 6.50)');
$query2 = DatabaseQuery::parse('notEqual("title", ["Iron Man", "Ant Man"])');

$this->queries = [$query1, $query2];
$this->assertEquals(false, $validator->isValid($this->queries));
$this->assertEquals("Index not found: price,title", $validator->getDescription());

// test fulltext

$query3 = Query::parse('search("description", "iron")');
$query3 = DatabaseQuery::parse('search("description", "iron")');
$this->queries = [$query3];
$this->assertEquals(false, $validator->isValid($this->queries));
$this->assertEquals("Search method requires fulltext index: description", $validator->getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Utopia\Tests\Validator;

use Utopia\Database\Validator\QueryValidator;
use Utopia\Database\Validator\Query;
use PHPUnit\Framework\TestCase;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Query as DatabaseQuery;

class QueryValidatorTest extends TestCase
class QueryTest extends TestCase
{
/**
* @var Document[]
Expand Down Expand Up @@ -105,128 +105,131 @@ public function tearDown(): void

public function testQuery()
{
$validator = new QueryValidator($this->schema);

$this->assertEquals(true, $validator->isValid(Query::parse('equal("$id", ["Iron Man", "Ant Man"])')));
$this->assertEquals(true, $validator->isValid(Query::parse('notEqual("title", ["Iron Man", "Ant Man"])')));
$this->assertEquals(true, $validator->isValid(Query::parse('equal("description", "Best movie ever")')));
$this->assertEquals(true, $validator->isValid(Query::parse('greaterThan("rating", 4)')), $validator->getDescription());
$this->assertEquals(true, $validator->isValid(Query::parse('lessThan("price", 6.50)')));
$this->assertEquals(true, $validator->isValid(Query::parse('contains("tags", "action")')));
$this->assertEquals(true, $validator->isValid(Query::parse('cursorAfter("docId")')));
$this->assertEquals(true, $validator->isValid(Query::parse('cursorBefore("docId")')));
$this->assertEquals(true, $validator->isValid(Query::parse('orderAsc("title")')));
$this->assertEquals(true, $validator->isValid(Query::parse('orderDesc("title")')));
$validator = new Query($this->schema);

$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('equal("$id", ["Iron Man", "Ant Man"])')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('notEqual("title", ["Iron Man", "Ant Man"])')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('equal("description", "Best movie ever")')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('greaterThan("rating", 4)')), $validator->getDescription());
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('lessThan("price", 6.50)')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('contains("tags", "action")')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('cursorAfter("docId")')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('cursorBefore("docId")')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('orderAsc("title")')));
$this->assertEquals(true, $validator->isValid(DatabaseQuery::parse('orderDesc("title")')));
}

public function testInvalidMethod()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('eqqual("title", "Iron Man")'));
$response = $validator->isValid(DatabaseQuery::parse('eqqual("title", "Iron Man")'));

$this->assertEquals(false, $response);
$this->assertEquals('Query method invalid: eqqual', $validator->getDescription());
}

public function testAttributeNotFound()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('equal("name", "Iron Man")'));
$response = $validator->isValid(DatabaseQuery::parse('equal("name", "Iron Man")'));

$this->assertEquals(false, $response);
$this->assertEquals('Attribute not found in schema: name', $validator->getDescription());

$response = $validator->isValid(Query::parse('orderAsc("name")'));
$response = $validator->isValid(DatabaseQuery::parse('orderAsc("name")'));

$this->assertEquals(false, $response);
$this->assertEquals('Attribute not found in schema: name', $validator->getDescription());
}

public function testAttributeWrongType()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('equal("title", 1776)'));
$response = $validator->isValid(DatabaseQuery::parse('equal("title", 1776)'));

$this->assertEquals(false, $response);
$this->assertEquals('Query type does not match expected: string', $validator->getDescription());
}

public function testMethodWrongType()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('contains("title", "Iron")'));
$response = $validator->isValid(DatabaseQuery::parse('contains("title", "Iron")'));

$this->assertEquals(false, $response);
$this->assertEquals('Query method only supported on array attributes: contains', $validator->getDescription());
}

public function testQueryDate()
{
$validator = new QueryValidator($this->schema);
$response = $validator->isValid(Query::parse('greaterThan("birthDay", "1960-01-01 10:10:10")'));
$validator = new Query($this->schema);
$response = $validator->isValid(DatabaseQuery::parse('greaterThan("birthDay", "1960-01-01 10:10:10")'));
$this->assertEquals(true, $response);
}

public function testQueryLimit()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('limit(25)'));
$response = $validator->isValid(DatabaseQuery::parse('limit(25)'));
$this->assertEquals(true, $response);

$response = $validator->isValid(Query::parse('limit()'));
$response = $validator->isValid(DatabaseQuery::parse('limit()'));
$this->assertEquals(false, $response);

$response = $validator->isValid(Query::parse('limit(-1)'));
$response = $validator->isValid(DatabaseQuery::parse('limit(-1)'));
$this->assertEquals(false, $response);

$response = $validator->isValid(Query::parse('limit(10000)'));
$response = $validator->isValid(DatabaseQuery::parse('limit(10000)'));
$this->assertEquals(false, $response);
}

public function testQueryOffset()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('offset(25)'));
$response = $validator->isValid(DatabaseQuery::parse('offset(25)'));
$this->assertEquals(true, $response);

$response = $validator->isValid(Query::parse('offset()'));
$response = $validator->isValid(DatabaseQuery::parse('offset()'));
$this->assertEquals(false, $response);

$response = $validator->isValid(Query::parse('offset(-1)'));
$response = $validator->isValid(DatabaseQuery::parse('offset(-1)'));
$this->assertEquals(false, $response);

$response = $validator->isValid(Query::parse('offset(10000)'));
$response = $validator->isValid(DatabaseQuery::parse('offset(10000)'));
$this->assertEquals(false, $response);
}

public function testQueryOrder()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('orderAsc("title")'));
$response = $validator->isValid(DatabaseQuery::parse('orderAsc("title")'));
$this->assertEquals(true, $response);

$response = $validator->isValid(Query::parse('orderAsc("")'));
$response = $validator->isValid(DatabaseQuery::parse('orderAsc("")'));
$this->assertEquals(true, $response);

$response = $validator->isValid(Query::parse('orderAsc("doesNotExist")'));
$response = $validator->isValid(DatabaseQuery::parse('orderAsc()'));
$this->assertEquals(true, $response);

$response = $validator->isValid(DatabaseQuery::parse('orderAsc("doesNotExist")'));
$this->assertEquals(false, $response);
}

public function testQueryCursor()
{
$validator = new QueryValidator($this->schema);
$validator = new Query($this->schema);

$response = $validator->isValid(Query::parse('cursorAfter("asdf")'));
$response = $validator->isValid(DatabaseQuery::parse('cursorAfter("asdf")'));
$this->assertEquals(true, $response);

$response = $validator->isValid(Query::parse('cursorAfter()'));
$response = $validator->isValid(DatabaseQuery::parse('cursorAfter()'));
$this->assertEquals(false, $response);
}
}

0 comments on commit ab84f65

Please sign in to comment.