ChadoQueryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }