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. ]);
  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. /**
  136. * @expectedException \OCP\PreConditionNotMetException
  137. */
  138. public function testSetValuesOverWritePreconditionFailed() {
  139. $this->makeTestTable();
  140. $this->connection->setValues('table', [
  141. 'integerfield' => 1
  142. ], [
  143. 'textfield' => 'foo',
  144. 'booleanfield' => true,
  145. 'clobfield' => 'not_null'
  146. ]);
  147. $this->connection->setValues('table', [
  148. 'integerfield' => 1
  149. ], [
  150. 'textfield' => 'bar'
  151. ], [
  152. 'booleanfield' => false
  153. ]);
  154. }
  155. public function testSetValuesSameNoError() {
  156. $this->makeTestTable();
  157. $this->connection->setValues('table', [
  158. 'integerfield' => 1
  159. ], [
  160. 'textfield' => 'foo',
  161. 'clobfield' => 'not_null'
  162. ]);
  163. // this will result in 'no affected rows' on certain optimizing DBs
  164. // ensure the PreConditionNotMetException isn't thrown
  165. $this->connection->setValues('table', [
  166. 'integerfield' => 1
  167. ], [
  168. 'textfield' => 'foo'
  169. ]);
  170. }
  171. }