Skip to content

Commit

Permalink
tests(DB): Add test for date time comparisons in query builder
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Dec 19, 2023
1 parent 65ed164 commit 58c4c1e
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

namespace Test\DB\QueryBuilder;

use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Types\Types;
use OC\DB\QueryBuilder\Literal;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\Server;
use Test\TestCase;

/**
Expand All @@ -31,11 +35,13 @@
class ExpressionBuilderDBTest extends TestCase {
/** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
protected $connection;
protected $schemaSetup = false;

protected function setUp(): void {
parent::setUp();

$this->connection = \OC::$server->getDatabaseConnection();
$this->prepareTestingTable();
}

public function likeProvider() {
Expand Down Expand Up @@ -150,6 +156,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')
Expand All @@ -160,4 +219,31 @@ protected function createConfig($appId, $key, $value) {
])
->execute();
}

protected function prepareTestingTable(): void {
if ($this->schemaSetup) {
$this->connection->getQueryBuilder()->delete('testing')->executeStatement();
}

$prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_');
$schema = $this->connection->createSchema();
try {
$schema->getTable($prefix . 'testing');
$this->connection->getQueryBuilder()->delete('testing')->executeStatement();
} catch (SchemaException $e) {
$this->schemaSetup = true;
$table = $schema->createTable($prefix . 'testing');
$table->addColumn('id', Types::BIGINT, [
'autoincrement' => true,
'notnull' => true,
]);

$table->addColumn('datetime', Types::DATETIME_MUTABLE, [
'notnull' => false,
]);

$table->setPrimaryKey(['id']);
$this->connection->migrateToSchema($schema);
}
}
}

0 comments on commit 58c4c1e

Please sign in to comment.