]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add database tests for INSERT/SELECT date format.
authorAndreas Fischer <bantu@owncloud.com>
Thu, 12 Sep 2013 20:36:28 +0000 (22:36 +0200)
committerAndreas Fischer <bantu@owncloud.com>
Thu, 12 Sep 2013 21:43:21 +0000 (23:43 +0200)
tests/data/db_structure.xml
tests/data/db_structure2.xml
tests/lib/db.php

index 8f6dc5e2ecd539b9571ec8a66da28563c97208e7..2e83bbb78c7577d3aef0c5b6039f12598ef36073 100644 (file)
   </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>
index 6f12f81f477d94cb7a118288d9a41c0285f0ccdc..bbfb24985cb1c67c4628f4b10448b0df35bc56d9 100644 (file)
 
  </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>
index 1977025cf12def4e8ed25f8b916a48112891b21d..befb52ee19803c670a357d014a63137510b67362 100644 (file)
@@ -145,4 +145,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.'
+               );
+       }
 }