diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-03-12 15:09:55 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2018-03-12 17:45:49 +0100 |
commit | 2b335fd607e15c9955d6d42f50c460f04f7fe445 (patch) | |
tree | fe30052a2a6c01794f15c7c934bd23ff946028e9 /tests/lib/DB | |
parent | 3655951dd7372be9193e9ddd6f4b717f8d2cc6b4 (diff) | |
download | nextcloud-server-2b335fd607e15c9955d6d42f50c460f04f7fe445.tar.gz nextcloud-server-2b335fd607e15c9955d6d42f50c460f04f7fe445.zip |
Use insertIfNotExist of the new interface
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'tests/lib/DB')
-rw-r--r-- | tests/lib/DB/ConnectionTest.php | 138 | ||||
-rw-r--r-- | tests/lib/DB/LegacyDBTest.php | 134 | ||||
-rw-r--r-- | tests/lib/DB/MDB2SchemaReaderTest.php | 2 | ||||
-rw-r--r-- | tests/lib/DB/testschema.xml | 1 |
4 files changed, 140 insertions, 135 deletions
diff --git a/tests/lib/DB/ConnectionTest.php b/tests/lib/DB/ConnectionTest.php index 3a4a80bdbd9..62b168fbb53 100644 --- a/tests/lib/DB/ConnectionTest.php +++ b/tests/lib/DB/ConnectionTest.php @@ -198,4 +198,142 @@ class ConnectionTest extends \Test\TestCase { $this->addToAssertionCount(1); } + + public function testInsertIfNotExist() { + $this->makeTestTable(); + $categoryEntries = [ + ['user' => 'test', 'category' => 'Family', 'expectedResult' => 1], + ['user' => 'test', 'category' => 'Friends', 'expectedResult' => 1], + ['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 1], + ['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 0], + ['user' => 'test', 'category' => 'School', 'expectedResult' => 1], + ['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 1], + ['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 0], + ['user' => 'test2', 'category' => 'School2', 'expectedResult' => 1], + ['user' => 'test2', 'category' => 'Coworkers', 'expectedResult' => 1], + ]; + + foreach($categoryEntries as $entry) { + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'textfield' => $entry['user'], + 'clobfield' => $entry['category'], + ]); + $this->assertEquals($entry['expectedResult'], $result); + } + + $query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`'); + $result = $query->execute(); + $this->assertTrue((bool)$result); + $this->assertEquals(7, count($query->fetchAll())); + } + + public function testInsertIfNotExistNull() { + $this->makeTestTable(); + $categoryEntries = [ + ['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1], + ['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0], + ['addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1], + ]; + + foreach($categoryEntries as $entry) { + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'integerfield_default' => $entry['addressbookid'], + 'clobfield' => $entry['fullname'], + ]); + $this->assertEquals($entry['expectedResult'], $result); + } + + $query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`'); + $result = $query->execute(); + $this->assertTrue((bool)$result); + $this->assertEquals(2, count($query->fetchAll())); + } + + public function testInsertIfNotExistDonTOverwrite() { + $this->makeTestTable(); + $fullName = 'fullname test'; + $uri = 'uri_1'; + + // Normal test to have same known data inserted. + $query = $this->connection->prepare('INSERT INTO `*PREFIX*table` (`textfield`, `clobfield`) VALUES (?, ?)'); + $result = $query->execute([$fullName, $uri]); + $this->assertEquals(1, $result); + $query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?'); + $result = $query->execute([$uri]); + $this->assertTrue($result); + $rowset = $query->fetchAll(); + $this->assertEquals(1, count($rowset)); + $this->assertArrayHasKey('textfield', $rowset[0]); + $this->assertEquals($fullName, $rowset[0]['textfield']); + + // Try to insert a new row + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'textfield' => $fullName, + 'clobfield' => $uri, + ]); + $this->assertEquals(0, $result); + + $query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?'); + $result = $query->execute([$uri]); + $this->assertTrue($result); + // Test that previously inserted data isn't overwritten + // And that a new row hasn't been inserted. + $rowset = $query->fetchAll(); + $this->assertEquals(1, count($rowset)); + $this->assertArrayHasKey('textfield', $rowset[0]); + $this->assertEquals($fullName, $rowset[0]['textfield']); + } + + public function testInsertIfNotExistsViolating() { + $this->makeTestTable(); + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'textfield' => md5('welcome.txt'), + 'clobfield' => $this->getUniqueID() + ]); + $this->assertEquals(1, $result); + + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'textfield' => md5('welcome.txt'), + 'clobfield' => $this->getUniqueID() + ],['textfield']); + + $this->assertEquals(0, $result); + } + + public function insertIfNotExistsViolatingThrows() { + return [ + [null], + [['clobfield']], + ]; + } + + /** + * @dataProvider insertIfNotExistsViolatingThrows + * @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException + * + * @param array $compareKeys + */ + public function testInsertIfNotExistsViolatingThrows($compareKeys) { + $this->makeTestTable(); + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'integerfield' => 1, + 'clobfield' => $this->getUniqueID() + ]); + $this->assertEquals(1, $result); + + $result = $this->connection->insertIfNotExist('*PREFIX*table', + [ + 'integerfield' => 1, + 'clobfield' => $this->getUniqueID() + ], $compareKeys); + + $this->assertEquals(0, $result); + } + } diff --git a/tests/lib/DB/LegacyDBTest.php b/tests/lib/DB/LegacyDBTest.php index 3cf40228225..578d28139bc 100644 --- a/tests/lib/DB/LegacyDBTest.php +++ b/tests/lib/DB/LegacyDBTest.php @@ -134,140 +134,6 @@ class LegacyDBTest extends \Test\TestCase { // now we can check if the two ids are in correct order $this->assertGreaterThan($id1, $id2); } - - public function testinsertIfNotExist() { - $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), - array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 0), - array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1), - ); - - foreach($categoryEntries as $entry) { - $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table3, - array( - 'uid' => $entry['user'], - 'type' => $entry['type'], - 'category' => $entry['category'], - )); - $this->assertEquals($entry['expectedResult'], $result); - } - - $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`'); - $result = $query->execute(); - $this->assertTrue((bool)$result); - $this->assertEquals(4, count($result->fetchAll())); - } - - public function testInsertIfNotExistNull() { - $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 = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2, - array( - 'addressbookid' => $entry['addressbookid'], - 'fullname' => $entry['fullname'], - )); - $this->assertEquals($entry['expectedResult'], $result); - } - - $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table2.'`'); - $result = $query->execute(); - $this->assertTrue((bool)$result); - $this->assertEquals(2, count($result->fetchAll())); - } - - 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)); - $this->assertEquals(1, $result); - $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); - $result = $query->execute(array($uri)); - $this->assertTrue((bool)$result); - $rowset = $result->fetchAll(); - $this->assertEquals(1, count($rowset)); - $this->assertArrayHasKey('carddata', $rowset[0]); - $this->assertEquals($carddata, $rowset[0]['carddata']); - - // Try to insert a new row - $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2, - array( - 'fullname' => $fullName, - 'uri' => $uri, - )); - $this->assertEquals(0, $result); - - $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); - $result = $query->execute(array($uri)); - $this->assertTrue((bool)$result); - // Test that previously inserted data isn't overwritten - // And that a new row hasn't been inserted. - $rowset = $result->fetchAll(); - $this->assertEquals(1, count($rowset)); - $this->assertArrayHasKey('carddata', $rowset[0]); - $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}"; diff --git a/tests/lib/DB/MDB2SchemaReaderTest.php b/tests/lib/DB/MDB2SchemaReaderTest.php index 3daf0dd7589..3b44f15f756 100644 --- a/tests/lib/DB/MDB2SchemaReaderTest.php +++ b/tests/lib/DB/MDB2SchemaReaderTest.php @@ -54,7 +54,7 @@ class MDB2SchemaReaderTest extends TestCase { $this->assertCount(8, $table->getColumns()); $this->assertEquals(4, $table->getColumn('integerfield')->getLength()); - $this->assertFalse($table->getColumn('integerfield')->getAutoincrement()); + $this->assertTrue($table->getColumn('integerfield')->getAutoincrement()); $this->assertEquals(0, $table->getColumn('integerfield')->getDefault()); $this->assertTrue($table->getColumn('integerfield')->getNotnull()); $this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $table->getColumn('integerfield')->getType()); diff --git a/tests/lib/DB/testschema.xml b/tests/lib/DB/testschema.xml index 8e6dc1a1d60..5f449c936d9 100644 --- a/tests/lib/DB/testschema.xml +++ b/tests/lib/DB/testschema.xml @@ -19,6 +19,7 @@ <notnull>true</notnull> <primary>true</primary> <length>4</length> + <autoincrement>1</autoincrement> </field> <field> <name>integerfield_default</name> |