Permalink
Browse files

Database: fixes type convert of 0.0 & improved tests [Closes #806]

  • Loading branch information...
dg committed Oct 17, 2012
1 parent 91f8e33 commit 2c094264abe331b025ca1e021a7c0071e4952747
@@ -112,7 +112,9 @@ public function normalizeRow($row)
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp; $row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === IReflection::FIELD_FLOAT) { } 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) { } elseif ($type === IReflection::FIELD_BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
@@ -16,82 +16,100 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/mysql-nette_test3.
$res = $connection->query("SELECT * FROM types"); $res = $connection->query("SELECT * FROM types");
Assert::equal( array( Assert::equal( array(
'int1' => 1, 'unsigned_int' => 1,
'int2' => 1, 'int' => 1,
'int3' => 1, 'smallint' => 1,
'int4' => '1', // PHP bug #48724 'tinyint' => '1', // PHP bug #48724
'int5' => 1, 'mediumint' => 1,
'int6' => 1, 'bigint' => 1,
'int7' => '1', // PHP bug #48724 'bit' => '1', // PHP bug #48724
'float1' => 1.0, 'decimal' => 1.0,
'float2' => 1.1, 'decimal2' => 1.1,
'float3' => 1.0, 'float' => 1.0,
'float4' => 1.0, 'double' => 1.1,
'date1' => new Nette\DateTime('2012-10-13'), 'date' => new Nette\DateTime('2012-10-13'),
'date2' => new Nette\DateTime('10:10:10'), 'time' => new Nette\DateTime('10:10:10'),
'date3' => new Nette\DateTime('2012-10-13 10:10:10'), 'datetime' => new Nette\DateTime('2012-10-13 10:10:10'),
'date4' => new Nette\DateTime('2012-10-13 10:10:10'), 'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
'date5' => '2012', // PHP bug #48724 'year' => '2012', // PHP bug #48724
'str1' => 'a', 'char' => 'a',
'str2' => 'a', 'varchar' => 'a',
'str3' => 'a', 'binary' => 'a',
'str4' => 'a', 'varbinary' => 'a',
'str5' => NULL, 'blob' => 'a',
'str6' => 'a', 'tinyblob' => 'a',
'str7' => 'a', 'mediumblob' => 'a',
'str8' => 'a', 'longblob' => 'a',
'text' => 'a',
'tinytext' => 'a',
'mediumtext' => 'a',
'longtext' => 'a',
'enum' => 'a',
'set' => 'a',
), (array) $res->fetch() ); ), (array) $res->fetch() );
Assert::equal( array( Assert::equal( array(
'int1' => 0, 'unsigned_int' => 0,
'int2' => 0, 'int' => 0,
'int3' => 0, 'smallint' => 0,
'int4' => '0', // PHP bug #48724 'tinyint' => '0', // PHP bug #48724
'int5' => 0, 'mediumint' => 0,
'int6' => 0, 'bigint' => 0,
'int7' => '0', // PHP bug #48724 'bit' => '0', // PHP bug #48724
'float1' => 0.5, 'decimal' => 0.0,
'float2' => 0.5, 'decimal2' => 0.5,
'float3' => 0.5, 'float' => 0.5,
'float4' => 0.5, 'double' => 0.5,
'date1' => new Nette\DateTime('0000-00-00 00:00:00'), 'date' => new Nette\DateTime('0000-00-00 00:00:00'),
'date2' => new Nette\DateTime('00:00:00'), 'time' => new Nette\DateTime('00:00:00'),
'date3' => new Nette\DateTime('0000-00-00 00:00:00'), 'datetime' => new Nette\DateTime('0000-00-00 00:00:00'),
'date4' => new Nette\DateTime('0000-00-00 00:00:00'), 'timestamp' => new Nette\DateTime('0000-00-00 00:00:00'),
'date5' => '2000', // PHP bug #48724 'year' => '2000', // PHP bug #48724
'str1' => '', 'char' => '',
'str2' => '', 'varchar' => '',
'str3' => NULL, 'binary' => "\x00",
'str4' => '', 'varbinary' => '',
'str5' => NULL, 'blob' => '',
'str6' => '', 'tinyblob' => '',
'str7' => 'b', 'mediumblob' => '',
'str8' => '', 'longblob' => '',
'text' => '',
'tinytext' => '',
'mediumtext' => '',
'longtext' => '',
'enum' => 'b',
'set' => '',
), (array) $res->fetch() ); ), (array) $res->fetch() );
Assert::equal( array( Assert::equal( array(
'int1' => NULL, 'unsigned_int' => NULL,
'int2' => NULL, 'int' => NULL,
'int3' => NULL, 'smallint' => NULL,
'int4' => NULL, 'tinyint' => NULL,
'int5' => NULL, 'mediumint' => NULL,
'int6' => NULL, 'bigint' => NULL,
'int7' => NULL, 'bit' => NULL,
'float1' => NULL, 'decimal' => NULL,
'float2' => NULL, 'decimal2' => NULL,
'float3' => NULL, 'float' => NULL,
'float4' => NULL, 'double' => NULL,
'date1' => NULL, 'date' => NULL,
'date2' => NULL, 'time' => NULL,
'date3' => NULL, 'datetime' => NULL,
'date4' => NULL, 'timestamp' => NULL,
'date5' => NULL, 'year' => NULL,
'str1' => NULL, 'char' => NULL,
'str2' => NULL, 'varchar' => NULL,
'str3' => NULL, 'binary' => NULL,
'str4' => NULL, 'varbinary' => NULL,
'str5' => NULL, 'blob' => NULL,
'str6' => NULL, 'tinyblob' => NULL,
'str7' => NULL, 'mediumblob' => NULL,
'str8' => NULL, 'longblob' => NULL,
'text' => NULL,
'tinytext' => NULL,
'mediumtext' => NULL,
'longtext' => NULL,
'enum' => NULL,
'set' => NULL,
), (array) $res->fetch() ); ), (array) $res->fetch() );
@@ -17,106 +17,106 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/pgsql-nette_test3.
$res = $connection->query("SELECT * FROM types"); $res = $connection->query("SELECT * FROM types");
$row = $res->fetch(); $row = $res->fetch();
Assert::true( is_string($row->float4) ); Assert::true( is_string($row->money) );
unset($row->float4); unset($row->money);
Assert::equal( array( Assert::equal( array(
'int1' => 1, 'smallint' => 1,
'int2' => 1, 'integer' => 1,
'int3' => 1, 'bigint' => 1,
'float1' => 1.0, 'numeric' => 1.0,
'float2' => 1.1, 'real' => 1.1,
'float3' => 1.11, 'double' => 1.11,
'bool' => TRUE, 'bool' => TRUE,
'date1' => new Nette\DateTime('2012-10-13'), 'date' => new Nette\DateTime('2012-10-13'),
'date2' => new Nette\DateTime('10:10:10'), 'time' => new Nette\DateTime('10:10:10'),
'date3' => new Nette\DateTime('2012-10-13 10:10:10'), 'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
'date4' => '1 year', 'interval' => '1 year',
'str1' => 'a ', 'character' => 'a ',
'str2' => 'a', 'character_varying' => 'a',
'str3' => 'a', 'text' => 'a',
'str4' => '\'a\'', 'tsquery' => '\'a\'',
'str5' => '\'a\'', 'tsvector' => '\'a\'',
'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'str7' => 'a', 'xml' => 'a',
'str8' => '192.168.1.0/24', 'cidr' => '192.168.1.0/24',
'str9' => '192.168.1.1', 'inet' => '192.168.1.1',
'str10' => '08:00:2b:01:02:03', 'macaddr' => '08:00:2b:01:02:03',
'bin1' => '1', 'bit' => '1',
'bin2' => '1', 'bit_varying' => '1',
'bin3' => NULL, 'bytea' => NULL,
'geo1' => '(30,40),(10,20)', 'box' => '(30,40),(10,20)',
'geo2' => '<(10,20),30>', 'circle' => '<(10,20),30>',
'geo3' => '[(10,20),(30,40)]', 'lseg' => '[(10,20),(30,40)]',
'geo4' => '((10,20),(30,40))', 'path' => '((10,20),(30,40))',
'geo5' => '(10,20)', 'point' => '(10,20)',
'geo6' => '((10,20),(30,40))', 'polygon' => '((10,20),(30,40))',
), (array) $row ); ), (array) $row );
Assert::equal( array( Assert::equal( array(
'int1' => 0, 'smallint' => 0,
'int2' => 0, 'integer' => 0,
'int3' => 0, 'bigint' => 0,
'float1' => 0.0, 'numeric' => 0.0,
'float2' => '0', 'real' => 0.0,
'float3' => '0', 'double' => 0.0,
'float4' => NULL, 'money' => NULL,
'bool' => FALSE, 'bool' => FALSE,
'date1' => NULL, 'date' => NULL,
'date2' => NULL, 'time' => NULL,
'date3' => NULL, 'timestamp' => NULL,
'date4' => '00:00:00', 'interval' => '00:00:00',
'str1' => ' ', 'character' => ' ',
'str2' => '', 'character_varying' => '',
'str3' => '', 'text' => '',
'str4' => '', 'tsquery' => '',
'str5' => '', 'tsvector' => '',
'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'str7' => 'a', 'xml' => 'a',
'str8' => '192.168.1.0/24', 'cidr' => '192.168.1.0/24',
'str9' => '192.168.1.1', 'inet' => '192.168.1.1',
'str10' => '08:00:2b:01:02:03', 'macaddr' => '08:00:2b:01:02:03',
'bin1' => '0', 'bit' => '0',
'bin2' => '0', 'bit_varying' => '0',
'bin3' => NULL, 'bytea' => NULL,
'geo1' => '(30,40),(10,20)', 'box' => '(30,40),(10,20)',
'geo2' => '<(10,20),30>', 'circle' => '<(10,20),30>',
'geo3' => '[(10,20),(30,40)]', 'lseg' => '[(10,20),(30,40)]',
'geo4' => '((10,20),(30,40))', 'path' => '((10,20),(30,40))',
'geo5' => '(10,20)', 'point' => '(10,20)',
'geo6' => '((10,20),(30,40))', 'polygon' => '((10,20),(30,40))',
), (array) $res->fetch() ); ), (array) $res->fetch() );
Assert::equal( array( Assert::equal( array(
'int1' => NULL, 'smallint' => NULL,
'int2' => NULL, 'integer' => NULL,
'int3' => NULL, 'bigint' => NULL,
'float1' => NULL, 'numeric' => NULL,
'float2' => NULL, 'real' => NULL,
'float3' => NULL, 'double' => NULL,
'float4' => NULL, 'money' => NULL,
'bool' => NULL, 'bool' => NULL,
'date1' => NULL, 'date' => NULL,
'date2' => NULL, 'time' => NULL,
'date3' => NULL, 'timestamp' => NULL,
'date4' => NULL, 'interval' => NULL,
'str1' => NULL, 'character' => NULL,
'str2' => NULL, 'character_varying' => NULL,
'str3' => NULL, 'text' => NULL,
'str4' => NULL, 'tsquery' => NULL,
'str5' => NULL, 'tsvector' => NULL,
'str6' => NULL, 'uuid' => NULL,
'str7' => NULL, 'xml' => NULL,
'str8' => NULL, 'cidr' => NULL,
'str9' => NULL, 'inet' => NULL,
'str10' => NULL, 'macaddr' => NULL,
'bin1' => NULL, 'bit' => NULL,
'bin2' => NULL, 'bit_varying' => NULL,
'bin3' => NULL, 'bytea' => NULL,
'geo1' => NULL, 'box' => NULL,
'geo2' => NULL, 'circle' => NULL,
'geo3' => NULL, 'lseg' => NULL,
'geo4' => NULL, 'path' => NULL,
'geo5' => NULL, 'point' => NULL,
'geo6' => NULL, 'polygon' => NULL,
), (array) $res->fetch() ); ), (array) $res->fetch() );
Oops, something went wrong.

0 comments on commit 2c09426

Please sign in to comment.