Adding unit test which shows insertIfNotExists to fall apart in certain situations

This commit is contained in:
Thomas Müller 2015-03-09 20:53:08 +01:00
parent 2747a83a49
commit b966a4eb17
2 changed files with 91 additions and 11 deletions

View File

@ -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>

View File

@ -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,7 +124,7 @@ class Test_DB extends \Test\TestCase {
array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1),
);
foreach($categoryentries as $entry) {
foreach($categoryEntries as $entry) {
$result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3,
array(
'uid' => $entry['user'],
@ -135,13 +141,13 @@ 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) {
foreach($categoryEntries as $entry) {
$result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
'addressbookid' => $entry['addressbookid'],
@ -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));
@ -176,7 +182,7 @@ class Test_DB extends \Test\TestCase {
// Try to insert a new row
$result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
array(
'fullname' => $fullname,
'fullname' => $fullName,
'uri' => $uri,
));
$this->assertEquals(0, $result);
@ -192,6 +198,25 @@ class Test_DB extends \Test\TestCase {
$this->assertEquals($carddata, $rowset[0]['carddata']);
}
public function testInsertIfNotExistsViolating() {
$result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table5,
array(
'storage' => 1,
'path_hash' => md5('welcome.txt'),
'etag' => $this->getUniqueID()
));
$this->assertEquals(1, $result);
$result = OC_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 testUtf8Data() {
$table = "*PREFIX*{$this->table2}";
$expected = "Ћö雙喜\xE2\x80\xA2";