summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-08 13:51:45 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-08 13:51:45 +0100
commit45fe8271ab1efb90ad0394bec0285f83b1ccd55c (patch)
treebd73ff50105f9ac125b7a8c02d1a367725d0005c /tests
parent85409b6701ffd46806f4a53b69dce0fa6a09d2a9 (diff)
parent9f9884930609addad3e37325731fb5406dd3aa44 (diff)
downloadnextcloud-server-45fe8271ab1efb90ad0394bec0285f83b1ccd55c.tar.gz
nextcloud-server-45fe8271ab1efb90ad0394bec0285f83b1ccd55c.zip
Merge pull request #21030 from owncloud/querybuilder-new-features
Querybuilder new features
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/db/querybuilder/querybuildertest.php83
1 files changed, 78 insertions, 5 deletions
diff --git a/tests/lib/db/querybuilder/querybuildertest.php b/tests/lib/db/querybuilder/querybuildertest.php
index ca3901ad049..c8e029d9e40 100644
--- a/tests/lib/db/querybuilder/querybuildertest.php
+++ b/tests/lib/db/querybuilder/querybuildertest.php
@@ -48,12 +48,12 @@ class QueryBuilderTest extends \Test\TestCase {
$this->queryBuilder = new QueryBuilder($this->connection);
}
- protected function createTestingRows() {
+ protected function createTestingRows($appId = 'testFirstResult') {
$qB = $this->connection->getQueryBuilder();
for ($i = 1; $i < 10; $i++) {
$qB->insert('*PREFIX*appconfig')
->values([
- 'appid' => $qB->expr()->literal('testFirstResult'),
+ 'appid' => $qB->expr()->literal($appId),
'configkey' => $qB->expr()->literal('testing' . $i),
'configvalue' => $qB->expr()->literal(100 - $i),
])
@@ -80,11 +80,11 @@ class QueryBuilderTest extends \Test\TestCase {
return $rows;
}
- protected function deleteTestingRows() {
+ protected function deleteTestingRows($appId = 'testFirstResult') {
$qB = $this->connection->getQueryBuilder();
$qB->delete('*PREFIX*appconfig')
- ->where($qB->expr()->eq('appid', $qB->expr()->literal('testFirstResult')))
+ ->where($qB->expr()->eq('appid', $qB->expr()->literal($appId)))
->execute();
}
@@ -272,6 +272,34 @@ class QueryBuilderTest extends \Test\TestCase {
$this->deleteTestingRows();
}
+ public function testSelectDistinct() {
+ $this->deleteTestingRows('testFirstResult1');
+ $this->deleteTestingRows('testFirstResult2');
+ $this->createTestingRows('testFirstResult1');
+ $this->createTestingRows('testFirstResult2');
+
+ $this->queryBuilder->selectDistinct('appid');
+
+ $this->queryBuilder->from('*PREFIX*appconfig')
+ ->where($this->queryBuilder->expr()->in(
+ 'appid',
+ [$this->queryBuilder->expr()->literal('testFirstResult1'), $this->queryBuilder->expr()->literal('testFirstResult2')]
+ ))
+ ->orderBy('appid', 'DESC');
+
+ $query = $this->queryBuilder->execute();
+ $rows = $query->fetchAll();
+ $query->closeCursor();
+
+ $this->assertEquals(
+ [['appid' => 'testFirstResult2'], ['appid' => 'testFirstResult1']],
+ $rows
+ );
+
+ $this->deleteTestingRows('testFirstResult1');
+ $this->deleteTestingRows('testFirstResult2');
+ }
+
public function dataAddSelect() {
$queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection());
return [
@@ -1086,6 +1114,31 @@ class QueryBuilderTest extends \Test\TestCase {
);
}
+ public function testGetLastInsertId() {
+ $qB = $this->connection->getQueryBuilder();
+
+ try {
+ $qB->getLastInsertId();
+ $this->fail('getLastInsertId() should throw an exception, when being called before insert()');
+ } catch (\BadMethodCallException $e) {
+ $this->assertTrue(true);
+ }
+
+ $qB->insert('appconfig')
+ ->values([
+ 'appid' => $qB->expr()->literal('testFirstResult'),
+ 'configkey' => $qB->expr()->literal('testing' . 50),
+ 'configvalue' => $qB->expr()->literal(100 - 50),
+ ])
+ ->execute();
+
+ $actual = $qB->getLastInsertId();
+
+ $this->assertNotNull($actual);
+ $this->assertInternalType('int', $actual);
+ $this->assertEquals($this->connection->lastInsertId('*PREFIX*appconfig'), $actual);
+ }
+
public function dataGetTableName() {
return [
['*PREFIX*table', null, '`*PREFIX*table`'],
@@ -1112,7 +1165,27 @@ class QueryBuilderTest extends \Test\TestCase {
$this->assertSame(
$expected,
- $this->invokePrivate($this->queryBuilder, 'getTableName', [$tableName])
+ $this->queryBuilder->getTableName($tableName)
+ );
+ }
+
+ public function dataGetColumnName() {
+ return [
+ ['column', '', '`column`'],
+ ['column', 'a', 'a.`column`'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetColumnName
+ * @param string $column
+ * @param string $prefix
+ * @param string $expected
+ */
+ public function testGetColumnName($column, $prefix, $expected) {
+ $this->assertSame(
+ $expected,
+ $this->queryBuilder->getColumnName($column, $prefix)
);
}
}