ChadoRecordTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Tests;
  3. use PHPUnit\Exception;
  4. use Faker\Factory;
  5. use StatonLab\TripalTestSuite\DBTransaction;
  6. use StatonLab\TripalTestSuite\TripalTestCase;
  7. module_load_include('inc', 'tripal_chado', 'includes/api/ChadoRecord');
  8. class ChadoRecordTest extends TripalTestCase {
  9. use DBTransaction;
  10. /**
  11. * Data provider. A variety of chado records.
  12. *
  13. * @return array
  14. */
  15. public function recordProvider() {
  16. //table, factory or NULL, record_id or NULL
  17. $faker = \Faker\Factory::create();
  18. $analysis = [
  19. 'name' => $faker->word,
  20. 'description' => $faker->text,
  21. 'program' => $faker->word,
  22. 'programversion' => $faker->word,
  23. ];
  24. $organism = [
  25. 'genus' => $faker->word,
  26. 'species' => $faker->word,
  27. 'common_name' => $faker->word,
  28. ];
  29. return [
  30. ['analysis', $analysis],
  31. ['organism', $organism],
  32. ];
  33. }
  34. /**
  35. * Tests that the class can be initiated with or without a record specified
  36. *
  37. * @group api
  38. * @group chado
  39. * @group wip
  40. * @dataProvider recordProvider
  41. */
  42. public function testInitClass($table, $values) {
  43. $record = new \ChadoRecord($table);
  44. $this->assertNotNull($record);
  45. $chado_record = factory('chado.' . $table)->create($values);
  46. $record_column = $table.'_id';
  47. $record = new \ChadoRecord($table, $chado_record->$record_column);
  48. $this->assertNotNull($record);
  49. }
  50. /**
  51. * @group api
  52. * @group chado
  53. * @group wip
  54. * @throws \Exception
  55. * @dataProvider recordProvider
  56. */
  57. public function testGetTable($table, $values) {
  58. $record = new \ChadoRecord($table);
  59. $this->assertEquals($table, $record->getTable());
  60. }
  61. /**
  62. * @group wip
  63. * @group api
  64. * @group chado
  65. * @dataProvider recordProvider
  66. *
  67. * @throws \Exception
  68. */
  69. public function testGetID($table, $values) {
  70. $chado_record = factory('chado.' . $table)->create();
  71. $record_column = $table.'_id';
  72. $id = $chado_record->$record_column;
  73. $record = new \ChadoRecord($table, $id);
  74. $returned_id = $record->getID();
  75. $this->assertEquals($id, $returned_id);
  76. }
  77. /**
  78. * @group api
  79. * @group wip
  80. * @group chado
  81. * @dataProvider recordProvider
  82. *
  83. *
  84. */
  85. public function testGetValues($table, $values) {
  86. $chado_record = factory('chado.' . $table)->create($values);
  87. $record_column = $table.'_id';
  88. $id = $chado_record->$record_column;
  89. $record = new \ChadoRecord($table, $id);
  90. $values = $record->getValues();
  91. $this->assertNotEmpty($values);
  92. foreach ($values as $key => $value) {
  93. $this->assertArrayHasKey($key, $values);
  94. $this->assertEquals($value, $values[$key]);
  95. }
  96. }
  97. /**
  98. * @group api
  99. * @group wip
  100. * @group chado
  101. * @dataProvider recordProvider
  102. *
  103. */
  104. public function testGetValue($table, $values) {
  105. $chado_record = factory('chado.' . $table)->create($values);
  106. $record_column = $table.'_id';
  107. $id = $chado_record->$record_column;
  108. $record = new \ChadoRecord($table, $id);
  109. foreach ($values as $key => $value) {
  110. $returned_value = $record->getValue($key);
  111. $this->assertEquals($value, $returned_value);
  112. }
  113. }
  114. /**
  115. * @group wip
  116. * @group chado
  117. * @group api
  118. * @dataProvider recordProvider
  119. */
  120. public function testFind($table, $values) {
  121. $chado_record = factory('chado.' . $table)->create($values);
  122. $record_column = $table.'_id';
  123. $id = $chado_record->$record_column;
  124. $record = new \ChadoRecord($table);
  125. $record->setValues($values);
  126. $found = $record->find();
  127. $this->assertNotNull($found);
  128. $this->assertEquals(1, $found);
  129. }
  130. /**
  131. * Check that the find method throws an exception when it cant find anything.
  132. *
  133. * @throws \Exception
  134. */
  135. public function testFindFail() {
  136. $table = 'organism';
  137. $record = new \ChadoRecord($table);
  138. $record->setValue($table . '_id', 'unfindable');
  139. $this->expectException(Exception);
  140. $found = $record->find();
  141. }
  142. /**
  143. * @param $table
  144. * @param $values
  145. *
  146. * @throws \Exception
  147. */
  148. public function testSetandGetValue($table, $values) {
  149. $record = new \ChadoRecord($table);
  150. $record->setValues($values);
  151. $vals = $record->getValues();
  152. foreach ($vals as $val_key => $val) {
  153. $this->assertEquals($values[$val_key], $val, "The getValues did not match what was provided for setValues");
  154. }
  155. }
  156. /**
  157. * Save should work for both an update and an insert
  158. * @group wip
  159. * @group chado
  160. * @group api
  161. *
  162. * @dataProvider recordProvider
  163. */
  164. public function testSave($table, $values){
  165. //first, test the insert case
  166. $record = new \ChadoRecord($table);
  167. $record->setValues($values);
  168. $record->save();
  169. $record_column = $table.'_id';
  170. $query = db_select('chado.' . $table, 't')
  171. ->fields('t', [$record_column]);
  172. foreach ($values as $key => $val){
  173. $query->condition($key, $val);
  174. }
  175. $result =$query->execute()->fetchAll();
  176. $this->assertNotEmpty($result, 'we couldnt insert our record on a save!');
  177. //change the last key
  178. //NOTE this will break if the last key isn't a string!
  179. $values[$key] = 'new_value_that_i_wantTOBEUNIQUE';
  180. $record->setValues($values);
  181. $record->save();
  182. $query = db_select('chado.' . $table, 't')
  183. ->fields('t', [$record_column]);
  184. foreach ($values as $key => $val){
  185. $query->condition($key, $val);
  186. }
  187. $result =$query->execute()->fetchAll();
  188. $this->assertNotEmpty($result, 'Our record wasnt updated when saving!');
  189. }
  190. /**
  191. * @group wip
  192. * @group chado
  193. * @group api
  194. *
  195. * @dataProvider recordProvider
  196. */
  197. public function testInsert($table, $values){
  198. //first, test the insert case
  199. $record = new \ChadoRecord($table);
  200. $record->setValues($values);
  201. $record->insert();
  202. $record_column = $table.'_id';
  203. $query = db_select('chado.' . $table, 't')
  204. ->fields('t', [$record_column]);
  205. foreach ($values as $key => $val){
  206. $query->condition($key, $val);
  207. }
  208. $result =$query->execute()->fetchAll();
  209. $this->assertNotEmpty($result, 'we couldnt insert our record on a save!');
  210. //If we insert again, it should fail
  211. $this->expectException(EXCEPTION);
  212. $record->insert();
  213. }
  214. /**
  215. * @group wip
  216. * @group chado
  217. * @group api
  218. *
  219. * @dataProvider recordProvider
  220. */
  221. public function testUpdate($table, $values){
  222. $id = $this->genChadoRecord($table, $values);
  223. $record = new \ChadoRecord($table, $id);
  224. $record_column = $table.'_id';
  225. //$dump_vals = $record->getValues();
  226. // var_dump($dump_vals);
  227. $key = array_keys($values)[0];
  228. $string = 'some_random_new_string34792387';
  229. $values[$key] = $string;
  230. $record->setValues($values);
  231. $record->update();
  232. //$dump_vals = $record->getValues();
  233. // var_dump($dump_vals);
  234. $query = db_select('chado.' . $table, 't')
  235. ->fields('t', [$key]);
  236. foreach ($values as $key => $val){
  237. $query->condition($key, $val);
  238. }
  239. $result =$query->execute()->fetchField();
  240. $this->assertNotFalse($result, 'we couldnt update our record.');
  241. $this->assertEquals($string, $result);
  242. }
  243. /**
  244. * @group wip
  245. * @group chado
  246. * @group api
  247. *
  248. * @dataProvider recordProvider
  249. *
  250. */
  251. public function testDelete($table, $values){
  252. $id = $this->genChadoRecord($table, $values);
  253. $record = new \ChadoRecord($table, $id);
  254. $record_column = $table.'_id';
  255. $record->delete();
  256. $query = db_select('chado.' . $table, 't')
  257. ->fields('t', [$record_column]);
  258. foreach ($values as $key => $val){
  259. $query->condition($key, $val);
  260. }
  261. $result =$query->execute()->fetchAll();
  262. $this->assertEmpty($result, 'we couldnt delete our record!');
  263. }
  264. private function genChadoRecord($table, $values){
  265. $chado_record = factory('chado.' . $table)->create($values);
  266. $record_column = $table.'_id';
  267. $id = $chado_record->$record_column;
  268. return $id;
  269. }
  270. }