diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-09-07 11:09:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-07 11:09:11 +0200 |
commit | 1cfb8513004864640be506b5ed288dea85d79855 (patch) | |
tree | 074411077a5b3a04f90028fbd2286c3b8258b8b1 /tests | |
parent | b7cf1f322d80c343127525333c89f5e41c0fb694 (diff) | |
parent | 8ef5a366eccaab850dab4641619c704a0b36f3d9 (diff) | |
download | nextcloud-server-1cfb8513004864640be506b5ed288dea85d79855.tar.gz nextcloud-server-1cfb8513004864640be506b5ed288dea85d79855.zip |
Merge pull request #17006 from nextcloud/querybuilder-max-min
add MAX and MIN to functionbuilder
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/DB/QueryBuilder/FunctionBuilderTest.php | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php index 136650741f5..a8af7f4fe07 100644 --- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php @@ -120,4 +120,82 @@ class FunctionBuilderTest extends TestCase { $this->assertGreaterThan(1, $query->execute()->fetchColumn()); } + + private function setUpMinMax($value) { + $query = $this->connection->getQueryBuilder(); + + $query->insert('appconfig') + ->values([ + 'appid' => $query->createNamedParameter('minmax'), + 'configkey' => $query->createNamedParameter(uniqid()), + 'configvalue' => $query->createNamedParameter((string)$value), + ]); + $query->execute(); + } + + private function clearMinMax() { + $query = $this->connection->getQueryBuilder(); + + $query->delete('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))); + $query->execute(); + } + + public function testMaxEmpty() { + $this->clearMinMax(); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(null, $query->execute()->fetchColumn()); + } + + public function testMinEmpty() { + $this->clearMinMax(); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(null, $query->execute()->fetchColumn()); + } + + public function testMax() { + $this->clearMinMax(); + $this->setUpMinMax(10); + $this->setUpMinMax(11); + $this->setUpMinMax(20); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(20, $query->execute()->fetchColumn()); + } + + public function testMin() { + $this->clearMinMax(); + $this->setUpMinMax(10); + $this->setUpMinMax(11); + $this->setUpMinMax(20); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(10, $query->execute()->fetchColumn()); + } } |