PostgreSqlPlatformTest.php 6.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Connection;

require_once __DIR__ . '/../../TestInit.php';
 
class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
{
    private $_platform;

    public function setUp()
    {
        $this->_platform = new PostgreSqlPlatform;
    }

20
    public function testGeneratesTableCreationSql()
21 22 23 24 25 26 27 28
    {
        $columns = array(
            'id' => array(
                'type' => Type::getType('integer'),
                'primary' => true,
                'notnull' => true
            ),
            'test' => array(
29
                'type' => Type::getType('string'),
30 31 32 33 34 35 36 37 38 39 40 41 42 43
                'length' => 255,
                'notnull' => true
            )
        );

        $options = array(
            'primary' => array('id')
        );

        $sql = $this->_platform->getCreateTableSql('test', $columns, $options);
        $this->assertEquals('CREATE TABLE test (id INT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id))', $sql[0]);
    
    }

44
    public function testGeneratesTableAlterationSqlForAddingAndRenaming()
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    {
        $changes = array(
            'name' => 'userlist',
            'add' => array(
                'quota' => array(
                'type' => Type::getType('integer')
                )
            ));

        $sql = $this->_platform->getAlterTableSql('mytable', $changes);

        $this->assertEquals(
            'ALTER TABLE mytable ADD quota INT DEFAULT NULL',
            $sql[0]
        );
        $this->assertEquals(
            'ALTER TABLE mytable RENAME TO userlist',
            $sql[1]
        );
    }

66
    public function testGeneratesIndexCreationSql()
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    {
        $indexDef = array(
            'fields' => array(
                'user_name',
                'last_login'
            )
        );

        $sql = $this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef);

        $this->assertEquals(
            'CREATE INDEX my_idx ON mytable (user_name, last_login)',
            $sql
        );
    }

83 84 85 86 87 88 89 90 91
    public function testGeneratesSqlSnippets()
    {
        $this->assertEquals('SIMILAR TO', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
        $this->assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
        $this->assertEquals('RANDOM()', $this->_platform->getRandomExpression(), 'Random function is not correct');
        $this->assertEquals('column1 || column2 || column3', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
    }

    public function testGeneratesTransactionCommands()
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    {
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
            $this->_platform->getSetTransactionIsolationSql(Connection::TRANSACTION_READ_UNCOMMITTED)
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
            $this->_platform->getSetTransactionIsolationSql(Connection::TRANSACTION_READ_COMMITTED)
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
            $this->_platform->getSetTransactionIsolationSql(Connection::TRANSACTION_REPEATABLE_READ)
        );
        $this->assertEquals(
            'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
            $this->_platform->getSetTransactionIsolationSql(Connection::TRANSACTION_SERIALIZABLE)
        );
    }

111
    public function testGeneratesDDLSnippets()
112 113 114 115 116 117
    {
        $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSql('foobar'));
        $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSql('foobar'));
        $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSql('foobar'));
    }

118
    public function testGeneratesTypeDeclarationForIntegers()
119 120 121 122 123 124 125 126 127 128 129 130 131 132
    {
        $this->assertEquals(
            'INT',
            $this->_platform->getIntegerTypeDeclarationSql(array())
        );
        $this->assertEquals(
            'SERIAL',
            $this->_platform->getIntegerTypeDeclarationSql(array('autoincrement' => true)
        ));
        $this->assertEquals(
            'SERIAL',
            $this->_platform->getIntegerTypeDeclarationSql(
                array('autoincrement' => true, 'primary' => true)
        ));
133 134 135 136
    }

    public function testGeneratesTypeDeclarationForStrings()
    {
137 138 139
        $this->assertEquals(
            'CHAR(10)',
            $this->_platform->getVarcharTypeDeclarationSql(
140 141
                array('length' => 10, 'fixed' => true))
        );
142 143
        $this->assertEquals(
            'VARCHAR(50)',
144 145
            $this->_platform->getVarcharTypeDeclarationSql(array('length' => 50)),
            'Variable string declaration is not correct'
146 147 148
        );
        $this->assertEquals(
            'TEXT',
149 150
            $this->_platform->getVarcharTypeDeclarationSql(array()),
            'Long string declaration is not correct'
151 152 153
        );
    }

154
    public function testGeneratesSequenceSqlCommands()
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    {
        $this->assertEquals(
            'CREATE SEQUENCE myseq INCREMENT BY 20 START 1',
            $this->_platform->getCreateSequenceSql('myseq', 1, 20)
        );
        $this->assertEquals(
            'DROP SEQUENCE myseq',
            $this->_platform->getDropSequenceSql('myseq')
        );
        $this->assertEquals(
            "SELECT NEXTVAL('myseq')",
            $this->_platform->getSequenceNextValSql('myseq')
        );
    }

170
    public function testDoesNotPreferIdentityColumns()
171 172
    {
        $this->assertFalse($this->_platform->prefersIdentityColumns());
173 174 175 176
    }

    public function testPrefersSequences()
    {
177
        $this->assertTrue($this->_platform->prefersSequences());
178 179 180 181
    }

    public function testSupportsIdentityColumns()
    {
182
        $this->assertTrue($this->_platform->supportsIdentityColumns());
183 184 185 186
    }

    public function testSupportsSavePoints()
    {
187
        $this->assertTrue($this->_platform->supportsSavepoints());
188 189 190 191
    }

    public function testSupportsSequences()
    {
192 193
        $this->assertTrue($this->_platform->supportsSequences());
    }
194
}