From cacb66480b4bebc8511842bcc588ffd750ef0c38 Mon Sep 17 00:00:00 2001
From: Oliver Gasser <oliver@flowriver.net>
Date: Wed, 18 Dec 2013 23:40:11 +0100
Subject: [PATCH] Add unit tests for decimal type usage

---
 tests/data/db_structure.xml  |  3 ++-
 tests/data/db_structure2.xml |  3 ++-
 tests/lib/db.php             | 29 +++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml
index 5f2edbbc516..bfff2143349 100644
--- a/tests/data/db_structure.xml
+++ b/tests/data/db_structure.xml
@@ -216,7 +216,8 @@
     <type>decimal</type>
     <default/>
     <notnull>true</notnull>
-    <length>15</length>
+    <precision>12</precision>
+    <scale>2</scale>
    </field>
   </declaration>
  </table>
diff --git a/tests/data/db_structure2.xml b/tests/data/db_structure2.xml
index 6cd071451df..ae5f22e9573 100644
--- a/tests/data/db_structure2.xml
+++ b/tests/data/db_structure2.xml
@@ -113,7 +113,8 @@
     <type>decimal</type>
     <default/>
     <notnull>true</notnull>
-    <length>15</length>
+    <precision>12</precision>
+    <scale>2</scale>
    </field>
   </declaration>
  </table>
diff --git a/tests/lib/db.php b/tests/lib/db.php
index 3fcdf8a7dc6..96d5f873b5c 100644
--- a/tests/lib/db.php
+++ b/tests/lib/db.php
@@ -40,6 +40,7 @@ class Test_DB extends PHPUnit_Framework_TestCase {
 		$this->table1 = $this->test_prefix.'cntcts_addrsbks';
 		$this->table2 = $this->test_prefix.'cntcts_cards';
 		$this->table3 = $this->test_prefix.'vcategory';
+		$this->table4 = $this->test_prefix.'decimal';
 	}
 
 	public function tearDown() {
@@ -172,4 +173,32 @@ class Test_DB extends PHPUnit_Framework_TestCase {
 		$actual = OC_DB::prepare("SELECT `fullname` FROM `$table`")->execute()->fetchOne();
 		$this->assertSame($expected, $actual);
 	}
+
+	public function testDecimal() {
+		$table = "*PREFIX*" . $this->table4;
+		$rowname = 'decimaltest';
+
+		// Insert, select and delete decimal(12,2) values
+		$inserts = array('1337133713.37', '1234567890');
+		$expects = array('1337133713.37', '1234567890.00');
+
+		for ($i = 0; $i < count($inserts); $i++) {
+			$insert = $inserts[$i];
+			$expect = $expects[$i];
+
+			$query = OC_DB::prepare('INSERT INTO `' . $table . '` (`' . $rowname . '`) VALUES (?)');
+			$result = $query->execute(array($insert));
+			$this->assertEquals(1, $result);
+			$query = OC_DB::prepare('SELECT `' . $rowname . '` FROM `' . $table . '`');
+			$result = $query->execute();
+			$this->assertTrue((bool)$result);
+			$row = $result->fetchRow();
+			$this->assertArrayHasKey($rowname, $row);
+			$this->assertEquals($expect, $row[$rowname]);
+			$query = OC_DB::prepare('DELETE FROM `' . $table . '`');
+			$result = $query->execute();
+			$this->assertTrue((bool)$result);
+		}
+	}
+
 }
-- 
GitLab