Skip to content

Commit

Permalink
test(22943): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-rollun committed Feb 15, 2024
1 parent 2738ffe commit 065533f
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
159 changes: 159 additions & 0 deletions test/functional/DataStore/DataStore/QueryTest/DateField/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\QueryTest\DateField;

use DateTimeImmutable;
use DateTimeInterface;
use rollun\datastore\DataStore\Interfaces\DataStoreInterface;
use rollun\test\functional\FunctionalTestCase;
use Xiag\Rql\Parser\Node\AbstractQueryNode;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\EqNode;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\GeNode;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\GtNode;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\LeNode;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\LtNode;
use Xiag\Rql\Parser\Query;

abstract class BaseTest extends FunctionalTestCase
{
protected const ID_NAME = 'id';
protected const FIELD_NAME = 'date';
protected const TABLE_NAME = 'date_test';

public function filterDataProvider(): array
{
return [
// Test equals
'Equal to itself' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())],
new EqNode(self::FIELD_NAME, $date),
[$expectedRecord],
],
'Do not equal to another date' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format(new DateTimeImmutable())],
new EqNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('-1 day'))),
[],
],
// 'Do not equal to ATOM date format' => [
// [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
// new EqNode(self::FIELD_NAME, $date->format(DateTimeInterface::ATOM)),
// [],
// ],
// 'Do not equal to RFC822 date format' => [
// [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
// new EqNode(self::FIELD_NAME, $date->format(DateTimeInterface::RFC822)),
// [],
// ],

// Test greater than
'Greater than test date is greater' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new GtNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))),
[],
],
'Greater than test date is greater (short)' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->shortFormat($date = new DateTimeImmutable())],
new GtNode(self::FIELD_NAME, $this->shortFormat($date->modify('+1 second'))),
[],
],
'Greater than test date is lower' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new GtNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))),
[$expectedRecord],
],
'Greater than test date is same' => [
[self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())],
new GtNode(self::FIELD_NAME, $date),
[],
],
'Greater than test date is same (short)' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable('2023-12-30 00:00:00'))],
new GtNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('2024-12-30'))),
[],
],

// Test lower than
'Lower than test date is greater' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new LtNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))),
[$expectedRecord],
],
'Lower than test date is lower' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new LtNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))),
[],
],
'Lower than test date is same' => [
[self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())],
new LtNode(self::FIELD_NAME, $date),
[],
],

// Test greater than or equals
'Greater than or equal test date is greater' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new GeNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))),
[],
],
'Greater than or equal test date is lower' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new GeNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))),
[$expectedRecord],
],
'Greater than or equal test date is same' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())],
new GeNode(self::FIELD_NAME, $date),
[$expectedRecord],
],
'Greater than or equal test date is same (short)' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format(new DateTimeImmutable('2023-12-30 00:00:00'))],
new GtNode(self::FIELD_NAME, $this->format(new DateTimeImmutable('2024-12-30'))),
[],
],

// Test lower than or equals
'Lower than or equal test date is greater' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new LeNode(self::FIELD_NAME, $this->format($date->modify('+1 second'))),
[$expectedRecord],
],
'Lower than or equal test date is lower' => [
[self::ID_NAME => 1, self::FIELD_NAME => $this->format($date = new DateTimeImmutable())],
new LeNode(self::FIELD_NAME, $this->format($date->modify('-1 second'))),
[],
],
'Lower than or equal test date is same' => [
$expectedRecord = [self::ID_NAME => 1, self::FIELD_NAME => $date = $this->format(new DateTimeImmutable())],
new LeNode(self::FIELD_NAME, $date),
[$expectedRecord],
],
];
}

/**
* @dataProvider filterDataProvider
*/
public function testFilter(array $record, AbstractQueryNode $queryNode, array $expectedRecords): void
{
$dataStore = $this->getDataStore();
$dataStore->create($record);

$query = new Query();
$query->setQuery($queryNode);
$records = $dataStore->query($query);

self::assertEquals($expectedRecords, $records);
}

private function format(DateTimeInterface $dateTime): string
{
return $dateTime->format('Y-m-d H:i:s');
}

private function shortFormat(DateTimeInterface $dateTime): string
{
return $dateTime->format('Y-m-d');
}

abstract protected function getDataStore(): DataStoreInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\QueryTest\DateField;

use rollun\datastore\DataStore\CsvBase;
use rollun\datastore\DataStore\Interfaces\DataStoreInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\FlockStore;

class CsvBaseTest extends BaseTest
{
/**
* @var CsvBase
*/
private $csvBase;

protected function getDataStore(): DataStoreInterface
{
if ($this->csvBase === null) {
$this->csvBase = $this->setUpCsvBase();
}
return $this->csvBase;
}

protected function tearDown(): void
{
parent::tearDown();
$path = __DIR__ . '/' . self::TABLE_NAME . '.csv';
if (file_exists($path)) {
unlink($path);
}
}

private function setUpCsvBase(): CsvBase
{
$path = __DIR__ . '/' . self::TABLE_NAME . '.csv';
if (file_exists($path)) {
unlink($path);
}

$file = fopen($path, 'w');
fputcsv($file, [self::ID_NAME, self::FIELD_NAME]);
fclose($file);

$lockFactory = new LockFactory(new FlockStore());
return new CsvBase($path, ',', $lockFactory->createLock($path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\QueryTest\DateField;

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\TableGateway\TableGateway;
use rollun\datastore\DataStore\DbTable;
use rollun\datastore\DataStore\Interfaces\DataStoreInterface;
use rollun\datastore\TableGateway\TableManagerMysql;

class DbTableTest extends BaseTest
{
/**
* @var DbTable
*/
private $dbTable;

/**
* @var TableManagerMysql
*/
private $mysqlManager;


protected function getDataStore(): DataStoreInterface
{
if ($this->dbTable === null) {
$this->dbTable = $this->setUpDbTable();
}
return $this->dbTable;
}

protected function tearDown(): void
{
parent::tearDown();
$this->getMysqlManager()->deleteTable(self::TABLE_NAME);
}

private function setUpDbTable(): DbTable
{
$mysqlManager = $this->getMysqlManager();

if ($mysqlManager->hasTable(self::TABLE_NAME)) {
$mysqlManager->deleteTable(self::TABLE_NAME);
}

$mysqlManager->createTable(self::TABLE_NAME, [
self::ID_NAME => [
'field_type' => TableManagerMysql::TYPE_INTEGER,
],
self::FIELD_NAME => [
'field_type' => TableManagerMysql::TYPE_DATETIME,
],
]);
$tableGateway = new TableGateway(self::TABLE_NAME, $this->getDbAdapter());

return new DbTable($tableGateway);
}

private function getMysqlManager(): TableManagerMysql
{
if ($this->mysqlManager === null) {
$this->mysqlManager = new TableManagerMysql($this->getDbAdapter());
}
return $this->mysqlManager;
}

private function getDbAdapter(): Adapter
{
return $this->getContainer()->get('db');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\QueryTest\DateField;

use rollun\datastore\DataStore\Interfaces\DataStoreInterface;
use rollun\datastore\DataStore\Memory;

class MemoryTest extends BaseTest
{
/**
* @var Memory
*/
private $dataStore;

protected function getDataStore(): DataStoreInterface
{
if ($this->dataStore === null) {
$this->dataStore = new Memory([self::ID_NAME, self::FIELD_NAME]);
}
return $this->dataStore;
}
}

1 comment on commit 065533f

@misha-rollun
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please sign in to comment.