diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/data/db_structure.xml | 84 | ||||
-rw-r--r-- | tests/lib/db.php | 63 | ||||
-rw-r--r-- | tests/lib/vcategories.php | 117 |
3 files changed, 264 insertions, 0 deletions
diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 03d7502c441..8a80819adf2 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -135,4 +135,88 @@ </table> + <table> + + <name>*dbprefix*vcategory</name> + + <declaration> + + <field> + <name>id</name> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <autoincrement>1</autoincrement> + <unsigned>true</unsigned> + <length>4</length> + </field> + + <field> + <name>uid</name> + <type>text</type> + <default></default> + <notnull>true</notnull> + <length>64</length> + </field> + + <field> + <name>type</name> + <type>text</type> + <default></default> + <notnull>true</notnull> + <length>64</length> + </field> + + <field> + <name>category</name> + <type>text</type> + <default></default> + <notnull>true</notnull> + <length>255</length> + </field> + + <index> + <name>uid_index</name> + <field> + <name>uid</name> + <sorting>ascending</sorting> + </field> + </index> + + <index> + <name>type_index</name> + <field> + <name>type</name> + <sorting>ascending</sorting> + </field> + </index> + + <index> + <name>category_index</name> + <field> + <name>category</name> + <sorting>ascending</sorting> + </field> + </index> + + <index> + <name>uid_type_category_index</name> + <unique>true</unique> + <field> + <name>uid</name> + <sorting>ascending</sorting> + </field> + <field> + <name>type</name> + <sorting>ascending</sorting> + </field> + <field> + <name>category</name> + <sorting>ascending</sorting> + </field> + </index> + + </declaration> + </table> + </database> diff --git a/tests/lib/db.php b/tests/lib/db.php index 2344f7d8ec4..c2eb38dae83 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -24,6 +24,7 @@ class Test_DB extends UnitTestCase { $this->test_prefix = $r; $this->table1 = $this->test_prefix.'contacts_addressbooks'; $this->table2 = $this->test_prefix.'contacts_cards'; + $this->table3 = $this->test_prefix.'vcategory'; } public function tearDown() { @@ -67,4 +68,66 @@ class Test_DB extends UnitTestCase { $result = $query->execute(array('uri_3')); $this->assertTrue($result); } + + public function testinsertIfNotExist() { + $categoryentries = array( + array('user' => 'test', 'type' => 'contact', 'category' => 'Family'), + array('user' => 'test', 'type' => 'contact', 'category' => 'Friends'), + array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'), + array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'), + array('user' => 'test', 'type' => 'contact', 'category' => 'School'), + ); + + foreach($categoryentries as $entry) { + $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3, + array( + 'uid' => $entry['user'], + 'type' => $entry['type'], + 'category' => $entry['category'], + )); + $this->assertTrue($result); + } + + $query = OC_DB::prepare('SELECT * FROM *PREFIX*'.$this->table3); + $result = $query->execute(); + $this->assertTrue($result); + $this->assertEqual('4', $result->numRows()); + } + + 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->assertTrue($result); + $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?'); + $result = $query->execute(array($uri)); + $this->assertTrue($result); + $row = $result->fetchRow(); + $this->assertArrayHasKey('carddata', $row); + $this->assertEqual($carddata, $row['carddata']); + $this->assertEqual('1', $result->numRows()); + + // Try to insert a new row + $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, + array( + 'fullname' => $fullname, + 'uri' => $uri, + )); + $this->assertTrue($result); + + $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?'); + $result = $query->execute(array($uri)); + $this->assertTrue($result); + $row = $result->fetchRow(); + $this->assertArrayHasKey('carddata', $row); + // Test that previously inserted data isn't overwritten + $this->assertEqual($carddata, $row['carddata']); + // And that a new row hasn't been inserted. + $this->assertEqual('1', $result->numRows()); + + } } diff --git a/tests/lib/vcategories.php b/tests/lib/vcategories.php new file mode 100644 index 00000000000..1d188297ad4 --- /dev/null +++ b/tests/lib/vcategories.php @@ -0,0 +1,117 @@ +<?php +/** +* ownCloud +* +* @author Thomas Tanghus +* @copyright 2012 Thomas Tanghus (thomas@tanghus.net) +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +//require_once("../lib/template.php"); + +class Test_VCategories extends UnitTestCase { + + protected $objectType; + protected $user; + protected $backupGlobals = FALSE; + + public function setUp() { + + OC_User::clearBackends(); + OC_User::useBackend('dummy'); + $this->user = uniqid('user_'); + $this->objectType = uniqid('type_'); + OC_User::createUser($this->user, 'pass'); + OC_User::setUserId($this->user); + + } + + public function tearDown() { + //$query = OC_DB::prepare('DELETE FROM `*PREFIX*vcategories` WHERE `item_type` = ?'); + //$query->execute(array('test')); + } + + public function testInstantiateWithDefaults() { + $defcategories = array('Friends', 'Family', 'Work', 'Other'); + + $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories); + + $this->assertEqual(4, count($catmgr->categories())); + } + + public function testAddCategories() { + $categories = array('Friends', 'Family', 'Work', 'Other'); + + $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories); + + foreach($categories as $category) { + $result = $catmgr->add($category); + $this->assertTrue($result); + } + + $this->assertFalse($catmgr->add('Family')); + $this->assertFalse($catmgr->add('fAMILY')); + + $this->assertEqual(4, count($catmgr->categories())); + } + + public function testdeleteCategories() { + $defcategories = array('Friends', 'Family', 'Work', 'Other'); + $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories); + $this->assertEqual(4, count($catmgr->categories())); + + $catmgr->delete('family'); + $this->assertEqual(3, count($catmgr->categories())); + + $catmgr->delete(array('Friends', 'Work', 'Other')); + $this->assertEqual(0, count($catmgr->categories())); + + } + + public function testAddToCategory() { + $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9); + + $catmgr = new OC_VCategories($this->objectType, $this->user); + + foreach($objids as $id) { + $catmgr->addToCategory($id, 'Family'); + } + + $this->assertEqual(1, count($catmgr->categories())); + $this->assertEqual(9, count($catmgr->idsForCategory('Family'))); + } + + /** + * @depends testAddToCategory + */ + public function testRemoveFromCategory() { + $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9); + + // Is this "legal"? + $this->testAddToCategory(); + $catmgr = new OC_VCategories($this->objectType, $this->user); + + foreach($objids as $id) { + $this->assertTrue(in_array($id, $catmgr->idsForCategory('Family'))); + $catmgr->removeFromCategory($id, 'Family'); + $this->assertFalse(in_array($id, $catmgr->idsForCategory('Family'))); + } + + $this->assertEqual(1, count($catmgr->categories())); + $this->assertEqual(0, count($catmgr->idsForCategory('Family'))); + } + +} |