ChadoSchemaTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Tests\tripal_chado\api;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
  7. /**
  8. * Tests the ChadoSchema class.
  9. *
  10. * @todo test "Check" functions in the ChadoSchema class.
  11. */
  12. class ChadoSchemaTest extends TripalTestCase {
  13. use DBTransaction;
  14. /**
  15. * Tests that the class can be initiated with or without a record specified
  16. *
  17. * @group api
  18. * @group chado
  19. * @group chado-schema
  20. */
  21. public function testInitClass() {
  22. // Test with no parameters.
  23. $chado_schema = new \ChadoSchema();
  24. $this->assertNotNull($chado_schema);
  25. // Test with version.
  26. $chado_schema = new \ChadoSchema('1.3');
  27. $this->assertNotNull($chado_schema);
  28. }
  29. /**
  30. * Tests the ChadoSchema->getVersion() method.
  31. *
  32. * @group api
  33. * @group chado
  34. * @group chado-schema
  35. */
  36. public function testGetVersion() {
  37. // Generate a fake version.
  38. $faker = Factory::create();
  39. $version = $faker->randomFloat(2, 1, 5);
  40. // Check version can be retrieved when we set it.
  41. $chado_schema = new \ChadoSchema($version);
  42. $retrieved_version = $chado_schema->getVersion();
  43. $this->assertEquals(
  44. $version,
  45. $retrieved_version,
  46. t('The version retrieved via ChadoSchema->getVersion, "!ret", should equal that set, "!set"',
  47. array('!ret' => $retrieved_version, '!set' => $version))
  48. );
  49. // @todo Check version can be retrieved when it's looked up?
  50. }
  51. /**
  52. * Tests the ChadoSchema->getSchemaName() method.
  53. *
  54. * @group api
  55. * @group chado
  56. * @group chado-schema
  57. */
  58. public function testGetSchemaName() {
  59. // Generate a fake version.
  60. $faker = Factory::create();
  61. $version = $faker->randomFloat(2, 1, 5);
  62. $schema_name = $faker->word();
  63. // Check the schema name can be retrieved when we set it.
  64. $chado_schema = new \ChadoSchema($version, $schema_name);
  65. $retrieved_schema = $chado_schema->getSchemaName();
  66. $this->assertEquals(
  67. $schema_name,
  68. $retrieved_schema,
  69. t('The schema name retrieved via ChadoSchema->getSchemaName, "!ret", should equal that set, "!set"',
  70. array('!ret' => $retrieved_schema, '!set' => $schema_name))
  71. );
  72. // @todo Check schema name can be retrieved when it's looked up?
  73. }
  74. /**
  75. * Tests ChadoSchema->getTableNames() method.
  76. *
  77. * @dataProvider knownTableProvider
  78. *
  79. * @group api
  80. * @group chado
  81. * @group chado-schema
  82. */
  83. public function testGetTableNames($version, $known_tables) {
  84. // Check: Known tables for a given version are returned.
  85. $chado_schema = new \ChadoSchema($version);
  86. $returned_tables = $chado_schema->getTableNames();
  87. foreach ($known_tables as $table_name) {
  88. $this->assertArrayHasKey(
  89. $table_name,
  90. $returned_tables,
  91. t('The table, "!known", should exist in the returned tables list for version !version.',
  92. array(':known' => $table_name, ':version' => $version))
  93. );
  94. }
  95. }
  96. /**
  97. * Tests ChadoSchema->getTableSchema() method.
  98. *
  99. * @dataProvider chadoTableProvider
  100. *
  101. * @group api
  102. * @group chado
  103. * @group chado-schema
  104. */
  105. public function testGetTableSchema($version, $table_name) {
  106. // Check: a schema is returned that matches what we expect.
  107. $chado_schema = new \ChadoSchema($version);
  108. $table_schema = $chado_schema->getTableSchema($table_name);
  109. $this->assertNotEmpty(
  110. $table_schema,
  111. t('Returned schema for "!table" in chado v!version should not be empty.',
  112. array('!table' => $table_name, '!version' => $version))
  113. );
  114. $this->assertArrayHasKey(
  115. 'fields',
  116. $table_schema,
  117. t('The schema array for "!table" should have columns listed in an "fields" array',
  118. array('!table' => $table_name))
  119. );
  120. $this->assertArrayHasKey(
  121. 'primary key',
  122. $table_schema,
  123. t('The schema array for "!table" should have the primary key listed in an "primary key" array',
  124. array('!table' => $table_name))
  125. );
  126. $this->assertArrayHasKey(
  127. 'unique keys',
  128. $table_schema,
  129. t('The schema array for "!table" should have unique keys listed in an "unique keys" array',
  130. array('!table' => $table_name))
  131. );
  132. $this->assertArrayHasKey(
  133. 'foreign keys',
  134. $table_schema,
  135. t('The schema array for "!table" should have foreign keys listed in an "foreign keys" array',
  136. array('!table' => $table_name))
  137. );
  138. }
  139. /**
  140. * Tests ChadoSchema->getCustomTableSchema() method.
  141. *
  142. * @dataProvider knownCustomTableProvider
  143. *
  144. * @group api
  145. * @group chado
  146. * @group chado-schema
  147. */
  148. public function testGetCustomTableSchema($table_name) {
  149. // Check: a schema is returned that matches what we expect.
  150. $chado_schema = new \ChadoSchema();
  151. $table_schema = $chado_schema->getCustomTableSchema($table_name);
  152. $this->assertNotEmpty(
  153. $table_schema,
  154. t('Returned schema for "!table" in chado v!version should not be empty.',
  155. array('!table' => $table_name, '!version' => $version))
  156. );
  157. $this->assertArrayHasKey(
  158. 'fields',
  159. $table_schema,
  160. t('The schema array for "!table" should have columns listed in an "fields" array',
  161. array('!table' => $table_name))
  162. );
  163. // NOTE: Other then ensuring fields are set, we can't test further since all other
  164. // keys are technically optional and these arrays are set by admins.
  165. }
  166. /**
  167. * Tests ChadoSchema->getBaseTables() method.
  168. *
  169. * @dataProvider knownBaseTableProvider
  170. *
  171. * @group api
  172. * @group chado
  173. * @group chado-schema
  174. */
  175. public function testGetBaseTables($version, $table_name) {
  176. // Check: Known base tables for a given version are returned.
  177. $chado_schema = new \ChadoSchema($version);
  178. $returned_tables = $chado_schema->getBaseTables();
  179. foreach ($known_tables as $table_name) {
  180. $this->assertArrayHasKey(
  181. $table_name,
  182. $returned_tables,
  183. t('The table, "!known", should exist in the returned base tables list for version !version.',
  184. array(':known' => $table_name, ':version' => $version))
  185. );
  186. }
  187. }
  188. /**
  189. * Tests ChadoSchema->getCvtermMapping() method.
  190. *
  191. * @dataProvider chadoTableProvider
  192. *
  193. * @group api
  194. * @group chado
  195. * @group chado-schema
  196. */
  197. public function testGetCvtermMapping($version, $table_name) {
  198. // @todo This should be tested...
  199. }
  200. /**
  201. * Data Provider: returns known tables specific to a given chado version.
  202. *
  203. * @return array
  204. */
  205. public function knownTableProvider() {
  206. // chado version, array of 3 tables specific to version.
  207. return [
  208. ['1.2', ['cell_line_relationship', 'cvprop', 'chadoprop']],
  209. ['1.3', ['analysis_cvterm', 'dbprop', 'organism_pub']],
  210. ];
  211. }
  212. /**
  213. * Data Provider: returns known tables specific to a given chado version.
  214. *
  215. * @return array
  216. */
  217. public function knownBaseTableProvider() {
  218. // chado version, array of 3 tables specific to version.
  219. return [
  220. ['1.2', ['organism', 'feature', 'stock', 'project','analysis', 'phylotree']],
  221. ['1.3', ['organism', 'feature', 'stock', 'project','analysis', 'phylotree']],
  222. ];
  223. }
  224. /**
  225. * Data Provider: returns known custom tables specific to a given chado version.
  226. *
  227. * NOTE: These tables are provided by core Tripal so we should be able to
  228. * depend on them. Also, for the same reason, chado version doesn't matter.
  229. *
  230. * @return array
  231. */
  232. public function knownCustomTableProvider() {
  233. return [
  234. ['library_feature_count'],
  235. ['organism_feature_count'],
  236. ['tripal_gff_temp'],
  237. ];
  238. }
  239. /**
  240. * DataProvider, a list of all chado tables.
  241. *
  242. * @return array
  243. */
  244. public function chadoTableProvider() {
  245. // Provide the table list for all versions.
  246. $dataset = [];
  247. foreach (array('1.11','1.2','1.3') as $version) {
  248. $chado_schema = new \ChadoSchema();
  249. $version = $chado_schema->getVersion();
  250. foreach ($chado_schema->getTableNames() as $table_name) {
  251. $dataset[] = [$version, $table_name];
  252. }
  253. }
  254. return $dataset;
  255. }
  256. }