Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2015-03-26 18:30:25 +0300
committerVincent Petry <pvince81@owncloud.com>2015-03-26 18:30:25 +0300
commitc0bcaa49804eb82fa23f3624e68b7e62c8ac687a (patch)
tree79d3d13f668ebeadc900388f9b29d2e9c689fb6a /tests
parente12c76ed666d2646d52e68b65f89acdb6558094f (diff)
parent9ad48e0fe9377189dc3ff09a820b7dc61d31ef9f (diff)
Merge pull request #14914 from owncloud/backport-14766-compare-array-insertifnotexists
[Stable8] Backport 14766 compare array insertifnotexists
Diffstat (limited to 'tests')
-rw-r--r--tests/data/db_structure.xml55
-rw-r--r--tests/lib/db.php85
2 files changed, 126 insertions, 14 deletions
diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml
index 858c9ab1002..371da944832 100644
--- a/tests/data/db_structure.xml
+++ b/tests/data/db_structure.xml
@@ -238,4 +238,59 @@
</declaration>
</table>
+ <table>
+ <name>*dbprefix*uniconst</name>
+ <declaration>
+
+ <field>
+ <name>id</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <autoincrement>1</autoincrement>
+ <length>4</length>
+ </field>
+
+ <!-- Foreign Key storages::numeric_id -->
+ <field>
+ <name>storage</name>
+ <type>integer</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>path_hash</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>32</length>
+ </field>
+
+ <field>
+ <name>etag</name>
+ <type>text</type>
+ <default></default>
+ <notnull>false</notnull>
+ <length>40</length>
+ </field>
+
+ <index>
+ <!--<name>fs_storage_path_hash</name>-->
+ <unique>true</unique>
+ <field>
+ <name>storage</name>
+ <sorting>ascending</sorting>
+ </field>
+ <field>
+ <name>path_hash</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+
+ </declaration>
+
+ </table>
+
</database>
diff --git a/tests/lib/db.php b/tests/lib/db.php
index aefbb3624ed..1fb384ca9c6 100644
--- a/tests/lib/db.php
+++ b/tests/lib/db.php
@@ -32,13 +32,18 @@ class Test_DB extends \Test\TestCase {
*/
private $table4;
+ /**
+ * @var string
+ */
+ private $table5;
+
protected function setUp() {
parent::setUp();
- $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
+ $dbFile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
- $r = '_'.OC_Util::generateRandomBytes(4).'_';
- $content = file_get_contents( $dbfile );
+ $r = $this->getUniqueID('_', 4).'_';
+ $content = file_get_contents( $dbFile );
$content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
file_put_contents( self::$schema_file, $content );
OC_DB::createDbFromStructure(self::$schema_file);
@@ -48,6 +53,7 @@ class Test_DB extends \Test\TestCase {
$this->table2 = $this->test_prefix.'cntcts_cards';
$this->table3 = $this->test_prefix.'vcategory';
$this->table4 = $this->test_prefix.'decimal';
+ $this->table5 = $this->test_prefix.'uniconst';
}
protected function tearDown() {
@@ -110,7 +116,7 @@ class Test_DB extends \Test\TestCase {
}
public function testinsertIfNotExist() {
- $categoryentries = array(
+ $categoryEntries = array(
array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1),
array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1),
array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1),
@@ -118,8 +124,8 @@ class Test_DB extends \Test\TestCase {
array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1),
);
- foreach($categoryentries as $entry) {
- $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3,
+ foreach($categoryEntries as $entry) {
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table3,
array(
'uid' => $entry['user'],
'type' => $entry['type'],
@@ -135,14 +141,14 @@ class Test_DB extends \Test\TestCase {
}
public function testInsertIfNotExistNull() {
- $categoryentries = array(
+ $categoryEntries = array(
array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1),
array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0),
array('addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1),
);
- foreach($categoryentries as $entry) {
- $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
+ foreach($categoryEntries as $entry) {
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
'addressbookid' => $entry['addressbookid'],
'fullname' => $entry['fullname'],
@@ -156,14 +162,14 @@ class Test_DB extends \Test\TestCase {
$this->assertEquals(2, count($result->fetchAll()));
}
- public function testinsertIfNotExistDontOverwrite() {
- $fullname = 'fullname test';
+ public function testInsertIfNotExistDonTOverwrite() {
+ $fullName = 'fullname test';
$uri = 'uri_1';
$carddata = 'This is a vCard';
// Normal test to have same known data inserted.
$query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
- $result = $query->execute(array($fullname, $uri, $carddata));
+ $result = $query->execute(array($fullName, $uri, $carddata));
$this->assertEquals(1, $result);
$query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
$result = $query->execute(array($uri));
@@ -174,9 +180,9 @@ class Test_DB extends \Test\TestCase {
$this->assertEquals($carddata, $rowset[0]['carddata']);
// Try to insert a new row
- $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
- 'fullname' => $fullname,
+ 'fullname' => $fullName,
'uri' => $uri,
));
$this->assertEquals(0, $result);
@@ -192,6 +198,57 @@ class Test_DB extends \Test\TestCase {
$this->assertEquals($carddata, $rowset[0]['carddata']);
}
+ public function testInsertIfNotExistsViolating() {
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
+ array(
+ 'storage' => 1,
+ 'path_hash' => md5('welcome.txt'),
+ 'etag' => $this->getUniqueID()
+ ));
+ $this->assertEquals(1, $result);
+
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
+ array(
+ 'storage' => 1,
+ 'path_hash' => md5('welcome.txt'),
+ 'etag' => $this->getUniqueID()
+ ),['storage', 'path_hash']);
+
+ $this->assertEquals(0, $result);
+ }
+
+ public function insertIfNotExistsViolatingThrows() {
+ return [
+ [null],
+ [['etag']],
+ ];
+ }
+
+ /**
+ * @dataProvider insertIfNotExistsViolatingThrows
+ * @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException
+ *
+ * @param array $compareKeys
+ */
+ public function testInsertIfNotExistsViolatingThrows($compareKeys) {
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
+ array(
+ 'storage' => 1,
+ 'path_hash' => md5('welcome.txt'),
+ 'etag' => $this->getUniqueID()
+ ));
+ $this->assertEquals(1, $result);
+
+ $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
+ array(
+ 'storage' => 1,
+ 'path_hash' => md5('welcome.txt'),
+ 'etag' => $this->getUniqueID()
+ ), $compareKeys);
+
+ $this->assertEquals(0, $result);
+ }
+
public function testUtf8Data() {
$table = "*PREFIX*{$this->table2}";
$expected = "ะ‹รถ้›™ๅ–œ\xE2\x80\xA2";