TripalChadoPropertyAPITest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Tests;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use StatonLab\TripalTestSuite\Database\Factory;
  6. class TripalChadoPropertyAPITest extends TripalTestCase {
  7. use DBTransaction;
  8. /**
  9. * Tests chado_insert_property() with all prop tables.
  10. *
  11. * @dataProvider propTableProvider
  12. *
  13. * @group chado
  14. * @group api
  15. * @group chado-property
  16. */
  17. public function test_chado_insert_property($prop_table, $base_table) {
  18. $base_record = factory('chado.'.$base_table)->create();
  19. $base_pkey = $base_table.'_id';
  20. $term = factory('chado.cvterm')->create();
  21. $value = 'chado_API_test_value';
  22. // Linker column
  23. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  24. $property = [
  25. 'type_id' => $term->cvterm_id,
  26. 'value' => $value,
  27. ];
  28. chado_insert_property($record, $property);
  29. $result = db_select('chado.'.$prop_table, 'p')
  30. ->fields('p')
  31. ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
  32. ->execute()
  33. ->fetchObject();
  34. $this->assertNotEmpty($result);
  35. $this->assertEquals($value, $result->value);
  36. $this->assertEquals($term->cvterm_id, $result->type_id);
  37. $this->assertEquals('0', $result->rank);
  38. }
  39. /**
  40. * Tests chado_get_property() with all prop tables.
  41. *
  42. * @dataProvider propTableProvider
  43. *
  44. * @group chado
  45. * @group api
  46. * @group chado-property
  47. */
  48. public function test_chado_get_property($prop_table, $base_table) {
  49. $base_record = factory('chado.'.$base_table)->create();
  50. $base_pkey = $base_table.'_id';
  51. $term = factory('chado.cvterm')->create();
  52. $value = 'chado_API_test_value';
  53. // Linker column
  54. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  55. $property = [
  56. 'type_id' => $term->cvterm_id,
  57. 'value' => $value,
  58. ];
  59. $prop = chado_insert_property($record, $property);
  60. $retrieved = chado_get_property($record, $property);
  61. $this->assertNotFalse($retrieved);
  62. $this->assertEquals($value, $retrieved->value);
  63. $record = ['prop_id' => $prop[$prop_table.'_id'], 'table' => $base_table];
  64. $retrieved = chado_get_property($record, $property);
  65. $this->assertNotNull($retrieved);
  66. $this->assertEquals($value, $retrieved->value);
  67. }
  68. /**
  69. * Tests chado_update_property() with all prop tables.
  70. *
  71. * @dataProvider propTableProvider
  72. *
  73. * @group chado
  74. * @group api
  75. * @group chado-property
  76. */
  77. public function test_chado_update_property($prop_table, $base_table) {
  78. $base_record = factory('chado.'.$base_table)->create();
  79. $base_pkey = $base_table.'_id';
  80. $term = factory('chado.cvterm')->create();
  81. $value = 'chado_API_test_value';
  82. $new_value = 'chado_API_new';
  83. // Linker column
  84. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  85. $property = [
  86. 'type_id' => $term->cvterm_id,
  87. 'value' => $value,
  88. ];
  89. chado_insert_property($record, $property);
  90. $property['value'] = $new_value;
  91. chado_update_property($record, $property);
  92. $result = db_select('chado.'.$prop_table, 'p')
  93. ->fields('p')
  94. ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
  95. ->execute()
  96. ->fetchObject();
  97. $this->assertNotEmpty($result);
  98. $this->assertEquals($new_value, $result->value);
  99. $this->assertEquals($term->cvterm_id, $result->type_id);
  100. $this->assertEquals('0', $result->rank);
  101. }
  102. /**
  103. * Tests chado_delete_property() with all prop tables.
  104. *
  105. * @dataProvider propTableProvider
  106. *
  107. * @group chado
  108. * @group api
  109. * @group chado-property
  110. */
  111. public function test_chado_delete_property($prop_table, $base_table) {
  112. $base_record = factory('chado.'.$base_table)->create();
  113. $base_pkey = $base_table.'_id';
  114. $term = factory('chado.cvterm')->create();
  115. $value = 'chado_API_test_value';
  116. // Linker column
  117. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  118. $property = [
  119. 'type_id' => $term->cvterm_id,
  120. 'value' => $value,
  121. ];
  122. chado_insert_property($record, $property);
  123. chado_delete_property($record, $property);
  124. $result = db_select('chado.'.$prop_table, 'p')
  125. ->fields('p')
  126. ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
  127. ->execute()
  128. ->fetchObject();
  129. $this->assertFalse($result);
  130. $prop = chado_insert_property($record, $property);
  131. $record = ['prop_id' => $prop[$prop_table.'_id'], 'table' => $base_table];
  132. chado_delete_property($record, $property);
  133. }
  134. /**
  135. * Tests chado_get_record_with_property() with all prop tables.
  136. *
  137. * Note: chado_get_record_with_property() gets all records in the base table
  138. * assigned one or more properties.
  139. *
  140. * @dataProvider propTableProvider
  141. *
  142. * @group chado
  143. * @group api
  144. * @group chado-property
  145. */
  146. function test_chado_get_record_with_property($prop_table, $base_table) {
  147. $base_record = factory('chado.'.$base_table)->create();
  148. $base_pkey = $base_table.'_id';
  149. $term = factory('chado.cvterm')->create();
  150. $value = 'chado_API_test_value';
  151. // Linker column
  152. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  153. $property = [
  154. 'type_id' => $term->cvterm_id,
  155. 'value' => $value,
  156. ];
  157. chado_insert_property($record, $property);
  158. unset($record['id']);
  159. $records = chado_get_record_with_property($record, $property);
  160. $this->assertNotEmpty($records);
  161. $this->assertEquals(1, count($records));
  162. $base_record = factory('chado.'.$base_table)->create();
  163. $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
  164. chado_insert_property($record, $property);
  165. $records = chado_get_record_with_property($record, $property);
  166. $this->assertNotEmpty($records);
  167. $this->assertEquals(2, count($records));
  168. }
  169. /**
  170. * Data Provider: All base tables with associated property tables.
  171. *
  172. * @return
  173. * An array where each item specifies the property table
  174. * and it's associated base table.
  175. */
  176. function propTableProvider() {
  177. $prop_tables = [];
  178. $base_tables = chado_get_base_tables();
  179. foreach ($base_tables as $base) {
  180. $prop = $base . 'prop';
  181. if (chado_table_exists($prop) AND Factory::exists('chado.'.$base)) {
  182. $prop_tables[] = [$prop, $base];
  183. }
  184. }
  185. return $prop_tables;
  186. }
  187. }