-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from austinhyde/column-value-defaults
Fix DEFAULT parens wrapping
- Loading branch information
Showing
2 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Tests functionality of pgsql8::column_value_default() | ||
* | ||
* @package DBSteward | ||
* @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License | ||
* @author Austin Hyde <austin109@gmail.com> | ||
*/ | ||
|
||
require_once __DIR__ . '/../dbstewardUnitTestBase.php'; | ||
|
||
/** | ||
* @group pgsql8 | ||
*/ | ||
class ColumnValueDefaultTest extends PHPUnit_Framework_TestCase { | ||
public function setUp() { | ||
dbsteward::set_sql_format('pgsql8'); | ||
} | ||
|
||
public function testNullColumnReturnsNull() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text"/>', | ||
'<col null="true">adsf</col>'); | ||
|
||
$this->assertEquals('NULL', $value, 'Expected NULL if null="true" is specified'); | ||
} | ||
|
||
public function testEmptyColumnGivesEmptyString() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text"/>', | ||
'<col empty="true">asdf</col>'); | ||
|
||
$expected = pgsql8::E_ESCAPE ? "E''" : "''"; | ||
|
||
$this->assertEquals($expected, $value, "Expected $expected if empty=\"true\" is specified"); | ||
} | ||
|
||
public function testSQLGivesLiteralButWrapped() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text"/>', | ||
'<col sql="true">some_function()</col>'); | ||
|
||
$this->assertEquals('(some_function())', $value, 'Expected literal column value wrapped in parens if sql="true"'); | ||
} | ||
|
||
public function testSQLWithDefaultDoesNotWrap() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text"/>', | ||
'<col sql="true">DEFAULT</col>'); | ||
|
||
$this->assertEquals('DEFAULT', $value, 'Expected DEFAULT without wrapping parens if sql="true" and column value is DEFAULT'); | ||
} | ||
|
||
public function testUsesColumnDefaultIfEmpty() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text" default="asdf"/>', | ||
'<col></col>'); | ||
|
||
$this->assertEquals('asdf', $value, 'Expected column default if data was empty'); | ||
} | ||
|
||
public function testUsesLiteralForInt() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="int"/>', | ||
'<col>42</col>'); | ||
|
||
$this->assertEquals('42', $value, 'Expected literal int value for integers'); | ||
} | ||
|
||
public function testQuotesStrings() { | ||
$value = $this->getColumnValue( | ||
'<column name="foo" type="text"/>', | ||
'<col>asdf</col>'); | ||
|
||
$expected = pgsql8::E_ESCAPE ? "E'asdf'" : "'asdf'"; | ||
$this->assertEquals($expected, $value, "Expected $expected for a string value"); | ||
} | ||
|
||
private function getColumnValue($def, $data) { | ||
$defNode = simplexml_load_string($def); | ||
$defNodeName = $defNode['name']; | ||
|
||
$schemaXml = <<<XML | ||
<schema name="test_schema"> | ||
<table name="test_table" primaryKey="$defNodeName"> | ||
$def | ||
<rows columns="$defNodeName"> | ||
<row>$data</row> | ||
</rows> | ||
</table> | ||
</schema> | ||
XML; | ||
|
||
$schema = simplexml_load_string($schemaXml); | ||
return pgsql8::column_value_default($schema, $schema->table, $defNodeName, $schema->table->rows->row->col); | ||
} | ||
} |