ChadoQueryTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. */
  59. public function test_chado_db_select_works_for_chado_tables() {
  60. $analysis_record = factory('chado.analysis')->create();
  61. $id = $analysis_record->analysis_id;
  62. // Test passing a table name without brackets or braces.
  63. $query = chado_db_select('analysis', 't');
  64. $analysis = $query
  65. ->condition('analysis_id', $id)
  66. ->fields('t')
  67. ->execute()
  68. ->fetchObject();
  69. $this->assertNotFalse($analysis);
  70. $this->assertNotEmpty($analysis);
  71. $this->assertEquals($id, $analysis->analysis_id);
  72. }
  73. /**
  74. * @group api
  75. * @group chado
  76. */
  77. public function test_chado_db_select_should_throw_an_exception_if_table_is_undefined() {
  78. $this->expectException(\Exception::class);
  79. chado_db_select('some_nonexistent_table', 'd')->execute();
  80. }
  81. /**
  82. * @group api
  83. * @group chado
  84. */
  85. public function test_chado_db_select_recognizes_non_chado_tables() {
  86. $query = chado_db_select('users');
  87. $query->range(0, 1);
  88. $results = $query->execute()->fetchAll();
  89. $this->assertNotEmpty($results);
  90. }
  91. /**
  92. * @group api
  93. * @group chado
  94. */
  95. public function test_joining_chado_tables_in_chado_db_select() {
  96. $feature = factory('chado.feature')->create();
  97. $cvterm = factory('chado.cvterm')->create();
  98. $pub = factory('chado.pub')->create();
  99. $feature_cvterm = chado_insert_record('feature_cvterm', [
  100. 'feature_id' => $feature->feature_id,
  101. 'cvterm_id' => $cvterm->cvterm_id,
  102. 'pub_id' => $pub->pub_id
  103. ]);
  104. $query = chado_db_select('feature', 'f');
  105. $query->join('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  106. $query->fields('f', ['name']);
  107. $query->fields('fcvt', ['cvterm_id']);
  108. $query->condition('f.feature_id', $feature->feature_id);
  109. $found = $query->execute()->fetchObject();
  110. $this->assertNotEmpty($found);
  111. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  112. }
  113. /**
  114. * @group api
  115. * @group chado
  116. */
  117. public function test_left_joining_chado_tables_in_chado_db_select() {
  118. $feature = factory('chado.feature')->create();
  119. $cvterm = factory('chado.cvterm')->create();
  120. $pub = factory('chado.pub')->create();
  121. $feature_cvterm = chado_insert_record('feature_cvterm', [
  122. 'feature_id' => $feature->feature_id,
  123. 'cvterm_id' => $cvterm->cvterm_id,
  124. 'pub_id' => $pub->pub_id
  125. ]);
  126. $query = chado_db_select('feature', 'f');
  127. $query->leftJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  128. $query->fields('f', ['name']);
  129. $query->fields('fcvt', ['cvterm_id']);
  130. $query->condition('f.feature_id', $feature->feature_id);
  131. $found = $query->execute()->fetchObject();
  132. $this->assertNotEmpty($found);
  133. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  134. }
  135. /**
  136. * @group api
  137. * @group chado
  138. */
  139. public function test_right_joining_chado_tables_in_chado_db_select() {
  140. $feature = factory('chado.feature')->create();
  141. $cvterm = factory('chado.cvterm')->create();
  142. $pub = factory('chado.pub')->create();
  143. $feature_cvterm = chado_insert_record('feature_cvterm', [
  144. 'feature_id' => $feature->feature_id,
  145. 'cvterm_id' => $cvterm->cvterm_id,
  146. 'pub_id' => $pub->pub_id
  147. ]);
  148. $query = chado_db_select('feature', 'f');
  149. $query->rightJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  150. $query->fields('f', ['name']);
  151. $query->fields('fcvt', ['cvterm_id']);
  152. $query->condition('f.feature_id', $feature->feature_id);
  153. $found = $query->execute()->fetchObject();
  154. $this->assertNotEmpty($found);
  155. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  156. }
  157. /**
  158. * @group api
  159. * @group chado
  160. */
  161. public function test_inner_joining_chado_tables_in_chado_db_select() {
  162. $feature = factory('chado.feature')->create();
  163. $cvterm = factory('chado.cvterm')->create();
  164. $pub = factory('chado.pub')->create();
  165. $feature_cvterm = chado_insert_record('feature_cvterm', [
  166. 'feature_id' => $feature->feature_id,
  167. 'cvterm_id' => $cvterm->cvterm_id,
  168. 'pub_id' => $pub->pub_id
  169. ]);
  170. $query = chado_db_select('feature', 'f');
  171. $query->innerJoin('feature_cvterm', 'fcvt', 'f.feature_id = fcvt.feature_id');
  172. $query->fields('f', ['name']);
  173. $query->fields('fcvt', ['cvterm_id']);
  174. $query->condition('f.feature_id', $feature->feature_id);
  175. $found = $query->execute()->fetchObject();
  176. $this->assertNotEmpty($found);
  177. $this->assertEquals($feature_cvterm['cvterm_id'], $found->cvterm_id);
  178. }
  179. }