From ab3cdcca8ad0373ceeef43f406be65850fdc41c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 10 Jul 2023 19:41:46 +0200 Subject: [PATCH] tests(DB): Add test for date time comparisons in query builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../Version1018Date20230710172259.php | 78 +++++++++++++++++++ .../QueryBuilder/ExpressionBuilderDBTest.php | 55 +++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 apps/testing/lib/Migration/Version1018Date20230710172259.php diff --git a/apps/testing/composer/composer/autoload_classmap.php b/apps/testing/composer/composer/autoload_classmap.php index c4688abedf4ee..72c1275f35d66 100644 --- a/apps/testing/composer/composer/autoload_classmap.php +++ b/apps/testing/composer/composer/autoload_classmap.php @@ -13,4 +13,5 @@ 'OCA\\Testing\\Controller\\LockingController' => $baseDir . '/../lib/Controller/LockingController.php', 'OCA\\Testing\\Controller\\RateLimitTestController' => $baseDir . '/../lib/Controller/RateLimitTestController.php', 'OCA\\Testing\\Locking\\FakeDBLockingProvider' => $baseDir . '/../lib/Locking/FakeDBLockingProvider.php', + 'OCA\\Testing\\Migration\\Version1018Date20230710172259' => $baseDir . '/../lib/Migration/Version1018Date20230710172259.php', ); diff --git a/apps/testing/composer/composer/autoload_static.php b/apps/testing/composer/composer/autoload_static.php index 15b4617183825..7514844ead2ba 100644 --- a/apps/testing/composer/composer/autoload_static.php +++ b/apps/testing/composer/composer/autoload_static.php @@ -28,6 +28,7 @@ class ComposerStaticInitTesting 'OCA\\Testing\\Controller\\LockingController' => __DIR__ . '/..' . '/../lib/Controller/LockingController.php', 'OCA\\Testing\\Controller\\RateLimitTestController' => __DIR__ . '/..' . '/../lib/Controller/RateLimitTestController.php', 'OCA\\Testing\\Locking\\FakeDBLockingProvider' => __DIR__ . '/..' . '/../lib/Locking/FakeDBLockingProvider.php', + 'OCA\\Testing\\Migration\\Version1018Date20230710172259' => __DIR__ . '/..' . '/../lib/Migration/Version1018Date20230710172259.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/apps/testing/lib/Migration/Version1018Date20230710172259.php b/apps/testing/lib/Migration/Version1018Date20230710172259.php new file mode 100644 index 0000000000000..e6845416a49c4 --- /dev/null +++ b/apps/testing/lib/Migration/Version1018Date20230710172259.php @@ -0,0 +1,78 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Testing\Migration; + +use Closure; +use Doctrine\DBAL\Types\Types; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version1018Date20230710172259 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + $schema = $schemaClosure(); + if (!$schema->hasTable('testing')) { + $table = $schema->createTable('testing'); + $table->addColumn('id', Types::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + ]); + + $table->addColumn('datetime', Types::DATETIME_MUTABLE, [ + 'notnull' => false, + ]); + + $table->setPrimaryKey(['id']); + return $schema; + } + + return null; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +} diff --git a/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php b/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php index 35d8b4faa343a..30963f3b2f49e 100644 --- a/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php +++ b/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php @@ -36,6 +36,8 @@ protected function setUp(): void { parent::setUp(); $this->connection = \OC::$server->getDatabaseConnection(); + + $this->connection->getQueryBuilder()->delete('testing')->executeStatement(); } public function likeProvider() { @@ -150,6 +152,59 @@ public function testLongText(): void { self::assertEquals('myvalue', $entries[0]['configvalue']); } + public function testDateTimeEquals() { + $dateTime = new \DateTime('2023-01-01'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->eq('datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + + public function testDateTimeLess() { + $dateTime = new \DateTime('2022-01-01'); + $dateTimeCompare = new \DateTime('2022-01-02'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->lt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + + public function testDateTimeGreater() { + $dateTime = new \DateTime('2023-01-02'); + $dateTimeCompare = new \DateTime('2023-01-01'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->gt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + protected function createConfig($appId, $key, $value) { $query = $this->connection->getQueryBuilder(); $query->insert('appconfig')