summaryrefslogtreecommitdiffstats
path: root/tests/lib/db/connection.php
blob: dd9b31f3ed703f20b02a2f057a0714a4a7e529c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

/**
 * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\DB;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use OC\DB\MDB2SchemaManager;

/**
 * Class Connection
 *
 * @group DB
 *
 * @package Test\DB
 */
class Connection extends \Test\TestCase {
	/**
	 * @var \OCP\IDBConnection
	 */
	private $connection;

	public static function setUpBeforeClass() {
		self::dropTestTable();
		parent::setUpBeforeClass();
	}

	public static function tearDownAfterClass() {
		self::dropTestTable();
		parent::tearDownAfterClass();
	}

	protected static function dropTestTable() {
		if (\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite') !== 'oci') {
			\OC::$server->getDatabaseConnection()->dropTable('table');
		}
	}

	public function setUp() {
		parent::setUp();
		$this->connection = \OC::$server->getDatabaseConnection();
	}

	/**
	 * @param string $table
	 */
	public function assertTableExist($table) {
		if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
			// sqlite removes the tables after closing the DB
			$this->assertTrue(true);
		} else {
			$this->assertTrue($this->connection->tableExists($table), 'Table ' . $table . ' exists.');
		}
	}

	/**
	 * @param string $table
	 */
	public function assertTableNotExist($table) {
		if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
			// sqlite removes the tables after closing the DB
			$this->assertTrue(true);
		} else {
			$this->assertFalse($this->connection->tableExists($table), 'Table ' . $table . ' doesnt exists.');
		}
	}

	private function makeTestTable() {
		$schemaManager = new MDB2SchemaManager($this->connection);
		$schemaManager->createDbFromStructure(__DIR__ . '/testschema.xml');
	}

	public function testTableExists() {
		$this->assertTableNotExist('table');
		$this->makeTestTable();
		$this->assertTableExist('table');
	}

	/**
	 * @depends testTableExists
	 */
	public function testDropTable() {
		$this->assertTableExist('table');
		$this->connection->dropTable('table');
		$this->assertTableNotExist('table');
	}

	private function getTextValueByIntergerField($integerField) {
		$builder = $this->connection->getQueryBuilder();
		$query = $builder->select('textfield')
			->from('table')
			->where($builder->expr()->eq('integerfield', $builder->createNamedParameter($integerField, \PDO::PARAM_INT)));

		$result = $query->execute();
		return $result->fetchColumn();
	}

	public function testSetValues() {
		$this->makeTestTable();
		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'foo',
			'clobfield' => 'not_null'
		]);

		$this->assertEquals('foo', $this->getTextValueByIntergerField(1));

		$this->connection->dropTable('table');
	}

	public function testSetValuesOverWrite() {
		$this->makeTestTable();
		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'foo',
			'clobfield' => 'not_null'
		]);

		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'bar'
		]);

		$this->assertEquals('bar', $this->getTextValueByIntergerField(1));

		$this->connection->dropTable('table');
	}

	public function testSetValuesOverWritePrecondition() {
		$this->makeTestTable();
		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'foo',
			'booleanfield' => true,
			'clobfield' => 'not_null'
		]);

		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'bar'
		], [
			'booleanfield' => true
		]);

		$this->assertEquals('bar', $this->getTextValueByIntergerField(1));

		$this->connection->dropTable('table');
	}

	/**
	 * @expectedException \OCP\PreConditionNotMetException
	 */
	public function testSetValuesOverWritePreconditionFailed() {
		$this->makeTestTable();
		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'foo',
			'booleanfield' => true,
			'clobfield' => 'not_null'
		]);

		$this->connection->setValues('table', [
			'integerfield' => 1
		], [
			'textfield' => 'bar'
		], [
			'booleanfield' => false
		]);
	}
}