diff options
author | Andreas Fischer <bantu@owncloud.com> | 2013-09-13 00:36:28 +0400 |
---|---|---|
committer | Andreas Fischer <bantu@owncloud.com> | 2013-09-24 01:59:59 +0400 |
commit | a8d7f1feae3580d12b3c0ca6bd9f3bddc1c2ab22 (patch) | |
tree | aed4c85fda3b055a97b70c1fd240296c4a02b840 /tests | |
parent | 40c6927b59e2ee5968b1102c3a753fb373577700 (diff) |
Add database tests for INSERT/SELECT date format.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/data/db_structure.xml | 21 | ||||
-rw-r--r-- | tests/data/db_structure2.xml | 21 | ||||
-rw-r--r-- | tests/lib/db.php | 38 |
3 files changed, 80 insertions, 0 deletions
diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 8f6dc5e2ecd..2e83bbb78c7 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -178,4 +178,25 @@ </declaration> </table> + <table> + <name>*dbprefix*timestamp</name> + <declaration> + <field> + <name>id</name> + <autoincrement>1</autoincrement> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <length>4</length> + </field> + + <field> + <name>timestamptest</name> + <type>timestamp</type> + <default></default> + <notnull>false</notnull> + </field> + </declaration> + </table> + </database> diff --git a/tests/data/db_structure2.xml b/tests/data/db_structure2.xml index 6f12f81f477..bbfb24985cb 100644 --- a/tests/data/db_structure2.xml +++ b/tests/data/db_structure2.xml @@ -75,4 +75,25 @@ </table> + <table> + <name>*dbprefix*timestamp</name> + <declaration> + <field> + <name>id</name> + <autoincrement>1</autoincrement> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <length>4</length> + </field> + + <field> + <name>timestamptest</name> + <type>timestamp</type> + <default></default> + <notnull>false</notnull> + </field> + </declaration> + </table> + </database> diff --git a/tests/lib/db.php b/tests/lib/db.php index 41bd7d623a2..d11b828844f 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -130,4 +130,42 @@ class Test_DB extends PHPUnit_Framework_TestCase { $this->assertEquals(1, $result->numRows()); } + + /** + * Tests whether the database is configured so it accepts and returns dates + * in the expected format. + */ + public function testTimestampDateFormat() { + $table = '*PREFIX*'.$this->test_prefix.'timestamp'; + $column = 'timestamptest'; + + $expectedFormat = 'Y-m-d H:i:s'; + $now = new \DateTime; + + $query = OC_DB::prepare("INSERT INTO `$table` (`$column`) VALUES (?)"); + $result = $query->execute(array($now->format($expectedFormat))); + $this->assertEquals( + 1, + $result, + "Database failed to accept dates in the format '$expectedFormat'." + ); + + $id = OC_DB::insertid($table); + $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `id` = ?"); + $result = $query->execute(array($id)); + $row = $result->fetchRow(); + + $dateFromDb = \DateTime::createFromFormat($expectedFormat, $row[$column]); + $this->assertInstanceOf( + '\DateTime', + $dateFromDb, + "Database failed to return dates in the format '$expectedFormat'." + ); + + $this->assertEquals( + $now->format('c u'), + $dateFromDb->format('c u'), + 'Failed asserting that the returned date is the same as the inserted.' + ); + } } |