Unit testing database access to old contacts tables

This commit is contained in:
Thomas Müller 2016-01-21 15:51:51 +01:00
parent b2976eb72c
commit 8f4ab55b4b
3 changed files with 267 additions and 3 deletions

View File

@ -39,8 +39,8 @@ class AddressBookAdapter {
public function foreachBook($user, \Closure $callBack) {
// get all addressbooks of that user
$query = $this->dbConnection->getQueryBuilder();
$stmt = $query->select()->from($this->sourceBookTable)
->where($query->expr()->eq('user', $query->createNamedParameter($user)))
$stmt = $query->select('*')->from($this->sourceBookTable)
->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
->execute();
while($row = $stmt->fetch()) {
@ -60,7 +60,7 @@ class AddressBookAdapter {
*/
public function foreachCard($addressBookId, \Closure $callBack) {
$query = $this->dbConnection->getQueryBuilder();
$stmt = $query->select()->from($this->sourceCardsTable)
$stmt = $query->select('*')->from($this->sourceCardsTable)
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
->execute();

View File

@ -0,0 +1,113 @@
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\Tests\Unit\Migration;
use DomainException;
use OCA\Dav\Migration\AddressBookAdapter;
use OCP\IDBConnection;
use Test\TestCase;
/**
* Class AddressbookAdapterTest
*
* @group DB
*
* @package OCA\DAV\Tests\Unit\Migration
*/
class AddressbookAdapterTest extends TestCase {
/** @var IDBConnection */
private $db;
/** @var AddressBookAdapter */
private $adapter;
/** @var array */
private $books = [];
/** @var array */
private $cards = [];
public function setUp() {
parent::setUp();
$this->db = \OC::$server->getDatabaseConnection();
$manager = new \OC\DB\MDB2SchemaManager($this->db);
$manager->createDbFromStructure(__DIR__ . '/contacts_schema.xml');
$this->adapter = new AddressBookAdapter($this->db);
}
public function tearDown() {
$this->db->dropTable('contacts_addressbooks');
$this->db->dropTable('contacts_cards');
parent::tearDown();
}
/**
* @expectedException DomainException
*/
public function testOldTablesDoNotExist() {
$adapter = new AddressBookAdapter(\OC::$server->getDatabaseConnection(), 'crazy_table_that_does_no_exist');
$adapter->setup();
}
public function test() {
// insert test data
$builder = $this->db->getQueryBuilder();
$builder->insert('contacts_addressbooks')
->values([
'userid' => $builder->createNamedParameter('test-user-666'),
'displayname' => $builder->createNamedParameter('Display Name'),
'uri' => $builder->createNamedParameter('contacts'),
'description' => $builder->createNamedParameter('An address book for testing'),
'ctag' => $builder->createNamedParameter('112233'),
'active' => $builder->createNamedParameter('1')
])
->execute();
$builder = $this->db->getQueryBuilder();
$builder->insert('contacts_cards')
->values([
'addressbookid' => $builder->createNamedParameter(6666),
'fullname' => $builder->createNamedParameter('Full Name'),
'carddata' => $builder->createNamedParameter('datadatadata'),
'uri' => $builder->createNamedParameter('some-card.vcf'),
'lastmodified' => $builder->createNamedParameter('112233'),
])
->execute();
// test the adapter
$this->adapter->foreachBook('test-user-666', function($row) {
$this->books[] = $row;
});
$this->assertArrayHasKey('id', $this->books[0]);
$this->assertEquals('test-user-666', $this->books[0]['userid']);
$this->assertEquals('Display Name', $this->books[0]['displayname']);
$this->assertEquals('contacts', $this->books[0]['uri']);
$this->assertEquals('An address book for testing', $this->books[0]['description']);
$this->assertEquals('112233', $this->books[0]['ctag']);
$this->adapter->foreachCard(6666, function($row) {
$this->cards[]= $row;
});
$this->assertArrayHasKey('id', $this->cards[0]);
$this->assertEquals(6666, $this->cards[0]['addressbookid']);
}
}

View File

@ -0,0 +1,151 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<database>
<name>*dbname*</name>
<create>true</create>
<overwrite>false</overwrite>
<charset>utf8</charset>
<table>
<name>*dbprefix*contacts_addressbooks</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>userid</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>displayname</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>255</length>
</field>
<field>
<name>uri</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>200</length>
</field>
<field>
<name>description</name>
<type>text</type>
<notnull>false</notnull>
<length>255</length>
</field>
<field>
<name>ctag</name>
<type>integer</type>
<default>1</default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>4</length>
</field>
<field>
<name>active</name>
<type>integer</type>
<default>1</default>
<notnull>true</notnull>
<length>4</length>
</field>
<index>
<name>c_addressbook_userid_index</name>
<field>
<name>userid</name>
<sorting>ascending</sorting>
</field>
</index>
</declaration>
</table>
<table>
<name>*dbprefix*contacts_cards</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>addressbookid</name>
<type>integer</type>
<default></default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>4</length>
</field>
<field>
<name>fullname</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>255</length>
</field>
<field>
<name>carddata</name>
<type>clob</type>
<notnull>false</notnull>
</field>
<field>
<name>uri</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>200</length>
</field>
<field>
<name>lastmodified</name>
<type>integer</type>
<default></default>
<notnull>false</notnull>
<unsigned>true</unsigned>
<length>4</length>
</field>
<index>
<name>c_addressbookid_index</name>
<field>
<name>addressbookid</name>
<sorting>ascending</sorting>
</field>
</index>
</declaration>
</table>
</database>