You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionTest.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\DB;
  9. use Doctrine\DBAL\Platforms\SqlitePlatform;
  10. use OC\DB\MDB2SchemaManager;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. /**
  13. * Class Connection
  14. *
  15. * @group DB
  16. *
  17. * @package Test\DB
  18. */
  19. class ConnectionTest extends \Test\TestCase {
  20. /**
  21. * @var \OCP\IDBConnection
  22. */
  23. private $connection;
  24. public static function setUpBeforeClass(): void {
  25. self::dropTestTable();
  26. parent::setUpBeforeClass();
  27. }
  28. public static function tearDownAfterClass(): void {
  29. self::dropTestTable();
  30. parent::tearDownAfterClass();
  31. }
  32. protected static function dropTestTable() {
  33. if (\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite') !== 'oci') {
  34. \OC::$server->getDatabaseConnection()->dropTable('table');
  35. }
  36. }
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->connection = \OC::$server->getDatabaseConnection();
  40. }
  41. protected function tearDown(): void {
  42. parent::tearDown();
  43. $this->connection->dropTable('table');
  44. }
  45. /**
  46. * @param string $table
  47. */
  48. public function assertTableExist($table) {
  49. if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
  50. // sqlite removes the tables after closing the DB
  51. $this->addToAssertionCount(1);
  52. } else {
  53. $this->assertTrue($this->connection->tableExists($table), 'Table ' . $table . ' exists.');
  54. }
  55. }
  56. /**
  57. * @param string $table
  58. */
  59. public function assertTableNotExist($table) {
  60. if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
  61. // sqlite removes the tables after closing the DB
  62. $this->addToAssertionCount(1);
  63. } else {
  64. $this->assertFalse($this->connection->tableExists($table), 'Table ' . $table . " doesn't exist.");
  65. }
  66. }
  67. private function makeTestTable() {
  68. $schemaManager = new MDB2SchemaManager($this->connection);
  69. $schemaManager->createDbFromStructure(__DIR__ . '/testschema.xml');
  70. }
  71. public function testTableExists() {
  72. $this->assertTableNotExist('table');
  73. $this->makeTestTable();
  74. $this->assertTableExist('table');
  75. }
  76. /**
  77. * @depends testTableExists
  78. */
  79. public function testDropTable() {
  80. $this->makeTestTable();
  81. $this->assertTableExist('table');
  82. $this->connection->dropTable('table');
  83. $this->assertTableNotExist('table');
  84. }
  85. private function getTextValueByIntergerField($integerField) {
  86. $builder = $this->connection->getQueryBuilder();
  87. $query = $builder->select('textfield')
  88. ->from('table')
  89. ->where($builder->expr()->eq('integerfield', $builder->createNamedParameter($integerField, IQueryBuilder::PARAM_INT)));
  90. $result = $query->execute();
  91. return $result->fetchColumn();
  92. }
  93. public function testSetValues() {
  94. $this->makeTestTable();
  95. $this->connection->setValues('table', [
  96. 'integerfield' => 1
  97. ], [
  98. 'textfield' => 'foo',
  99. 'clobfield' => 'not_null'
  100. ]);
  101. $this->assertEquals('foo', $this->getTextValueByIntergerField(1));
  102. }
  103. public function testSetValuesOverWrite() {
  104. $this->makeTestTable();
  105. $this->connection->setValues('table', [
  106. 'integerfield' => 1
  107. ], [
  108. 'textfield' => 'foo'
  109. ]);
  110. $this->connection->setValues('table', [
  111. 'integerfield' => 1
  112. ], [
  113. 'textfield' => 'bar'
  114. ]);
  115. $this->assertEquals('bar', $this->getTextValueByIntergerField(1));
  116. }
  117. public function testSetValuesOverWritePrecondition() {
  118. $this->makeTestTable();
  119. $this->connection->setValues('table', [
  120. 'integerfield' => 1
  121. ], [
  122. 'textfield' => 'foo',
  123. 'booleanfield' => true,
  124. 'clobfield' => 'not_null'
  125. ]);
  126. $this->connection->setValues('table', [
  127. 'integerfield' => 1
  128. ], [
  129. 'textfield' => 'bar'
  130. ], [
  131. 'booleanfield' => true
  132. ]);
  133. $this->assertEquals('bar', $this->getTextValueByIntergerField(1));
  134. }
  135. public function testSetValuesOverWritePreconditionFailed() {
  136. $this->expectException(\OCP\PreConditionNotMetException::class);
  137. $this->makeTestTable();
  138. $this->connection->setValues('table', [
  139. 'integerfield' => 1
  140. ], [
  141. 'textfield' => 'foo',
  142. 'booleanfield' => true,
  143. 'clobfield' => 'not_null'
  144. ]);
  145. $this->connection->setValues('table', [
  146. 'integerfield' => 1
  147. ], [
  148. 'textfield' => 'bar'
  149. ], [
  150. 'booleanfield' => false
  151. ]);
  152. }
  153. public function testSetValuesSameNoError() {
  154. $this->makeTestTable();
  155. $this->connection->setValues('table', [
  156. 'integerfield' => 1
  157. ], [
  158. 'textfield' => 'foo',
  159. 'clobfield' => 'not_null'
  160. ]);
  161. // this will result in 'no affected rows' on certain optimizing DBs
  162. // ensure the PreConditionNotMetException isn't thrown
  163. $this->connection->setValues('table', [
  164. 'integerfield' => 1
  165. ], [
  166. 'textfield' => 'foo'
  167. ]);
  168. $this->addToAssertionCount(1);
  169. }
  170. public function testInsertIfNotExist() {
  171. $this->makeTestTable();
  172. $categoryEntries = [
  173. ['user' => 'test', 'category' => 'Family', 'expectedResult' => 1],
  174. ['user' => 'test', 'category' => 'Friends', 'expectedResult' => 1],
  175. ['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 1],
  176. ['user' => 'test', 'category' => 'Coworkers', 'expectedResult' => 0],
  177. ['user' => 'test', 'category' => 'School', 'expectedResult' => 1],
  178. ['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 1],
  179. ['user' => 'test2', 'category' => 'Coworkers2', 'expectedResult' => 0],
  180. ['user' => 'test2', 'category' => 'School2', 'expectedResult' => 1],
  181. ['user' => 'test2', 'category' => 'Coworkers', 'expectedResult' => 1],
  182. ];
  183. foreach ($categoryEntries as $entry) {
  184. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  185. [
  186. 'textfield' => $entry['user'],
  187. 'clobfield' => $entry['category'],
  188. ]);
  189. $this->assertEquals($entry['expectedResult'], $result);
  190. }
  191. $query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`');
  192. $result = $query->execute();
  193. $this->assertTrue((bool)$result);
  194. $this->assertEquals(7, count($query->fetchAll()));
  195. }
  196. public function testInsertIfNotExistNull() {
  197. $this->makeTestTable();
  198. $categoryEntries = [
  199. ['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1],
  200. ['addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0],
  201. ['addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1],
  202. ];
  203. foreach ($categoryEntries as $entry) {
  204. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  205. [
  206. 'integerfield_default' => $entry['addressbookid'],
  207. 'clobfield' => $entry['fullname'],
  208. ]);
  209. $this->assertEquals($entry['expectedResult'], $result);
  210. }
  211. $query = $this->connection->prepare('SELECT * FROM `*PREFIX*table`');
  212. $result = $query->execute();
  213. $this->assertTrue((bool)$result);
  214. $this->assertEquals(2, count($query->fetchAll()));
  215. }
  216. public function testInsertIfNotExistDonTOverwrite() {
  217. $this->makeTestTable();
  218. $fullName = 'fullname test';
  219. $uri = 'uri_1';
  220. // Normal test to have same known data inserted.
  221. $query = $this->connection->prepare('INSERT INTO `*PREFIX*table` (`textfield`, `clobfield`) VALUES (?, ?)');
  222. $result = $query->execute([$fullName, $uri]);
  223. $this->assertEquals(1, $result);
  224. $query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?');
  225. $result = $query->execute([$uri]);
  226. $this->assertTrue($result);
  227. $rowset = $query->fetchAll();
  228. $this->assertEquals(1, count($rowset));
  229. $this->assertArrayHasKey('textfield', $rowset[0]);
  230. $this->assertEquals($fullName, $rowset[0]['textfield']);
  231. // Try to insert a new row
  232. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  233. [
  234. 'textfield' => $fullName,
  235. 'clobfield' => $uri,
  236. ]);
  237. $this->assertEquals(0, $result);
  238. $query = $this->connection->prepare('SELECT `textfield`, `clobfield` FROM `*PREFIX*table` WHERE `clobfield` = ?');
  239. $result = $query->execute([$uri]);
  240. $this->assertTrue($result);
  241. // Test that previously inserted data isn't overwritten
  242. // And that a new row hasn't been inserted.
  243. $rowset = $query->fetchAll();
  244. $this->assertEquals(1, count($rowset));
  245. $this->assertArrayHasKey('textfield', $rowset[0]);
  246. $this->assertEquals($fullName, $rowset[0]['textfield']);
  247. }
  248. public function testInsertIfNotExistsViolating() {
  249. $this->makeTestTable();
  250. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  251. [
  252. 'textfield' => md5('welcome.txt'),
  253. 'clobfield' => $this->getUniqueID()
  254. ]);
  255. $this->assertEquals(1, $result);
  256. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  257. [
  258. 'textfield' => md5('welcome.txt'),
  259. 'clobfield' => $this->getUniqueID()
  260. ],['textfield']);
  261. $this->assertEquals(0, $result);
  262. }
  263. public function insertIfNotExistsViolatingThrows() {
  264. return [
  265. [null],
  266. [['clobfield']],
  267. ];
  268. }
  269. /**
  270. * @dataProvider insertIfNotExistsViolatingThrows
  271. *
  272. * @param array $compareKeys
  273. */
  274. public function testInsertIfNotExistsViolatingUnique($compareKeys) {
  275. $this->makeTestTable();
  276. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  277. [
  278. 'integerfield' => 1,
  279. 'clobfield' => $this->getUniqueID()
  280. ]);
  281. $this->assertEquals(1, $result);
  282. $result = $this->connection->insertIfNotExist('*PREFIX*table',
  283. [
  284. 'integerfield' => 1,
  285. 'clobfield' => $this->getUniqueID()
  286. ], $compareKeys);
  287. $this->assertEquals(0, $result);
  288. }
  289. public function testUniqueConstraintViolating() {
  290. $this->expectException(\Doctrine\DBAL\Exception\UniqueConstraintViolationException::class);
  291. $this->makeTestTable();
  292. $testQuery = 'INSERT INTO `*PREFIX*table` (`integerfield`, `textfield`) VALUES(?, ?)';
  293. $testParams = [1, 'hello'];
  294. $this->connection->executeUpdate($testQuery, $testParams);
  295. $this->connection->executeUpdate($testQuery, $testParams);
  296. }
  297. }