ConnectionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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() {
  25. self::dropTestTable();
  26. parent::setUpBeforeClass();
  27. }
  28. public static function tearDownAfterClass() {
  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. public function setUp() {
  38. parent::setUp();
  39. $this->connection = \OC::$server->getDatabaseConnection();
  40. }
  41. public function tearDown() {
  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->assertTrue(true);
  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->assertTrue(true);
  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. 'clobfield' => 'not_null'
  110. ]);
  111. $this->connection->setValues('table', [
  112. 'integerfield' => 1
  113. ], [
  114. 'textfield' => 'bar'
  115. ]);
  116. $this->assertEquals('bar', $this->getTextValueByIntergerField(1));
  117. }
  118. public function testSetValuesOverWritePrecondition() {
  119. $this->makeTestTable();
  120. $this->connection->setValues('table', [
  121. 'integerfield' => 1
  122. ], [
  123. 'textfield' => 'foo',
  124. 'booleanfield' => true,
  125. 'clobfield' => 'not_null'
  126. ]);
  127. $this->connection->setValues('table', [
  128. 'integerfield' => 1
  129. ], [
  130. 'textfield' => 'bar'
  131. ], [
  132. 'booleanfield' => true
  133. ]);
  134. $this->assertEquals('bar', $this->getTextValueByIntergerField(1));
  135. }
  136. /**
  137. * @expectedException \OCP\PreConditionNotMetException
  138. */
  139. public function testSetValuesOverWritePreconditionFailed() {
  140. $this->makeTestTable();
  141. $this->connection->setValues('table', [
  142. 'integerfield' => 1
  143. ], [
  144. 'textfield' => 'foo',
  145. 'booleanfield' => true,
  146. 'clobfield' => 'not_null'
  147. ]);
  148. $this->connection->setValues('table', [
  149. 'integerfield' => 1
  150. ], [
  151. 'textfield' => 'bar'
  152. ], [
  153. 'booleanfield' => false
  154. ]);
  155. }
  156. public function testSetValuesSameNoError() {
  157. $this->makeTestTable();
  158. $this->connection->setValues('table', [
  159. 'integerfield' => 1
  160. ], [
  161. 'textfield' => 'foo',
  162. 'clobfield' => 'not_null'
  163. ]);
  164. // this will result in 'no affected rows' on certain optimizing DBs
  165. // ensure the PreConditionNotMetException isn't thrown
  166. $this->connection->setValues('table', [
  167. 'integerfield' => 1
  168. ], [
  169. 'textfield' => 'foo'
  170. ]);
  171. }
  172. }