Skip to content

Added test for LENGTH operand #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions tests/06_Query/QuerySql2OperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,151 @@ public function testQueryMultiValuedProperty()
$this->assertSame('foo bar', $rows->current()->getValue('tags'));
}

public function testLengthOperandOnStringProperty()
{
/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.foo IS NOT NULL
AND LENGTH(data.foo) = 3
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(1, $rows, 'Expected 1 node with property "foo" with a value with 3 characters (bar)');

/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.foo IS NOT NULL
AND LENGTH(data.foo) = 4
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(1, $rows, 'Expected 1 node with property "foo" with a value with 4 characters (bar2)');

/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.foo IS NOT NULL
AND LENGTH(data.foo) > 2
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(2, $rows, 'Expected 2 nodes with property "foo" with a value with more then 2 characters (bar and bar2)');
}

public function testLengthOperandOnEmptyProperty()
{
/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.empty-value IS NOT NULL
AND LENGTH(data.empty-value) < 1
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(1, $rows, 'Expected 1 node with property "empty-value" with a length smaller then 1');

/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.empty-value IS NOT NULL
AND LENGTH(data.empty-value) = 0
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(1, $rows, 'Expected 1 node with property "empty-value" with a length equal to 0');

/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE
data.empty-value IS NOT NULL
AND LENGTH(data.empty-value) > -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a very complicated way of expressing 0. what exactly is the idea? testing the smaller and bigger operators? could we do that with something more meaningful? and could you create a separate test for the LENGTH of a binary property and a string property? i guess binary will always be a special case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the idea was to test =, < and >. But might be better to test those in seperate queries.

The jcr:data is a binary property, will pull it out and put it in a separate test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(y) excellent!

AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(1, $rows, 'Expected 1 node with property "empty-value" with a length greater then -1');
}

public function testLengthOperandOnBinaryProperty()
{
/** @var $query QueryInterface */
$query = $this->sharedFixture['qm']->createQuery('
SELECT data.*
FROM [nt:unstructured] AS data
WHERE LENGTH(data.jcr:data) = 121
AND ISDESCENDANTNODE([/tests_general_base])
',
QueryInterface::JCR_SQL2
);

$this->assertInstanceOf('\PHPCR\Query\QueryInterface', $query);
$result = $query->execute();
$this->assertInstanceOf('\PHPCR\Query\QueryResultInterface', $result);

$rows = $result->getRows();

$this->assertCount(3, $rows, 'Expected 3 nodes with a (binary) jcr:data property with length 121');
}

}