ChadoSchemaTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. ['!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. ['!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. [':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. ['!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. ['!table' => $table_name])
  119. );
  120. // Instead of asserting these keys exist. Lets assert that if they do exist,
  121. // they match the expected format.
  122. if (isset($table_schema['primary key'])) {
  123. $this->assertTrue(is_array($table_schema['primary key']),
  124. t('The primary key of the Tripal Schema definition for "!table" must be an array.',
  125. ['!table' => $table_name]));
  126. }
  127. $this->assertArrayHasKey(
  128. 'foreign keys',
  129. $table_schema,
  130. t('The schema array for "!table" should have foreign keys listed in an "foreign keys" array',
  131. ['!table' => $table_name])
  132. );
  133. }
  134. /**
  135. * Tests ChadoSchema->getCustomTableSchema() method.
  136. *
  137. * @dataProvider knownCustomTableProvider
  138. *
  139. * @group api
  140. * @group chado
  141. * @group chado-schema
  142. */
  143. public function testGetCustomTableSchema($table_name) {
  144. // Check: a schema is returned that matches what we expect.
  145. $chado_schema = new \ChadoSchema();
  146. $table_schema = $chado_schema->getCustomTableSchema($table_name);
  147. $this->assertNotEmpty(
  148. $table_schema,
  149. t('Returned schema for "!table" in chado v!version should not be empty.',
  150. ['!table' => $table_name, '!version' => $version])
  151. );
  152. $this->assertArrayHasKey(
  153. 'fields',
  154. $table_schema,
  155. t('The schema array for "!table" should have columns listed in an "fields" array',
  156. ['!table' => $table_name])
  157. );
  158. // NOTE: Other then ensuring fields are set, we can't test further since all other
  159. // keys are technically optional and these arrays are set by admins.
  160. }
  161. /**
  162. * Tests ChadoSchema->getBaseTables() method.
  163. *
  164. * @dataProvider knownBaseTableProvider
  165. *
  166. * @group api
  167. * @group chado
  168. * @group chado-schema
  169. */
  170. public function testGetBaseTables($version, $known_tables) {
  171. // Check: Known base tables for a given version are returned.
  172. $chado_schema = new \ChadoSchema($version);
  173. $returned_tables = $chado_schema->getBaseTables();
  174. foreach ($known_tables as $table_name) {
  175. $found = FALSE;
  176. foreach ($returned_tables as $check_table) {
  177. if ($check_table == $table_name) {
  178. $found = TRUE;
  179. }
  180. }
  181. $this->assertTrue($found, "{$table_name} was not returned by getBaseTables for Chado v {$version}");
  182. }
  183. }
  184. /**
  185. * Tests ChadoSchema->getCvtermMapping() method.
  186. *
  187. * @dataProvider chadoTableProvider
  188. *
  189. * @group api
  190. * @group chado
  191. * @group chado-schema
  192. */
  193. // public function testGetCvtermMapping($version, $table_name) {
  194. //
  195. // // Ideally we would create a new chado table + mapping and then test this pulls it out
  196. // // since admin can re-map terms. However, that's more then I meant to bite off right
  197. // // now...
  198. //
  199. // // @todo Test that known terms match the tables we expect.
  200. //
  201. // // @todo Test that a non-existent term throws an error.
  202. //
  203. // // @todo Test that an fake unmapped term returns no mapping.
  204. // }
  205. /**
  206. * Data Provider: returns known tables specific to a given chado version.
  207. *
  208. * @return array
  209. */
  210. public function knownTableProvider() {
  211. // chado version, array of 3 tables specific to version.
  212. return [
  213. ['1.2', ['cell_line_relationship', 'cvprop', 'chadoprop']],
  214. ['1.3', ['analysis_cvterm', 'dbprop', 'organism_pub']],
  215. ];
  216. }
  217. /**
  218. * Data Provider: returns known tables specific to a given chado version.
  219. *
  220. * @return array
  221. */
  222. public function knownBaseTableProvider() {
  223. // chado version, array of 3 tables specific to version.
  224. return [
  225. [
  226. '1.2',
  227. ['organism', 'feature', 'stock', 'project', 'analysis', 'phylotree'],
  228. ],
  229. [
  230. '1.3',
  231. ['organism', 'feature', 'stock', 'project', 'analysis', 'phylotree'],
  232. ],
  233. ];
  234. }
  235. /**
  236. * Data Provider: returns known custom tables specific to a given chado
  237. * version.
  238. *
  239. * NOTE: These tables are provided by core Tripal so we should be able to
  240. * depend on them. Also, for the same reason, chado version doesn't matter.
  241. *
  242. * @return array
  243. */
  244. public function knownCustomTableProvider() {
  245. return [
  246. ['library_feature_count'],
  247. ['organism_feature_count'],
  248. ['tripal_gff_temp'],
  249. ];
  250. }
  251. /**
  252. * DataProvider, a list of all chado tables.
  253. *
  254. * @return array
  255. */
  256. public function chadoTableProvider() {
  257. // Provide the table list for all versions.
  258. $dataset = [];
  259. foreach (['1.11', '1.2', '1.3'] as $version) {
  260. $chado_schema = new \ChadoSchema();
  261. $version = $chado_schema->getVersion();
  262. foreach ($chado_schema->getTableNames() as $table_name) {
  263. $dataset[] = [$version, $table_name];
  264. }
  265. }
  266. return $dataset;
  267. }
  268. }