TripalChadoMViewsAPITest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Tests\tripal_chado\api;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. class TripalChadoMViewsAPITest extends TripalTestCase {
  6. // Use a transaction to roll back changes after every test.
  7. use DBTransaction;
  8. // This variable holds example materialized views that can be used
  9. // by the unit tests below.
  10. private $example_mviews = [
  11. 'analysis_organism_test' => [
  12. 'schema' => [
  13. 'table' => 'analysis_organism_test',
  14. 'description' => 'This view is for associating an organism (via it\'s associated features) to an analysis.',
  15. 'fields' => [
  16. 'analysis_id' => [
  17. 'size' => 'big',
  18. 'type' => 'int',
  19. 'not null' => true,
  20. ],
  21. 'organism_id' => [
  22. 'size' => 'big',
  23. 'type' => 'int',
  24. 'not null' => true,
  25. ],
  26. ],
  27. 'indexes' => [
  28. 'networkmod_qtl_indx0' => [
  29. 0 => 'analysis_id',
  30. ],
  31. 'networkmod_qtl_indx1' => [
  32. 0 => 'organism_id',
  33. ],
  34. ],
  35. 'foreign keys' => [
  36. 'analysis' => [
  37. 'table' => 'analysis',
  38. 'columns' => [
  39. 'analysis_id' => 'analysis_id',
  40. ],
  41. ],
  42. 'organism' => [
  43. 'table' => 'organism',
  44. 'columns' => [
  45. 'organism_id' => 'organism_id',
  46. ],
  47. ],
  48. ],
  49. ],
  50. 'sql' => "
  51. SELECT DISTINCT A.analysis_id, O.organism_id
  52. FROM analysis A
  53. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  54. INNER JOIN feature F ON AF.feature_id = F.feature_id
  55. INNER JOIN organism O ON O.organism_id = F.organism_id
  56. ",
  57. 'comment' => 'This view is for associating an organism (via it\'s associated features) to an analysis.',
  58. 'module' => 'tripal_chado',
  59. ],
  60. ];
  61. /**
  62. * Test creation of a new materialized view.
  63. *
  64. * @group api
  65. */
  66. public function test_chado_add_mview() {
  67. // Add the analysis_organism mview.
  68. $mview_name = 'analysis_organism_test';
  69. $mview_module = $this->example_mviews[$mview_name]['module'];
  70. $mview_sql = $this->example_mviews[$mview_name]['sql'];
  71. $mview_schema = $this->example_mviews[$mview_name]['schema'];
  72. $mview_comment = $this->example_mviews[$mview_name]['comment'];
  73. $success = chado_add_mview($mview_name, $mview_module, $mview_schema, $mview_sql, $mview_comment, FALSE);
  74. $this->assertTrue($success, "Failed to create materialized view: $mview_name");
  75. // Make sure that the entry is now there.
  76. $mview = db_select('tripal_mviews', 'tm')
  77. ->fields('tm')
  78. ->condition('name', $mview_name)
  79. ->execute()
  80. ->fetchObject();
  81. $this->assertTrue(is_object($mview),
  82. "Failed to find the materialized view, $mview_name, in the tripal_mviews table");
  83. // Make sure that all of the fields exist and were properly added.
  84. $this->assertTrue($mview->modulename == $mview_module,
  85. "Failed to create a proper materialized the modulename field is incorrect: '$mview_module' != '$mview->modulename'");
  86. $this->assertTrue($mview->mv_table == $mview_name,
  87. "Failed to create a proper materialized the mv_table field does not match input.");
  88. $this->assertTrue($mview->query == $mview_sql,
  89. "Failed to create a proper materialized the query field does not match input.");
  90. $this->assertTrue($mview->comment == $mview_comment,
  91. "Failed to create a proper materialized the comment field does not match input.");
  92. $this->assertNULL($mview->status,
  93. "Failed to create a proper materialized the status field should be NULL.");
  94. $this->assertNULL($mview->last_update,
  95. "Failed to create a proper materialized the last_update field should be NULL.");
  96. // Make sure the table exists.
  97. $this->assertTrue(chado_table_exists($mview_name),
  98. "Materialized view, $mview_name, was added to the tripal_mviews table but the table was not created.");
  99. }
  100. /**
  101. * Test deletion of a materialized view.
  102. *
  103. * @group api
  104. */
  105. public function test_chado_delete_mview() {
  106. // TODO: this is currently a stub for a test function that neds
  107. // implementation. For now it returns true to get past unit testing.
  108. $this->assertTrue(true);
  109. }
  110. /**
  111. * Test modifications to a materialized view
  112. *
  113. * @group api
  114. */
  115. public function test_chado_edit_mview() {
  116. // TODO: this is currently a stub for a test function that neds
  117. // implementation. For now it returns true to get past unit testing.
  118. $this->assertTrue(true);
  119. }
  120. /**
  121. * Test adding a Tripal Job to re-populate a materialized view
  122. *
  123. * @group api
  124. */
  125. public function test_chado_refresh_mview() {
  126. // TODO: this is currently a stub for a test function that neds
  127. // implementation. For now it returns true to get past unit testing.
  128. $this->assertTrue(true);
  129. }
  130. /**
  131. * Test re-populating a materialized view.
  132. *
  133. * @group api
  134. */
  135. public function test_chado_populate_mview() {
  136. // TODO: this is currently a stub for a test function that neds
  137. // implementation. For now it returns true to get past unit testing.
  138. $this->assertTrue(true);
  139. }
  140. /**
  141. * Test modifications to a materialized view
  142. *
  143. * @group api
  144. */
  145. public function test_chado_get_mview_id() {
  146. // TODO: this is currently a stub for a test function that neds
  147. // implementation. For now it returns true to get past unit testing.
  148. $this->assertTrue(true);
  149. }
  150. /**
  151. * Test retrieving names of the materialized views.
  152. *
  153. * @group api
  154. */
  155. public function test_chado_get_mview_table_names() {
  156. // TODO: this is currently a stub for a test function that neds
  157. // implementation. For now it returns true to get past unit testing.
  158. $this->assertTrue(true);
  159. }
  160. /**
  161. * Test retrieving all materialized view objects.
  162. *
  163. * @group api
  164. */
  165. public function test_chado_get_mviews() {
  166. // TODO: this is currently a stub for a test function that neds
  167. // implementation. For now it returns true to get past unit testing.
  168. $this->assertTrue(true);
  169. }
  170. /**
  171. * Issue 322 reported the problem of re-adding a materialized view after
  172. * the actual table had been manually removed outside of Tripal. The
  173. * function reported errors.
  174. *
  175. * @ticket 322
  176. */
  177. public function test_re_adding_deleted_mview_issue_322() {
  178. // Add the analysis_organism mview.
  179. $mview_name = 'analysis_organism_test';
  180. $mview_module = $this->example_mviews[$mview_name]['module'];
  181. $mview_sql = $this->example_mviews[$mview_name]['sql'];
  182. $mview_schema = $this->example_mviews[$mview_name]['schema'];
  183. $mview_comment = $this->example_mviews[$mview_name]['comment'];
  184. $success = chado_add_mview($mview_name, $mview_module, $mview_schema, $mview_sql, $mview_comment, FALSE);
  185. $this->assertTrue($success, "Failed to create materialized view: $mview_name");
  186. // Now simulate manual deletion of the table outside of the API.
  187. chado_query('DROP TABLE {' . $mview_name . '}');
  188. // Make sure the table no longer exists.
  189. $this->assertFalse(chado_table_exists($mview_name),
  190. "Failed to manually remove the materialized view, cannot complete the test.");
  191. // Now try to read the mview. Previously, the behavior was the the mview
  192. // table would not be created because Tripal thinks it's already there.
  193. $success = chado_add_mview($mview_name, $mview_module, $mview_schema, $mview_sql, $mview_comment, FALSE);
  194. $this->assertTrue($success, "Failed to re-create materialized view: $mview_name");
  195. $this->assertTrue(chado_table_exists($mview_name),
  196. "Manually removing a materialized views throws off the chado_add_mview function when the mview is re-added. See Issue #322");
  197. }
  198. }