From 9607e5f0fb7898664cf8bac426dcc8a274883458 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Sun, 22 Nov 2020 17:11:33 -0300 Subject: [PATCH] Fix Oracle tests --- src/TestUtility/TestCommandTrait.php | 60 ++++++++++++++++++----- src/TestUtility/TestConnectionTrait.php | 38 +++++++------- src/TestUtility/TestQueryBuilderTrait.php | 30 ++++++------ src/TestUtility/TestQueryTrait.php | 14 ++++-- 4 files changed, 92 insertions(+), 50 deletions(-) diff --git a/src/TestUtility/TestCommandTrait.php b/src/TestUtility/TestCommandTrait.php index 3261787c4..e4edb4072 100644 --- a/src/TestUtility/TestCommandTrait.php +++ b/src/TestUtility/TestCommandTrait.php @@ -4,16 +4,8 @@ namespace Yiisoft\Db\TestUtility; -use function call_user_func_array; -use function date; -use function is_array; use PDO; -use function range; -use function rtrim; -use function setlocale; use Throwable; -use function time; - use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Data\DataReader; use Yiisoft\Db\Exception\Exception; @@ -22,6 +14,14 @@ use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Schema; +use function call_user_func_array; +use function date; +use function is_array; +use function range; +use function rtrim; +use function setlocale; +use function time; + trait TestCommandTrait { public function testConstruct(): void @@ -283,7 +283,7 @@ public function testBatchInsertDataTypesLocale(): void $this->markTestSkipped('Your platform does not support locales.'); } - $db = $this->getConnection(); + $db = $this->getConnection(true); try { /* This one sets decimal mark to comma sign */ @@ -300,12 +300,17 @@ public function testBatchInsertDataTypesLocale(): void /* clear data in "type" table */ $db->createCommand()->delete('type')->execute(); + /* change, for point oracle. */ + if ($db->getDriverName() === 'oci') { + $db->createCommand("ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'")->execute(); + } + /* batch insert on "type" table */ $db->createCommand()->batchInsert('type', $cols, $data)->execute(); $data = $db->createCommand( - 'SELECT int_col, char_col, float_col, bool_col FROM {{type}} WHERE [[int_col]] IN (1,2,3) - ORDER BY [[int_col]];' + 'SELECT [[int_col]], [[char_col]], [[float_col]], [[bool_col]] ' . + 'FROM {{type}} WHERE [[int_col]] IN (1,2,3) ORDER BY [[int_col]]' )->queryAll(); $this->assertCount(3, $data); @@ -1403,4 +1408,37 @@ public function upsertProviderTrait(): array ], ]; } + + public function testAlterTable(): void + { + $db = $this->getConnection(); + + if ($db->getDriverName() === 'sqlite') { + $this->markTestSkipped('Sqlite does not support alterTable'); + } + + if ($db->getSchema()->getTableSchema('testAlterTable') !== null) { + $db->createCommand()->dropTable('testAlterTable')->execute(); + } + + $db->createCommand()->createTable( + 'testAlterTable', + [ + 'id' => Schema::TYPE_PK, + 'bar' => Schema::TYPE_INTEGER, + ] + )->execute(); + + $db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute(); + + $db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute(); + + $db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute(); + + $records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}}')->queryAll(); + $this->assertEquals([ + ['id' => 1, 'bar' => 1], + ['id' => 2, 'bar' => 'hello'], + ], $records); + } } diff --git a/src/TestUtility/TestConnectionTrait.php b/src/TestUtility/TestConnectionTrait.php index 76d6ef3fd..97681935c 100644 --- a/src/TestUtility/TestConnectionTrait.php +++ b/src/TestUtility/TestConnectionTrait.php @@ -4,14 +4,14 @@ namespace Yiisoft\Db\TestUtility; -use function serialize; -use function unserialize; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; - use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Transaction\Transaction; +use function serialize; +use function unserialize; + trait TestConnectionTrait { public function testSerialize(): void @@ -49,7 +49,7 @@ public function testTransaction(): void $this->assertFalse($transaction->isActive()); $this->assertNull($db->getTransaction()); $this->assertEquals(0, $db->createCommand( - "SELECT COUNT(*) FROM profile WHERE description = 'test transaction';" + "SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'" )->queryScalar()); $transaction = $db->beginTransaction(); @@ -61,7 +61,7 @@ public function testTransaction(): void $this->assertFalse($transaction->isActive()); $this->assertNull($db->getTransaction()); $this->assertEquals(1, $db->createCommand( - "SELECT COUNT(*) FROM profile WHERE description = 'test transaction';" + "SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'" )->queryScalar()); } @@ -91,20 +91,18 @@ public function testTransactionIsolation(): void public function testTransactionShortcutException(): void { - $db = $this->getConnection(); - - $result = $db->transaction(static function (ConnectionInterface $db) { - $db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute(); - return true; - }, Transaction::READ_UNCOMMITTED); + $db = $this->getConnection(true); - $this->assertTrue($result, 'transaction shortcut valid value should be returned from callback'); + $this->expectException(Exception::class); + $db->transaction(function () use ($db) { + $db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute(); + throw new Exception('Exception in transaction shortcut'); + }); $profilesCount = $db->createCommand( - "SELECT COUNT(*) FROM profile WHERE description = 'test transaction shortcut';" + "SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'" )->queryScalar(); - - $this->assertEquals(1, $profilesCount, 'profile should be inserted in transaction shortcut'); + $this->assertEquals(0, $profilesCount, 'profile should not be inserted in transaction shortcut'); } public function testTransactionShortcutCorrect(): void @@ -119,7 +117,7 @@ public function testTransactionShortcutCorrect(): void $this->assertTrue($result, 'transaction shortcut valid value should be returned from callback'); $profilesCount = $db->createCommand( - "SELECT COUNT(*) FROM profile WHERE description = 'test transaction shortcut';" + "SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'" )->queryScalar(); $this->assertEquals(1, $profilesCount, 'profile should be inserted in transaction shortcut'); @@ -203,7 +201,7 @@ public function testEnableQueryLog(): void $this->logger->flush(); $this->profiler->flush(); - $db->createCommand('SELECT * FROM qlog1')->queryAll(); + $db->createCommand('SELECT * FROM {{qlog1}}')->queryAll(); $this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); $this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); @@ -224,7 +222,7 @@ public function testEnableQueryLog(): void $this->logger->flush(); $this->profiler->flush(); - $db->createCommand('SELECT * FROM qlog2')->queryAll(); + $db->createCommand('SELECT * FROM {{qlog2}}')->queryAll(); $this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); $this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages')); @@ -245,7 +243,7 @@ public function testEnableQueryLog(): void $this->logger->flush(); $this->profiler->flush(); - $db->createCommand('SELECT * FROM qlog3')->queryAll(); + $db->createCommand('SELECT * FROM {{qlog3}}')->queryAll(); $this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages')); $this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); @@ -263,7 +261,7 @@ public function testEnableQueryLog(): void $this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); $this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); - $db->createCommand('SELECT * FROM qlog4')->queryAll(); + $db->createCommand('SELECT * FROM {{qlog4}}')->queryAll(); $this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages')); $this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages')); diff --git a/src/TestUtility/TestQueryBuilderTrait.php b/src/TestUtility/TestQueryBuilderTrait.php index 40f34adc5..5f7835d52 100644 --- a/src/TestUtility/TestQueryBuilderTrait.php +++ b/src/TestUtility/TestQueryBuilderTrait.php @@ -4,25 +4,25 @@ namespace Yiisoft\Db\TestUtility; -use function array_key_exists; -use function array_merge; -use function array_values; -use function is_array; -use function preg_match_all; -use function str_replace; -use function strncmp; -use function substr; use Yiisoft\Db\Connection\Connection; - use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Query\Conditions\BetweenColumnsCondition; use Yiisoft\Db\Query\Conditions\InCondition; use Yiisoft\Db\Query\Conditions\LikeCondition; +use Yiisoft\Db\Query\Conditions\BetweenColumnsCondition; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryBuilder; use Yiisoft\Db\Schema\Schema; use Yiisoft\Db\Schema\SchemaBuilderTrait; +use function array_key_exists; +use function array_merge; +use function array_values; +use function is_array; +use function preg_match_all; +use function str_replace; +use function strncmp; +use function substr; + trait TestQueryBuilderTrait { use SchemaBuilderTrait; @@ -937,10 +937,10 @@ public function columnTypes(): array $this->primaryKey()->first(), [ 'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST', - 'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY', 'sqlsrv' => 'int IDENTITY PRIMARY KEY', ], [ + 'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY FIRST', 'sqlsrv' => 'pk', ], ], @@ -949,10 +949,10 @@ public function columnTypes(): array $this->integer()->first(), [ 'mysql' => 'int(11) FIRST', - 'oci' => 'NUMBER(10)', 'sqlsrv' => 'int', ], [ + 'oci' => 'NUMBER(10) FIRST', 'pgsql' => 'integer', 'sqlsrv' => 'integer', ], @@ -962,10 +962,10 @@ public function columnTypes(): array $this->string()->first(), [ 'mysql' => 'varchar(255) FIRST', - 'oci' => 'VARCHAR2(255)', 'sqlsrv' => 'nvarchar(255)', ], [ + 'oci' => 'VARCHAR2(255) FIRST', 'sqlsrv' => 'string', ], ], @@ -974,10 +974,10 @@ public function columnTypes(): array $this->integer()->append('NOT NULL')->first(), [ 'mysql' => 'int(11) NOT NULL FIRST', - 'oci' => 'NUMBER(10) NOT NULL', 'sqlsrv' => 'int NOT NULL', ], [ + 'oci' => 'NUMBER(10) NOT NULL FIRST', 'sqlsrv' => 'integer NOT NULL', ], ], @@ -986,10 +986,10 @@ public function columnTypes(): array $this->string()->append('NOT NULL')->first(), [ 'mysql' => 'varchar(255) NOT NULL FIRST', - 'oci' => 'VARCHAR2(255) NOT NULL', 'sqlsrv' => 'nvarchar(255) NOT NULL', ], [ + 'oci' => 'VARCHAR2(255) NOT NULL FIRST', 'sqlsrv' => 'string NOT NULL', ], ], diff --git a/src/TestUtility/TestQueryTrait.php b/src/TestUtility/TestQueryTrait.php index a41d60fec..f2d4979ab 100644 --- a/src/TestUtility/TestQueryTrait.php +++ b/src/TestUtility/TestQueryTrait.php @@ -483,19 +483,21 @@ public function testLimitOffsetWithExpression(): void $result = $query->column(); $this->assertCount(2, $result); - if ($db->getDriverName() !== 'sqlsrv') { + + if ($db->getDriverName() !== 'sqlsrv' && $db->getDriverName() !== 'oci') { $this->assertContains(2, $result); $this->assertContains(3, $result); } else { $this->assertContains('2', $result); $this->assertContains('3', $result); } + $this->assertNotContains(1, $result); } public function testOne(): void { - $db = $this->getConnection(); + $db = $this->getConnection(true); $result = (new Query($db))->from('customer')->where(['status' => 2])->one(); @@ -773,10 +775,14 @@ public function testMultipleLikeConditions(): void */ public function testExpressionInFrom(): void { - $db = $this->getConnection(); + $db = $this->getConnection(true); $query = (new Query($db)) - ->from(new Expression('(SELECT id, name, email, address, status FROM customer) c')) + ->from( + new Expression( + '(SELECT [[id]], [[name]], [[email]], [[address]], [[status]] FROM {{customer}}) c' + ) + ) ->where(['status' => 2]); $result = $query->one();