ChadoRecordTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. *
  144. *
  145. * @group chado
  146. * @group api
  147. *
  148. * @dataProvider recordProvider
  149. *
  150. * @throws \Exception
  151. */
  152. public function testSetandGetValue($table, $values) {
  153. $record = new \ChadoRecord($table);
  154. $record->setValues($values);
  155. $vals = $record->getValues();
  156. foreach ($vals as $val_key => $val) {
  157. $this->assertEquals($values[$val_key], $val, "The getValues did not match what was provided for setValues");
  158. }
  159. }
  160. /**
  161. * Save should work for both an update and an insert
  162. *
  163. * @group chado
  164. * @group api
  165. *
  166. * @dataProvider recordProvider
  167. */
  168. public function testSave($table, $values) {
  169. //first, test the insert case
  170. $record = new \ChadoRecord($table);
  171. $record->setValues($values);
  172. $record->save();
  173. $record_column = $table . '_id';
  174. $query = db_select('chado.' . $table, 't')
  175. ->fields('t', [$record_column]);
  176. foreach ($values as $key => $val) {
  177. $query->condition($key, $val);
  178. }
  179. $result = $query->execute()->fetchAll();
  180. $this->assertNotEmpty($result, 'we couldnt insert our record on a save!');
  181. //change the last key
  182. //NOTE this will break if the last key isn't a string!
  183. $values[$key] = 'new_value_that_i_wantTOBEUNIQUE';
  184. $record->setValues($values);
  185. $record->save();
  186. $query = db_select('chado.' . $table, 't')
  187. ->fields('t', [$record_column]);
  188. foreach ($values as $key => $val) {
  189. $query->condition($key, $val);
  190. }
  191. $result = $query->execute()->fetchAll();
  192. $this->assertNotEmpty($result, 'Our record wasnt updated when saving!');
  193. }
  194. /**
  195. *
  196. * @group chado
  197. * @group api
  198. *
  199. * @dataProvider recordProvider
  200. */
  201. public function testInsert($table, $values) {
  202. //first, test the insert case
  203. $record = new \ChadoRecord($table);
  204. $record->setValues($values);
  205. $record->insert();
  206. $record_column = $table . '_id';
  207. $query = db_select('chado.' . $table, 't')
  208. ->fields('t', [$record_column]);
  209. foreach ($values as $key => $val) {
  210. $query->condition($key, $val);
  211. }
  212. $result = $query->execute()->fetchAll();
  213. $this->assertNotEmpty($result, 'we couldnt insert our record on a save!');
  214. //If we insert again, it should fail
  215. $this->expectException(EXCEPTION);
  216. $record->insert();
  217. }
  218. /**
  219. *
  220. * @group chado
  221. * @group api
  222. *
  223. * @dataProvider recordProvider
  224. */
  225. public function testUpdate($table, $values) {
  226. $id = $this->genChadoRecord($table, $values);
  227. $record = new \ChadoRecord($table, $id);
  228. $record_column = $table . '_id';
  229. //$dump_vals = $record->getValues();
  230. // var_dump($dump_vals);
  231. $key = array_keys($values)[0];
  232. $string = 'some_random_new_string34792387';
  233. $values[$key] = $string;
  234. $record->setValues($values);
  235. $record->update();
  236. //$dump_vals = $record->getValues();
  237. // var_dump($dump_vals);
  238. $query = db_select('chado.' . $table, 't')
  239. ->fields('t', [$key]);
  240. foreach ($values as $key => $val) {
  241. $query->condition($key, $val);
  242. }
  243. $result = $query->execute()->fetchField();
  244. $this->assertNotFalse($result, 'we couldnt update our record.');
  245. $this->assertEquals($string, $result);
  246. }
  247. /**
  248. *
  249. * @group chado
  250. * @group api
  251. *
  252. * @dataProvider recordProvider
  253. *
  254. */
  255. public function testDelete($table, $values) {
  256. $id = $this->genChadoRecord($table, $values);
  257. $record = new \ChadoRecord($table, $id);
  258. $record_column = $table . '_id';
  259. $record->delete();
  260. $query = db_select('chado.' . $table, 't')
  261. ->fields('t', [$record_column]);
  262. foreach ($values as $key => $val) {
  263. $query->condition($key, $val);
  264. }
  265. $result = $query->execute()->fetchAll();
  266. $this->assertEmpty($result, 'we couldnt delete our record!');
  267. }
  268. private function genChadoRecord($table, $values) {
  269. $chado_record = factory('chado.' . $table)->create($values);
  270. $record_column = $table . '_id';
  271. $id = $chado_record->$record_column;
  272. return $id;
  273. }
  274. }