mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 13:58:55 +08:00
Fix MSSQL tests (#17426)
This commit is contained in:
committed by
Alexander Makarov
parent
7e77dd2322
commit
d98f4e69b5
@ -7,7 +7,6 @@
|
||||
|
||||
namespace yiiunit\framework\db;
|
||||
|
||||
use Yii;
|
||||
use yii\db\BatchQueryResult;
|
||||
use yii\db\Query;
|
||||
use yiiunit\data\ar\ActiveRecord;
|
||||
@ -23,10 +22,6 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
|
||||
public function testQuery()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
|
||||
// initialize property test
|
||||
@ -40,20 +35,14 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
// normal query
|
||||
$query = new Query();
|
||||
$query->from('customer')->orderBy('id');
|
||||
$allRows = [];
|
||||
$batch = $query->batch(2, $db);
|
||||
foreach ($batch as $rows) {
|
||||
$allRows = array_merge($allRows, $rows);
|
||||
}
|
||||
$allRows = $this->getAllRowsFromBach($batch);
|
||||
$this->assertCount(3, $allRows);
|
||||
$this->assertEquals('user1', $allRows[0]['name']);
|
||||
$this->assertEquals('user2', $allRows[1]['name']);
|
||||
$this->assertEquals('user3', $allRows[2]['name']);
|
||||
// rewind
|
||||
$allRows = [];
|
||||
foreach ($batch as $rows) {
|
||||
$allRows = array_merge($allRows, $rows);
|
||||
}
|
||||
$allRows = $this->getAllRowsFromBach($batch);
|
||||
$this->assertCount(3, $allRows);
|
||||
// reset
|
||||
$batch->reset();
|
||||
@ -71,10 +60,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
// query with index
|
||||
$query = new Query();
|
||||
$query->from('customer')->indexBy('name');
|
||||
$allRows = [];
|
||||
foreach ($query->batch(2, $db) as $rows) {
|
||||
$allRows = array_merge($allRows, $rows);
|
||||
}
|
||||
$allRows = $this->getAllRowsFromBach($query->batch(2, $db));
|
||||
$this->assertCount(3, $allRows);
|
||||
$this->assertEquals('address1', $allRows['user1']['address']);
|
||||
$this->assertEquals('address2', $allRows['user2']['address']);
|
||||
@ -83,10 +69,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
// each
|
||||
$query = new Query();
|
||||
$query->from('customer')->orderBy('id');
|
||||
$allRows = [];
|
||||
foreach ($query->each(2, $db) as $index => $row) {
|
||||
$allRows[$index] = $row;
|
||||
}
|
||||
$allRows = $this->getAllRowsFromEach($query->each(2, $db));
|
||||
$this->assertCount(3, $allRows);
|
||||
$this->assertEquals('user1', $allRows[0]['name']);
|
||||
$this->assertEquals('user2', $allRows[1]['name']);
|
||||
@ -95,10 +78,7 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
// each with key
|
||||
$query = new Query();
|
||||
$query->from('customer')->orderBy('id')->indexBy('name');
|
||||
$allRows = [];
|
||||
foreach ($query->each(100, $db) as $key => $row) {
|
||||
$allRows[$key] = $row;
|
||||
}
|
||||
$allRows = $this->getAllRowsFromEach($query->each(100, $db));
|
||||
$this->assertCount(3, $allRows);
|
||||
$this->assertEquals('address1', $allRows['user1']['address']);
|
||||
$this->assertEquals('address2', $allRows['user2']['address']);
|
||||
@ -107,17 +87,10 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
|
||||
public function testActiveQuery()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
|
||||
$query = Customer::find()->orderBy('id');
|
||||
$customers = [];
|
||||
foreach ($query->batch(2, $db) as $models) {
|
||||
$customers = array_merge($customers, $models);
|
||||
}
|
||||
$customers = $this->getAllRowsFromBach($query->batch(2, $db));
|
||||
$this->assertCount(3, $customers);
|
||||
$this->assertEquals('user1', $customers[0]->name);
|
||||
$this->assertEquals('user2', $customers[1]->name);
|
||||
@ -125,16 +98,33 @@ abstract class BatchQueryResultTest extends DatabaseTestCase
|
||||
|
||||
// batch with eager loading
|
||||
$query = Customer::find()->with('orders')->orderBy('id');
|
||||
$customers = [];
|
||||
foreach ($query->batch(2, $db) as $models) {
|
||||
$customers = array_merge($customers, $models);
|
||||
foreach ($models as $model) {
|
||||
$this->assertTrue($model->isRelationPopulated('orders'));
|
||||
}
|
||||
$customers = $this->getAllRowsFromBach($query->batch(2, $db));
|
||||
foreach ($customers as $customer) {
|
||||
$this->assertTrue($customer->isRelationPopulated('orders'));
|
||||
}
|
||||
$this->assertCount(3, $customers);
|
||||
$this->assertCount(1, $customers[0]->orders);
|
||||
$this->assertCount(2, $customers[1]->orders);
|
||||
$this->assertCount(0, $customers[2]->orders);
|
||||
}
|
||||
|
||||
protected function getAllRowsFromBach(BatchQueryResult $batch)
|
||||
{
|
||||
$allRows = [];
|
||||
foreach ($batch as $rows) {
|
||||
$allRows = array_merge($allRows, $rows);
|
||||
}
|
||||
|
||||
return $allRows;
|
||||
}
|
||||
|
||||
protected function getAllRowsFromEach(BatchQueryResult $each)
|
||||
{
|
||||
$allRows = [];
|
||||
foreach ($each as $index => $row) {
|
||||
$allRows[$index] = $row;
|
||||
}
|
||||
|
||||
return $allRows;
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,6 @@ use yii\db\Connection;
|
||||
use yii\db\DataReader;
|
||||
use yii\db\Exception;
|
||||
use yii\db\Expression;
|
||||
use yii\db\JsonExpression;
|
||||
use yii\db\Query;
|
||||
use yii\db\Schema;
|
||||
|
||||
@ -218,7 +217,7 @@ SQL;
|
||||
}
|
||||
$this->assertEquals($numericCol, $row['numeric_col']);
|
||||
if ($this->driverName === 'mysql' || $this->driverName === 'oci' || (\defined('HHVM_VERSION') && \in_array($this->driverName, ['sqlite', 'pgsql']))) {
|
||||
$this->assertEquals($boolCol, (int) $row['bool_col']);
|
||||
$this->assertEquals($boolCol, (int)$row['bool_col']);
|
||||
} else {
|
||||
$this->assertEquals($boolCol, $row['bool_col']);
|
||||
}
|
||||
@ -237,7 +236,7 @@ SQL;
|
||||
|
||||
public function paramsNonWhereProvider()
|
||||
{
|
||||
return[
|
||||
return [
|
||||
['SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email GROUP BY SUBSTR(name, :len)'],
|
||||
['SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email ORDER BY SUBSTR(name, :len)'],
|
||||
['SELECT SUBSTR(name, :len) FROM {{customer}} WHERE [[email]] = :email'],
|
||||
@ -428,10 +427,6 @@ SQL;
|
||||
*/
|
||||
public function testBatchInsertSQL($table, $columns, $values, $expected, array $expectedParams = [])
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$command = $this->getConnection()->createCommand();
|
||||
$command->batchInsert($table, $columns, $values);
|
||||
$command->prepare(false);
|
||||
@ -467,22 +462,22 @@ SQL;
|
||||
*/
|
||||
public function testNoTablenameReplacement()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
|
||||
$db->createCommand()->insert(
|
||||
'{{customer}}',
|
||||
[
|
||||
'id' => 43,
|
||||
'name' => 'Some {{weird}} name',
|
||||
'email' => 'test@example.com',
|
||||
'address' => 'Some {{%weird}} address',
|
||||
]
|
||||
)->execute();
|
||||
$customer = $db->createCommand('SELECT * FROM {{customer}} WHERE id=43')->queryOne();
|
||||
if ($this->driverName === 'pgsql') {
|
||||
$customerId = $db->getLastInsertID('public.customer_id_seq');
|
||||
} else {
|
||||
$customerId = $db->getLastInsertID();
|
||||
}
|
||||
$customer = $db->createCommand('SELECT * FROM {{customer}} WHERE id=' . $customerId)->queryOne();
|
||||
$this->assertEquals('Some {{weird}} name', $customer['name']);
|
||||
$this->assertEquals('Some {{%weird}} address', $customer['address']);
|
||||
|
||||
@ -492,9 +487,9 @@ SQL;
|
||||
'name' => 'Some {{updated}} name',
|
||||
'address' => 'Some {{%updated}} address',
|
||||
],
|
||||
['id' => 43]
|
||||
['id' => $customerId]
|
||||
)->execute();
|
||||
$customer = $db->createCommand('SELECT * FROM {{customer}} WHERE id=43')->queryOne();
|
||||
$customer = $db->createCommand('SELECT * FROM {{customer}} WHERE id=' . $customerId)->queryOne();
|
||||
$this->assertEquals('Some {{updated}} name', $customer['name']);
|
||||
$this->assertEquals('Some {{%updated}} address', $customer['address']);
|
||||
}
|
||||
@ -678,10 +673,6 @@ SQL;
|
||||
|
||||
public function testsInsertQueryAsColumnValue()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$time = time();
|
||||
|
||||
$db = $this->getConnection();
|
||||
@ -689,29 +680,33 @@ SQL;
|
||||
|
||||
$command = $db->createCommand();
|
||||
$command->insert('{{order}}', [
|
||||
'id' => 42,
|
||||
'customer_id' => 1,
|
||||
'created_at' => $time,
|
||||
'total' => 42,
|
||||
])->execute();
|
||||
if ($this->driverName === 'pgsql') {
|
||||
$orderId = $db->getLastInsertID('public.order_id_seq');
|
||||
} else {
|
||||
$orderId = $db->getLastInsertID();
|
||||
}
|
||||
|
||||
$columnValueQuery = new \yii\db\Query();
|
||||
$columnValueQuery->select('created_at')->from('{{order}}')->where(['id' => '42']);
|
||||
$columnValueQuery->select('created_at')->from('{{order}}')->where(['id' => $orderId]);
|
||||
|
||||
$command = $db->createCommand();
|
||||
$command->insert(
|
||||
'{{order_with_null_fk}}',
|
||||
[
|
||||
'customer_id' => 42,
|
||||
'customer_id' => $orderId,
|
||||
'created_at' => $columnValueQuery,
|
||||
'total' => 42,
|
||||
]
|
||||
)->execute();
|
||||
|
||||
$this->assertEquals($time, $db->createCommand('SELECT [[created_at]] FROM {{order_with_null_fk}} WHERE [[customer_id]] = 42')->queryScalar());
|
||||
$this->assertEquals($time, $db->createCommand('SELECT [[created_at]] FROM {{order_with_null_fk}} WHERE [[customer_id]] = ' . $orderId)->queryScalar());
|
||||
|
||||
$db->createCommand('DELETE FROM {{order_with_null_fk}}')->execute();
|
||||
$db->createCommand('DELETE FROM {{order}} WHERE [[id]] = 42')->execute();
|
||||
$db->createCommand('DELETE FROM {{order}} WHERE [[id]] = ' . $orderId)->execute();
|
||||
}
|
||||
|
||||
public function testCreateTable()
|
||||
@ -1386,6 +1381,8 @@ SQL;
|
||||
public function testAutoRefreshTableSchema()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
|
||||
// related to https://github.com/yiisoft/yii2/pull/17364
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
|
||||
@ -120,6 +120,7 @@ abstract class QueryTest extends DatabaseTestCase
|
||||
}
|
||||
|
||||
use GetTablesAliasTestTrait;
|
||||
|
||||
protected function createQuery()
|
||||
{
|
||||
return new Query();
|
||||
@ -338,13 +339,9 @@ abstract class QueryTest extends DatabaseTestCase
|
||||
|
||||
public function testUnion()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$connection = $this->getConnection();
|
||||
$query = new Query();
|
||||
$query->select(['id', 'name'])
|
||||
$query = (new Query())
|
||||
->select(['id', 'name'])
|
||||
->from('item')
|
||||
->limit(2)
|
||||
->union(
|
||||
@ -445,10 +442,6 @@ abstract class QueryTest extends DatabaseTestCase
|
||||
|
||||
public function testCount()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
|
||||
$count = (new Query())->from('customer')->count('*', $db);
|
||||
@ -457,7 +450,7 @@ abstract class QueryTest extends DatabaseTestCase
|
||||
$count = (new Query())->from('customer')->where(['status' => 2])->count('*', $db);
|
||||
$this->assertEquals(1, $count);
|
||||
|
||||
$count = (new Query())->select('[[status]], COUNT([[id]])')->from('customer')->groupBy('status')->count('*', $db);
|
||||
$count = (new Query())->select('[[status]], COUNT([[id]]) cnt')->from('customer')->groupBy('status')->count('*', $db);
|
||||
$this->assertEquals(2, $count);
|
||||
|
||||
// testing that orderBy() should be ignored here as it does not affect the count anyway.
|
||||
@ -605,7 +598,7 @@ abstract class QueryTest extends DatabaseTestCase
|
||||
->where($whereCondition)
|
||||
->count('*', $db);
|
||||
if (is_numeric($result)) {
|
||||
$result = (int) $result;
|
||||
$result = (int)$result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
namespace yiiunit\framework\db\mssql;
|
||||
|
||||
use yii\db\BatchQueryResult;
|
||||
|
||||
/**
|
||||
* @group db
|
||||
* @group mssql
|
||||
@ -14,4 +16,37 @@ namespace yiiunit\framework\db\mssql;
|
||||
class BatchQueryResultTest extends \yiiunit\framework\db\BatchQueryResultTest
|
||||
{
|
||||
public $driverName = 'sqlsrv';
|
||||
private $noMoreRowsErrorMessage = 'SQLSTATE[IMSSP]: There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved.';
|
||||
|
||||
protected function getAllRowsFromBach(BatchQueryResult $batch)
|
||||
{
|
||||
$allRows = [];
|
||||
try {
|
||||
foreach ($batch as $rows) {
|
||||
$allRows = array_merge($allRows, $rows);
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
if ($e->getMessage() !== $this->noMoreRowsErrorMessage) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
return $allRows;
|
||||
}
|
||||
|
||||
protected function getAllRowsFromEach(BatchQueryResult $each)
|
||||
{
|
||||
$allRows = [];
|
||||
try {
|
||||
foreach ($each as $index => $row) {
|
||||
$allRows[$index] = $row;
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
if ($e->getMessage() !== $this->noMoreRowsErrorMessage) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
return $allRows;
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,7 +121,9 @@ class CommandTest extends \yiiunit\framework\db\CommandTest
|
||||
{
|
||||
$data = parent::batchInsertSqlProvider();
|
||||
$data['issue11242']['expected'] = 'INSERT INTO [type] ([int_col], [float_col], [char_col]) VALUES (NULL, NULL, \'Kyiv {{city}}, Ukraine\')';
|
||||
$data['wrongBehavior']['expected'] = 'INSERT INTO [type] ([int_col], [float_col], [char_col]) VALUES (\'\', \'\', \'Kyiv {{city}}, Ukraine\')';
|
||||
$data['wrongBehavior']['expected'] = 'INSERT INTO [type] ([type].[int_col], [float_col], [char_col]) VALUES (\'\', \'\', \'Kyiv {{city}}, Ukraine\')';
|
||||
$data['batchInsert binds params from expression']['expected'] = 'INSERT INTO [type] ([int_col]) VALUES (:qp1)';
|
||||
unset($data['batchIsert empty rows represented by ArrayObject']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
namespace yiiunit\framework\db\mssql;
|
||||
|
||||
use yii\db\Query;
|
||||
|
||||
/**
|
||||
* @group db
|
||||
* @group mssql
|
||||
@ -14,4 +16,33 @@ namespace yiiunit\framework\db\mssql;
|
||||
class QueryTest extends \yiiunit\framework\db\QueryTest
|
||||
{
|
||||
protected $driverName = 'sqlsrv';
|
||||
|
||||
public function testUnion()
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
|
||||
// MSSQL supports limit only in sub queries with UNION
|
||||
$query = (new Query())
|
||||
->select(['id', 'name'])
|
||||
->from(
|
||||
(new Query())
|
||||
->select(['id', 'name'])
|
||||
->from('item')
|
||||
->limit(2)
|
||||
)
|
||||
->union(
|
||||
(new Query())
|
||||
->select(['id', 'name'])
|
||||
->from(
|
||||
(new Query())
|
||||
->select(['id', 'name'])
|
||||
->from(['category'])
|
||||
->limit(2)
|
||||
)
|
||||
);
|
||||
|
||||
$result = $query->all($connection);
|
||||
$this->assertNotEmpty($result);
|
||||
$this->assertCount(4, $result);
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,6 +16,28 @@ use yiiunit\framework\db\DatabaseTestCase;
|
||||
class ProfileFixture extends ActiveFixture
|
||||
{
|
||||
public $modelClass = 'yiiunit\data\ar\Profile';
|
||||
|
||||
public function beforeLoad()
|
||||
{
|
||||
if ($this->db->driverName === 'sqlsrv') {
|
||||
$this->db->createCommand()->truncateTable('profile')->execute();
|
||||
}
|
||||
|
||||
parent::beforeLoad();
|
||||
}
|
||||
|
||||
protected function getData()
|
||||
{
|
||||
$data = parent::getData();
|
||||
|
||||
if ($this->db->driverName === 'sqlsrv') {
|
||||
array_walk($data, static function (&$item) {
|
||||
unset($item['id']);
|
||||
});
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
class CustomerFixture extends ActiveFixture
|
||||
@ -25,6 +47,15 @@ class CustomerFixture extends ActiveFixture
|
||||
public $depends = [
|
||||
'yiiunit\framework\test\ProfileFixture',
|
||||
];
|
||||
|
||||
public function beforeLoad()
|
||||
{
|
||||
if ($this->db->driverName === 'sqlsrv') {
|
||||
$this->db->createCommand()->truncateTable('customer')->execute();
|
||||
}
|
||||
|
||||
parent::beforeLoad();
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDirectoryFixture extends ActiveFixture
|
||||
@ -32,6 +63,15 @@ class CustomDirectoryFixture extends ActiveFixture
|
||||
public $modelClass = 'yiiunit\data\ar\Customer';
|
||||
|
||||
public $dataDirectory = '@app/framework/test/custom';
|
||||
|
||||
public function beforeLoad()
|
||||
{
|
||||
if ($this->db->driverName === 'sqlsrv') {
|
||||
$this->db->createCommand()->truncateTable('customer')->execute();
|
||||
}
|
||||
|
||||
parent::beforeLoad();
|
||||
}
|
||||
}
|
||||
|
||||
class AnimalFixture extends ActiveFixture
|
||||
@ -122,10 +162,6 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testGetData()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$test = new CustomerDbTestCase();
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
@ -145,10 +181,6 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testGetModel()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$test = new CustomerDbTestCase();
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
@ -167,10 +199,6 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testDataDirectory()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$test = new CustomDirectoryDbTestCase();
|
||||
|
||||
$test->setUp();
|
||||
@ -185,10 +213,6 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testDataPath()
|
||||
{
|
||||
if ($this->driverName === 'sqlsrv') {
|
||||
$this->markTestSkipped('Should be fixed');
|
||||
}
|
||||
|
||||
$test = new DataPathDbTestCase();
|
||||
|
||||
$test->setUp();
|
||||
|
||||
Reference in New Issue
Block a user