Skip to content

Commit

Permalink
Database: fixes type convert of 0.0 & improved tests [Closes #806]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 22, 2012
1 parent c7706ee commit 3c47da1
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 225 deletions.
4 changes: 3 additions & 1 deletion Nette/Database/Statement.php
Expand Up @@ -112,7 +112,9 @@ public function normalizeRow($row)
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;

} elseif ($type === IReflection::FIELD_FLOAT) {
$row[$key] = (string) ($tmp = (float) $value) === rtrim(rtrim($value, '0'), '.') ? $tmp : $value;
$value = strpos($value, '.') === FALSE ? $value : rtrim(rtrim($value, '0'), '.');
$float = (float) $value;
$row[$key] = (string) $float === $value ? $float : $value;

} elseif ($type === IReflection::FIELD_BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
Expand Down
162 changes: 90 additions & 72 deletions tests/Nette/Database/Statement.normalizeRow.mysql.phpt
Expand Up @@ -16,82 +16,100 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/mysql-nette_test3.
$res = $connection->query("SELECT * FROM types");

Assert::equal( array(
'int1' => 1,
'int2' => 1,
'int3' => 1,
'int4' => '1', // PHP bug #48724
'int5' => 1,
'int6' => 1,
'int7' => '1', // PHP bug #48724
'float1' => 1.0,
'float2' => 1.1,
'float3' => 1.0,
'float4' => 1.0,
'date1' => new Nette\DateTime('2012-10-13'),
'date2' => new Nette\DateTime('10:10:10'),
'date3' => new Nette\DateTime('2012-10-13 10:10:10'),
'date4' => new Nette\DateTime('2012-10-13 10:10:10'),
'date5' => '2012', // PHP bug #48724
'str1' => 'a',
'str2' => 'a',
'str3' => 'a',
'str4' => 'a',
'str5' => NULL,
'str6' => 'a',
'str7' => 'a',
'str8' => 'a',
'unsigned_int' => 1,
'int' => 1,
'smallint' => 1,
'tinyint' => '1', // PHP bug #48724
'mediumint' => 1,
'bigint' => 1,
'bit' => '1', // PHP bug #48724
'decimal' => 1.0,
'decimal2' => 1.1,
'float' => 1.0,
'double' => 1.1,
'date' => new Nette\DateTime('2012-10-13'),
'time' => new Nette\DateTime('10:10:10'),
'datetime' => new Nette\DateTime('2012-10-13 10:10:10'),
'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
'year' => '2012', // PHP bug #48724
'char' => 'a',
'varchar' => 'a',
'binary' => 'a',
'varbinary' => 'a',
'blob' => 'a',
'tinyblob' => 'a',
'mediumblob' => 'a',
'longblob' => 'a',
'text' => 'a',
'tinytext' => 'a',
'mediumtext' => 'a',
'longtext' => 'a',
'enum' => 'a',
'set' => 'a',
), (array) $res->fetch() );

Assert::equal( array(
'int1' => 0,
'int2' => 0,
'int3' => 0,
'int4' => '0', // PHP bug #48724
'int5' => 0,
'int6' => 0,
'int7' => '0', // PHP bug #48724
'float1' => 0.5,
'float2' => 0.5,
'float3' => 0.5,
'float4' => 0.5,
'date1' => new Nette\DateTime('0000-00-00 00:00:00'),
'date2' => new Nette\DateTime('00:00:00'),
'date3' => new Nette\DateTime('0000-00-00 00:00:00'),
'date4' => new Nette\DateTime('0000-00-00 00:00:00'),
'date5' => '2000', // PHP bug #48724
'str1' => '',
'str2' => '',
'str3' => NULL,
'str4' => '',
'str5' => NULL,
'str6' => '',
'str7' => 'b',
'str8' => '',
'unsigned_int' => 0,
'int' => 0,
'smallint' => 0,
'tinyint' => '0', // PHP bug #48724
'mediumint' => 0,
'bigint' => 0,
'bit' => '0', // PHP bug #48724
'decimal' => 0.0,
'decimal2' => 0.5,
'float' => 0.5,
'double' => 0.5,
'date' => new Nette\DateTime('0000-00-00 00:00:00'),
'time' => new Nette\DateTime('00:00:00'),
'datetime' => new Nette\DateTime('0000-00-00 00:00:00'),
'timestamp' => new Nette\DateTime('0000-00-00 00:00:00'),
'year' => '2000', // PHP bug #48724
'char' => '',
'varchar' => '',
'binary' => "\x00",
'varbinary' => '',
'blob' => '',
'tinyblob' => '',
'mediumblob' => '',
'longblob' => '',
'text' => '',
'tinytext' => '',
'mediumtext' => '',
'longtext' => '',
'enum' => 'b',
'set' => '',
), (array) $res->fetch() );

Assert::equal( array(
'int1' => NULL,
'int2' => NULL,
'int3' => NULL,
'int4' => NULL,
'int5' => NULL,
'int6' => NULL,
'int7' => NULL,
'float1' => NULL,
'float2' => NULL,
'float3' => NULL,
'float4' => NULL,
'date1' => NULL,
'date2' => NULL,
'date3' => NULL,
'date4' => NULL,
'date5' => NULL,
'str1' => NULL,
'str2' => NULL,
'str3' => NULL,
'str4' => NULL,
'str5' => NULL,
'str6' => NULL,
'str7' => NULL,
'str8' => NULL,
'unsigned_int' => NULL,
'int' => NULL,
'smallint' => NULL,
'tinyint' => NULL,
'mediumint' => NULL,
'bigint' => NULL,
'bit' => NULL,
'decimal' => NULL,
'decimal2' => NULL,
'float' => NULL,
'double' => NULL,
'date' => NULL,
'time' => NULL,
'datetime' => NULL,
'timestamp' => NULL,
'year' => NULL,
'char' => NULL,
'varchar' => NULL,
'binary' => NULL,
'varbinary' => NULL,
'blob' => NULL,
'tinyblob' => NULL,
'mediumblob' => NULL,
'longblob' => NULL,
'text' => NULL,
'tinytext' => NULL,
'mediumtext' => NULL,
'longtext' => NULL,
'enum' => NULL,
'set' => NULL,
), (array) $res->fetch() );
182 changes: 91 additions & 91 deletions tests/Nette/Database/Statement.normalizeRow.postgre.phpt
Expand Up @@ -17,106 +17,106 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/pgsql-nette_test3.
$res = $connection->query("SELECT * FROM types");

$row = $res->fetch();
Assert::true( is_string($row->float4) );
unset($row->float4);
Assert::true( is_string($row->money) );
unset($row->money);

Assert::equal( array(
'int1' => 1,
'int2' => 1,
'int3' => 1,
'float1' => 1.0,
'float2' => 1.1,
'float3' => 1.11,
'smallint' => 1,
'integer' => 1,
'bigint' => 1,
'numeric' => 1.0,
'real' => 1.1,
'double' => 1.11,
'bool' => TRUE,
'date1' => new Nette\DateTime('2012-10-13'),
'date2' => new Nette\DateTime('10:10:10'),
'date3' => new Nette\DateTime('2012-10-13 10:10:10'),
'date4' => '1 year',
'str1' => 'a ',
'str2' => 'a',
'str3' => 'a',
'str4' => '\'a\'',
'str5' => '\'a\'',
'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'str7' => 'a',
'str8' => '192.168.1.0/24',
'str9' => '192.168.1.1',
'str10' => '08:00:2b:01:02:03',
'bin1' => '1',
'bin2' => '1',
'bin3' => NULL,
'geo1' => '(30,40),(10,20)',
'geo2' => '<(10,20),30>',
'geo3' => '[(10,20),(30,40)]',
'geo4' => '((10,20),(30,40))',
'geo5' => '(10,20)',
'geo6' => '((10,20),(30,40))',
'date' => new Nette\DateTime('2012-10-13'),
'time' => new Nette\DateTime('10:10:10'),
'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
'interval' => '1 year',
'character' => 'a ',
'character_varying' => 'a',
'text' => 'a',
'tsquery' => '\'a\'',
'tsvector' => '\'a\'',
'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'xml' => 'a',
'cidr' => '192.168.1.0/24',
'inet' => '192.168.1.1',
'macaddr' => '08:00:2b:01:02:03',
'bit' => '1',
'bit_varying' => '1',
'bytea' => NULL,
'box' => '(30,40),(10,20)',
'circle' => '<(10,20),30>',
'lseg' => '[(10,20),(30,40)]',
'path' => '((10,20),(30,40))',
'point' => '(10,20)',
'polygon' => '((10,20),(30,40))',
), (array) $row );

Assert::equal( array(
'int1' => 0,
'int2' => 0,
'int3' => 0,
'float1' => 0.0,
'float2' => '0',
'float3' => '0',
'float4' => NULL,
'smallint' => 0,
'integer' => 0,
'bigint' => 0,
'numeric' => 0.0,
'real' => 0.0,
'double' => 0.0,
'money' => NULL,
'bool' => FALSE,
'date1' => NULL,
'date2' => NULL,
'date3' => NULL,
'date4' => '00:00:00',
'str1' => ' ',
'str2' => '',
'str3' => '',
'str4' => '',
'str5' => '',
'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'str7' => 'a',
'str8' => '192.168.1.0/24',
'str9' => '192.168.1.1',
'str10' => '08:00:2b:01:02:03',
'bin1' => '0',
'bin2' => '0',
'bin3' => NULL,
'geo1' => '(30,40),(10,20)',
'geo2' => '<(10,20),30>',
'geo3' => '[(10,20),(30,40)]',
'geo4' => '((10,20),(30,40))',
'geo5' => '(10,20)',
'geo6' => '((10,20),(30,40))',
'date' => NULL,
'time' => NULL,
'timestamp' => NULL,
'interval' => '00:00:00',
'character' => ' ',
'character_varying' => '',
'text' => '',
'tsquery' => '',
'tsvector' => '',
'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'xml' => 'a',
'cidr' => '192.168.1.0/24',
'inet' => '192.168.1.1',
'macaddr' => '08:00:2b:01:02:03',
'bit' => '0',
'bit_varying' => '0',
'bytea' => NULL,
'box' => '(30,40),(10,20)',
'circle' => '<(10,20),30>',
'lseg' => '[(10,20),(30,40)]',
'path' => '((10,20),(30,40))',
'point' => '(10,20)',
'polygon' => '((10,20),(30,40))',
), (array) $res->fetch() );

Assert::equal( array(
'int1' => NULL,
'int2' => NULL,
'int3' => NULL,
'float1' => NULL,
'float2' => NULL,
'float3' => NULL,
'float4' => NULL,
'smallint' => NULL,
'integer' => NULL,
'bigint' => NULL,
'numeric' => NULL,
'real' => NULL,
'double' => NULL,
'money' => NULL,
'bool' => NULL,
'date1' => NULL,
'date2' => NULL,
'date3' => NULL,
'date4' => NULL,
'str1' => NULL,
'str2' => NULL,
'str3' => NULL,
'str4' => NULL,
'str5' => NULL,
'str6' => NULL,
'str7' => NULL,
'str8' => NULL,
'str9' => NULL,
'str10' => NULL,
'bin1' => NULL,
'bin2' => NULL,
'bin3' => NULL,
'geo1' => NULL,
'geo2' => NULL,
'geo3' => NULL,
'geo4' => NULL,
'geo5' => NULL,
'geo6' => NULL,
'date' => NULL,
'time' => NULL,
'timestamp' => NULL,
'interval' => NULL,
'character' => NULL,
'character_varying' => NULL,
'text' => NULL,
'tsquery' => NULL,
'tsvector' => NULL,
'uuid' => NULL,
'xml' => NULL,
'cidr' => NULL,
'inet' => NULL,
'macaddr' => NULL,
'bit' => NULL,
'bit_varying' => NULL,
'bytea' => NULL,
'box' => NULL,
'circle' => NULL,
'lseg' => NULL,
'path' => NULL,
'point' => NULL,
'polygon' => NULL,
), (array) $res->fetch() );

0 comments on commit 3c47da1

Please sign in to comment.