ChadoQueryTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Tests;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class ChadoQueryTest extends TripalTestCase {
  6. // Uncomment to auto start and rollback db transactions per test method.
  7. use DBTransaction;
  8. /**
  9. * @group filter
  10. * See PR 827.
  11. */
  12. public function test_filter_level() {
  13. $stock = factory('chado.stock')->create(['uniquename' => 'octopus_core_test_name']);
  14. // Test 1. Pass a single filter.
  15. $selector = [
  16. 'stock_id' => $stock->stock_id,
  17. 'uniquename' => [
  18. 'op' => 'LIKE',
  19. 'data' => 'octopus%',
  20. ],
  21. ];
  22. $object = chado_generate_var('stock', $selector);
  23. $this->assertNotNull($object->stock_id);
  24. $this->assertEquals($stock->stock_id, $object->stock_id);
  25. // Test 2 Pass an array of filters with a single item.
  26. $selector = [
  27. 'stock_id' => $stock->stock_id,
  28. 'uniquename' => [
  29. [
  30. 'op' => 'LIKE',
  31. 'data' => 'octopus%',
  32. ],
  33. ],
  34. ];
  35. $object = chado_generate_var('stock', $selector);
  36. $this->assertNotNull($object->stock_id);
  37. $this->assertEquals($stock->stock_id, $object->stock_id);
  38. // Test 3 Pass an array of filters with multiple items.
  39. $selector = [
  40. 'type_id' => [
  41. [
  42. 'op' => '>',
  43. 'data' => ($stock->type_id - 1),
  44. ],
  45. [
  46. 'op' => '<',
  47. 'data' => ($stock->type_id + 1),
  48. ],
  49. ],
  50. ];
  51. $object = chado_generate_var('stock', $selector);
  52. $this->assertNotNull($object->stock_id);
  53. $this->assertEquals($stock->stock_id, $object->stock_id);
  54. }
  55. /**
  56. * @group api
  57. * @group chado
  58. * @group chado_db_select
  59. */
  60. public function test_chado_db_select_works_for_chado_tables() {
  61. $analysis_record = factory('chado.analysis')->create();
  62. $id = $analysis_record->analysis_id;
  63. // Test passing a table name without brackets or braces.
  64. $query = chado_db_select('analysis', 't');
  65. $analysis = $query
  66. ->condition('analysis_id', $id)
  67. ->fields('t')
  68. ->execute()
  69. ->fetchObject();
  70. $this->assertNotFalse($analysis);
  71. $this->assertNotEmpty($analysis);
  72. $this->assertEquals($id, $analysis->analysis_id);
  73. }
  74. /**
  75. * @group api
  76. * @group chado
  77. * @group chado_db_select
  78. */
  79. public function test_chado_db_select_should_throw_an_exception_if_table_is_undefined() {
  80. $this->expectException(\Exception::class);
  81. chado_db_select('some_nonexistent_table', 'd')->execute();
  82. }
  83. /**
  84. * @group api
  85. * @group chado
  86. * @group chado_db_select
  87. */
  88. public function test_chado_db_select_recognizes_non_chado_tables() {
  89. $query = chado_db_select('users', 'u');
  90. $query->fields('u');
  91. $query->range(0, 1);
  92. $results = $query->execute()->fetchAll();
  93. $this->assertNotEmpty($results);
  94. }
  95. /**
  96. * @group api
  97. * @group chado
  98. * @group chado_db_select
  99. */
  100. public function test_chado_db_select_handles_aliases_correctly() {
  101. $query = chado_db_select('public.users');
  102. $query->fields('public_users');
  103. $query->range(0, 1);
  104. $results = $query->execute()->fetchAll();
  105. $this->assertNotEmpty($results);
  106. }
  107. /**
  108. * @group api
  109. * @group chado
  110. * @group chado_db_select
  111. */
  112. public function test_joining_chado_tables_in_chado_db_select() {
  113. $feature = factory('chado.feature')->create();
  114. $cvterm = factory('chado.cvterm')->create();
  115. $pub = factory('chado.pub')->create();
  116. $feature_cvterm = chado_insert_record('feature_cvterm', [
  117. 'feature_id' => $feature->feature_id,
  118. 'cvterm_id' => $cvterm->cvterm_id,
  119. 'pub_id' => $pub->pub_id,
  120. ]);
  121. $query = chado_db_select('feature', 'f');
  122. $query->join('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  123. $query->fields('f', ['name']);
  124. $query->fields('fcvt', ['cvterm_id']);
  125. $query->condition('f.feature_id', $feature->feature_id);
  126. $found = $query->execute()->fetchObject();
  127. $this->assertNotEmpty($found);
  128. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  129. }
  130. /**
  131. * @group api
  132. * @group chado
  133. * @group chado_db_select
  134. */
  135. public function test_left_joining_chado_tables_in_chado_db_select() {
  136. $feature = factory('chado.feature')->create();
  137. $cvterm = factory('chado.cvterm')->create();
  138. $pub = factory('chado.pub')->create();
  139. $feature_cvterm = chado_insert_record('feature_cvterm', [
  140. 'feature_id' => $feature->feature_id,
  141. 'cvterm_id' => $cvterm->cvterm_id,
  142. 'pub_id' => $pub->pub_id,
  143. ]);
  144. $query = chado_db_select('feature', 'f');
  145. $query->leftJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  146. $query->fields('f', ['name']);
  147. $query->fields('fcvt', ['cvterm_id']);
  148. $query->condition('f.feature_id', $feature->feature_id);
  149. $found = $query->execute()->fetchObject();
  150. $this->assertNotEmpty($found);
  151. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  152. }
  153. /**
  154. * @group api
  155. * @group chado
  156. * @group chado_db_select
  157. */
  158. public function test_right_joining_chado_tables_in_chado_db_select() {
  159. $feature = factory('chado.feature')->create();
  160. $cvterm = factory('chado.cvterm')->create();
  161. $pub = factory('chado.pub')->create();
  162. $feature_cvterm = chado_insert_record('feature_cvterm', [
  163. 'feature_id' => $feature->feature_id,
  164. 'cvterm_id' => $cvterm->cvterm_id,
  165. 'pub_id' => $pub->pub_id,
  166. ]);
  167. $query = chado_db_select('feature', 'f');
  168. $query->rightJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  169. $query->fields('f', ['name']);
  170. $query->fields('fcvt', ['cvterm_id']);
  171. $query->condition('f.feature_id', $feature->feature_id);
  172. $found = $query->execute()->fetchObject();
  173. $this->assertNotEmpty($found);
  174. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  175. }
  176. /**
  177. * @group api
  178. * @group chado
  179. * @group chado_db_select
  180. */
  181. public function test_inner_joining_chado_tables_in_chado_db_select() {
  182. $feature = factory('chado.feature')->create();
  183. $cvterm = factory('chado.cvterm')->create();
  184. $pub = factory('chado.pub')->create();
  185. $feature_cvterm = chado_insert_record('feature_cvterm', [
  186. 'feature_id' => $feature->feature_id,
  187. 'cvterm_id' => $cvterm->cvterm_id,
  188. 'pub_id' => $pub->pub_id,
  189. ]);
  190. $query = chado_db_select('feature', 'f');
  191. $query->innerJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  192. $query->fields('f', ['name']);
  193. $query->fields('fcvt', ['cvterm_id']);
  194. $query->condition('f.feature_id', $feature->feature_id);
  195. $found = $query->execute()->fetchObject();
  196. $this->assertNotEmpty($found);
  197. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  198. }
  199. /**
  200. * @group api
  201. * @group chado
  202. * @group chado_db_select
  203. */
  204. public function test_is_chado_table_returns_correct_results() {
  205. $this->assertTrue(\ChadoPrefixExtender::isChadoTable('analysis'));
  206. $this->assertTrue(\ChadoPrefixExtender::isChadoTable('feature_cvtermprop'));
  207. $this->assertFalse(\ChadoPrefixExtender::isChadoTable('users'));
  208. }
  209. /**
  210. * @group api
  211. * @group chado
  212. * @group chado_db_select
  213. */
  214. public function test_get_real_schema_returns_correct_results() {
  215. $chado = chado_get_schema_name('chado');
  216. $public = chado_get_schema_name('drupal');
  217. $this->assertEquals($chado . '.analysis', \ChadoPrefixExtender::getRealSchema('chado.analysis'));
  218. $this->assertEquals($public . '.users', \ChadoPrefixExtender::getRealSchema('public.users'));
  219. $this->assertEquals('users', \ChadoPrefixExtender::getRealSchema('users'));
  220. }
  221. }