diff --git a/tests/unit/data/postgres.sql b/tests/unit/data/postgres.sql index 071d25133f..c5656f2ad0 100644 --- a/tests/unit/data/postgres.sql +++ b/tests/unit/data/postgres.sql @@ -4,6 +4,7 @@ * and create an account 'postgres/postgres' which owns this test database. */ +DROP TABLE IF EXISTS "composite_fk" CASCADE; DROP TABLE IF EXISTS "order_item" CASCADE; DROP TABLE IF EXISTS "item" CASCADE; DROP TABLE IF EXISTS "order_item_with_null_fk" CASCADE; @@ -78,6 +79,14 @@ CREATE TABLE "order_item_with_null_fk" ( subtotal decimal(10,0) NOT NULL ); +CREATE TABLE "composite_fk" ( + id integer NOT NULL, + order_id integer NOT NULL, + item_id integer NOT NULL, + PRIMARY KEY (id), + CONSTRAINT FK_composite_fk_order_item FOREIGN KEY (order_id, item_id) REFERENCES "order_item" (order_id, item_id) ON DELETE CASCADE +); + CREATE TABLE "null_values" ( id INT NOT NULL, var1 INT NULL, @@ -101,7 +110,7 @@ CREATE TABLE "type" ( bool_col smallint NOT NULL, bool_col2 smallint DEFAULT '1', ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - bit_col BIT NOT NULL DEFAULT B'10000010' + bit_col BIT(8) NOT NULL DEFAULT B'10000010' ); INSERT INTO "profile" (description) VALUES ('profile customer 1'); diff --git a/tests/unit/framework/db/BatchQueryResultTest.php b/tests/unit/framework/db/BatchQueryResultTest.php index 0097415465..b5b10b3f76 100644 --- a/tests/unit/framework/db/BatchQueryResultTest.php +++ b/tests/unit/framework/db/BatchQueryResultTest.php @@ -14,8 +14,8 @@ use yii\db\BatchQueryResult; use yiiunit\data\ar\Customer; /** - * @author Qiang Xue - * @since 2.0 + * @group db + * @group mysql */ class BatchQueryResultTest extends DatabaseTestCase { diff --git a/tests/unit/framework/db/CommandTest.php b/tests/unit/framework/db/CommandTest.php index 0bb51bf33e..d6a587b0f1 100644 --- a/tests/unit/framework/db/CommandTest.php +++ b/tests/unit/framework/db/CommandTest.php @@ -159,7 +159,7 @@ class CommandTest extends DatabaseTestCase $sql = 'INSERT INTO type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)'; $command = $db->createCommand($sql); $intCol = 123; - $charCol = 'abc'; + $charCol = str_repeat('abc', 33) . 'x'; // a 100 char string $floatCol = 1.23; $blobCol = "\x10\x11\x12"; $numericCol = '1.23'; @@ -172,13 +172,21 @@ class CommandTest extends DatabaseTestCase $command->bindParam(':bool_col', $boolCol); $this->assertEquals(1, $command->execute()); - $sql = 'SELECT * FROM type'; - $row = $db->createCommand($sql)->queryOne(); + $command = $db->createCommand('SELECT int_col, char_col, float_col, blob_col, numeric_col, bool_col FROM type'); +// $command->prepare(); +// $command->pdoStatement->bindColumn('blob_col', $bc, \PDO::PARAM_LOB); + $row = $command->queryOne(); $this->assertEquals($intCol, $row['int_col']); $this->assertEquals($charCol, $row['char_col']); $this->assertEquals($floatCol, $row['float_col']); - $this->assertEquals($blobCol, $row['blob_col']); + if ($this->driverName === 'mysql' || $this->driverName === 'sqlite') { + $this->assertEquals($blobCol, $row['blob_col']); + } else { + $this->assertTrue(is_resource($row['blob_col'])); + $this->assertEquals($blobCol, stream_get_contents($row['blob_col'])); + } $this->assertEquals($numericCol, $row['numeric_col']); + $this->assertEquals($boolCol, $row['bool_col']); // bindValue $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; diff --git a/tests/unit/framework/db/SchemaTest.php b/tests/unit/framework/db/SchemaTest.php index e826bba591..b9f783ebe5 100644 --- a/tests/unit/framework/db/SchemaTest.php +++ b/tests/unit/framework/db/SchemaTest.php @@ -87,7 +87,7 @@ class SchemaTest extends DatabaseTestCase $schema = $this->getConnection()->schema; foreach ($values as $value) { - $this->assertEquals($value[1], $schema->getPdoType($value[0])); + $this->assertEquals($value[1], $schema->getPdoType($value[0]), 'type for value ' . print_r($value[0], true) . ' does not match.'); } fclose($fp); } diff --git a/tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php b/tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php index 0bbb8d429c..b810a0a770 100644 --- a/tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php +++ b/tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php @@ -3,6 +3,10 @@ namespace yii\tests\unit\framework\db\pgsql; use yiiunit\framework\db\CommandTest; +/** + * @group db + * @group pgsql + */ class PostgreSQLCommandTest extends CommandTest { public $driverName = 'pgsql'; @@ -15,9 +19,4 @@ class PostgreSQLCommandTest extends CommandTest $command = $db->createCommand($sql); $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); } - - public function testBindParamValue() - { - $this->markTestIncomplete('TODO: impement it'); - } -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/unit/framework/db/pgsql/PostgreSQLSchemaTest.php b/tests/unit/framework/db/pgsql/PostgreSQLSchemaTest.php index df94f5f5a3..a93027aad2 100644 --- a/tests/unit/framework/db/pgsql/PostgreSQLSchemaTest.php +++ b/tests/unit/framework/db/pgsql/PostgreSQLSchemaTest.php @@ -53,8 +53,31 @@ class PostgreSQLSchemaTest extends SchemaTest $columns['bool_col2']['scale'] = 0; $columns['ts_default']['defaultValue'] = new Expression('now()'); $columns['bit_col']['dbType'] = 'bit'; - $columns['bit_col']['size'] = 1; // TODO should be 8??? + $columns['bit_col']['size'] = 8; $columns['bit_col']['precision'] = null; return $columns; } + + public function testGetPDOType() + { + $values = [ + [null, \PDO::PARAM_NULL], + ['', \PDO::PARAM_STR], + ['hello', \PDO::PARAM_STR], + [0, \PDO::PARAM_INT], + [1, \PDO::PARAM_INT], + [1337, \PDO::PARAM_INT], + [true, \PDO::PARAM_INT], + [false, \PDO::PARAM_INT], + [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB], + ]; + + /* @var $schema Schema */ + $schema = $this->getConnection()->schema; + + foreach ($values as $value) { + $this->assertEquals($value[1], $schema->getPdoType($value[0])); + } + fclose($fp); + } } diff --git a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php index ef7387ce98..3d5a861833 100644 --- a/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php +++ b/tests/unit/framework/db/sqlite/SqliteQueryBuilderTest.php @@ -84,6 +84,9 @@ class SqliteQueryBuilderTest extends QueryBuilderTest public function testBatchInsert() { + if (version_compare(\SQLite3::version()['versionString'], '3.7.11', '>=')) { + $this->markTestSkipped('This test is only relevant for SQLite < 3.7.11'); + } $sql = $this->getQueryBuilder()->batchInsert('{{customer}} t', ['t.id', 't.name'], [[1, 'a'], [2, 'b']]); $this->assertEquals("INSERT INTO {{customer}} t (`t`.`id`, `t`.`name`) SELECT 1, 'a' UNION SELECT 2, 'b'", $sql); }